The Simplest Way to Make GCP Secret Manager and Terraform Work Like They Should

Published
Length
3 min read

Luis Felipe Dias

All posts

Using gcp secret manager and terraform lets teams create secret containers through code. This setup keeps passwords out of source control while automating cloud builds. Teams define the secret shell in code first. Then a separate process or person adds the actual secret value later. This division of work stops raw keys from leaking into git repos. It also gives every app a steady path to read its keys. How gcp secret manager and terraform handle roles and data flow Terraform talks to Google

Your coding agents run with your access.

Claude Code reaches prod with a developer's credentials, and approval prompts get clicked through. hoop.dev checks every query, masks sensitive data, and holds risky changes for a human.

Using gcp secret manager and terraform lets teams create secret containers through code. This setup keeps passwords out of source control while automating cloud builds.

Teams define the secret shell in code first. Then a separate process or person adds the actual secret value later. This division of work stops raw keys from leaking into git repos. It also gives every app a steady path to read its keys.

How gcp secret manager and terraform handle roles and data flow

Terraform talks to Google Cloud through an identity with specific cloud roles. When a build runs, the tool asks the cloud API to make a secret. This first step creates an empty secret container with a set name. It also sets up rules for where the cloud stores that container.

Next, the build tool attaches identity rules to that specific secret container. These rules state which app service accounts can read the secret value. A human or an outside deploy script then writes the secret version. At runtime, the app logs in with its own cloud identity. The app asks the secret service for the latest version and starts up.

Some teams also use the build tool to read existing secrets. During a run, the tool fetches a token or password from the vault. It then passes that value to another cloud resource, like a database. This flow works fast, but it creates a big risk in state files.

Common failure modes and how to scope roles

The biggest mistake with these two tools involves the state file. Terraform saves every value it reads or creates into a plain state file. If the tool writes a secret version, that secret sits in the state. Anyone who can read the state bucket can read every database password.

To fix this, never put raw secret payloads inside your code files. Use the build tool only to make the empty secret container and roles. Let a secure deploy pipeline or a manager upload the actual secret string. Store the state file in a private cloud bucket with strict read rules. Turn on bucket encryption and block all public access to that storage location.

GCP Secret Manager + Terraform Security (tfsec, Checkov): architecture patterns and best practices

Continue reading? Get the full guide.

Free. No spam. Unsubscribe anytime.

Shared service accounts cause another major problem for cloud teams. Many teams give their build runner one huge admin role for convenience. If an attacker steals that runner key, they own every secret in the cloud. Instead, give the runner permission to manage secret containers, not read values. Grant read permissions only to the specific app that needs that one secret.

Auditing secret reads across cloud builds and runtime

Good role scoping is only half of the security job. Teams also need clear logs showing who used each secret and when. Turn on data access logs in your cloud project to track secret reads. Check these logs often to spot odd reads from build runners or engineers.

When engineers fix a broken app, they often ask for direct database keys. Handing out secret values from the vault breaks your audit trail. The cloud log shows one secret read, but not what the engineer ran. Keep human users away from raw secret values whenever you can. Tie every database query and server check back to an individual user identity.

Check database and API queries on the wire

Identity tools, roles, and network rules decide who opens a session. Setup alone cannot check what each query does or mask sensitive values in a response once a session starts. When workflows touch Postgres, MySQL, SQL Server, MongoDB, ClickHouse, Kubernetes APIs, SSH, or internal HTTP APIs, hoop.dev runs beside those services as an open-source proxy, a sidecar for your Postgres or API. It inspects requests coming in and results going back on the wire.

Your login details and client tools stay as they are while hoop.dev guardrails refuse bad deletes or drops before they run. On responses, hoop.dev data masking rewrites matched sensitive values in memory. Furthermore, hoop.dev holds SOC 2 Type II, is MIT licensed, and writes every query and decision to an audit trail that generates evidence for HIPAA, GDPR, and PCI DSS. See the source on GitHub.

Frequently asked questions

Does Terraform store Google Cloud secrets in plain text?

Yes, if Terraform creates or reads a secret version, it saves the value. That raw string goes straight into the state file as plain text. Create only the secret container in code and add the value outside Terraform.

How should a build pipeline log in to Google Cloud?

Use workload identity federation instead of long-lived service account keys. This method lets your pipeline trade its local token for a short cloud token. You never have to store a static cloud key in your build tool.

Should one service account read all project secrets?

No, binding roles at the project level gives every app too much power. Attach the secret accessor role directly to each single secret container instead. This setup ensures an app can only read the exact keys it needs.

The query runs. The emails stay hidden.