Hands pods short-lived GitHub, Gitea, and atproto credentials for their ServiceAccount tokens
README.md

lanyard #

lanyard hands credentials to pods. A pod proves who it is with its ServiceAccount token, and lanyard answers with what that ServiceAccount is allowed to have: a GitHub token made for one repository, good for an hour, a Gitea token made as one user, good for an hour, an atproto service token for one method on one service, good for two minutes, an atproto account's access token, good for up to two hours, or the contents of a Secret.

It is built for AI agents that run unattended. Such a pod starts with no credential but its own identity, and asks for each one when it needs it.

plans/00-design.md is the design, and plans/completed/ holds what is built and what each step showed.

Use it #

Install the service from the OCI artifact oci://atcr.io/guid.foo/lanyard-deploy. CI publishes it from deploy/ on each push to main, with the tag of the image it pins. The tag is the UTC time of the build and the short commit, such as 20261124093000-1a2b3c4, and the image also carries latest. A weekly run rebuilds both on the newest Go release and base image.

Declare each GitHub App once, by the owner it is installed on, with its private key in a Secret in lanyard's namespace:

apiVersion: lanyard.guid.foo/v1alpha1
kind: GitHubApp
metadata:
  name: example-org
spec:
  clientID: Iv23liEXAMPLE0000000
  privateKey:
    secretKeyRef: { name: github-app-example-org, key: private-key }

Declare each Gitea user that pods act as once, with its password in a Secret in lanyard's namespace. Gitea makes a token only for a request signed in with the user's password, and lanyard cannot sign in as a user with two-factor authentication:

apiVersion: lanyard.guid.foo/v1alpha1
kind: GiteaAccount
metadata:
  name: forge
spec:
  url: https://code.example.com
  username: claude
  password:
    secretKeyRef: { name: gitea-forge, key: password }

Declare each atproto account that pods act as once, with its PDS and its app password in a Secret in lanyard's namespace. lanyard resolves no handle and no DID, so the PDS is written here:

apiVersion: lanyard.guid.foo/v1alpha1
kind: AtprotoAccount
metadata:
  name: claude
spec:
  pds: https://pds.example.com
  identifier: claude.example.com
  appPassword:
    secretKeyRef: { name: atproto-claude, key: password }

Then say what a ServiceAccount's pods may have, with a Badge in their namespace:

apiVersion: lanyard.guid.foo/v1alpha1
kind: Badge
metadata:
  name: github-write
  namespace: agents
spec:
  serviceAccounts: [fixer]
  github:
    - owners: [example-org]
      permissions: { contents: write, pull_requests: write }
  gitea:
    - accounts: [forge]
      scopes: [write:repository, write:issue]
  atproto:
    - accounts: [claude]
      services:
        - audience: did:web:knot.example.com
          methods: [org.tangled.git.receivePack]
      session: true

A Gitea scope is read: or write: and one of Gitea's categories, such as repository, issue, or user. A Gitea token reaches every repository its user can, within its scopes, because Gitea has no scope for one repository.

An atproto entry's services are the service tokens a pod may ask for: each one for an audience, the service's DID, and one method that the service names. session: true gives the account's access token, which can write any record of the account and make any service token until it ends, two hours after the PDS makes it. lanyard keeps the session's refresh token, and no answer holds it.

A ServiceAccount can hold many Badges, and its pods get everything they add up to.

Give the pod a token for lanyard and the service's address:

containers:
  - name: agent
    env:
      - name: LANYARD_URL
        value: http://lanyard.lanyard-system.svc:8080
      - name: LANYARD_TOKEN_FILE
        value: /var/run/secrets/lanyard/token
    volumeMounts:
      - name: lanyard
        mountPath: /var/run/secrets/lanyard
        readOnly: true
volumes:
  - name: lanyard
    projected:
      sources:
        - serviceAccountToken: { audience: lanyard, path: token }

Copy the lanyard binary from the image into the workload's image. ko builds the image, so the binary is at /ko-app/lanyard:

COPY --from=atcr.io/guid.foo/lanyard /ko-app/lanyard /usr/local/bin/lanyard

Make it git's credential helper for GitHub:

git config --global credential.https://github.com.helper "!lanyard git-credential"
git config --global credential.https://github.com.useHttpPath true

The ! makes git run the command as it is written. git then asks lanyard for a token each time it talks to GitHub, for the repository it is talking to.

For a Gitea, make lanyard the helper for the Gitea's address:

git config --global credential.https://code.example.com.helper "!lanyard git-credential"

git then gets a token of the GiteaAccount whose url has the remote's protocol, host, and port, as that account's user. A port that is the scheme's default, such as 443 for https, matches whether it is written or not. A Gitea needs no useHttpPath, because its token is not for one repository, and the setting does no harm when it is on. When no account of the pod's Badges has the host, lanyard answers nothing and git asks its next helper. When two do, lanyard cannot choose and fails with their names.

For a Tangled knot, make lanyard the helper for the knot's address:

git config --global credential.https://knot.example.com.helper "!lanyard git-credential"

git then gets a service token for org.tangled.git.receivePack, as the atproto account whose Badge entry names the service did:web: and the knot's host, such as did:web:knot.example.com. A knot on a port other than 443 is did:web:knot.example.com%3A8443. The knot reads the token as the password.

gh, tea, and tg have no helper to ask, so lanyard runs them. Put a wrapper with the tool's name before the tool on PATH:

#!/bin/sh
exec lanyard run /usr/bin/gh "$@"

lanyard run gets the command its credential and then becomes the tool. gh runs with a token, in GH_TOKEN, for the repository its command works on: the one that -R names, a URL or an API path among its arguments, GH_REPO, or the checkout's remote. tea runs with a login in its config file for each Gitea account of the pod's Badges, named for the account. tg runs with its credentials file written for the one atproto account whose session the pod's Badges give, with the access token and no refresh token. A command that needs nothing of lanyard's runs as it is, and so does every command in a pod with no LANYARD_URL. When lanyard refuses the credential, the tool does not run, and the error is lanyard's reason.

The other commands:

lanyard github-token example-org/widgets      # print a token
lanyard gitea-token forge                     # print a Gitea token
lanyard gitea-token https://code.example.com  # the same, as the one account that works there
lanyard secret github-write/forge-login       # print a secret's keys as JSON
lanyard secret github-write/forge-login password
lanyard secret forge-login password           # the name alone, when one Badge holds it
lanyard show                                  # print what this pod may have

A pod without the binary sends the same requests itself, with its token as the bearer token: GET /v1/github/<owner>/<repo>, GET /v1/gitea/<account>, GET /v1/atproto/<account>/service?aud=<did>&lxm=<nsid>, GET /v1/atproto/<account>/session, GET /v1/secrets/<badge>/<name>, and GET /v1/lanyard. The Gitea route answers the token with the user it acts as and the Gitea's address: {"token": "...", "username": "claude", "url": "https://code.example.com", "expiresAt": "..."}. The atproto routes answer the token with the account's DID and handle, and the session route with its PDS as well: {"accessJwt": "...", "did": "did:plc:...", "handle": "claude.example.com", "pds": "https://pds.example.com", "expiresAt": "..."}.

The service serves its metrics, under lanyard_, at :9200/metrics, and a /healthz there that fails until its watches have synced. plans/completed/05-metrics.md lists the series and what each answers. The component deploy/monitoring adds a PodMonitor for a cluster that runs the prometheus-operator.

Developing #

make test runs every check that CI runs, and adds the race detector, which CI leaves out. It needs the Go toolchain, a C compiler for the race detector, and kubectl for the kustomize build.

CI builds the image with ko, which ko-build.sh wraps. ./ko-build.sh --push=false builds it without pushing, with KO_DOCKER_REPO=atcr.io/guid.foo/lanyard set.