Blog / Agent Infrastructure

How to Secure AI Agents Running Google Cloud Commands

Seen from above, a clay orca surfaces in the one narrow lead of open water that runs through a vast sheet of white and pale-blue pack ice, its edges glowing cyan.

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 checkout errorskey 1
  • Read payments errorskey 2
  • List instanceskey 3
  • Get one instancekey 4

Your agent holds every key, and each one can leak.

One key behind a gateway

  • Read checkout errorsrule
  • Read payments errorsrule
  • List instancesrule
  • Get one instancerule

Your agent holds tool calls. The key stays in the gateway.

Figure 1. Swap a key per job for one key and a rule per job.

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.

Figure 2. Codex sends a tool call and gets your errors back. The key never leaves the gateway.

Your key can read

  • xAll logs from payments and web
  • yYour project's audit logs
  • zErrors from checkout

Your agent can read

  • xAll logs from payments and web403
  • yYour project's audit logs403
  • zErrors from checkoutallowed
Figure 3. Your key can reach x, y and z. Your rules let the agent reach only z.

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 api in one project, nothing else

Allowed

What failed in api this week?

13 8 6 4 3 2 4 × 1

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

Figure 4. Our live run: the read returned 42 errors. Google received 2 requests, both for that read.

Built on II Agent Runtime

This is backed by II Agent Runtime, open infrastructure for controlling what your agents can reach. Try it yourself:

intelligent-iterations / ii-agent-runtimeSecure. Deploy. Measure. Open infrastructure for agent harnesses. View on GitHub