Table of Contents

Short-lived certificates for CI jobs

Give each CI job its own SSH access that ends with the job: a key generated for the job, and a certificate for it that your own certificate authority issues for twenty minutes, for one user, from the build network, running one command. No long-lived deploy key sits in the CI system's secrets, and no authorized_keys file lists one.

The approach

  • The servers trust a CA, not keys. TrustedUserCAKeys in sshd_config names the CA's public key; any certificate it issued for the user logs in, within its validity.
  • A key per job. The job generates an Ed25519 key in memory. It never touches the disk and is gone when the job ends.
  • Your service is the CA. It checks that the job may deploy (from your CI system's identity token, for example), then issues the certificate with SshCertificateBuilder. Here the issuing and the deploy are in one process for brevity.
  • The certificate carries the limits: Principals (the deploy user only), ValidBefore (the job's timeout), no extensions (no terminal, no forwarding), source-address (the build network) and force-command (the deploy script, whatever the job asks to run).
  • The CA key never enters the process. It stays in an SSH agent on the issuing host, or in a key vault or KMS through an SshKeySigner that signs only SshSignaturePurpose.Certificate requests.
flowchart LR
    Job[CI job] -- "public key, job id" --> CA[Issuing service]
    CA -- "sign (Certificate)" --> KMS[(KMS / agent: CA key)]
    CA -- "certificate: deploy, 20 min" --> Job
    Job -- "key + certificate" --> App1[app1: sshd trusts the CA]

The code

Issuing:

/// <summary>
/// Issues a certificate for one CI job's key, for the deploy user, for as long as the job may run. The CA key is the caller's: from
/// an agent, or a key service through SshKeySigner.
/// </summary>
public static async Task<SshCertificate> IssueAsync(SshPrivateKey ca, SshPublicKey jobKey, string jobId, ulong jobNumber,
    TimeSpan jobTimeout, CancellationToken cancellationToken)
{
    var builder = new SshCertificateBuilder(jobKey, SshCertificateType.User, keyId: $"ci-{jobId}")
    {
        Serial = jobNumber,
        Principals = { "deploy" },
        ValidBefore = DateTimeOffset.UtcNow + jobTimeout,
    };

    // Commands only, from the build network, as nothing but the deploy script.
    builder.Extensions.Clear();
    builder.CriticalOptions[SshCertificateBuilder.SourceAddress] = "192.0.2.0/24";
    builder.CriticalOptions[SshCertificateBuilder.ForceCommand] = "/usr/local/bin/deploy";

    var certificate = await builder.SignAsync(ca, cancellationToken);
    Console.WriteLine($"Issued {certificate.KeyId} (serial {certificate.Serial}) until {certificate.ValidBefore:u}");
    return certificate;
}

Using it in the job:

public static async Task DeployAsync(SshPrivateKey ca, string jobId, ulong jobNumber, CancellationToken cancellationToken)
{
    // A key for this job only: it lives in memory and is gone when the job ends.
    using var key = SshPrivateKey.Generate(SshKeyType.Ed25519, $"ci-{jobId}");
    var certificate = await IssueAsync(ca, key.GetPublicKey(), jobId, jobNumber, TimeSpan.FromMinutes(20), cancellationToken);

    // The servers trust the CA (TrustedUserCAKeys) and prove themselves with host certificates from it (@cert-authority).
    await using var client = await SshClient.ConnectAsync("app1.example.com", 22, new SshClientOptions
    {
        UserName = "deploy",
        PrivateKeys = [key.WithCertificate(certificate)],
        HostKeyVerifier = new KnownHostsVerifier(["/etc/ci/ssh_known_hosts"]),
    }, cancellationToken);

    // force-command runs the deploy script whatever is asked; what was asked reaches it as SSH_ORIGINAL_COMMAND.
    var result = await client.RunCommandAsync(jobId, cancellationToken);
    result.EnsureSuccess();
}

On each server, sshd_config:

TrustedUserCAKeys /etc/ssh/user_ca.pub
# Optional: which principals each account accepts (one per line), instead of the account's own name.
AuthorizedPrincipalsFile /etc/ssh/principals/%u

and for the job's known_hosts, the same or another CA for host certificates:

@cert-authority *.example.com ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA...

For production

  • Who may get a certificate. The issuing service is now what decides who can log in. Authenticate the job (an OIDC token from the CI system, checked for repository and branch), and log every certificate issued: key id, serial, principals, validity.
  • The CA key. Keep it in a KMS or HSM behind an SshKeySigner that allows SshSignaturePurpose.Certificate only (see Certificates). Rotate it by adding the new CA's key to TrustedUserCAKeys before issuing with it, and removing the old one after its last certificate has expired.
  • Serials. Give each certificate its own Serial (a database sequence, or the job number) so that RevokedKeys can revoke one. With twenty-minute certificates, waiting for expiry is usually the revocation.
  • Clock skew. ValidAfter defaults to one minute before signing. Servers whose clocks are further off need NTP, not a longer window.
  • source-address must match the address the server sees: behind NAT, that is the NAT's address.

See also