hammerkit

Sources

The repositories a server may read the code of a run from, the token it reads them with, and the branches a sandbox may push to.

sources lists the repositories a run with source: ref may get its code from, and how the server reads each. The caller names a repository and a ref, never a URL, and the server fetches the code itself.

server.yaml
sources:                            # the repositories a run may get its code from
  - match: github.com/corp/*        # an organisation
    secret: { from: 'gsm:github-readonly-token', account: scm-read }
  - match: gitlab.corp.com/platform/**   # a group with its subgroups
    secret: { from: 'gsm:gitlab-group-token', account: scm-read, username: oauth2 }
KeyWhat it does
matchThe repositories the entry covers, as a pattern of host/path. The first entry that matches decides.
secret.fromThe token, as provider:reference of a secret provider. The server reads with it and sandboxes push with it. Without secret the repository is read anonymously and nothing pushes to it.
secret.accountThe account the token is read as. It needs credentials for that provider.
secret.usernameThe user name of the basic authentication the token is sent with, x-access-token by default. A GitLab token is commonly sent as oauth2.

Which repositories a run may use

A run from a ref names a repository and a ref in its request. The repository is written host/path, such as github.com/corp/payments or gitlab.corp.com/platform/team/tool; the server builds the address from it (https://host/path.git). A leading https:// and a trailing .git are dropped; credentials, a port or a query are refused with 400. Two lists decide whether it may be used, and a run needs both:

  1. The caller's repos, the patterns of their entitlements added up (Entitlements). A repository outside of them is 403 you may not run from <repo> at submit, before anything is approved or queued. A caller without repos can not start runs from a ref.
  2. The server's sources, the repositories it can read at all. match is a pattern (github.com/corp/payments exactly, github.com/corp/* for one level, gitlab.corp.com/platform/** for a group with its subgroups, which covers GitLab subgroups) and the first source that matches decides. A repository none covers is 400 this server can not read <repo>.

Both lists use the same patterns, host names and paths are compared in lower case.

The credential

The credential is a secret of a secret provider, read as a service account of the operator, like the secrets of a task: secret.from is provider:reference, account is the account it is read as, and username (default x-access-token) is the user name of the basic authentication the token is sent with. A provider command can mint a short-lived token, for example of a GitHub App; it is read again after a minute. A source without a secret is read anonymously. The token goes to git for that one fetch through the environment of the git process, scoped to the host of the repository, so it is never an argument, never written to .git/config or the checkout, never in the environment of a task, and masked in the logs of the run. The account that reads repositories is the server's own: no entitlement may name it and no build file may use it for a secret.

A GitHub App

A GitHub App gives the server tokens that belong to no person: each lasts an hour, reaches only the repositories the App is installed on and has only the App's permissions. hammerkit github-app-token mints one from the App's private key, so a server needs no script and no other runtime for it: the released image runs it as it is.

server.yaml
secretProviders:
  github-app:
    command: [hammerkit, github-app-token, '{{ref}}']
accounts:
  scm:
    description: The GitHub App of the server
    providers:
      github-app:
        env:
          GITHUB_APP_ID: '123456'
          GITHUB_APP_KEY_SECRET: projects/ci-project/secrets/github-app-key/versions/latest
sources:
  - match: github.com/corp/payments
    secret: { from: 'github-app:corp/payments', account: scm }   # this repository only
  - match: github.com/corp/*
    secret: { from: 'github-app:corp', account: scm }            # all the installation reaches

The reference is owner/repo for a token limited to that one repository, or owner for one that reaches every repository the App's installation on that owner reaches. The command prints the token and nothing else. It reads its settings from the account's env, because a provider command sees nothing else of the server's environment:

VariableWhat it does
GITHUB_APP_IDThe App's ID (or its client ID). Required.
GITHUB_APP_KEY_SECRETThe App's private key in Google Secret Manager, as a version: projects/<project>/secrets/<secret>/versions/<version>.
GITHUB_APP_KEY_FILEThe App's private key as a file, such as a mounted Kubernetes Secret, for a server outside Google Cloud. Set this or GITHUB_APP_KEY_SECRET, not both.
GITHUB_API_URLThe API of a GitHub Enterprise Server, such as https://github.corp.com/api/v3. https://api.github.com by default.
GOOGLE_APPLICATION_CREDENTIALSWith GITHUB_APP_KEY_SECRET: a credential configuration of workload identity federation (type: external_account) whose credential_source is a file, such as a projected Kubernetes ServiceAccount token. Without it the key is read as the identity of the metadata server (Compute Engine, Cloud Run, GKE with Workload Identity). A key of a Google service account is not taken.

The identity that reads the key needs roles/secretmanager.secretAccessor on that secret, and nothing else of Google's. Give the App the permissions the server needs: Contents: Read-only to read, Contents: Read and write when sandboxes push, and no Workflows permission, so a push that changes .github/workflows is refused by GitHub. An error says which step failed (the key, Google, Secret Manager, the installation, the token) and what the service answered, never the key or a token. The command writes it to stderr, and the server puts it in the error of the run or sandbox that needed the token.

Pinning

The server resolves the ref when the run is submitted, with git ls-remote: refs/heads/<ref>, else refs/tags/<ref> (an annotated tag to its commit). A name that is both a branch and a tag, or neither, is 400, a repository it can not reach is 502. The run stores the repository, the ref and the commit, and the approver sees them. The executor fetches the ref and fails the run if it no longer points to that commit, so a branch that moved after the approval does not run other code than the one approved. A 40-digit commit hash is taken as it is; an account with refs refuses one, because it names no branch.

Pushing

The server pushes the changes of sandboxes with the secret of the source, the same token it reads the repository with. A repository the server reads anonymously is not pushed to. Who may push to which branches is not the source's to say: the sandboxes of an entitlement give a branch pattern the type push.

server.yaml
sources:
  - match: github.com/corp/*
    secret: { from: 'gsm:github-token', account: scm }   # reads, and pushes the sandboxes
entitlements:
  - subject: { iss: https://idp.corp, sub: 4f1c... }
    sandboxes:
      - repo: github.com/corp/payments
        branches:
          main: pull
          'agent/**': push          # agent/fix-login, agent/team/x; not main

A branch pattern is written like an account's refs: a name (agent/fix), * for a part of one level (agent/*) and ** as the last level for everything below (agent/**). Keep the push patterns to branches that nobody else writes to and that no deployment runs from: a sandbox holds changes nobody reviewed, the push is how they get to a review.

For a push the token needs write access to the repository's contents; a server that never pushes can keep a read-only one. As for a read, the token goes to one git process through its environment, is masked, and the account is the server's own: no entitlement may name it and no build file may use it. The pushing git process gets nothing else to sign in with: no global or system git configuration, no credential helper, no .netrc of a home directory and no ssh agent, so a push is always the service account's and never the identity of whoever runs the server. hammerkit github-app-token mints a short-lived token of a GitHub App with write access to the repository's contents. A token can not be limited to branches on the host's side, so protect the branches people work on (main, releases) in the repository as well: a pattern that is too wide is then caught twice.

Pushed branches run in your CI. Most repositories run CI on a push to any branch, with the repository's secrets: GitHub Actions on: push, GitLab pipelines, Buildkite. A branch a sandbox pushed holds code nobody reviewed, including the build scripts and tests CI runs, and the workflow files themselves when the token may write them. Before you give a pattern the type push:

  • keep the push patterns out of CI that holds secrets (branches-ignore: ['agent/**']), or put the jobs with secrets behind a review (environments with required reviewers, protected variables);
  • give the token no permission over workflow files (a GitHub App without workflows: write), so a push that changes them is refused by the host.

On this page