[[["容易理解","easyToUnderstand","thumb-up"],["確實解決了我的問題","solvedMyProblem","thumb-up"],["其他","otherUp","thumb-up"]],[["難以理解","hardToUnderstand","thumb-down"],["資訊或程式碼範例有誤","incorrectInformationOrSampleCode","thumb-down"],["缺少我需要的資訊/範例","missingTheInformationSamplesINeed","thumb-down"],["翻譯問題","translationIssue","thumb-down"],["其他","otherDown","thumb-down"]],["上次更新時間:2025-09-04 (世界標準時間)。"],[],[],null,["# Secure and store sensitive data using the Secret Manager connector\n\nSecret Manager is a secure and convenient storage system for\nAPI keys, passwords, certificates, and other sensitive data. Secret Manager\nprovides a central place and single source of truth to manage, access, and audit\nsecrets across Google Cloud.\n\nYou can use Workflows'\n[connector for the Secret Manager API](https://cloud.google.com/workflows/docs/reference/googleapis/secretmanager/Overview)\nto access Secret Manager within a workflow. This simplifies the\nintegration for you, because the connector handles the formatting of\nrequests, and provides methods and arguments so that you don't need to know\nthe details of the Secret Manager API. The connector also has\nbuilt-in behavior for handling retries and long-running operations. To learn\nmore about using Workflows connectors, see\n[Understand connectors](/workflows/docs/connectors).\n\nGrant the Workflows service account access to Secret Manager\n------------------------------------------------------------\n\nSecret Manager uses Identity and Access Management (IAM) for access\ncontrol. To create, manage, list, or access a secret, the appropriate\nIAM permissions must be granted at the project level and at the\nindividual resource level. For more information, see\n[Access control with IAM](/secret-manager/docs/access-control).\n\nWorkflows uses service accounts to give workflows access to\nGoogle Cloud resources. To\n[access a secret version](/secret-manager/docs/access-secret-version), you must\ngrant the Secret Manager Secret Accessor role\n(`roles/secretmanager.secretAccessor`) on the secret, project, folder, or\norganization to the service account. Learn more about\n[deploying a workflow with a user-managed service account](/workflows/docs/authentication).\n\nEnable the APIs\n---------------\n\nBefore using the Workflows' connector for the\nSecret Manager API, ensure that you enable the\nSecret Manager and Workflows APIs. \n\n### Console\n\n[Enable the APIs](https://console.cloud.google.com/flows/enableapi?apiid=secretmanager.googleapis.com,workflows.googleapis.com)\n\n### gcloud\n\n gcloud services enable secretmanager.googleapis.com workflows.googleapis.com\n\nInvoke a connector call\n-----------------------\n\nSimilar to invoking an HTTP endpoint, a connector call requires `call` and `args`\nfields. For more information, see\n[Invoke a connector call](/workflows/docs/reference/googleapis#invoke_a_connector_call).\n\nIn addition to using a call step, you can call the helper methods in an\nexpression like this: \n\n```genshi\n${googleapis.secretmanager.v1.projects.secrets.versions.accessString(secret_id, version, project_id)}\n```\n\nFor example, you can use the helper method `accessString` to retrieve the secret\ndata as a string. This is simpler than using the `access` API as the secret data\nis automatically decoded to a string format.\n\nYou can also use the helper method `addVersionString` to add a new secret value\nto an existing secret. This is simpler than using the `addVersion` API as the\nsecret data is automatically encoded to a base-64 string, which is required by\n`addVersion`.\n\nRetrieve a secret using the Secret Manager connector\n----------------------------------------------------\n\nThe following workflow demonstrates how to use the Secret Manager\nconnector to retrieve a secret.\n\n### YAML\n\n # This workflow demonstrates how to use the Secret Manager connector:\n # Retrieve a secret using three different methods\n # Expected output: the secret data (thrice)\n - init:\n assign:\n - project_id: ${sys.get_env(\"GOOGLE_CLOUD_PROJECT_ID\")}\n - secret_id: \"test-secret\" # Make sure you have this secret and it has a version of 1.\n - version: \"1\"\n # Add data to an existing secret without base-64 encoding\n - add_version_string:\n call: googleapis.secretmanager.v1.projects.secrets.addVersionString\n args:\n secret_id: ${secret_id}\n project_id: ${project_id}\n data: \"a new secret\"\n # Retrieve the secret in string format without base-64 decoding and assume\n # that the secret data is a valid UTF-8 string; if not, raise an error\n - access_string_secret:\n call: googleapis.secretmanager.v1.projects.secrets.versions.accessString\n args:\n secret_id: ${secret_id}\n version: ${version} # if not set, \"latest\" is used\n project_id: ${project_id}\n result: str_secret\n # Retrieve the secret in string format without base-64 decoding\n - access_secret:\n call: googleapis.secretmanager.v1.projects.secrets.versions.access\n args:\n name: ${\"projects/\" + project_id + \"/secrets/\" + secret_id + \"/versions/\" + version}\n result: base64_encoded_secret\n # Retrieve the secret using positional arguments in an expression\n - expression:\n assign:\n - secret_str_from_exp: ${googleapis.secretmanager.v1.projects.secrets.versions.accessString(secret_id, version, project_id)}\n - the_end:\n return:\n - ${str_secret}\n - ${text.decode(base64.decode(base64_encoded_secret.payload.data))}\n - ${secret_str_from_exp}\n\n### JSON\n\n [\n {\n \"init\": {\n \"assign\": [\n {\n \"project_id\": \"${sys.get_env(\\\"GOOGLE_CLOUD_PROJECT_ID\\\")}\"\n },\n {\n \"secret_id\": \"test-secret\"\n },\n {\n \"version\": \"1\"\n }\n ]\n }\n },\n {\n \"add_version_string\": {\n \"call\": \"googleapis.secretmanager.v1.projects.secrets.addVersionString\",\n \"args\": {\n \"secret_id\": \"${secret_id}\",\n \"project_id\": \"${project_id}\",\n \"data\": \"a new secret\"\n }\n }\n },\n {\n \"access_string_secret\": {\n \"call\": \"googleapis.secretmanager.v1.projects.secrets.versions.accessString\",\n \"args\": {\n \"secret_id\": \"${secret_id}\",\n \"version\": \"${version}\",\n \"project_id\": \"${project_id}\"\n },\n \"result\": \"str_secret\"\n }\n },\n {\n \"access_secret\": {\n \"call\": \"googleapis.secretmanager.v1.projects.secrets.versions.access\",\n \"args\": {\n \"name\": \"${\\\"projects/\\\" + project_id + \\\"/secrets/\\\" + secret_id + \\\"/versions/\\\" + version}\"\n },\n \"result\": \"base64_encoded_secret\"\n }\n },\n {\n \"expression\": {\n \"assign\": [\n {\n \"secret_str_from_exp\": \"${googleapis.secretmanager.v1.projects.secrets.versions.accessString(secret_id, version, project_id)}\"\n }\n ]\n }\n },\n {\n \"the_end\": {\n \"return\": [\n \"${str_secret}\",\n \"${text.decode(base64.decode(base64_encoded_secret.payload.data))}\",\n \"${secret_str_from_exp}\"\n ]\n }\n }\n ]\n\nWhat's next\n-----------\n\n- [Learn more about Secret Manager](/secret-manager/docs/overview)"]]