Skip to content
Agentic Cyber Kit
Published

Project 1

Making AI agents safer with an open-source gateway

AI agents can do more than search files or send email. They can query databases, update records, run code, create tickets, manage cloud services, and hand work to other agents. Depending on their access, they may also make decisions or take actions without a person reviewing each step. In this project, I put a security checkpoint between an AI agent, the AI model it talks to, and the tools it can use.

I tried the setup on my own computer and checked six areas of security using a framework from the U.S. National Institute of Standards and Technology (NIST).

Tools used: agentgateway, a local Kubernetes setup called kind · Time: about 90 minutes

Some of the gateway’s settings for Kubernetes are still early versions. The lab guide explains what that means.

01 / The goal

The goal

The goal of this project is to make AI agent systems easier to understand and safer to use. I’m exploring practical ways to manage what agents can access, check the messages they send, and keep a record of their actions, then sharing small, repeatable labs so others can learn and build on them.

02 / The problem

The problem

An AI agent is software that can ask an AI model questions and use tools to take action. For example, it might ask a model to summarize a document, then use a file tool to find that document.

When agents connect directly to models and tools, it can be hard to keep track of what they can access or what they’ve done. If something goes wrong, it may be unclear which agent made a request.

03 / The approach

The approach

I put a gateway between the agents, the AI model, and the tools. Think of it as a checkpoint: each request passes through it, where it can check who’s asking, whether the request is allowed, and what should be recorded.

I used agentgateway, an open-source project. In this lab, it checks each agent’s identity, limits which tools it can use, looks for certain sensitive information, and records requests.

How the setup works

  • Agent: the program making a request. In my lab, it’s a small Python script.
  • Gateway: the checkpoint that checks requests.
  • AI model: the service that generates an answer. I use a pretend model for the lab.
  • Tools: services the agent can use, such as email, files, or databases. My lab uses a demo tool server.

The agent sends a request to the gateway. The gateway checks it before passing it to the model or a tool.

AI agent

The one asking

In my lab: a small Python script

In real life: a terminal agent such as Claude Code, Codex CLI or Gemini CLI

Claude CodeCodex CLIGemini CLIPython

Gateway

The security guard

1. Who is asking?

2. Is it allowed?

3. Is it safe to send?

4. Write it in the log

In my lab and in real life: agentgateway

AI model

The brain that answers

In my lab: a fake model

In real life: Claude Opus, Claude Sonnet, GPT-5, Gemini 2.5 Pro

Claude Opus, Claude SonnetGPT-5Gemini 2.5 Pro

Tools (MCP)

What the agent can do

In my lab: a demo tool server

In real life: email, files, GitHub, databases

EmailFilesGitHubDatabases
MCP is a common way for AI agents to connect to tools.

04 / Architecture

Architecture

Lab architecture: agentgateway on a kind clusterOn the laptop, check.py or curl acts as the orchestrator and worker agents and calls agentgateway-proxy through a port-forward on port 8080 with a Bearer token; tokens.py signs the JWTs and gives the proxy its public key. Inside the kind cluster, the agentgateway controller programs the proxy in the agentgateway-system namespace. The proxy checks the JWT, applies tool rules, prompt guards and a rate limit, writes logs, and forwards to mock-llm with the provider key read from a Secret, and to the MCP server with allowed tools only, both in the agentic-lab namespace.Agentic gateway lab — agentgateway v1.6.0 on kindLegendagent trafficconfiguration, keyslocal envcheck.py / curlacts as the agentsorchestrator, workertokens.pystand-in identity providersigns JWTs (private key)kind cluster: agenticnamespace: agentgateway-systemagentgateway controllercontrol planewatches Gateway, HTTPRoute,Backend and Policy resourcesagentgateway-proxydata plane · single policy point1. JWT check (agent identity)2. Tool rules per agent (CEL)3. Prompt guards (req / resp)4. Rate limit per agent5. Access logs and metricsPolicy: trusted public key (JWKS)namespace: agentic-labmock-llmOpenAI-compatible APIService :8000Secret: llm-provider-keynever given to an agentMCP server: everythingService :3001 /mcpecho — any agentget-sum — orchestrator onlyget-env — nobody (sensitive)port-forward :8080Bearer <agent token>LLM API /v1+ provider keyMCP /mcpallowed tools onlymints tokensprograms (config)read by gatewayverifies tokenspublic key (kubectl apply)
fig. 1 — every call leaves the agent through one policy point.

How to read it

  • Controller and proxy are separate. The controller turns Kubernetes resources into configuration; the proxy is what traffic passes through.
  • One policy point. Agents never talk to the model or the tool server directly.
  • Agents are identities. orchestrator and worker are tokens I sign myself; the gateway trusts only the public key.
  • The provider key stays with the gateway. Agents present their own identity and never hold the model provider's key.

05 / Six areas I looked at

Six areas I looked at

I used the six functions of the NIST Cybersecurity Framework (CSF) 2.0 to organize the project:

  • Govern: Who sets the rules?
  • Identify: What systems and risks are involved?
  • Protect: How can harmful actions be prevented?
  • Detect: How will we know when something happens?
  • Respond: What should we do if something goes wrong?
  • Recover: How do we return to normal?

GOVERN

Who owns this and what are the rules?

What I did

  • I keep every rule as a file in Git.
  • I pinned the versions of the gateway, the Gateway API and the MCP server.
  • Every resource has an owner label.
  • Only namespaces I labelled can publish routes on the gateway.
  • The token signing key stays out of Git.

Base practices

  • Assign ownership: every agent, tool server, model and policy has a named owner and a risk tier.
  • Write the rules of use: which data may go to which model, which actions need a human approval.
  • Change policy only through review: pull request, second reviewer, CI validation, pipeline deploy.
  • Control the supply chain: an approved catalogue of models and MCP servers, pinned and reviewed before upgrade.
  • Gate onboarding: a team publishes through the gateway only after registering its agent and tools.
  • Keep evidence and report: Git history answers "who allowed this and when".

IDENTIFY

What do I have and what can go wrong?

What I did

  • I listed the backends, routes and policies straight from the cluster and wrote a one-page threat model.
  • Then I ran my attack script against the unprotected setup to get a baseline: no identity needed, a secret reaches the model, every agent sees every tool, and one tool returns the server's environment with a password I planted.

Base practices

  • Keep a live inventory generated from gateway configuration and logs, not a spreadsheet.
  • Find shadow usage: teams calling model APIs or MCP servers directly.
  • Classify tools (read-only, writing, destructive) and data by sensitivity; flag agents that combine untrusted input, private data and a way to send data out.
  • Threat-model every agent path before go-live with one template.
  • Measure the baseline and repeat after each significant change.

PROTECT

What stops a bad action?

What I did

  • I added JWT authentication on the gateway, so every call carries an agent identity.
  • I wrote one tool rule in CEL: anyone can use echo, only the orchestrator can use get-sum, nobody can use get-env.
  • I added prompt guards that reject a prompt containing an AWS key or a card number and mask emails and keys in answers.
  • The gateway injects the provider key from a Secret, so the agents never hold it.
  • Each agent gets 5 LLM requests per minute.
yaml
matchExpressions:
- 'mcp.tool.name == "echo" || (jwt.sub == "orchestrator" && mcp.tool.name == "get-sum")'

Base practices

  • Give every agent its own short-lived identity from the corporate identity provider; no shared keys.
  • Least privilege on tools: deny by default, allow per agent, human approval for destructive actions.
  • Hold provider keys centrally and rotate them.
  • Limit rate and spend per identity.
  • Inspect content in both directions; start rules in audit mode, then enforce.
  • Close the bypass: the gateway must be the only route to models and tools.
  • Train the people who build and review agents.

DETECT

How do I know something happened?

What I did

  • I read one access log for both model calls and tool calls.
  • Each line has the agent identity, the route, the status and a reason; tool calls also have the tool name.
  • I also checked the Prometheus metrics for MCP requests, guardrail checks and token usage.
log
2026-10-04T15:02:58.323097Z	error	request gateway=agentgateway-system/agentgateway-proxy listener=http route=agentic-lab/llm src.addr=127.0.0.1:53068 http.method=POST http.host=localhost http.path=/v1/chat/completions http.version=HTTP/1.1 http.status=429 jwt.sub=worker protocol=http error="rate limit exceeded" reason=RateLimit duration=0ms

Base practices

  • Centralise gateway logs in the security monitoring platform.
  • Define a short list of alerts from a baseline: bursts of denied calls, any guardrail rejection, first use of a tool, repeated rate-limit hits.
  • Watch cost and volume per agent.
  • Trace end to end with OpenTelemetry.
  • Keep an audit trail for decisions an agent contributes to.
  • Test the detections on a schedule.

RESPOND

What do I do about it?

What I did

  • My scenario: the worker agent's token is stolen.
  • I apply a deny policy I prepared in advance.
  • It blocks that one identity on every route while the orchestrator keeps working.
  • Then I rotate the signing key so every old token stops working, and I commit the change under an incident reference.

Base practices

  • Write runbooks for agent incidents: stolen credential, malicious tool, prompt injection, runaway agent.
  • Prepare and test containment levers in advance: block one agent, hide one tool, close one route.
  • Revoke and rotate centrally.
  • Scope the incident from the gateway logs.
  • Record and communicate under an incident reference.
  • Exercise it and measure time to contain.

RECOVER

How do I get back to normal?

What I did

  • I delete the cluster and rebuild it from the repository only, then I run the proof script again.
  • The rebuild time is my recovery time for this setup.
  • The one thing that is not in Git is the signing key, so I re-issue it.

Base practices

  • Make everything rebuildable from Git through a pipeline.
  • Roll back by reverting a commit, then run automated control tests before reopening traffic.
  • Set recovery objectives and prove them with a timed rebuild.
  • Protect what is not in Git: keys and secrets.
  • Be able to undo what agents changed.
  • Feed lessons back into the rules, the threat model and the policies.

I also collected the base practices in a separate note.

06 / What I tested

What I tested

I ran a script before and after turning on the gateway’s protections. The tests showed that the gateway could, for example:

  • Block requests from callers without an identity.
  • Stop a request containing a sample secret from reaching the model.
  • Limit which tools each agent could use.
  • Block access to a tool containing sensitive information.
  • Slow down an agent making too many requests.

In the results below, “Blocked” is the expected outcome for requests that should not be allowed. The lab guide includes the full output from each check.

Results of the eleven checks before and after the gateway policies
#CheckBeforeAfter
1Caller with no identity reaches the LLMAllowed (200)Blocked (401)
2Expired tokenAllowed (200)Blocked (401)
3Orchestrator with a valid token reaches the LLMAllowedAllowed
4Prompt containing an AWS keyReaches the modelBlocked (403)
5Model answer containing an email and a keyReturned as isMasked
6Tools each agent can seeBoth see all 13Orchestrator 2, worker 1
7Worker calls a tool reserved for the orchestratorAllowedBlocked
8Any agent dumps the tool server's environmentPassword exposedBlocked
9Orchestrator calls its own toolAllowedAllowed
10Caller with no identity opens an MCP sessionAllowed (200)Blocked (401)
11Worker loops eight LLM callsAll servedThrottled after 5 (429)

From my run

These are exemple from running the lab on my laptop. The full output for each step is in the lab guide.

A FAIL means the open behaviour the baseline script expects is gone.

baseline script re-run after the controls · 4 Oct 2026
[FAIL] PROTECT  1. Caller with no identity reaches the LLM -> allowed (no policy yet)
         HTTP 401
[FAIL] PROTECT  4. Prompt containing an AWS key -> allowed (no policy yet)
         HTTP 403 Blocked by gateway: the prompt contains a secret or card number.
[FAIL] PROTECT  5. Model answer containing an email and a key -> returned as is
         HTTP 200 content":"Sure, the admin contact is <EMAIL_ADDRESS> and the key is <masked>.","role":"assistant"},"index":0,"
[FAIL] IDENTIFY 6. Tool inventory seen by each agent
         orchestrator sees 2: ['echo', 'get-sum'] | worker sees 1: ['echo']
[FAIL] PROTECT  10. Caller with no identity opens an MCP session -> allowed (no policy yet)
         HTTP 401
[FAIL] PROTECT  11. Worker loops 8 LLM calls in a row -> all served
         200 200 200 200 200 429 429 429
gateway access log · 4 Oct 2026
2026-10-04T15:02:58.323097Z	error	request gateway=agentgateway-system/agentgateway-proxy listener=http route=agentic-lab/llm src.addr=127.0.0.1:53068 http.method=POST http.host=localhost http.path=/v1/chat/completions http.version=HTTP/1.1 http.status=429 jwt.sub=worker protocol=http error="rate limit exceeded" reason=RateLimit duration=0ms
2026-10-04T15:02:58.326208Z	error	request gateway=agentgateway-system/agentgateway-proxy listener=http route=agentic-lab/llm src.addr=127.0.0.1:53074 http.method=POST http.host=localhost http.path=/v1/chat/completions http.version=HTTP/1.1 http.status=429 jwt.sub=worker protocol=http error="rate limit exceeded" reason=RateLimit duration=0ms
2026-10-04T15:02:58.331921Z	error	request gateway=agentgateway-system/agentgateway-proxy listener=http route=agentic-lab/llm src.addr=127.0.0.1:53076 http.method=POST http.host=localhost http.path=/v1/chat/completions http.version=HTTP/1.1 http.status=429 jwt.sub=worker protocol=http error="rate limit exceeded" reason=RateLimit duration=0ms
See all outputs in the lab guide

07 / What I plan to improve next

What I plan to improve next

This is a small demonstration, and there are several things I’d improve before using an approach like this in a real system:

  • Use Kubernetes network policies to stop agents from reaching the model or tools except through the gateway.
  • Use a proper identity service instead of a script to identify agents.
  • Add guardrails to check messages passed between agents for unsafe or sensitive content.
  • Improve the guardrail rules to catch more patterns of prompt injection and data leakage.
  • Encrypt traffic between parts of the setup.
  • Send logs somewhere they can be collected and reviewed.

feedback

Questions or corrections? Open an issue on the repo.