使用 Secret Manager 設定密鑰

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

使用 Cloud de Confiance 控制台建立密鑰

  1. 在 Cloud de Confiance 控制台中啟用 Secret Manager API。

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

    前往「Secret Manager」頁面

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

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

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

  6. 選擇你的區域。

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

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

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

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

  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 執行管道。

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

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