A KMS-held key forwarded through a bastion
Deploy to application servers that are reachable only from a bastion, with a key that lives in a cloud KMS or an HSM. ssh on the
bastion logs in to each application server with your key, through a forwarded agent, but the private key never leaves the KMS, no
ssh-agent runs anywhere, and the bastion can use the key only for what you allow: logging in as deploy to the application
servers you know.
The approach
- The key stays in the KMS. An
SshKeySignerasks the KMS for each signature (see Use a key held by a remote service). - An agent in this process.
SshAgent.FromKeysserves that key over the agent protocol: this client authenticates to the bastion through it, and forwards it (ForwardAgent), sosshon the bastion can use it. - A filter decides each request.
ForwardAgentFiltersees every request the bastion's programs make: the key, what the signature is for, the user name, and the host key of the server being logged in to (from the session binding that OpenSSH 8.9 and later send). It allows logins asdeployto the application servers' host keys, and refuses the rest: another user, another server, a destination it cannot tell, a file or commit signature.
sequenceDiagram
participant App as This process
participant KMS
participant Bastion as bastion.example.com
participant App1 as app1.example.com
App->>Bastion: log in (signed through the in-process agent, by the KMS)
App->>Bastion: run "ssh deploy@app1.example.com ..." with the agent forwarded
Bastion->>App1: key exchange, host key
Bastion->>App: agent channel: session-bind (app1's host key), sign the login
App->>App: ForwardAgentFilter: deploy, app1's host key known? allow
App->>KMS: sign
KMS-->>App: signature
App-->>Bastion: signature
Bastion->>App1: log in as deploy
The code
The agent:
/// <summary>
/// An agent in this process whose one key is in the KMS: the bastion's programs can ask it to sign, and every request reaches
/// <see cref="AllowAsync"/> first.
/// </summary>
public static SshAgent CreateAgent(Keys.IKeyService kms, ReadOnlyMemory<byte> publicKeyBlob) =>
SshAgent.FromKeys(SshPrivateKey.FromSigner(new Keys.KmsSigner(kms, "ssh-deploy", publicKeyBlob), comment: "kms:ssh-deploy"));
The filter:
/// <summary>
/// What the bastion may do with the forwarded key: list it, and log in with it as deploy to the application servers, recognised by
/// their host keys. Everything else (another user, another server, an unknown destination, a file signature) is refused and logged.
/// </summary>
public static ValueTask<bool> AllowAsync(SshAgentRequest request, IReadOnlySet<SshPublicKey> appServers)
{
var allowed = request switch
{
{ Operation: SshAgentOperation.ListKeys } => true,
{ Purpose: SshSignaturePurpose.Authentication, UserName: "deploy", DestinationHostKey: { } destination } => appServers.Contains(destination),
_ => false,
};
Console.WriteLine($"{(allowed ? "allowed" : "refused")}: {request.Operation} {request.Purpose} {request.Key.Fingerprint} " +
$"to {request.DestinationHostKey?.Fingerprint ?? "an unknown server"}");
return ValueTask.FromResult(allowed);
}
The deployment:
public static async Task DeployAsync(Keys.IKeyService kms, ReadOnlyMemory<byte> publicKeyBlob, CancellationToken cancellationToken)
{
string[] servers = ["app1.example.com", "app2.example.com"];
var knownHosts = await KnownHostsFile.LoadAsync("/etc/deploy/known_hosts", cancellationToken);
var appServers = servers.SelectMany(s => knownHosts.Find(s)).Where(e => e.Marker == KnownHostsMarker.None).Select(e => e.GetPublicKey()).ToHashSet();
var agent = CreateAgent(kms, publicKeyBlob);
await using var bastion = await SshClient.ConnectAsync("bastion.example.com", 22, new SshClientOptions
{
UserName = "deploy",
PrivateKeys = await agent.GetKeysAsync(cancellationToken), // the KMS key logs in to the bastion too
HostKeyVerifier = new KnownHostsVerifier(["/etc/deploy/known_hosts"]),
ForwardAgent = agent,
ForwardAgentFilter = (request, _) => AllowAsync(request, appServers),
}, cancellationToken);
// ssh on the bastion logs in to each application server with the forwarded key; the key never leaves the KMS.
foreach (var server in servers)
{
var result = await bastion.RunCommandAsync($"ssh -o BatchMode=yes deploy@{server} /usr/local/bin/deploy", cancellationToken);
result.EnsureSuccess();
}
}
For production
- The bastion's
sshmust bind its sessions.DestinationHostKeyis known only when the program on the bastion binds its agent connection to the session it logs in to (OpenSSH 8.9 and later do). With an olderssh, this filter refuses every login; a looser filter has nothing but the user name to go on. - The filter narrows, it does not make the bastion trusted. While you are connected, someone with root on the bastion can still
ask for what the filter allows: a login as
deployto the application servers. Keep connections short, and givedeployonly what deploying needs. - The KMS policy is a second check. Let the KMS key sign for authentication only (the signer sees
SshSignaturePurpose), and log its use on the KMS side as well. - Host keys. Keep the application servers' host keys in a
known_hostsfile you control. Hosts that use host certificates present the certified key here: compareDestinationHostKeywith the keys the certificates certify. - Without the bastion's cooperation,
ProxyJump(a jump host) avoids forwarding the agent at all: the connection to each application server is made from this process, through the bastion, and the bastion never sees a request. Prefer it when the bastion allows TCP forwarding.