Infrastructure · Chapter 2 of 3 · 3 min

Service Accounts and Least Privilege

Machines have identities too. Who builds, who deploys, what the running site may touch, and how to give each one only what it needs.

Machines need identities

In the cloud, programs act under an identity just like people do. Cloud Build pushes images as someone. The running site reads Firestore as someone. These machine identities are service accounts, and their permissions decide the damage a mistake or an attacker can do.

Least privilege, in one sentence

Give every identity the smallest set of permissions it needs to do its job, and nothing for "just in case". If the running site never deletes a storage bucket, it should not be able to.

The identities behind this site

Identity Needs to Should not be able to
The build's service account Push images to the registry; deploy the Cloud Run service Read user data in Firestore
The Cloud Run runtime service account Read and write Firestore; read and write its buckets; read its secrets Deploy services, change permissions, delete buckets
The scheduler Call one protected address on the site Anything else
You, the owner Everything, carefully Use your own account inside automation

The default account trap

When a project is new, many services run as the project's default compute service account, which often has the broad Editor role: it can change almost anything in the project. It works, which is why it lingers. But if the site is ever compromised, the attacker inherits Editor.

The fix is to create a dedicated service account for the running site, grant it only the roles it needs, and tell Cloud Run to use it:

gcloud iam service-accounts create site-runtime --project=PROJECT_ID --display-name="Site runtime"

gcloud projects add-iam-policy-binding PROJECT_ID \
  --member="serviceAccount:site-runtime@PROJECT_ID.iam.gserviceaccount.com" \
  --role="roles/datastore.user"

gcloud run services update iamravi-site --region asia-south1 --project=PROJECT_ID \
  --service-account=site-runtime@PROJECT_ID.iam.gserviceaccount.com

Add storage and Secret Manager roles the same way, ideally granted on the specific bucket or secret rather than the whole project. Then watch the logs for permission errors after the switch: each one is either a role you forgot or a capability the site should not have had.

Narrow the scope, not just the role

A role can be granted on the whole project or on one resource. Granting Secret Accessor on one secret is much safer than on every secret in the project. Granting storage access on one bucket is safer than on all of them.

Protecting scheduled jobs

The health check on this site is started by Cloud Scheduler calling an address on the site. That address must not be open to the world, or anyone could trigger it repeatedly. Here it requires a shared secret in a request header, compared in constant time, and the job is refused with a 401 if the header is wrong. If the secret is not set on the server at all, the job answers 503 instead of running unprotected. A test checks each of these cases.

People, too

Try it yourself

Find out which service account your running service uses:

gcloud run services describe SERVICE --region REGION --project=PROJECT_ID --format="value(spec.template.spec.serviceAccountName)"

If it is the default compute account, list its roles. Anything broader than what the service needs is your first infrastructure fix.