使用 Secret Manager 配置密文

您可以将 Secret Manager 的 Python 客户端与 Agent Platform Pipelines 搭配使用,以访问存储在 Secret Manager 上的密文。

使用 Google Cloud 控制台创建密文

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

  2. 转到 Cloud 控制台中的 Secret Manager 页面。

    转到 Secret Manager 页面

  3. 在 Secret Manager 页面上,点击创建密钥

  4. 创建密钥页面的“名称”下,输入密钥的名称(例如“universe-secret”)。

  5. 如需在创建初始密文时添加密文版本,请在密文值字段中输入密文的值(例如 42)。

  6. 选择您所在的国家/地区。

  7. 点击创建密钥按钮。

使用基于 Python 函数的组件构建和运行流水线

以下是一个输出组件(用于输出之前创建的 Secret)的示例组件。

  1. 授予使用 Secret Manager 权限运行流水线的服务帐号。如需了解详情,请参阅为 Agent Platform Pipelines 配置项目中的“配置具有精细权限的服务帐号” 部分。 Google Cloud

  2. 使用 Kubeflow Pipelines SDK,只需一个任务即可构建简单的流水线。

     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:此流水线在其中运行的 Google Cloud 项目。
    • SECRET_ID:在前面步骤中创建的 Secret ID(例如 universe-secret)。
    • VERSION_ID:密文的版本名称。
    • REGION:此流水线运行所在的区域。
    • PIPELINE_ROOT:指定流水线服务账号可以访问的 Cloud Storage URI。流水线运行的工件存储在流水线根目录中。
    • SERVICE_ACCOUNT:您使用 Secret Manager Accessor 权限创建的服务账号的电子邮件地址。

job.submit() 函数的输出中,您应该能够点击使您可以在 Google Cloud 控制台中查看流水线执行的 链接。