GCP Secret Manager
Secret Agent can authenticate with GCP Secret Manager using the following methods:
Attached service account
This is the recommended way to authenticate with GCP because it does not
require any credentials in the Secret Agent configuration file. When Secret
Agent runs on a GCP VM with a service account attached, it automatically
uses the credentials of that service account. The attached service account
must have the Secret Manager Secret Accessor role.
To configure Secret Agent on a GCP VM with an attached service account:
- Create a secret in GCP Secret Manager. In this example, the secret is named
TestingSecret. - Create a service account with the
Secret Manager Secret Accessorrole. In this example, the service account is namedtest-service-account@xxx.iam.gserviceaccount.com. - Create a VM with the
test-service-account@xxx.iam.gserviceaccount.comservice account attached. From the GCP console:- Click Create Instance in VM Instances under Compute Engine.
- Go to Identity and API access.
- Select
test-service-account@xxx.iam.gserviceaccount.comin Service account.
- Install Secret Agent on the GCP VM.
- Configure Secret Agent to fetch secrets from GCP Secret Manager.
- Start Secret Agent.
Sample configuration file:
service: tcp: endpoint: 0.0.0.0:3005
secret-manager: gcp: resources: TestRes: projects/xxxxxxxxxxxx/secrets/TestingSecret/versions/latest
log: level: infoApplication Default Credentials
Application Default Credentials (ADC) is a strategy that the Google Cloud client libraries use to automatically find credentials based on the application environment. With ADC, you can provide credentials to Secret Agent by setting an environment variable, placing a credentials file on disk, or specifying a credential file path directly in the Secret Agent configuration.
Use ADC when Secret Agent runs outside of GCP (for example, on-premises or in another cloud provider), when the GCP VM does not have a service account attached, or when you need to authenticate with a different service account from the one attached to the VM.
Secret Agent looks for Application Default Credentials in the following order:
- Secret Agent configuration file.
- The environment variable
GOOGLE_APPLICATION_CREDENTIALS. - The default credentials file
~/.config/gcloud/application_default_credentials.json. - GCP VM attached service account credentials.
To configure Secret Agent with Application Default Credentials:
- Create a service account with the
Secret Manager Secret Accessorrole. In this example, the service account is namedtest-service-account@xxx.iam.gserviceaccount.com. - Create a secret in GCP Secret Manager. In this example, the secret is named
TestingSecret. - Download the key file in JSON format for the
test-service-account@xxx.iam.gserviceaccount.comservice account. - Install Secret Agent on the machine.
- Configure Secret Agent and set the
credential-fileparameter to the path of the downloaded service account credential file. - Start Secret Agent.
Sample configuration file:
service: tcp: endpoint: 0.0.0.0:3005
secret-manager: gcp: resources: TestRes: projects/xxxxxxxxxxxx/secrets/TestingSecret/versions/latest credential-file: PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE
log: level: infoService account impersonation
You can use service account impersonation with either of the preceding authentication methods (attached service account and Application Default Credentials). With impersonation, you start with an authenticated principal (your Google Account or a service account) and request short-lived credentials for a service account that has authorization to access secrets. The authenticated principal must have permission to impersonate the target service account.
Service account impersonation is more secure than using a service account key because it requires a prior authenticated identity, and the credentials created through impersonation do not persist. By comparison, authenticating with a service account key requires no prior authentication, and the persistent key is a high-risk credential if exposed.
To configure Secret Agent with service account impersonation:
- Create a secret in GCP Secret Manager. In this example, the secret is named
TestingSecret. - Create a service account with the
Service Account Token Creatorrole. In this example, this service account is namedbase-service-account@xxx.iam.gserviceaccount.com. - Create a service account with the
Secret Manager Secret Accessorrole. In this example, this service account is namedtest-service-account@xxx.iam.gserviceaccount.com. - Verify that the
IAM Service Account Credentials APIis enabled for your GCP project. - Create a GCP VM with
base-service-account@xxx.iam.gserviceaccount.comattached. From the GCP console:- Click Create Instance in VM Instances under Compute Engine.
- Go to Identity and API access.
- Select
base-service-account@xxx.iam.gserviceaccount.comin Service account.
- Install Secret Agent on the GCP VM.
- Configure Secret Agent to fetch secrets from GCP Secret Manager.
Set
impersonatetotest-service-account@xxx.iam.gserviceaccount.comin the configuration file. This service account has permission to access secrets. - Start Secret Agent.
Sample configuration file:
service: tcp: endpoint: 0.0.0.0:3005
secret-manager: gcp: resources: TestRes: projects/xxxxxxxxxxxx/secrets/TestingSecret/versions/latest credential-file: PATH_TO_BASE_SERVICE_ACCOUNT_CREDENTIAL_FILE impersonate: test-service-account@xxx.iam.gserviceaccount.com
log: level: infoAccessing secrets across projects
You can access secrets across GCP projects using the same service account credentials.
- Create a service account with the
Secret Manager Secret Accessorrole inProject1. In this example, the service account is namedtest-service-account-project1@xxx.iam.gserviceaccount.com. - Create a secret in another GCP project (
Project2). In this example, the secret is namedTestingSecretProject2. - Download the key file in JSON format for
test-service-account-project1@xxx.iam.gserviceaccount.com. - Grant
test-service-account-project1@xxx.iam.gserviceaccount.comtheSecret Manager Secret Accessorrole inProject2. From the GCP console:- Select
Project2from the project list. - Go to Grant Access in IAM.
- Add
test-service-account-project1@xxx.iam.gserviceaccount.comas a principal and assign theSecret Manager Secret Accessorrole.
- Select
- Install Secret Agent on the machine.
- Configure Secret Agent and set the
credential-fileparameter to the path of the downloaded service account credential file. - Start Secret Agent.
Sample configuration file for cross-project access:
service: tcp: endpoint: 0.0.0.0:3005
secret-manager: gcp: resources: TestResProj2: projects/yyyyyyyyyyyy/secrets/TestingSecretProject2/versions/latest credential-file: PATH_TO_SERVICE_ACCOUNT_CREDENTIAL_FILE
log: level: infoAccessing multiple versions of a secret
Secrets in GCP Secret Manager can have multiple versions. To access specific versions, define each as a separate resource in the configuration file:
service: tcp: endpoint: 0.0.0.0:3005
secret-manager: gcp: resources: TestResV1: projects/yyyyyyyyyyyy/secrets/TestingSecret/versions/1 TestResV2: projects/yyyyyyyyyyyy/secrets/TestingSecret/versions/2 TestResLatest: projects/yyyyyyyyyyyy/secrets/TestingSecret/versions/latest
log: level: info