# A machine agents may SSH into: it trusts the swarm's SSH CA and lists which # agent may log in as which local user. Exported as # `hyperhive.nixosModules.agent-ssh-target`, and meant for any NixOS machine, # not only a hive host. # # sshd accepts a user certificate when the CA that signed it is in # `TrustedUserCAKeys` and one of its principals is in that user's # `AuthorizedPrincipalsFile`. The swarm's SSH role puts exactly one principal on # a certificate, the agent's name, so listing an agent here is what lets it in. # No `authorized_keys` entry is written or needed. Removing a name from # `principals` ends that agent's logins at the next attempt, whatever # certificate it holds. The swarm has no switch that stops signing for an # agent that is still declared; a certificate already signed expires within # three hours. # # The agent's side is ../agent-modules/ssh.nix. { lib, config, ... }: let cfg = config.services.hyperhive.agentSshTarget; in { options.services.hyperhive.agentSshTarget = { enable = lib.mkEnableOption "logins by agents holding a certificate from the swarm's SSH CA"; caPublicKey = lib.mkOption { type = lib.types.strMatching "ssh-ed25519 [A-Za-z0-9+/]+=*[[:space:]]*"; example = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAA…"; description = '' The public key of the swarm's SSH CA: a public value, set here once. Read it on the swarm's store host, as an operator with a token that may read the mount: ``` bao read -field=public_key ssh-agents/config/ca ``` The same key is served without a token at `/v1/ssh-agents/public_key`. A CA that is regenerated has a new key, and every target must then be given it. ''; }; principals = lib.mkOption { type = lib.types.attrsOf (lib.types.listOf (lib.types.strMatching "[A-Za-z0-9_-]+")); default = { }; example = { test = [ "atlas" "maia" ]; }; description = '' For each local account, the names of the agents that may log in as it. The account has to exist on this machine. An account that is not listed accepts no certificate. ''; }; }; config = lib.mkIf cfg.enable { assertions = [ { assertion = lib.any (agents: agents != [ ]) (lib.attrValues cfg.principals); message = "services.hyperhive.agentSshTarget.principals lists no agent, so this machine would trust the CA and admit nobody"; } ]; services.openssh.enable = lib.mkDefault true; services.openssh.settings.TrustedUserCAKeys = "/etc/ssh/hyperhive-agent-ssh-ca.pub"; environment.etc."ssh/hyperhive-agent-ssh-ca.pub".text = "${lib.trim cfg.caPublicKey}\n"; # NixOS writes `/etc/ssh/authorized_principals.d/` for each of these # and points `AuthorizedPrincipalsFile` at it. users.users = lib.mapAttrs (_: agents: { openssh.authorizedPrincipals = agents; }) cfg.principals; }; }