使用 Secret Manager 設定密鑰

您可以搭配 Vertex AI Pipelines 使用 Secret Manager 的 Python 用戶端,存取儲存在 Secret Manager 中的密鑰。

使用 Google Cloud 控制台建立密鑰

  1. 在 Google Cloud 控制台中啟用 Secret Manager API

  2. 前往 Cloud 控制台的「Secret Manager」頁面。

    前往 Secret Manager 頁面

  3. 在 Secret Manager 頁面中,按一下「建立密鑰」

  4. 在「建立密鑰」頁面的「名稱」下方,輸入密鑰名稱 (例如 `universe-secret`)。

  5. 如要在建立初始密鑰時新增密鑰版本,請在「Secret value」(密鑰值) 欄位中輸入密鑰值 (例如 42)。

  6. 選擇你所在的國家/地區。

  7. 按一下「建立密鑰」按鈕。

使用以 Python 函式為基礎的元件建構及執行管道

以下是範例元件,會列印先前建立的密鑰。

  1. 授予執行管道的服務帳戶 Secret Manager 權限。詳情請參閱「為 Vertex AI Pipelines 設定專案 Google Cloud 」一文的「設定具有精細權限的服務帳戶」一節。

  2. 使用 Kubeflow Pipelines SDK 建構只有一項工作的簡單 pipeline。

     from kfp import compiler
     from kfp import dsl
    
     # A simple component that prints a secret stored in Secret Manager
     # Be sure to specify "google-cloud-secret-manager" as one of packages_to_install
     @dsl.component(
         packages_to_install=['google-cloud-secret-manager']
     )
     def print_secret_op(project_id: str, secret_id: str, version_id: str) -> str:
         from google.cloud import secretmanager
    
         secret_client = secretmanager.SecretManagerServiceClient()
         secret_name = f'projects/{project_id}/secrets/{secret_id}/versions/{version_id}'
         response = secret_client.access_secret_version(request={"name": secret_name})
         payload = response.payload.data.decode("UTF-8")
         answer = "The secret is: {}".format(payload)
         print(answer)
         return answer
    
     # A simple pipeline that contains a single print_secret task
     @dsl.pipeline(
         name='secret-manager-demo-pipeline')
     def secret_manager_demo_pipeline(project_id: str, secret_id: str, version_id: str):
         print_secret_task = print_secret_op(project_id, secret_id, version_id)
    
     # Compile the pipeline
     compiler.Compiler().compile(pipeline_func=secret_manager_demo_pipeline,
                                 package_path='secret_manager_demo_pipeline.yaml')
    
  3. 使用 Vertex AI SDK 執行 pipeline。

     from google.cloud import aiplatform
    
     parameter_values = {
         "project_id": PROJECT_ID,
         "secret_id": SECRET_ID,
         "version_id": VERSION_ID
     }
    
     aiplatform.init(
         project=PROJECT_ID,
         location=REGION,
     )
    
     job = aiplatform.PipelineJob(
         display_name=f'test-secret-manager-pipeline',
         template_path='secret_manager_demo_pipeline.yaml',
         pipeline_root=PIPELINE_ROOT,
         enable_caching=False,
         parameter_values=parameter_values
     )
    
     job.submit(
         service_account=SERVICE_ACCOUNT
     )
    

    更改下列內容:

    • PROJECT_ID:這個管道執行的 Google Cloud 專案。
    • SECRET_ID:先前步驟中建立的密碼 ID (例如 universe-secret)。
    • VERSION_ID:密鑰的版本名稱。
    • REGION:這個管道執行的區域。
    • PIPELINE_ROOT:指定管道服務帳戶可存取的 Cloud Storage URI。管道執行作業的構件會儲存在管道根目錄中。
    • SERVICE_ACCOUNT:您使用 Secret Manager 存取者權限建立的服務帳戶電子郵件地址。

job.submit() 函式的輸出內容中,您應該可以按一下連結,前往 Google Cloud 控制台查看管道執行作業。