Skip to content

Agent lesson 5: Keys for agents

Let an agent use some of the organization's keys and not others - born limited to them, it encrypts and decrypts with the one it was given and is refused the rest.

Time: 20 minutes. Track: agent builder. Before this: agent lesson 2.

By the end of this lesson you will be able to:

  • start an agent that may use only some of the organization’s keys;
  • have it encrypt and decrypt through the forge, its keys never leaving the vault;
  • say what happens when it asks for a key it was not given.

Two keys

The organization’s keys live in the forge’s vault, and are used through it: nothing outside the forge ever holds one. Start a forge, and make two keys you may use, one to encrypt the app’s secrets and one, post-quantum, to sign releases:

$ af-server --dev
...
$ source ~/af-dev/dev/env
$ af kms create app-secrets --use-person dev@localhost
done: it took effect at once, on your word as an administrator
$ af kms create releases --spec ml-dsa-65 --use-person dev@localhost
done: it took effect at once, on your word as an administrator

The agent’s part is played by the small MCP client of lesson 1:

$ cat > ~/agent-session <<'END'
> #!/bin/bash
> coproc MCP { af "${@:2}" mcp "$1"; }
> say() { printf '%s\n' "$1" >&"${MCP[1]}"; }
> say '{"jsonrpc":"2.0","id":0,"method":"initialize","params":{"protocolVersion":"2025-06-18","capabilities":{},"clientInfo":{"name":"agent-session","version":"1"}}}'
> read -r _ <&"${MCP[0]}"
> say '{"jsonrpc":"2.0","method":"notifications/initialized"}'
> id=0
> while IFS= read -r step; do
>   case "$step" in
>     '! '*) eval "${step#! }" ;;
>     ?*) id=$((id + 1)); name=${step%% *}
>         say "{\"jsonrpc\":\"2.0\",\"id\":$id,\"method\":\"tools/call\",\"params\":{\"name\":\"$name\",\"arguments\":${step#* }}}"
>         read -r reply <&"${MCP[0]}"
>         case "$reply" in
>           *'"structuredContent":'*) reply=${reply#*\"structuredContent\":}; reply=${reply%,\"isError\":*} ;;
>           *'"error":'*) reply=${reply#*\"error\":}; reply=${reply%\}} ;;
>         esac
>         printf '%s -> %s\n' "$name" "$reply" ;;
>   esac
> done
> END
$ chmod +x ~/agent-session

An agent with one key

Make the agent’s own key, and start it, as in lesson 2, limited to app-secrets. It could be given no key you may not use yourself; here it is given fewer:

$ af --credential ~/helper.key key
30554fe19ccdf588726c72ffe25d128284b567e44d81a4f250500d9bed1c93f7
$ ~/agent-session ~/me <<END | tee ~/helper.birth
> start_agent {"public_key":"$(af --credential ~/helper.key key --public)","program":"secrets-helper","version":"1.0","model":"example-model","instructions":"Keep the app's secrets.","purpose":"keeps the app's secrets","keys":["app-secrets"]}
> END
start_agent -> {"agent":"c3e63981dc220bbfd83c96157eb7184c1e0b9374dc6ec4019d0eae457cc3f7f8","expires_ms":1795705469600}
$ export AGENT=$(sed -n 's/.*"agent":"\([0-9a-f]*\)".*/\1/p' ~/helper.birth)

Using it

The agent runs af mcp with its own key and ID. It encrypts a secret with the key it was given, bound to a context it must give again to decrypt, and is refused the signing key. The lab keeps the answers in a file and shows them with the ciphertext cut short, as it differs on every run:

$ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<'END' > ~/helper.out
> encrypt {"key":"app-secrets","text":"hunter2","context":{"env":"prod"}}
> sign {"key":"releases","text":"v1.0"}
> END
$ sed 's/"ciphertext":"[^"]*"/"ciphertext":"…"/' ~/helper.out
encrypt -> {"ciphertext":"…","key":"app-secrets","version":1}
sign -> {"code":-32603,"message":"PermissionDenied: refused: c3e63981dc220bbfd83c96157eb7184c1e0b9374dc6ec4019d0eae457cc3f7f8 was born limited to the keys [\"app-secrets\"], which name no key \"releases\""}

The ciphertext names its key and version, so decrypting needs only it and the context:

$ export SECRET=$(sed -n 's/^encrypt -> .*"ciphertext":"\([^"]*\)".*/\1/p' ~/helper.out)
$ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<END
> decrypt {"ciphertext":"$SECRET","context":{"env":"prod"}}
> END
decrypt -> {"base64":"aHVudGVyMg==","key":"app-secrets","text":"hunter2","version":1}

The key itself never reaches the agent: the forge does the cryptography in its vault and answers with the result, and each use is recorded, by which actor and for which person.

Exercises

  1. Decrypt with another context. What does the forge answer?

    $ ~/agent-session ~/helper --credential ~/helper.key --actor $AGENT <<END
    > decrypt {"ciphertext":"$SECRET","context":{"env":"test"}}
    > END
    decrypt -> {"code":-32603,"message":"InvalidState: the ciphertext does not open: another key, version or context, or changed"}
  2. Sign the release yourself. You may use releases; the agent you started may not.

    $ printf 'v1.0' | af kms sign releases > v1.sig
    $ printf 'v1.0' | af kms verify releases --signature "$(cat v1.sig)"
    valid

Check your understanding

Why limit an agent to some keys, when it is yours?

So that a mistake, or an agent made to do what it should not, reaches only what its task needs. An agent that keeps the app’s secrets has no reason to sign a release; born without the signing key, it cannot, whatever it is asked.

Can the agent give itself, or an agent it starts, a key it was not born with?

No. An agent never has more than its parent, and a key is part of what it has: an agent it starts can be given only keys it may use itself.

Where is the key while the agent encrypts?

In the forge’s vault, sealed. The agent sends what to encrypt and gets the ciphertext back; it never holds the key, so it cannot leak it.

Next

Agent lesson 6: An agent’s record reads what an agent did, and who answers for it. The curriculum map lists the other tracks. Keys and the vault explains the organization’s keys in depth.

Type to search…

↑↓ navigate↵ selectEsc close