How to Secure AI Agents Running Google Cloud Commands

AI agent security on Google Cloud starts with keeping credentials outside the agent. Put the key behind a gateway, define the calls the agent may make, and give it only the tool it needs. In this example, Codex can read one service’s error logs without learning the Google credential.
Key takeaways:
- You don't need a million finely scoped keys to keep your agent secure.
- Put your keys behind a gateway, and your agent never learns their values.
- Define narrow rules on top of the key, and give your agent only the tool call.
- Your agent still gets the job done. In our live run, Codex read 42 real errors from our service.
Clone it yourself
We have a tool that does exactly this.
You need Node 22.16 or later, Git, and a Google Cloud project with a Cloud Run service or Cloud Function.
git clone https://github.com/intelligent-iterations/ii-agent-runtime.git
cd ii-agent-runtime
npm ci
cd example/cloud-logging-agent
npm ci
cp config.example.json config.json # set project, resourceType and resource
gcloud auth application-default login
npm run demo
npm run demo reads your service's errors through the gateway, the same call your agent makes. Then it tries two calls your agent shouldn't make, another service and a delete. The gateway refuses both before Google ever sees them.
When you run Codex (steps in the README), it works in a sandbox where a gateway controls everything it sends out. We give it one tool: read logs. Codex reads your service's errors and tells you what failed. That tool is backed by a GCP key with far more authority than the job needs.
Codex can't abuse the key, because it never has it. The gateway holds the key and checks every call against your rules. Codex never learns the key's value, even if it tries.
How it works
One scoped key per job
- Read
checkouterrorskey 1 - Read
paymentserrorskey 2 - List instanceskey 3
- Get one instancekey 4
Your agent holds every key, and each one can leak.
One key behind a gateway
- Read
checkouterrorsrule - Read
paymentserrorsrule - List instancesrule
- Get one instancerule
Your agent holds tool calls. The key stays in the gateway.
Sandbox
Codex
- Holds
- A short-lived gateway token
- Can reach
- The gateway, nothing else
- Can call
- One tool: read logs
Your machine
Gateway
- Holds
- Your Google Cloud key
- Checks
- The project and service against your rules
- Builds
- The query, from your config
Google Cloud
Cloud Logging
- Receives
- Only calls your rules allow
- Returns
- Errors from your one service
AllowedCodex asks for your service's errors. The gateway reads them with your key and sends Codex a summary.
RefusedAnother service, or a delete, stops at the gateway with HTTP 403. Google never sees it.
Your key can read
- xAll logs from
paymentsandweb - yYour project's audit logs
- zErrors from
checkout
Your agent can read
- xAll logs from
paymentsandweb403 - yYour project's audit logs403
- zErrors from
checkoutallowed
Live run · Nov 4, 2026
Setup
- Command:
npm run demo, which runs no model and makes the same calls Codex would - Service: one of our Cloud Run services,
api; ERROR and above; the last 7 days - Identity: a service account that can read one log view of
api, signed in without a key file - Rule: read errors from
apiin one project, nothing else
Allowed
What failed in api this week?
Through the gateway, with the key
42 errors read, top 10 groups shown
Refused
Read payments instead.
- projectallowed ✓
- service paymentsrefused ×
- Google keynot used
The gateway checks your rules first
403 and Google never sees it
Refused
Delete a log.
- operationdelete
- tool for itnone ×
- Google keynot used
The gateway has no write operation
403 and Google never sees it
Built on II Agent Runtime
This is backed by II Agent Runtime, open infrastructure for controlling what your agents can reach. Try it yourself: