Sidecar patterns
Result: a deployment where the agent reads its secrets once at startup, a separate renewal
loop keeps the agent's expiring credential fresh, SSH goes through a socket, git signs
commits through a broker's socket, and an operator hears about an expired credential from
something other than the agent. Every tk step in this workflow is a command another workflow already documents; this workflow fixes where each one runs and what state it keeps.
Reference
- sessions: the four
sessioncommands and their records. - secrets:
secret envand pending exports. - ssh-agent: the agent socket and its lifecycle.
- gpg-signing:
gpg agent serveand the client socket. - authentication: the
whoamirecord and howHOMEandTK_PROFILEselect a profile.
Rules
- Separation means one boundary (a container, VM, sandbox, or OS user), one
HOME, one process tree, and one set of mounts per principal; the agent's profile and the provisioner's profile never share a credential directory. Separate OS users are the default. Under the shared-uid socket model the application and broker share a numeric uid and nothing else, and the application mounts the broker's runtime directory read-only. Host root can still read every boundary; no boundary is a secrecy boundary against the host. - The renewal loop is the single writer of the agent's profile. Its agent-side half (status, request, activate) runs as the agent's OS user on a timer outside the agent's own control loop; its provisioner-side half (provision) runs as the provisioner. Both hold a lock while they run, update their state file atomically, and persist the agent's user id so renewal works after the current key has expired.
- Secrets go from
tk secret envinto the authorized process's environment. No file the agent can read holds a plaintext value; no transcript does. - The public-key handoff from agent to provisioner is not secret but must be authenticated: the provisioner binds the request to the expected organization, agent user id, and lifetime before it mints anything.
- The SSH socket is signing authority. Bind it under the agent's
HOME, mode-restricted to that OS user or a supplemental group for clients under another uid. A socket anything else can reach takes--allowed-hosts-file, which signs only SSH logins to listed host keys and refusesssh-keygen -Y sign(using-ssh). Only a socket shared with its one git-signing boundary carries--allow-namespace; never forward it over SSH or relay it beyond that boundary. - The OpenPGP socket is signing authority too. The broker alone holds the signing profile and serves one fingerprint. The agent's boundary gets the socket and the public key, never the broker's profile or registry, and the agent's own profile has no signing policy on that wallet.
- Alert on expiry from the loop, not from the agent: an expired agent cannot.
- A deployment that stages no provider secrets by hand still holds Turnkey credential files. Treat the host as secret-bearing.
Instructions
Inputs: the agent profile (agent) and its persisted user id (AGENT_USER_ID), the
provisioner profile (provisioner) on the sidecar, the scope prefix (service/), the
chosen approval cell, and the key lifetime and renewal lead time. Prerequisites: the
agent and provisioner exist with their minting policies
(provisioning-session-agent), and the
export policies and secrets exist
(managing-secrets steps 1 and 2).
Lay out the principals. Decide, and record, which OS user,
HOME, and profile each of these runs as: the agent process, the renewal loop (its agent half asagent, its provisioner half asprovisioner), the SSH daemon (asagent), and the GPG broker (asbroker). Installtkin the image or as a read-only host mount and checktk --versionfrom each principal's shell; a missing binary at a bind-mount path becomes an empty directory, so check the file type, not just the path.Start the agent with its environment. The agent's entrypoint runs, before the agent code starts:
<!-- shared: secret-env-startup --> <!-- example: sidecar.startup-env -->tk --profile agent --message-format json secret env --name-prefix service/ --property consensus=unilateralSource
data.envinto the process and start it. Exit1withapproval_requiredmeans an approval-gated secret is in the selection; keep those out of startup paths and fetch them on demand instead.Run the renewal loop. On a timer shorter than the lifetime minus the lead time, once per expiring principal:
<!-- example: sidecar.renew -->agentandbrokereach run it as their own OS user with their own profile, state file, and lock.status,request, andactivaterun as that principal with itsHOME;provisionruns on the sidecar as the provisioner, reading the public key and user id from the handoff:tk --message-format json session status --profile-name agent --warn-before 1h tk --message-format json session request --profile-name agent tk --profile provisioner --message-format json session provision --user-id AGENT_USER_ID --public-key PUBLIC_KEY --expires-in 4h tk --message-format json session activate --profile-name agentThe tick is a state machine, not a script that runs all four lines:
Observed Do session statusexits0nothing; the key is healthy exits 1withsession_expiringand no pending requestsession request; savedata.publicKey; ifdata.userIdisnulluse the persistedAGENT_USER_IDa pending request exists session provision;pendingmeans a human must approve;alreadyRegistered: trueorcompletedmeans proceedprovision completed session activate, thenwhoamias that principalactivate fails unauthorizedthe key is not registered yet; leave the request in place and try next tick the mint activity was rejected session request --replace, alert (step 6), start over next tickPersist the state and the request's public key in one file written atomically; lock the whole tick so two timers cannot both request. Deleting an old key file locally revokes nothing; the anchor key stays.
Serve SSH from a socket. As the agent principal, in a directory only that user can enter, serving one pinned key:
<!-- example: sidecar.ssh-agent -->install -d -m 0700 /run/agent tk --profile agent --message-format json ssh agent start --key SSH_FINGERPRINT --socket /run/agent/ssh.sock --pid-file /run/agent/ssh.pid # Clients under another uid: share the directory and socket with a supplemental group. install -d -m 0750 -g agents /run/agent tk --profile agent --message-format json ssh agent start --key SSH_FINGERPRINT --socket /run/agent/ssh.sock --pid-file /run/agent/ssh.pid --socket-mode 660Export
SSH_AUTH_SOCK=/run/agent/ssh.sockin the agent's environment. The daemon picks up the key step 3 activates on its next signature. Registration is in using-ssh.Sign commits through the broker. Run
gpg agent serveasbrokerper deploying-signing-broker. It serves one--keyon a socket in a runtime directory the agent side mounts read-only:--socket-mode 660shared through the agent's supplemental group, or600under the shared-uid model. The agent side getsTK_GPG_AGENT_SOCKand the public key, and none of the broker's profile, registry, or credential. A missing socket fails signing closed.Alert independently. The loop, not the agent, raises an alert when
session statusreportssession_expiringtwice in a row, when a mint stayspendingpast the lead time, or when a mint is rejected. Includedetails.publicKey,details.expiresAt, and the activity id. The alert path uses no Turnkey credential.Gate the deployment. Before the agent is left unattended, check by hand:
Check Expect separation the model in force: separate OS users, or under the shared-uid model one uid for agent and broker with separate HOMEs, process trees, and mountsHOMEsthe agent's holds only its own profile; the sidecar's holds only the provisioner's SSH socket directory 0700to the agent's user, or0750to the shared group with a0660socketbroker socket its model: 0660owned by the broker and the shared group, or0600owned by the shared uidtimer fires forced expiry ( --expires-inshorter than the tick)renews through the loop loop stopped the alert fires git commit -Son the agent sideverifies with the broker up, fails with it stopped secrets in the process environment and in no file Hand off. Report the principals table, the timer interval and lead time, the state file and lock paths, the socket path, the signing fingerprint, the alert channel, and the gate results. Stop.
Runtime examples
These place steps, not requirements. Example: systemd timers run step 3 as one unit
per renewed principal, User=agent and User=broker, for status, request, and activate,
and one User=provisioner unit for provision. Example: a Docker Compose deployment gives
the agent and the sidecar separate services and HOME volumes, sharing one only for the
public handoff. Example: an LLM runtime hook runs step 2 as its secrets command.
Verified by
| Examples | Test |
|---|---|
| sidecar.startup-env | secrets::managing_secrets_env_and_rotation |
| sidecar.renew | sessions::session_loop_rotates_an_agent_profile_and_reports_status |
| sidecar.renew | sessions::provisioning_session_agent_recovers_after_expiry |
| sidecar.renew | sessions::provisioning_session_agent_human_mint_unilateral_export |
| sidecar.ssh-agent | ssh_agent::using_ssh_register_serve_sign |
Step 7 is a manual deployment gate; the suite does not prove OS isolation, timers, mounts, or alert delivery.
Troubleshooting
session requestreportsdata.userId: null: the agent's key has already expired. Provision with the persistedAGENT_USER_ID; activation still verifies with the new key.session requestfailsinvalid_inputnaming a pending request: a previous tick requested and did not finish. Provision that request; use--replaceonly after its activity was rejected.whoamisucceeds from the shell but the agent process getsunauthorized: it runs with a differentHOMEorTK_PROFILE. Pin both.secret envexits1withapproval_requiredat boot: an approval-gated secret matched the prefix. Add--property consensus=unilateralor move that secret to an on-demand export.- Two ticks both requested keys: the loop passes
--replaceunconditionally. Add a lock and drop--replace; without--replacethe secondsession requestfailsinvalid_inputwhile one is pending.
Related Skills
- provisioning-session-agent: the policies and the first provisioning this loop renews.
- managing-secrets: importing the values step 2 reads.
- using-ssh: registering the key the socket serves.
- deploying-signing-broker: the broker, its socket, and the client configuration behind step 5.
- monitoring-activities: approving a pending mint the loop reports.