Guardrails with organisation policy, Secret Manager and KMS
Apply an organisation policy constraint, read the rejection it causes, find the effective rule, and store a secret and key correctly.
By Invicly Tech, , in Google Cloud Engineering and Services. About 11 minutes to read.
In short
IAM decides who may act, but guardrails decide what may exist at all. Organisation policies set rules such as allowed regions or no service account keys for every project below them, Secret Manager keeps passwords and API keys out of code, and Cloud KMS manages encryption keys when you need control over them. When a guardrail blocks you, the error names the constraint, and the effective policy tells you where it came from.
Think of it like this: IAM is the list of who holds keys to the building; organisation policy is the building code that says no one may add a door to the roof, whoever holds a key.
Explained simply
IAM controls who may do things. Guardrails control what may exist at all, whoever asks. In Google Cloud the main guardrail is the Organization Policy Service. You set rules such as resources only in Europe or no service account keys, and every project under that organisation or folder must follow them.
Some rules are simple on or off switches, called boolean constraints. Others hold lists, called list constraints, such as the regions that are allowed. When something breaks a rule, the request fails with an error that names the constraint, so you know which rule to look up.
Passwords, API keys and database connection strings do not belong in code or plain environment variables. Secret Manager stores them, keeps every version, and lets you grant one service account permission to read one secret.
Google already encrypts all stored data. Cloud KMS is for when you need to control the keys yourself: where they live, when they rotate and the power to switch them off. Logging and labels complete the picture, showing who changed what and which team owns the cost.
Press Play on a diagram to walk through it one step at a time.
- 1Organisation policy
What may exist: locations, keys, public access
- 2IAM allow and deny
Who may act on which resource
- 3Secret Manager
Where credentials live and who reads them
- 4Cloud KMS
Who controls encryption keys and when they rotate
- 5Audit logs and labels
Evidence of who did what, and cost ownership
- What may exist?
- Applies to everyone
- Set on org, folder, project
- Example: only Europe
- Who may act?
- Per principal and role
- Set on any resource
- Example: Ana reads one bucket
- How are secrets and keys held?
- Per secret or key
- Versioned and audited
- Example: DB password, CMEK
In depth
Boolean and list constraints
Each organisation policy configures one constraint on one organisation, folder or project. Boolean constraints are enforced or not, for example iam.managed.disableServiceAccountKeyCreation or storage.uniformBucketLevelAccess. List constraints allow or deny values, for example gcp.resourceLocations with in:europe-locations, or iam.allowedPolicyMemberDomains to keep outside accounts out of IAM.
You write the policy as a YAML or JSON file and apply it with gcloud org-policies set-policy. You need the Organization Policy Administrator role, usually at organisation level, and an organisation to exist. Organisations created since May 2024 start with a baseline set enforced, including no service account key creation or upload and uniform bucket-level access. Without an organisation, treat this topic as design-only and write the policy files without applying them.
- Boolean: enforce: true
- List: allowedValues or deniedValues
- Value groups: in:europe-locations
- Apply: gcloud org-policies set-policy FILE
- Dry run: test a policy without blocking
Reading a rejection and finding the effective rule
When a request breaks a policy, the error usually includes the constraint name, often in the form constraints/gcp.resourceLocations, plus the value that was rejected. The exact wording and HTTP code differ between services, so search the message for constraints/. Policies are not retroactive: resources that already break a new rule keep working, and you must fix them yourself.
Next, find where the rule comes from. gcloud org-policies describe CONSTRAINT --project=ID shows only a policy set directly on the project, which may be empty. Adding --effective shows the merged result after inheritance from folders and the organisation. If the effective policy is strict but the project has none, list the policy on each ancestor folder to find the owner, then ask them for an exception or change your design, such as picking an allowed region.
- 1Read the error
Find constraints/NAME and the rejected value
- 2Effective policy
describe NAME --project=ID --effective
- 3Direct policy
describe without --effective
- 4Walk up
Check folders, then organisation
- 5Resolve
Change design or request an exception
Secret Manager in practice
A secret is a named container, and each value you store is an immutable version. The alias latest points to the newest enabled version. Create a secret with gcloud secrets create, add versions with gcloud secrets versions add using --data-file, and read one with gcloud secrets versions access latest --secret=NAME. Each version can hold up to 64 KiB.
Grant roles/secretmanager.secretAccessor on the individual secret to the runtime service account, not on the project. Cloud Run can mount a secret as a file or expose it as an environment variable, which keeps it out of your image and deploy scripts. To rotate, add a new version, roll out, then disable the old version, and destroy it only after you are sure nothing uses it. Locally, a .env file that is never committed is a fine stand-in.
- Secret = container; version = immutable value
- secretAccessor on one secret only
- Cloud Run: --set-secrets=DB_PASS=db-pass:latest
- Rotate: add, roll out, disable, then destroy
When Cloud KMS keys apply
By default Google encrypts data at rest with keys it manages, at no extra cost and with nothing for you to do. Cloud KMS lets you create your own keys in key rings in a chosen location. Services such as Cloud Storage, BigQuery and Cloud SQL can then use a customer-managed encryption key, or CMEK, for your data. To allow it, you grant the service's service agent roles/cloudkms.cryptoKeyEncrypterDecrypter on the key.
Choose CMEK when a regulation or customer contract demands control over rotation, key location or the ability to revoke access by disabling the key. It adds cost, operations work and risk: if a key version is destroyed, data encrypted with it cannot be read again. Destruction is scheduled with a waiting period for this reason. Keys and key rings cannot be deleted, only their versions destroyed, so name them carefully. Labels and audit logs on keys and secrets show who owns and uses them.
- Default: Google-managed encryption, nothing to do
- CMEK: your key, service agent gets EncrypterDecrypter
- Key ring and key are permanent; versions can be destroyed
- Destroyed key version = unreadable data
Learn it in 5 short steps
About 30m each. Track them with a timer in your plan.
- 1Set a boolean or list constraint~30m
- 2Read a policy rejection error~30m
- 3Find the effective policy~30m
- 4Store secrets in Secret Manager~30m
- 5Know when Cloud KMS keys apply~30m
Key points
- Boolean constraints are on or off; list constraints allow or deny values such as in:europe-locations.
- A constraint set on the organisation or a folder is inherited unless a lower level overrides it where that is allowed.
- gcloud org-policies describe CONSTRAINT --project=ID --effective shows the rule that actually applies.
- Store secrets in Secret Manager and grant roles/secretmanager.secretAccessor to the one service account that needs them.
- Google encrypts data at rest by default; use Cloud KMS customer-managed keys when you must control rotation, location or revocation.
Key terms
Tap a card to flip it. 0/6 known
Examples
# policy.yaml: keep new resources in European locations
name: projects/orders-lab-4821/policies/gcp.resourceLocations
spec:
rules:
- values:
allowedValues:
- in:europe-locations
# apply: gcloud org-policies set-policy policy.yaml
# check: gcloud org-policies describe gcp.resourceLocations \
# --project=orders-lab-4821 --effectiveA list constraint as a project-level policy file (needs an organisation and Organization Policy Administrator).
Enforce European locations and read the rejection
Prerequisites: an organisation, Organization Policy Administrator and a lab project. Step 1: apply a gcp.resourceLocations policy that allows in:europe-locations. Step 2: try to create a bucket in us-central1. Expected evidence: the create fails with an error naming constraints/gcp.resourceLocations, while a bucket in europe-west2 succeeds. Step 3: run describe with --effective to confirm. Rollback: gcloud org-policies delete removes the project policy so inheritance applies again. Without an organisation, write the file and the expected error as a design exercise.
cat > loc.yaml <<'EOF'
name: projects/orders-lab-4821/policies/gcp.resourceLocations
spec:
rules:
- values:
allowedValues: ["in:europe-locations"]
EOF
gcloud org-policies set-policy loc.yaml
gcloud storage buckets create gs://orders-lab-4821-us --location=us-central1
gcloud org-policies describe gcp.resourceLocations \
--project=orders-lab-4821 --effective
# rollback
gcloud org-policies delete gcp.resourceLocations --project=orders-lab-4821Give the orders API a database password from Secret Manager
Prerequisites: Secret Manager API enabled and the orders-api service account. Step 1: create the secret from standard input so the value never lands in shell history files. Step 2: grant secretAccessor on that secret to orders-api. Step 3: deploy with --set-secrets so Cloud Run injects it as DB_PASS. Expected evidence: the revision shows the secret reference, not the value. Cleanup: delete the secret.
printf '%s' "$DB_PASS" | gcloud secrets create db-pass \
--replication-policy=automatic --data-file=-
gcloud secrets add-iam-policy-binding db-pass \
--member=serviceAccount:orders-api@orders-lab-4821.iam.gserviceaccount.com \
--role=roles/secretmanager.secretAccessor
gcloud run deploy orders-api --region=europe-west2 \
--image=us-docker.pkg.dev/cloudrun/container/hello \
--set-secrets=DB_PASS=db-pass:latest
# cleanup
gcloud secrets delete db-pass --quietQuick reference
- gcloud org-policies describe CONSTRAINT --project=ID --effective shows the enforced rule
- Policy errors name the constraint: search the message for constraints/
- Boolean: enforce: true. List: allowedValues such as in:europe-locations
- gcloud secrets versions access latest --secret=NAME reads the newest value
- Grant roles/secretmanager.secretAccessor per secret, to one service account
- CMEK: grant the service agent roles/cloudkms.cryptoKeyEncrypterDecrypter on the key
- Destroying a KMS key version makes data encrypted with it unreadable
Common mistakes
- Reading a policy only on the project and missing the stricter one inherited from a folder; always check --effective.
- Putting a database password in a Cloud Run environment variable in plain text instead of referencing a secret.
- Destroying a KMS key version that still protects data, which makes that data unreadable.
Quick quiz
Pick an answer to see if you're right and why.
Practice exercises
Try each one yourself first. Use the hint if you get stuck, then compare with the solution.
- Exercise 1easy
Write a policy file that enforces uniform bucket-level access on project orders-lab-4821.
- Exercise 2medium
Store an API key as a secret, add a second version, and read version 1 and latest.
- Exercise 3hard
Create a Cloud KMS key in europe-west2 and make it the default key for the receipts bucket. List each permission step.
Check yourself
Answer in your head, reveal, then be honest with yourself.
Feel confident about this topic?
Frequently asked questions
- Can I use organisation policies without an organisation?
- No. They need an organisation resource. With a personal account, write the policy files as a design exercise or set up Cloud Identity on a domain you own.
- Are Secret Manager and Cloud KMS the same thing?
- No. Secret Manager stores small secret values such as passwords. Cloud KMS stores encryption keys that never leave the service and are used to encrypt other data.
- Does Cloud KMS cost money?
- Yes, a small monthly charge per active key version plus per-operation fees. Destroy lab key versions and note that the key ring itself cannot be deleted.