2021年3月5日 星期五

[Elasticsearch] Adjust index mapping by using reindex

最近在做的 log 遇到一開始沒有設定 index mapping type,所以 elasticsearch 自動判斷 value 給予 long type,但實際上該欄位資料應該是 float,只是剛好 index doc 那一筆是整數,讓 ES 判斷為 long。

以下範例說明: 

PUT test-index
POST test-index/_doc
{
  "name":"xxx",
  "price":22
}

建完 index 新增第一筆資料,再查詢 mapping api,會看到 price 為 long type

GET test-index/_mapping/

{
  "test-index" : {
    "mappings" : {
      "properties" : {
        "name" : {
          "type" : "text",
          "fields" : {
            "keyword" : {
              "type" : "keyword",
              "ignore_above" : 256
            }
          }
        },
        "price" : {
          "type" : "long"
        }
      }
    }
  }
}

接著新增其他資料,price type 也一直會是 long

POST test-index/_doc
{
  "name":"yyy",
  "price":10.5
}

下面就進入這篇的重點啦,利用 elasticsearch 提供的 reindex api 將錯誤的 type 歸正。

首先需要新增一個 backup index,然後將其 price properties mapping type 修改為 float 。

PUT test-index-bak
PUT test-index-bak/_mapping
{
  "properties": {
    "price":{
      "type":"float"
    }
  }
}

查看一下

GET test-index-bak/_mapping/field/price
{
  "test-index-bak" : {
    "mappings" : {
      "price" : {
        "full_name" : "price",
        "mapping" : {
          "price" : {
            "type" : "float"
          }
        }
      }
    }
  }
}

接著就是將原本的資料透過 reindex 匯入 backup index

POST _reindex
{
  "source": {
    "index": "test-index"
  },
  "dest": {
    "index": "test-index-bak"
  }
}

匯入完以後刪掉原有的 index,再重複一次上面的動作,只是這次是針對原有的 test-index 做操作,再將備份的資料倒回來。

DELETE test-index
PUT test-index

PUT test-index/_mapping
{
  "properties": {
    "price":{
      "type":"float"
    }
  }
}

GET test-index/_mapping/

POST _reindex
{
  "source": {
    "index": "test-index-bak"
  },
  "dest": {
    "index": "test-index"
  }
}
DELETE test-index-bak

最後刪掉 backup index 就結束啦,是不是很簡單~~

2021年1月19日 星期二

SwaggerUI 的 Content Security Policy (SCP) 設定

最近工作上用到靜態代碼分析工具,並且在修正 Critical Issue 時碰到一些問題,來分享紀錄一下

Fortify Static Code Analyzer 是一個靜態應用程式安全分析工具

我這次遇到的是 Content Security Policy (CSP) is not configured.
那什麼是 CSP 呢?看 Wiki 就知道了

Content Security Policy (CSP) is a computer security standard introduced to prevent cross-site scripting (XSS), clickjacking and other code injection attacks resulting from execution of malicious content in the trusted web page context.

https://en.wikipedia.org/wiki/Content_Security_Policy




一開始以為,喔~只要把 Content-Security-Policy: default-src 'none' 這個 Header 加上去就好了吧!沒錯,加上去再做一次 Fortify Scan 就沒有 CSP Critical Issue 了,但是 SwaggerUI 卻壞了…


因為 Swagger 的 css, js, icon 資源載入,全被 Browser 擋掉了。
這是因為 default-src 'none' 的意思是任何資源都不允許,所以必須開一些白名單讓自己網站的資源可以正常載入,最後改成這樣就正常了。

default-src 'self'; style-src 'self' 'unsafe-inline'; script-src 'self' 'unsafe-inline'; img-src 'self' data:;

其他相關 CSP 設定可參考這裡

2020年12月11日 星期五

MacOS Git auto completion, bash and zsh

最近換電腦在安裝 git auto completion,按照之前的設定使用 brew install bash-completion,但怎麼做都沒用,後來才發現,原來 macOS terminal 的 default login shell 從 2019 的 Catalina 版本就已經改成 Zsh

In 2019, macOS Catalina adopted Zsh as the default login shell, replacing the aging GPLv2 licensed version of Bash, and when Bash is run interactively on Catalina, a warning is shown by default.

那解決方式有三種

  1. 將 default login shell 改回 bash
  2. 改為使用 zsh-completions
  3. 使用 zsh 自帶的 tab-completion library

第一種方式怎麼改呢?
要進入 系統偏好設定 -> 使用者與群組 按右下角解除鎖頭後,在使用者上按右鍵,點選進階選項,就可以看到登入 shell 的下拉選單。





第二種方式其實就是 brew install zsh-completions,官方的教學就滿清楚的,請參考這裡


第三種是只要在 ~/.zshrc 的檔案裡加上這一行,再重啟 terminal 就好了。

autoload -Uz compinit && compinit

如果不行就試試 force rebuild zcompdump

rm -f ~/.zcompdump; compinit

2020年12月9日 星期三

Homebrew - 如何安裝舊版套件

Homebrew 是一款軟體套件管理系統,用以簡化 macOS 系統上的軟體安裝過程,通常安裝 package 時不指定版本會下載最新穩定版本,但有時會有需要安裝特定版本,可以先利用 search 找找看是否在 Homebrew Formulae 上有提供 

$ brew search postgresql

接著就可以透過 @ 指定安裝版本

brew install postgresql@9.6

但如果想要的版本在 Homebrew Formulae 上找不到呢?

則可以透過進入 homebrew/core repository 檢查歷史紀錄

cd "$(brew --repo homebrew/core)"
git log master -- Formula/memcached.rb

然後可以看到如下的 commit log 資訊

commit 5ec463decefeaab3d1825b923ad2dbee73ffc6dc
Author: Adam Vandenberg <flangy@gmail.com>
Date:   Fri Apr 9 21:19:48 2010 -0700

    Update memcached to 1.4.5

接著記住所需要的 package 版本所在的 commit id,並 checkout 至該版本,然後再安裝,就可以安裝到想要的版本了

cd "$(brew --repo homebrew/core)" && git checkout 5ec463decefeaab3d1825b923ad2dbee73ffc6dc
HOMEBREW_NO_AUTO_UPDATE=1 brew install memcached

最後再將 homebrew/core repository 切回最新版本就好了

git checkout master

Reference: homebrew - how to install older versions

2020年11月26日 星期四

Serverless

 最近做的 Serverless 概念的課程簡報

Serverless
 留言

開發工程必備的雲端架構知識

Paul Chen
DevOps @ 104 開發營運部

    


  • Serverless 由來
  • FaaS?SaaS?Serverless?
  • Serverless Architecture
  • Serverless Limits
  • Serverless Pros and Cons
  • Recap
  •  留言

Serverless 由來


Serverless Providers

  • AWS Lambda 
  • Microsoft Azure Function
  • Google Cloud Functions
  • IBM Cloud Functions

FaaS?SaaS?Serverless?

Note:

IaaS example: AWS EC2, OpenStack
PaaS example: Heroku, AWS Elastic Beanstalk
FaaS example: AWS Lambda
SaaS example: Gmail, Youtube, Office365, TravisCI


So… What is Serverless?


Serverless computing is a cloud computing execution model in which the cloud provider runs the server, and dynamically manages the allocation of machine resources. – Wiki


Function as a service (FaaS) is a category of cloud computing services that provides a platform allowing customers to develop, run, and manage application functionalities without the complexity of building and maintaining the infrastructure. Building an application following this model is one way of achieving a “serverless” architecture. – Wiki


Serverless Architecture on AWS


The AWS serverless platform


The AWS serverless platform


Serverless Develop Limits

  • Functional Programming
  • Execution Duration
  • Stateless

Serverless Pros and Cons


Pros : Cost-effective


Pros : Scalability and Availability



Pros of Serverless

  • Cost-effective
  • Efficient scalability
  • High availability
  • Reduced operational costs
  • Focus on business, not on infrastructure

Serverless Cons

and how AWS can help


Cons of Serverless

  • Complex system architecture
    • Latency
    • Difficulty in debugging and running local test
  • Vendor lock-in

AWS X-Ray


AWS X-Ray Traces


Serverless Application Model (SAM)

$ sam build --use-container
$ sam local invoke -e event.json ExampleLambda

Recap

  • Serverless architecture have servers, but they are not managed by you
  • Functions are a core concept
  • Scaling and High Availability is done by cloud providers
  • Keeps cost low, by covering spikes and idle
  • Focus on business, not on infrastructure

End