Secrets and Security · Chapter 2 of 3 · 3 min

Moving Secrets into Secret Manager

Step by step: create each secret, give only the running site access, swap the plain variable for a secret reference, verify, and rotate. No code changes needed.

Why move them

As plain Cloud Run environment variables, secrets are visible to anyone who can view the service settings, appear in gcloud run services describe output, and have no history or rotation support. Secret Manager stores them encrypted, records every version, controls exactly who may read each one, and logs access.

On this site the secrets started life as plain environment variables, which is common for a new project. Moving them is the step this chapter walks through.

The good news: the code does not change

Cloud Run can present a Secret Manager secret as an environment variable. The app still reads process.env.NAME. Only where the value comes from changes.

Step 1: list the secrets

Use the classification from the previous chapter. Write the list down: name, what it is for, where to get a new value if it must be rotated.

Step 2: create each secret

Read the value without leaving it in your shell history, and create the secret from standard input:

read -rs VALUE
printf '%s' "$VALUE" | gcloud secrets create EMAIL_API_KEY \
  --project=PROJECT_ID --replication-policy=automatic --data-file=-
unset VALUE

printf '%s' avoids adding a trailing newline, a classic cause of "the key is right but it does not work".

Step 3: let only the running site read it

Grant the runtime service account the Secret Accessor role on that one secret, not on the whole project:

gcloud secrets add-iam-policy-binding EMAIL_API_KEY --project=PROJECT_ID \
  --member="serviceAccount:RUNTIME_SA_EMAIL" \
  --role="roles/secretmanager.secretAccessor"

Step 4: swap the plain variable for the secret

A name cannot be both a plain variable and a secret reference, so remove one and add the other in the same update:

gcloud run services update iamravi-site --region asia-south1 --project=PROJECT_ID \
  --remove-env-vars=EMAIL_API_KEY \
  --update-secrets=EMAIL_API_KEY=EMAIL_API_KEY:latest

This creates a new revision. Cloud Run sends it traffic only once it starts, so if something is wrong with access, the old revision keeps serving.

Do one or two secrets at a time, not all at once. If a step fails, you know exactly which one.

Step 5: verify

Step 6: make sure deploys do not undo it

The build file uses --update-env-vars for the one variable it sets, which leaves secret references alone. A deploy command that used --set-env-vars, or listed the old plain values, would put them back. Read the deploy step once after the move.

Rotation

With secrets in one place, rotation becomes routine:

  1. create the new key at the provider;
  2. add it as a new version: printf '%s' "$NEW" | gcloud secrets versions add NAME --data-file=- --project=PROJECT_ID;
  3. deploy or update the service so a new revision reads latest;
  4. check that the feature works;
  5. revoke the old key at the provider, then disable the old version.

Pinning :latest means new revisions pick up the newest version. Running instances keep the value they started with until they are replaced.

The local side

Locally, .env stays as it is. It never leaves the laptop and Git ignores it. If you rotate a production secret, update .env only if you also use that key locally; better still, use separate test keys for local work.

Try it yourself

Pick the least critical secret you have and move it first, following the six steps. Once one has gone through cleanly, the rest are repetition.