Provisioning a session agent
Result: a tagged non-root agent on expiring API keys, a tagged provisioner that
may register keys on that agent and nothing else, the minting row's policies,
and a renewal loop the agent host runs by itself. Pick the branch first: first
provisioning runs steps 1 to 5; renewal runs step 5 only; recovery runs
one branch of step 6. A renewal never repeats bootstrap, replaces a policy, or
recreates the agent. Inputs: the root profile (admin), AGENT_TAG,
PROVISIONER_TAG, and HUMAN_APPROVER_TAG set to the tag ids from
bootstrapping, the minting row chosen from
approval-models.md, the agent host, and the provisioner host.
Reference
- sessions:
session request,provision,activate, andstatusrecords and errors. - resources:
user createflags,api-keyrecords,policy createflags. - authentication:
profile create,login, andwhoamirecords and the credential file location. - activities:
activity approveandactivity geton a pending mint.
Rules
- Two principals, two credential stores: different users, different profiles, different homes. A provisioner that can read the agent's key file is the agent.
- The agent generates every keypair. Only
data.publicKeyanddata.userIdcross to the provisioner. Neither is secret, but the handoff is authenticated and integrity-checked, and the provisioner accepts a key only for the organization, agent user id, and lifetime it expects. - Policy sees
activity.params.user_idand each key'sexpiration_seconds, not the target's tags. The mint ALLOW pins both the target set and the lifetime cap; keep the cap at or above the--expires-inthe renewal loop passes. The approver readsdata.userId,data.publicKey, anddata.expiresInfrom thesession provisionrecord. Every id in the mint ALLOW'sin [...]list is resolved withuser getand shown before the policy is written; a root-quorum id there mints keys on root. session statusdefaults to--warn-before 48h; pass a window shorter than the lifetime.- Deleting a local key file revokes nothing;
api-key deleteby a scoped revoker does (revoking-agent-keys). - Persist
AGENT_USER_ID: after expirysession requestreportsuserId: null.
Instructions
Create the agent's profile. Agent host, as the agent's OS user. The private key is generated here and stays here; send
<!-- example: session.profile-create -->data.publicKey(PUBLIC_KEY) to root.tk --message-format json --organization-id ORG_UUID profile create --profile-name agentCreate the agent user with its first expiring key. Root. The anchor key meets Turnkey's one-permanent-credential rule; its private half is discarded on the spot.
<!-- example: session.user-create -->tk --profile admin --message-format json user create --user-name agent --tag-name agent --public-key PUBLIC_KEY --expires-in 7d --anchor-keySet
AGENT_USER_IDtodata.activity.result.createUsersResult.userIds[0]and persist it.Create the provisioner and the minting row's policies. Root. The provisioner's key comes from step 1's command run on the provisioner host as
<!-- example: session.provisioner-create -->--profile-name provisioner:tk --profile admin --message-format json user create --user-name provisioner --tag-name provisioner --public-key PROVISIONER_PUBLIC_KEYSet
PROVISIONER_USER_IDto its id. Apply the minting row's set from policy-patterns.md:Policy Shape provisioners-mint-agent-keyspinned to AGENT_USER_IDand a lifetime cap; drop theHUMAN_APPROVER_TAGclause in the unilateral-minting rowprovisioners-nothing-elseas written provisioners-no-self-keysone per provisioner agents-no-credentialsas written The two that carry ids:
<!-- shared: provisioners-mint-agent-keys --> <!-- example: session.policy-mint -->
<!-- shared: provisioners-no-self-keys --> <!-- example: session.policy-self-deny -->tk --profile admin --message-format json policy create --name provisioners-mint-agent-keys --effect allow \ --consensus "approvers.any(user, user.tags.contains('${PROVISIONER_TAG}')) && approvers.any(user, user.tags.contains('${HUMAN_APPROVER_TAG}'))" \ --condition "activity.type == 'ACTIVITY_TYPE_CREATE_API_KEYS_V2' && activity.params.user_id in ['${AGENT_USER_ID}'] && activity.params.api_keys.all(k, k.expiration_seconds <= 604800)"tk --profile admin --message-format json policy create --name provisioners-no-self-keys --effect deny \ --consensus "approvers.any(user, user.tags.contains('${PROVISIONER_TAG}'))" \ --condition "activity.type == 'ACTIVITY_TYPE_CREATE_API_KEYS_V2' && activity.params.user_id == '${PROVISIONER_USER_ID}'"Adding an agent later edits the
in [...]list. Before the edit, resolve every id in the final list withtk --profile admin --message-format json user get --id USER_ID, confirm each is the intended non-root user carryingAGENT_TAG(orBROKER_TAG), show the final target set, and stop on any mismatch: a root-quorum user's id there lets the provisioner mint a key on root. Step 5 run by the real users is the acceptance.Log in as the agent. Agent host;
<!-- example: session.login -->data.identity.userIdmust equalAGENT_USER_ID.tk --message-format json login --profile-name agentRenew. The loop the agent host runs on a timer. Agent host first:
<!-- example: session.status -->tk --message-format json session status --profile-name agent --warn-before 1hExit
<!-- example: session.request -->0withdata.secondsLeft: done. Exit1session_expiring: go on.tk --message-format json session request --profile-name agentSave
<!-- example: session.provision -->data.publicKeyanddata.userId; the request is remembered per profile untilactivateconsumes it. Provisioner host:tk --profile provisioner --message-format json session provision --user-id AGENT_USER_ID --public-key PUBLIC_KEY --expires-in 7dHuman-minting rows:
<!-- example: session.activate -->status: "pending"; the human checksdata.userId,data.publicKey, anddata.expiresIn, then runstk --profile approver --message-format json activity approve --id ACTIVITY_ID; the provisioner's rerun returnscompletedwithalreadyRegistered: true. Unilateral row: the first run completes withdata.apiKeyId. Agent host:tk --message-format json session activate --profile-name agent tk --profile agent --message-format json whoamiactivateverifies the new key withwhoamibefore repointing the profile and removes the previous key file only iftkstored it (data.previousKeyFileRemoved); the old key stays valid until it expires.Recover. Pick the branch that matches the observed state.
- Expired before renewal:
whoamifailsunauthorized;session requeststill succeeds but reportsuserId: null. Pass the persistedAGENT_USER_IDtosession provision.activateworks as soon as the key is registered, becauseactivateauthenticates with the new key. - Rejected mint or stale request: the agent's request stays pending locally; after a
rejection the provisioner's rerun finds no registered key and submits a new activity for
the same public key. Have the human approve that activity, or on the agent host run
tk --message-format json session request --profile-name agent --replaceto discard the keypair (its generated file is removed) and redo step 5. - Concurrent renewal: one writer per profile. A second
session requestwhile one is pending failsinvalid_inputnaming the pending public key; never--replacefrom a second process.
- Expired before renewal:
Hand off. Report
AGENT_USER_ID,AGENT_TAG, and the agent's profile name and host;PROVISIONER_USER_ID,PROVISIONER_TAG, and the provisioner's profile name and host; the minting row in force; each policy's name and id; the current key'sdata.expiresAtfromsession status; the step-5 results from the real users, which are the acceptance; any mint activity id still awaiting approval; and the exact step-5 commands each host runs and the agent host's timer interval. The capability workflows start from this identity: managing-secrets for exports, signing-git-commits for commit signing, and using-ssh for SSH. Stop.
Verified by
| Examples | Test |
|---|---|
| session.profile-create, session.user-create, session.provisioner-create, session.policy-mint, session.policy-self-deny, session.login, session.status, session.request, session.provision, session.activate | sessions::provisioning_session_agent_human_mint_human_export |
| session.provision, session.activate | sessions::provisioning_session_agent_human_mint_unilateral_export |
| session.provision, session.activate | sessions::provisioning_session_agent_unilateral_mint_human_export |
| session.provision, session.activate | sessions::provisioning_session_agent_unilateral_mint_unilateral_export |
| session.policy-mint, session.policy-self-deny, session.provision | sessions::provisioning_session_agent_provisioner_cannot_self_mint |
| session.policy-mint | sessions::provisioning_session_agent_unpinned_allow_leaves_self_mint_pending |
| session.policy-mint, session.provision | sessions::provisioning_session_agent_mint_allow_caps_key_lifetime |
| session.request, session.provision, session.activate | sessions::provisioning_session_agent_recovers_after_expiry |
| session.status, session.request, session.activate | sessions::session_loop_rotates_an_agent_profile_and_reports_status |
Troubleshooting
session requestfailsinvalid_input"already pending": activate it, or--replaceif that key was never registered. "Unknown profile": do step 1.session activatefailsunauthorized: the key is not registered yet.activity getthe provision activity, approve or wait, rerunactivate; the profile is unchanged until it succeeds.session statusexits1session_expiring: run step 5; firing every run means the window exceeds the lifetime.session provisionfailsunauthorized403: no ALLOW selects this provisioner for this target,--expires-inexceeds the mint ALLOW's lifetime cap, or the self DENY fired. Inspect withpolicy evaluationswhen an activity id exists. Do not widen the condition to every user; do not use root.
Related Skills
- bootstrapping-organization: the tags and approval model this assumes.
- provisioning-agent-identity: the long-lived alternative when nobody can approve renewals.
- monitoring-activities: approve, reject, or wait on a pending mint.
- sidecar-patterns: where the renewal loop runs and how its alert reaches an operator.
- revoking-agent-keys: cut off a session key before it expires.