Google Cloud Secret Manager

本教學說明如何設定一個使用 GCP Secret Manager 作為持久金鑰儲存的 KES 伺服器。

K E S C l i e n t K E S S e r v e r G C P S e c r e t M a n a g e r

Google Cloud Secret Manager

Google Cloud Secret Manager 是一個用於密碼、存取權杖和加密金鑰等機密的鍵值儲存。

  1. 登入 GCP 主控台

  2. 建立一個新專案或選擇現有專案

  3. 如果您的專案尚未啟用,請啟用 Secret Manager 服務

  4. 前往 GCP IAM 服務帳戶,並為 KES 建立新的服務帳戶。KES 使用此服務帳戶向 GCP 進行身份驗證,並存取 Secret Manager。

  5. 將一個或多個角色指派給新帳戶

    如果您想快速開始,請指派 Secret Manager 管理員角色。然而,這會授予 KES 不需要的更多權限。

    或者,為 KES 建立一個新的角色,並具有所需的最小權限

     secretmanager.secrets.create
     secretmanager.secrets.delete
     secretmanager.secrets.get
    
  6. 透過 動作 - 建立金鑰,為服務帳戶建立金鑰

    使用 JSON 金鑰格式。

    GCP 讓您下載一個具有以下結構的 JSON 檔案

    {
      "type":           "service_account",
      "project_id":     "<your-project-id>",
      "private_key_id": "<your-private-key-id>",
      "private_key":    "-----BEGIN PRIVATE KEY-----\n ... -----END PRIVATE KEY-----\n",
      "client_email":   "<your-service-account>@<your-project-id>.iam.gserviceaccount.com",
      "client_id":      "<your-client-id>"
    }
    

    使用此憑證檔案來設定 KES 以向 GCP 進行身份驗證,並存取 SecretManager。

KES 伺服器設定

KES 伺服器需要 TLS 私密金鑰和憑證。

KES 伺服器是 預設安全 的,且只能在 TLS 的情況下執行。本教學為簡化起見,使用自我簽署憑證。

對於生產環境設定,我們強烈建議使用由信任的憑證授權機構簽署的憑證。這可以是您的內部 CA 或像是 Let’s Encrypt 的公用 CA。
  1. 為 KES 伺服器產生 TLS 私密金鑰和憑證

    以下命令會產生一個新的 TLS 私密金鑰 server.key 和一個自我簽署的 X.509 憑證 server.cert,該憑證是為 IP 127.0.0.1 和 DNS 名稱 localhost(作為 SAN)頒發的。請自訂命令以符合您的設定。

    kes tool identity new --server --key server.key --cert server.cert --ip "127.0.0.1" --dns localhost
    

    任何其他用於產生 X.509 憑證的工具也可以使用。例如,您可以使用 openssl

    $ openssl ecparam -genkey -name prime256v1 | openssl ec -out server.key
    
    $ openssl req -new -x509 -days 30 -key server.key -out server.cert \
        -subj "/C=/ST=/L=/O=/CN=localhost" -addext "subjectAltName = IP:127.0.0.1"
    
  2. 為應用程式建立私密金鑰和憑證

    kes tool identity new --key=app.key --cert=app.cert app
    

    您可以隨時計算 app 身分。

    kes tool identity of app.cert
    
  3. 建立 組態檔 server-config.yml

    address: 0.0.0.0:7373
    root:    disabled  # We disable the root identity since we don't need it in this guide 
    
    tls:
      key : server.key
      cert: server.cert
    
    policy:
      my-app:
        allow:
        - /v1/key/create/my-app*
        - /v1/key/generate/my-app*
        - /v1/key/decrypt/my-app*
        identities:
        - ${APP_IDENTITY}
    
    keystore:
      gcp:
        secretmanager:
          project_id: "<your-project-id>"                  # Use your GCP project ID
          credentials:
            client_email: "<your-client-email>"            # Use the client email from your GCP credentials file
            client_id: "<your-client-id>"                  # Use the client ID from your GCP credentials file
            private_key_id: "<your-private-key-id"         # Use the private key ID from your GCP credentials file
            private_key: "-----BEGIN PRIVATE KEY----- ..." # Use the private key from your GCP credentials file
    
  4. 在新視窗/分頁中啟動 KES 伺服器

    Linux

    export APP_IDENTITY=$(kes tool identity of app.cert)
    
    kes server --config=server-config.yml --auth=off
    
    該命令使用 --auth=off,因為我們的 root.certapp.cert 憑證是自我簽署的。

    容器

    以下說明使用 Podman 來管理容器。您也可以使用 Docker。

    根據您的部署需要修改位址和檔案路徑。

    sudo podman pod create  \
      -p 9000:9000 -p 9001:9001 -p 7373:7373  \
      -v ~/minio-kes-gcp/certs:/certs  \
      -v ~/minio-kes-gcp/minio:/mnt/minio  \
      -v ~/minio-kes-gcp/config:/etc/default/  \
      -n minio-kes-gcp
    
    sudo podman run -dt  \
      --cap-add IPC_LOCK  \
      --name kes-server  \
      --pod "minio-kes-gcp"  \
      -e KES_SERVER=https://127.0.0.1:7373  \
      -e KES_CLIENT_KEY=/certs/kes-server.key  \
      -e KES_CLIENT_CERT=/certs/kes-server.cert  \
      quay.io/minio/kes:2024-01-11T13-09-29Z server  \
        --auth  \
        --config=/etc/default/kes-config.yaml  \
    
    sudo podman run -dt  \
      --name minio-server  \
      --pod "minio-kes-gcp"  \
      -e "MINIO_CONFIG_ENV_FILE=/etc/default/minio"  \
      quay.io/minio/minio:RELEASE.2024-01-31T20-20-33Z server  \
        --console-address ":9001"
    

    您可以使用以下命令驗證容器的狀態。該命令應顯示三個 Pod,一個用於 Pod、一個用於 KES,另一個用於 MinIO。

    sudo podman container list
    
  5. 在其他視窗或分頁中,連線到伺服器

    export KES_CLIENT_CERT=app.cert
    export KES_CLIENT_KEY=app.key
    kes key create -k my-app-key
    
    export APP_IDENTITY=$(kes tool identity of app.cert)
    
    kes server --config=server-config.yml --auth=off
    
    該命令使用 --auth=off,因為我們的 root.certapp.cert 憑證是自我簽署的。

    現在,如果您前往 GCP Secret Manager,您應該會看到一個名為 my-app-key 的機密金鑰。

  6. 從先前建立的 my-app-key 衍生並解密資料金鑰

    kes key derive -k my-app-key
    {
      plaintext : ...
      ciphertext: ...
    }
    
    kes key decrypt -k my-app-key <base64-ciphertext>
    

搭配 MinIO 伺服器使用 KES

MinIO 伺服器需要 KES 才能啟用伺服器端資料加密。

請參閱 KES for MinIO 指示指南,了解使用您的新 KES 伺服器搭配 MinIO 伺服器所需的其他步驟。

組態參考

以下章節說明金鑰加密服務 (KES) 的組態設定,以使用 Google Cloud Secret Manager 作為根 KMS 來儲存外部金鑰,例如用於 MinIO 伺服器上的伺服器端加密的金鑰。

MinIO 伺服器需要擴充的權限
MinIO 伺服器 RELEASE.2023-02-17T17-52-43Z 開始,MinIO 需要擴充的 KES 權限才能正常運作。本節中的範例組態包含所有需要的權限。