Using GCP Secret Manager as a Secrets Backend
Bruin supports using Google Cloud Secret Manager as a secrets backend for managing connection credentials. This is controlled via the --secrets-backend flag.
Enabling GCP Secret Manager
To use GCP Secret Manager as your secrets backend, pass the flag:
bruin run --secrets-backend gcpYou can also set the backend via environment variable:
export BRUIN_SECRETS_BACKEND=gcp
bruin runConfiguring the GCP Connection
Bruin authenticates with Application Default Credentials (ADC), the same way gcloud and the Google Cloud client libraries do. On GKE, GCE and Cloud Run, and with a workload identity federation config, no Bruin-specific credentials are needed:
export BRUIN_GCP_PROJECT=my-project
bruin run --secrets-backend gcpEnvironment Variables
| Variable | Required | Description |
|---|---|---|
BRUIN_GCP_PROJECT | No | The project that holds the secrets. Defaults to the project the credentials report. See Project. |
BRUIN_GCP_CREDENTIALS_FILE | No | Path to a credentials file to use instead of ADC. Any type ADC accepts works: a service account key, an external_account workload identity federation config, an impersonated_service_account config, or authorized_user credentials. |
BRUIN_GCP_CREDENTIALS_JSON | No | The contents of a credentials file, as an alternative to BRUIN_GCP_CREDENTIALS_FILE. Set only one of the two. |
BRUIN_GCP_IMPERSONATE_SERVICE_ACCOUNT | No | Email of a service account to impersonate before reading secrets, e.g. bruin-secrets@my-project.iam.gserviceaccount.com. See Impersonating a service account. |
BRUIN_GCP_SECRET_PREFIX | No | Prepended to connection names to form secret IDs, e.g. bruin-prod-. See Namespacing secrets with a prefix. |
BRUIN_GCP_SECRET_VERSION | No | The secret version to read for every connection: latest (the default), a version number, or a version alias. |
Credential Precedence
Bruin picks credentials in this order and uses the first that is available:
BRUIN_GCP_CREDENTIALS_JSONorBRUIN_GCP_CREDENTIALS_FILE, if set.- Application Default Credentials, which itself tries, in order:
- The file
GOOGLE_APPLICATION_CREDENTIALSpoints at. This is how a workload identity federation config is usually passed in. - The gcloud application default credentials file, written by
gcloud auth application-default login. - The metadata server: GKE Workload Identity, or the service account attached to a GCE VM or Cloud Run service.
- The file
If BRUIN_GCP_IMPERSONATE_SERVICE_ACCOUNT is set, Bruin then impersonates that service account using whichever credentials were found above, and reads secrets as that service account.
To see which source was used, run with --debug. Bruin logs the credential type and where it came from, for example GCP Secret Manager: using external_account credentials from GOOGLE_APPLICATION_CREDENTIALS. It never logs credential values.
Project
Bruin reads secrets from the first project it finds:
BRUIN_GCP_PROJECTGOOGLE_CLOUD_PROJECT- The project the credentials report: the
project_idof a service account key, or the project of the GKE cluster or GCE VM from the metadata server - The quota project of the credentials:
quota_project_idin the credentials file, orGOOGLE_CLOUD_QUOTA_PROJECT
Bruin fails at startup with no GCP project configured for GCP Secret Manager when none of these is set. Workload identity federation configs don't name a project, so set BRUIN_GCP_PROJECT with them. Also set it when the secrets live in a different project from the credentials.
Example: Local Development
gcloud auth application-default login
export BRUIN_GCP_PROJECT=my-project
bruin run --secrets-backend gcpExample: Service Account Key
export BRUIN_GCP_CREDENTIALS_FILE=/path/to/service-account.json
bruin run --secrets-backend gcpThe project defaults to the key's project_id. Prefer workload identity over keys wherever you can.
Impersonating a Service Account
Set BRUIN_GCP_IMPERSONATE_SERVICE_ACCOUNT to read secrets as another service account, for example one that only has access to Bruin's secrets:
export BRUIN_GCP_IMPERSONATE_SERVICE_ACCOUNT=bruin-secrets@my-project.iam.gserviceaccount.com
export BRUIN_GCP_PROJECT=my-projectThe credentials Bruin found need roles/iam.serviceAccountTokenCreator on that service account. The project is resolved from the original credentials, so set BRUIN_GCP_PROJECT if the secrets are in the impersonated service account's project.
Storing Secrets in GCP Secret Manager
Each connection is one secret, whose ID is the connection name. The secret value is a JSON string with two required fields:
type: The connection type (must match a valid Bruin connection type)details: An object containing the connection-specific configuration
Example: PostgreSQL Connection
Create a secret named my-postgres:
printf '%s' '{
"type": "postgres",
"details": {
"host": "localhost",
"port": 5432,
"username": "myuser",
"password": "mypassword",
"database": "mydatabase",
"schema": "public"
}
}' | gcloud secrets create my-postgres --project my-project --data-file=-To change it later, add a new version. Bruin reads the latest version by default:
printf '%s' '{"type": "postgres", "details": {...}}' | gcloud secrets versions add my-postgres --data-file=-Example: Snowflake Connection
Create a secret named my-snowflake with this value:
{
"type": "snowflake",
"details": {
"account": "my-account",
"username": "myuser",
"password": "mypassword",
"warehouse": "my-warehouse",
"database": "my-database",
"schema": "my-schema"
}
}Connection Names
Secret IDs may only contain letters, digits, underscores and hyphens (a-z, A-Z, 0-9, _, -) and be at most 255 characters long. They are case-sensitive. Connection names stored in Secret Manager must follow the same rule, including any prefix. A connection named my.postgres can't be stored in Secret Manager; Bruin reports:
connection 'my.postgres' can't be read from GCP Secret Manager: secret 'my.postgres' is not a valid secret ID, which may only contain letters, digits, underscores and hyphens (a-z, A-Z, 0-9, _, -) and be at most 255 characters; rename the connection to matchRename the connection in both the secret and the assets that use it. Bruin doesn't rewrite names for you.
Namespacing Secrets with a Prefix
Set BRUIN_GCP_SECRET_PREFIX to keep Bruin's secrets apart from others in the same project, or to keep one set of secrets per environment in a shared project:
export BRUIN_GCP_SECRET_PREFIX=bruin-prod-With this prefix, the connection my-postgres is read from the secret bruin-prod-my-postgres. The prefix must only use letters, digits, underscores and hyphens, since secret IDs can't contain /. Without a prefix, the secret ID is the connection name. Using a separate project per environment is often simpler than a prefix.
Listing Connections
bruin connections list --secrets-backend gcp lists the connections stored in the project without a local .bruin.yml:
bruin connections list --secrets-backend gcp -o jsonBruin lists the secrets in the project (only those starting with BRUIN_GCP_SECRET_PREFIX, if set) and reads the configured version of each. Secrets without that version, with the version disabled or destroyed, and secrets that aren't in the Bruin format are skipped.
IAM Permissions
Bruin only needs to read secrets.
roles/secretmanager.secretAccessorlets Bruin read secret values (secretmanager.versions.access). Grant it on the project, or on each secret Bruin reads.bruin connections listalso needssecretmanager.secrets.liston the project.roles/secretmanager.secretAccessordoesn't include it;roles/secretmanager.viewerdoes, or use a custom role.
gcloud projects add-iam-policy-binding my-project \
--role=roles/secretmanager.secretAccessor \
--member=serviceAccount:bruin-secrets@my-project.iam.gserviceaccount.com
# Only needed for `bruin connections list`.
gcloud projects add-iam-policy-binding my-project \
--role=roles/secretmanager.viewer \
--member=serviceAccount:bruin-secrets@my-project.iam.gserviceaccount.comTo grant access to a single secret instead of the whole project:
gcloud secrets add-iam-policy-binding my-postgres \
--project=my-project \
--role=roles/secretmanager.secretAccessor \
--member=serviceAccount:bruin-secrets@my-project.iam.gserviceaccount.comWith workload identity, the --member is the workload's principal instead of a service account, as shown below.
Running on Kubernetes
Bruin picks up the pod's identity through Application Default Credentials, so no BRUIN_GCP_* credentials are needed. Don't set BRUIN_GCP_CREDENTIALS_FILE or BRUIN_GCP_CREDENTIALS_JSON in the pod: they take precedence over the pod's identity.
GKE Workload Identity
On GKE, use Workload Identity Federation for GKE so the Bruin pod's Kubernetes ServiceAccount has a Google Cloud identity. Autopilot clusters have it enabled; on Standard clusters enable it on the cluster and its node pools:
gcloud container clusters update my-cluster --location=europe-west1 \
--workload-pool=my-project.svc.id.goog
gcloud container node-pools update my-pool --cluster=my-cluster --location=europe-west1 \
--workload-metadata=GKE_METADATAThen give the ServiceAccount access to the secrets in one of two ways.
Direct principal binding (recommended). Grant the roles to the Kubernetes ServiceAccount itself. 123456789012 is the project number of the cluster's project:
gcloud projects add-iam-policy-binding my-project \
--role=roles/secretmanager.secretAccessor \
--member=principal://iam.googleapis.com/projects/123456789012/locations/global/workloadIdentityPools/my-project.svc.id.goog/subject/ns/docs/bruin/sa/bruinThe ServiceAccount needs no annotation:
apiVersion: v1
kind: ServiceAccount
metadata:
name: bruin
namespace: bruinThrough a Google service account. Grant the roles to a Google service account as shown in IAM Permissions, allow the Kubernetes ServiceAccount to act as it, and annotate the Kubernetes ServiceAccount with its email:
gcloud iam service-accounts add-iam-policy-binding bruin-secrets@my-project.iam.gserviceaccount.com \
--role=roles/iam.workloadIdentityUser \
--member="serviceAccount:my-project.svc.id.goog[bruin/bruin]"apiVersion: v1
kind: ServiceAccount
metadata:
name: bruin
namespace: bruin
annotations:
iam.gke.io/gcp-service-account: bruin-secrets@my-project.iam.gserviceaccount.comEither way, run the Bruin pod with that ServiceAccount:
apiVersion: v1
kind: Pod
metadata:
name: bruin
namespace: bruin
spec:
serviceAccountName: bruin
containers:
- name: bruin
image: ghcr.io/bruin-data/bruin:latest
# /project holds your Bruin project, e.g. baked into the image or mounted from a volume.
command: ["bruin", "run", "--secrets-backend", "gcp", "/project"]
env:
- name: BRUIN_GCP_SECRET_PREFIX
value: bruin-prod-Bruin gets credentials and the project from the GKE metadata server. Set BRUIN_GCP_PROJECT if the secrets are in another project.
Workload Identity Federation (EKS, AKS and Other Clusters)
A cluster outside Google Cloud, such as EKS or AKS, can read GCP Secret Manager without a service account key through Workload Identity Federation. The pod exchanges its Kubernetes ServiceAccount token for Google credentials, using a credential configuration file that Bruin picks up through GOOGLE_APPLICATION_CREDENTIALS.
Find the cluster's OIDC issuer URL:
bash# EKS aws eks describe-cluster --name my-cluster --query cluster.identity.oidc.issuer --output text # AKS (needs --enable-oidc-issuer) az aks show --resource-group my-rg --name my-cluster --query oidcIssuerProfile.issuerUrl -o tsvCreate a workload identity pool and an OIDC provider for the cluster:
bashgcloud iam workload-identity-pools create bruin --project=my-project --location=global gcloud iam workload-identity-pools providers create-oidc my-cluster \ --project=my-project \ --location=global \ --workload-identity-pool=bruin \ --issuer-uri=https://oidc.eks.eu-west-1.amazonaws.com/id/EXAMPLED539D4633E53DE1B71EXAMPLE \ --attribute-mapping=google.subject=assertion.subGrant the roles to the pod's Kubernetes ServiceAccount, whose subject is
system:serviceaccount:<namespace>:<name>.123456789012is the project number ofmy-project:bashgcloud projects add-iam-policy-binding my-project \ --role=roles/secretmanager.secretAccessor \ --member=principal://iam.googleapis.com/projects/123456789012/locations/global/workloadIdentityPools/docs/bruin/subject/system:serviceaccount:bruin:bruinTo act as a Google service account instead, grant that service account the roles, give the principal above
roles/iam.workloadIdentityUseron it, and add--service-account=<email>in the next step.Create the credential configuration. It says where the pod's token is mounted and contains no secrets:
bashgcloud iam workload-identity-pools create-cred-config \ projects/123456789012/locations/global/workloadIdentityPools/docs/bruin/providers/my-cluster \ --credential-source-file=/var/run/service-account/token \ --credential-source-type=text \ --output-file=credential-configuration.json kubectl create configmap gcp-credential-configuration -n bruin \ --from-file=credential-configuration.jsonRun the Bruin pod with a projected ServiceAccount token for the provider's audience, the configuration mounted, and
GOOGLE_APPLICATION_CREDENTIALSpointing at it:yamlapiVersion: v1 kind: ServiceAccount metadata: name: bruin namespace: bruin --- apiVersion: v1 kind: Pod metadata: name: bruin namespace: bruin spec: serviceAccountName: bruin containers: - name: bruin image: ghcr.io/bruin-data/bruin:latest command: ["bruin", "run", "--secrets-backend", "gcp", "/project"] env: - name: GOOGLE_APPLICATION_CREDENTIALS value: /etc/workload-identity/credential-configuration.json - name: BRUIN_GCP_PROJECT value: my-project volumeMounts: - name: token mountPath: /var/run/service-account readOnly: true - name: workload-identity-credential-configuration mountPath: /etc/workload-identity readOnly: true volumes: - name: token projected: sources: - serviceAccountToken: audience: https://iam.googleapis.com/projects/123456789012/locations/global/workloadIdentityPools/docs/bruin/providers/my-cluster expirationSeconds: 3600 path: token - name: workload-identity-credential-configuration configMap: name: gcp-credential-configuration
The pod's IRSA or AKS workload identity setup, if any, keeps working alongside this: the token above is a separate projected token with Google's audience.
How It Works
When you run Bruin with --secrets-backend gcp:
- Bruin resolves credentials and the project as described in Credential Precedence and Project
- For each connection referenced in your pipeline, Bruin reads the configured version (
latestby default) of the secret with that ID (withBRUIN_GCP_SECRET_PREFIXprepended, if set) - The secret is parsed and validated according to the connection type
- Each secret is fetched once and cached in memory for the duration of the run, even when several assets use it at the same time
Troubleshooting
Credentials Not Found
failed to initialize GCP Secret Manager client: failed to find GCP credentials: ...No credentials were found. Run gcloud auth application-default login locally, set GOOGLE_APPLICATION_CREDENTIALS or BRUIN_GCP_CREDENTIALS_FILE, or check that the pod or VM has a service account. On GKE, check that the node pool uses GKE_METADATA.
Project Not Found
no GCP project configured for GCP Secret Manager: set BRUIN_GCP_PROJECT, ...Set BRUIN_GCP_PROJECT to the project that holds the secrets.
Secret Not Found
secret 'my-connection' (version 'latest') in GCP project 'my-project' not found in GCP Secret ManagerCheck that the secret exists in this project with the connection's name (plus BRUIN_GCP_SECRET_PREFIX, if set), and that it has the version Bruin reads (an enabled version, or the BRUIN_GCP_SECRET_VERSION alias).
Permission Denied
permission denied reading secret 'my-connection' (version 'latest') in GCP project 'my-project': the identity needs the secretmanager.versions.access permission on the secret, ...The identity authenticated but can't read the secret. Grant roles/secretmanager.secretAccessor as shown in IAM Permissions. When the role is granted on single secrets rather than the project, a secret that doesn't exist is also reported as permission denied, so check the name too. IAM changes can take a few minutes to apply.
For bruin connections list, the error names secretmanager.secrets.list, which has to be granted on the project.
Secret Manager API Not Enabled
the Secret Manager API is not enabled in GCP project 'my-project'; enable it with `gcloud services enable secretmanager.googleapis.com --project my-project` and retry after a few minutesRun the command from the message. With user credentials from gcloud auth application-default login, the project named here is the credentials' quota project, which may differ from the project that holds the secrets. The change takes a few minutes to apply.
Workload Identity Federation Errors
If the token exchange fails, run with --debug to confirm Bruin uses external_account credentials from GOOGLE_APPLICATION_CREDENTIALS, then check that:
- the projected token's
audiencematches the provider (https://iam.googleapis.com/projects/<number>/locations/global/workloadIdentityPools/<pool>/providers/<provider>, or the provider's--allowed-audiences) - the provider's issuer URL is the cluster's OIDC issuer
- the
principal://member uses the project number and the exactsystem:serviceaccount:<namespace>:<name>subject