GCP Secret Manager
Secret Agent can fetch secrets from GCP Secrets Manager in the following ways:
Attaching a Service Account
This is the recommended way to fetch secrets from GCP, because it does not require any
GCP credentials to be explicitly specified in the configuration file. When Secret Agent
runs on a GCP VM with a service account attached to it, it automatically uses the
credentials of the attached service account. The attached service account must have a
Secret Manager Secret Accessor
role attached to it.
Use the following steps to configure Secret Agent on GCP VM and fetch the secrets:
- Create a secret in the GCP secret manager. In this example, the secret is named
TestingSecret
. - Create a service account with the
Secret Manager Secret Accessor
role. In this example, the service account is namedtest-service-account@xxx.iam.gserviceaccount.com
. - Create a VM with
test-service-account@xxx.iam.gserviceaccount.com
service account attached to it. From the GCP console, follow this sequence of steps.- Click
Create Instance
inVM Instances
underCompute Engine
- Go to
Identity and API access
- Select
test-service-account@xxx.iam.gserviceaccount.com
inService account
- Click
- Install Secret Agent on the GCP VM.
- Configure Secret Agent to fetch secrets from GCP Secrets Manager.
- Start the Secret Agent.
- Use the Secret Agent endpoint to fetch secrets.
The following is a sample configuration file for Secret Agent:
service:
tcp:
endpoint: 0.0.0.0:3005
secret-manager:
gcp:
resources:
TestRes: projects/xxxxxxxxxxxx/secrets/TestingSecret/versions/latest
log:
level: info
Application Default Credentials
Secret Agent can also run on any machine with GCP static credentials configured in the configuration file. You can use Application Default Credentials even when Secret Agent is running on an GCP VM instance without any service account attached to it. Application Default Credentials are also useful when Secret Agent uses a different service account from the one attached to the GCP VM instance.
Secret Agent looks for Application Default Credentials in the following order:
- Secret Agent configuration file.
- The environment variables
GOOGLE_APPLICATION_CREDENTIALS
. - The default credentials file
~/.config/gcloud/application_default_credentials.json
. - GCP VM attached service account credentials.
Use the following procedure to configure Secret Agent to use GCP service account's Application Default Credentials from GCP console:
- Create a service account with the
Secret Manager Secret Accessor
role. In this example, the service account is namedtest-service-account@xxx.iam.gserviceaccount.com
. - Create a secret in the GCP secret manager. In this example, the secret is named
TestingSecret
. - Download Key file in Json format for
test-service-account@xxx.iam.gserviceaccount.com
service account. - Install Secret Agent on the GCP VM instance.
- Configure Secret Agent and specify the downloaded service account credential file path
in the config file by setting the
credential-file
config. - Start Secret Agent.
- Use the Secret Agent endpoint to fetch secrets.
The following is a sample configuration file for Secret Agent:
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: info
Service Account Impersonation
You can use service account Impersonation mode on top of the above two modes (Attaching a Service Account & Application Default Credentials). When you use service account 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 the authorization for accessing secrets. The authenticated principal must have the necessary permissions to impersonate the service account.
Service account impersonation is more secure than using a service account key because service account impersonation requires a prior authenticated identity, and the credentials that are created by using impersonation do not persist. In comparison, authenticating with a service account key requires no prior authentication, and the persistent key is a high risk credential if exposed.
Use the following procedure to configure Secret Agent to fetch secrets by using impersonated service account:
- Create a secret in the GCP secret manager. In this example, the secret is named
TestingSecret
. - Create a service account with
Service Account Token Creator
role attached to it. In this example, this service account is namedbase-service-account@xxx.iam.gserviceaccount.com
. - Create a service account with a
Secret Manager Secret Accessor
role attached to it. In this example, this service account is namedtest-service-account@xxx.iam.gserviceaccount.com
. - Make sure that
IAM Service Account Credentials API
is enabled for GCP project. - Create a GCP VM instance with
base-service-account@xxx.iam.gserviceaccount.com
service account attached to it. To do it from the GCP console follow this sequence of steps- Click
Create Instance
inVM Instances
underCompute Engine
- Go to
Identity and API access
- Select
base-service-account@xxx.iam.gserviceaccount.com
inService account
- Click
- Install Secret Agent on the GCP VM instance.
- Configure Secret Agent to fetch secrets from GCP Secrets Manager.
Add
impersonate: test-service-account@xxx.iam.gserviceaccount.com
in the config file to impersonate this service account which has permissions to access secrets. - Start Secret Agent.
- Use the Secret Agent endpoint to fetch secrets.
The following is a sample configuration file for Secret Agent:
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: info
Accessing Secrets across projects using same service account
You can access secrets across projects using the same service account credentials. Follow the following steps to do so.
- Create a service account with the
Secret Manager Secret Accessor
role inProject1
. In this example, the service account fromProject1
is namedtest-service-account-project1@xxx.iam.gserviceaccount.com
. - Create secret in GCP project say
Project2
. In this example, the secret fromProject2
is namedTestingSecretProject2
- Download Key file in Json format for
test-service-account_project1@xxx.iam.gserviceaccount.com
service account. - Grant access to
test-service-account-project1@xxx.iam.gserviceaccount.com
inProject2
asSecret Manager Secret Accessor
. From the GCP console follow these steps as- Select
Project2
from project list. - Go to
Grant Access
in IAM - Add
test-service-account-project1@xxx.iam.gserviceaccount.com
as principal and Assign role asSecret Manager Secret Accessor
.
- Select
- Install Secret Agent on the GCP VM instance.
- Configure Secret Agent and specify the downloaded service account credential file path
in the config file by setting
credential-file
configuration parameter. - Start Secret Agent.
- Use the Secret Agent endpoint to fetch secrets.
The following is a sample configuration file for Secret Agent to access secrets across projects:
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> #Credentials file for `test-service-account-project1@xxx.iam.gserviceaccount.com`.
log:
level: info
Accessing multiple versions of Secrets
The value of the Secret in GCP Secret Manager can have multiple versions. To access multiple versions of the Secret, specify them in the configuration file as shown in the following sample 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