add an ssh ca to bao, agent ssh certificates and a target module
This commit is contained in:
parent
ac01ef1bdb
commit
0afb111862
3 changed files with 1056 additions and 17 deletions
|
|
@ -6,6 +6,8 @@
|
|||
# fetches it, and ../agent-modules/forge.nix's readers find it. The same fixtures
|
||||
# cover ../agent-modules/egress-proxy.nix, the agent's proxy credential and the
|
||||
# environment built from it.
|
||||
# They also cover ../agent-modules/ssh.nix: the agent's SSH certificate unit and
|
||||
# client config, on the same store address.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
|
|
@ -32,12 +34,30 @@ let
|
|||
# A forge and a store: the fetch exists and every reader points at it.
|
||||
agentForgeBao = agentWith {
|
||||
services.hyperhive.agent.bao.addr = baoAddr;
|
||||
services.hyperhive.agent.ssh = {
|
||||
enable = true;
|
||||
hosts.testvm = {
|
||||
hostName = "10.0.0.7";
|
||||
user = "test";
|
||||
hostKey = hostKeyA;
|
||||
};
|
||||
hosts.altvm = {
|
||||
hostName = "vm2.t.local";
|
||||
user = "ops";
|
||||
port = 2222;
|
||||
hostKey = hostKeyB;
|
||||
};
|
||||
};
|
||||
services.hyperhive.agent.forge.url = forgeUrl;
|
||||
# A second `EnvironmentFile=` on the harness, which the proxy file must join.
|
||||
services.hyperhive.agent.backendEnvironmentFile = "/var/lib/t/backend.env";
|
||||
services.hyperhive.agent.icon = pkgs.emptyFile;
|
||||
};
|
||||
|
||||
# Outbound SSH on the same agent: two hosts, one on a non-default port.
|
||||
hostKeyA = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOHfYeWemIBqSc5Ac51ecuhi0G9LTN+57DFRq6mFKKk0";
|
||||
hostKeyB = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIMY1xboa8d2ncPDhTSPKsq5WMyYfWwIqLcCOnkHLO3ad";
|
||||
|
||||
# A forge and no store: the absence arm, and what makes the cases above able
|
||||
# to fail.
|
||||
agentForgeNoBao = agentWith {
|
||||
|
|
@ -45,6 +65,15 @@ let
|
|||
services.hyperhive.agent.icon = pkgs.emptyFile;
|
||||
};
|
||||
|
||||
# SSH asked for with no store: the certificate has nowhere to be signed.
|
||||
agentSshNoBao = agentWith {
|
||||
services.hyperhive.agent.ssh.enable = true;
|
||||
services.hyperhive.agent.icon = pkgs.emptyFile;
|
||||
};
|
||||
|
||||
sshUnit = machine: machine.systemd.services.hive-agent-ssh-cert;
|
||||
sshCa = import ../host-modules/lib/ssh-agent-ca.nix { };
|
||||
|
||||
fetchUnit = machine: machine.systemd.services.hive-agent-forge-token;
|
||||
proxyUnit = machine: machine.systemd.services.hive-agent-proxy-credential;
|
||||
accountsUnit = machine: machine.systemd.services.hive-agent-forge-accounts;
|
||||
|
|
@ -521,6 +550,88 @@ let
|
|||
&& lib.hasInfix "NODE_USE_ENV_PROXY=1" s
|
||||
&& lib.hasInfix "url=\"http://${name}:$secret@$proxy\"" s;
|
||||
}
|
||||
{
|
||||
# ../agent-modules/ssh.nix. Presence arm first, then the agent that
|
||||
# did not ask for SSH, which gets neither the unit nor a host block.
|
||||
name = "an agent that enables ssh gets the certificate unit and its timer, and one that does not gets neither";
|
||||
ok =
|
||||
agentForgeBao.systemd.services ? hive-agent-ssh-cert
|
||||
&& agentForgeBao.systemd.timers ? hive-agent-ssh-cert
|
||||
&& !(agentForgeNoBao.systemd.services ? hive-agent-ssh-cert)
|
||||
&& !(agentForgeNoBao.systemd.timers ? hive-agent-ssh-cert)
|
||||
&& !(lib.hasInfix "Host testvm" agentForgeNoBao.environment.etc."ssh/ssh_config".text);
|
||||
}
|
||||
{
|
||||
# The key and the certificate stay in the unit's own tmpfs directory,
|
||||
# owner-only, and the unit asks for no principal: the role's default is
|
||||
# the agent's own name, and a `valid_principals` here would be the one
|
||||
# place the unit could name another.
|
||||
name = "the ssh certificate unit keeps the key in a 0700 directory under /run as the agent user, asks for no principal, and renames the certificate in";
|
||||
ok =
|
||||
let
|
||||
u = sshUnit agentForgeBao;
|
||||
s = u.script;
|
||||
in
|
||||
u.serviceConfig.User == "a1"
|
||||
&& u.serviceConfig.RuntimeDirectory == "hive-agent-ssh-cert"
|
||||
&& u.serviceConfig.RuntimeDirectoryMode == "0700"
|
||||
&& u.serviceConfig.UMask == "0377"
|
||||
&& lib.hasInfix "key=/run/hive-agent-ssh-cert/key" s
|
||||
&& lib.hasInfix "ssh-keygen -q -t ed25519" s
|
||||
&& lib.hasInfix "bao write -field=signed_key ssh-agents/sign/swarm-agent" s
|
||||
&& lib.hasInfix "mv -f \"$staged\" \"$cert\"" s
|
||||
&& !(lib.hasInfix "valid_principals" s)
|
||||
&& !(lib.hasInfix ".ssh" s)
|
||||
&& lib.elem "hive-agent-bao-identity.service" u.after
|
||||
&& u.environment.BAO_CLIENT_CERT == agentForgeBao.services.hyperhive.agent.bao.clientCertFile;
|
||||
}
|
||||
{
|
||||
# The certificate lives an hour and the timer fires every third of
|
||||
# that, so one missed run still leaves the old certificate valid.
|
||||
name = "the ssh certificate is renewed at least twice within its lifetime";
|
||||
ok =
|
||||
let
|
||||
every = lib.toInt (
|
||||
lib.removeSuffix "min" agentForgeBao.systemd.timers.hive-agent-ssh-cert.timerConfig.OnUnitInactiveSec
|
||||
);
|
||||
in
|
||||
every * 2 < sshCa.certTtlMinutes && every > 0;
|
||||
}
|
||||
{
|
||||
# `ssh testvm` has to work with no other flag: the host block names the
|
||||
# target, the user, the port, the one identity and its certificate, and
|
||||
# refuses an unknown host key.
|
||||
name = "each ssh host gets a Host block offering only the agent's certificate, and a pinned known_hosts line";
|
||||
ok =
|
||||
let
|
||||
conf = agentForgeBao.environment.etc."ssh/ssh_config".text;
|
||||
known = agentForgeBao.environment.etc."ssh/ssh_known_hosts".text;
|
||||
block = lib.concatStringsSep "\n" [
|
||||
"Host testvm"
|
||||
" HostName 10.0.0.7"
|
||||
" User test"
|
||||
" Port 22"
|
||||
" IdentityFile /run/hive-agent-ssh-cert/key"
|
||||
" CertificateFile /run/hive-agent-ssh-cert/key-cert.pub"
|
||||
" IdentitiesOnly yes"
|
||||
" StrictHostKeyChecking yes"
|
||||
];
|
||||
in
|
||||
lib.hasInfix block conf
|
||||
&& lib.hasInfix "Host altvm\n HostName vm2.t.local\n User ops\n Port 2222\n" conf
|
||||
&& lib.hasInfix "10.0.0.7 ${hostKeyA}" known
|
||||
# A port other than 22 is spelled the way known_hosts keys it.
|
||||
&& lib.hasInfix "[vm2.t.local]:2222 ${hostKeyB}" known
|
||||
&& !(lib.hasInfix "\nvm2.t.local " "\n${known}");
|
||||
}
|
||||
{
|
||||
name = "enabling ssh on an agent with no store address is refused, naming the option";
|
||||
ok =
|
||||
let
|
||||
refuses = m: lib.any (a: !a.assertion && lib.hasInfix "agent.bao.addr" a.message) m.assertions;
|
||||
in
|
||||
refuses agentSshNoBao && !(refuses agentForgeBao);
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "agent-forge-bao" cases
|
||||
|
|
|
|||
120
nix/module-eval/agent-ssh-target.nix
Normal file
120
nix/module-eval/agent-ssh-target.nix
Normal file
|
|
@ -0,0 +1,120 @@
|
|||
# `checks.module-eval-agent-ssh-target` — see ./lib.nix for the shared
|
||||
# rationale (why this suite exists, naming convention, "evaluates
|
||||
# not executes").
|
||||
#
|
||||
# ../target-modules/agent-ssh.nix, the module for a machine agents SSH into. It
|
||||
# needs a fixture of its own: it is not a hive host or an agent, and the
|
||||
# agent-side cases in ./agent-forge-bao.nix cannot evaluate it.
|
||||
{
|
||||
pkgs,
|
||||
lib,
|
||||
self,
|
||||
nixosSystem,
|
||||
}:
|
||||
let
|
||||
inherit
|
||||
(import ./lib.nix {
|
||||
inherit
|
||||
pkgs
|
||||
lib
|
||||
self
|
||||
nixosSystem
|
||||
;
|
||||
})
|
||||
runGroup
|
||||
;
|
||||
|
||||
caKey = "ssh-ed25519 AAAAC3NzaC1lZDI1NTE5AAAAIOHfYeWemIBqSc5Ac51ecuhi0G9LTN+57DFRq6mFKKk0";
|
||||
|
||||
machine =
|
||||
extra:
|
||||
(nixosSystem {
|
||||
system = pkgs.stdenv.hostPlatform.system;
|
||||
modules = [
|
||||
self.nixosModules.agent-ssh-target
|
||||
{
|
||||
fileSystems."/" = {
|
||||
device = "/dev/null";
|
||||
fsType = "tmpfs";
|
||||
};
|
||||
boot.loader.grub.enable = false;
|
||||
system.stateVersion = "25.11";
|
||||
users.users.test.isNormalUser = true;
|
||||
users.users.ops.isNormalUser = true;
|
||||
}
|
||||
extra
|
||||
];
|
||||
}).config;
|
||||
|
||||
trusting = machine {
|
||||
services.hyperhive.agentSshTarget = {
|
||||
enable = true;
|
||||
# The command's output ends in a newline.
|
||||
caPublicKey = "${caKey}\n";
|
||||
principals.test = [
|
||||
"atlas"
|
||||
"maia"
|
||||
];
|
||||
};
|
||||
};
|
||||
|
||||
# The module imported and left off.
|
||||
untouched = machine { };
|
||||
|
||||
# Enabled with nobody listed.
|
||||
nobody = machine {
|
||||
services.hyperhive.agentSshTarget = {
|
||||
enable = true;
|
||||
caPublicKey = caKey;
|
||||
};
|
||||
};
|
||||
|
||||
principalsOf = m: user: m.environment.etc."ssh/authorized_principals.d/${user}".text;
|
||||
|
||||
cases = [
|
||||
{
|
||||
name = "an enabled target trusts the CA's public key and ties sshd to the per-user principals file";
|
||||
ok =
|
||||
trusting.services.openssh.enable
|
||||
&& trusting.services.openssh.settings.TrustedUserCAKeys == "/etc/ssh/hyperhive-agent-ssh-ca.pub"
|
||||
&& trusting.environment.etc."ssh/hyperhive-agent-ssh-ca.pub".text == "${caKey}\n"
|
||||
&&
|
||||
trusting.services.openssh.settings.AuthorizedPrincipalsFile
|
||||
== "/etc/ssh/authorized_principals.d/%u";
|
||||
}
|
||||
{
|
||||
# `ops` is a real account on the machine that no agent was listed for.
|
||||
name = "only the listed agents are principals of the listed user, and an unlisted user has no principals file";
|
||||
ok =
|
||||
principalsOf trusting "test" == "atlas\nmaia"
|
||||
&& !(trusting.environment.etc ? "ssh/authorized_principals.d/ops");
|
||||
}
|
||||
{
|
||||
# No key is ever copied: the module writes none for any user.
|
||||
name = "the target module writes no authorized key";
|
||||
ok =
|
||||
trusting.users.users.test.openssh.authorizedKeys.keys == [ ]
|
||||
&& trusting.users.users.test.openssh.authorizedKeys.keyFiles == [ ];
|
||||
}
|
||||
{
|
||||
# The absence arm, and the control that makes the cases above able to
|
||||
# fail: the same module, imported and not enabled, changes nothing.
|
||||
name = "an imported but disabled target trusts no CA";
|
||||
ok =
|
||||
!(untouched.services.openssh.settings ? TrustedUserCAKeys)
|
||||
&& !(untouched.environment.etc ? "ssh/hyperhive-agent-ssh-ca.pub")
|
||||
&& untouched.services.openssh.settings.AuthorizedPrincipalsFile == "none"
|
||||
&& !untouched.services.openssh.enable;
|
||||
}
|
||||
{
|
||||
name = "an enabled target that lists no agent is refused";
|
||||
ok =
|
||||
let
|
||||
refuses =
|
||||
m: lib.any (a: !a.assertion && lib.hasInfix "agentSshTarget.principals" a.message) m.assertions;
|
||||
in
|
||||
refuses nobody && !(refuses trusting);
|
||||
}
|
||||
];
|
||||
in
|
||||
runGroup "agent-ssh-target" cases
|
||||
|
|
@ -182,6 +182,7 @@ let
|
|||
"swarm-bao-services-issuer-policy"
|
||||
"swarm-bao-nats-tls-policy"
|
||||
"swarm-bao-agent-pki"
|
||||
"swarm-bao-agent-ssh-policy"
|
||||
"swarm-bao-nats-auth-policy"
|
||||
"swarm-bao-operator-viewer-policy"
|
||||
"swarm-bao-hive-leaf-policy"
|
||||
|
|
@ -1340,7 +1341,7 @@ let
|
|||
{
|
||||
# The granter's grants, whole. Pinned as the full list, because an added
|
||||
# path or capability is exactly what a presence check misses.
|
||||
name = "the granter's policy is exactly these twenty stanzas";
|
||||
name = "the granter's policy is exactly these twenty-four stanzas";
|
||||
ok =
|
||||
let
|
||||
cu = [
|
||||
|
|
@ -1420,6 +1421,22 @@ let
|
|||
path = "pki-agents/roles/swarm-*";
|
||||
caps = cu;
|
||||
}
|
||||
{
|
||||
path = "sys/mounts/ssh-agents";
|
||||
caps = cu;
|
||||
}
|
||||
{
|
||||
path = "ssh-agents/issuers";
|
||||
caps = [ "list" ];
|
||||
}
|
||||
{
|
||||
path = "ssh-agents/config/ca";
|
||||
caps = cu;
|
||||
}
|
||||
{
|
||||
path = "ssh-agents/roles/swarm-*";
|
||||
caps = cu;
|
||||
}
|
||||
{
|
||||
path = "auth/oidc/config";
|
||||
caps = cu;
|
||||
|
|
@ -2806,6 +2823,94 @@ let
|
|||
&& !(lib.any (a: !a.assertion && names a) baoGrantHere.assertions);
|
||||
}
|
||||
|
||||
{
|
||||
# The SSH CA is the trust every target machine holds by value, so it is
|
||||
# made once, only when the mount lists no issuer, and the granter can
|
||||
# neither read it back nor delete it. The presence arms: the same unit
|
||||
# does write the mount, the CA and the role.
|
||||
name = "the agent SSH CA is generated once from an ed25519 key, only when the mount has no issuer, and nothing deletes it";
|
||||
ok =
|
||||
let
|
||||
s = baoGrantHere.systemd.services.swarm-bao-agent-ssh-policy.script;
|
||||
generate = "ssh-agents/config/ca";
|
||||
in
|
||||
lib.length (lib.splitString generate s) == 2
|
||||
&& lib.hasInfix "generate_signing_key=true" s
|
||||
&& lib.hasInfix "key_type=ssh-ed25519" s
|
||||
&& lib.hasInfix "bao secrets enable -path=ssh-agents ssh" s
|
||||
&& lib.hasInfix "elif [ \"$issuers\" = '{}' ]; then" s
|
||||
&& !(lib.hasInfix "bao delete" s)
|
||||
&&
|
||||
(grantFor granterGrants "ssh-agents/config/ca").caps == [
|
||||
"create"
|
||||
"update"
|
||||
]
|
||||
&& grantFor granterGrants "ssh-agents/sign/swarm-agent" == null
|
||||
&& grantFor granterGrants "ssh-agents/roles/other" == null;
|
||||
}
|
||||
{
|
||||
# What keeps an agent from a certificate for another agent: the
|
||||
# principal is the caller's entity name, requested or not, and the role
|
||||
# signs nothing but ed25519 user keys with one extension. The role is
|
||||
# a single JSON document, so each fragment below is one property of it.
|
||||
name = "the agent SSH role names the caller's own entity as the only principal, signs ed25519 user keys for an hour, and one unit writes it";
|
||||
ok =
|
||||
let
|
||||
s = baoGrantHere.systemd.services.swarm-bao-agent-ssh-policy.script;
|
||||
roleWriters = lib.filter (u: matches "bao write '?ssh-agents/roles/" (u.script or "") != [ ]) (
|
||||
lib.attrValues baoGrantHere.systemd.services
|
||||
);
|
||||
in
|
||||
lib.all (t: lib.hasInfix t s) [
|
||||
''"allowed_users":"{{identity.entity.name}}"''
|
||||
''"allowed_users_template":true''
|
||||
''"default_user":"{{identity.entity.name}}"''
|
||||
''"default_user_template":true''
|
||||
''"allow_user_certificates":true''
|
||||
''"allow_host_certificates":false''
|
||||
''"allow_user_key_ids":false''
|
||||
''"allowed_user_key_lengths":{"ssh-ed25519":0}''
|
||||
''"allowed_extensions":"permit-pty"''
|
||||
''"key_type":"ca"''
|
||||
''"ttl":"60m"''
|
||||
''"max_ttl":"180m"''
|
||||
"bao write ssh-agents/roles/swarm-agent -"
|
||||
]
|
||||
&& lib.length roleWriters == 1;
|
||||
}
|
||||
{
|
||||
# The agents' `update` on the sign path comes from the shared agent
|
||||
# policy (swarm-secret-client), not from a unit here. Of the policies
|
||||
# these units write, only the granter's names the mount: the controller,
|
||||
# which writes the shared one, holds nothing on it.
|
||||
name = "of the policies the host writes, only the granter's has a grant on the agent SSH mount";
|
||||
ok =
|
||||
let
|
||||
cg = grantsIn baoGrantHere.systemd.services.swarm-bao-controller-policy.script;
|
||||
onMount = lib.filter (g: lib.hasPrefix "ssh-agents" g.path);
|
||||
in
|
||||
onMount cg == [ ]
|
||||
# The control: the granter's grants do name the mount.
|
||||
&& onMount granterGrants != [ ]
|
||||
&& lib.all (u: onMount (grantsIn baoGrantHere.systemd.services.${u}.script) == [ ]) (
|
||||
lib.filter (u: u != "swarm-bao-agent-ssh-policy") grantingUnitNames
|
||||
);
|
||||
}
|
||||
{
|
||||
# The shared agent policy (swarm-secret-client) names the mount and the
|
||||
# role; the unit above and the agent module spell the same two strings
|
||||
# from ../host-modules/lib/ssh-agent-ca.nix.
|
||||
name = "the SSH mount and role the shared agent policy names are the ones the swarm writes";
|
||||
ok =
|
||||
let
|
||||
ca = import ../host-modules/lib/ssh-agent-ca.nix { };
|
||||
policy = builtins.readFile ../../swarm-secret-client/src/policy.rs;
|
||||
in
|
||||
lib.hasInfix ''pub const SSH_MOUNT: &str = "${ca.mountPath}";'' policy
|
||||
&& lib.hasInfix ''pub const SSH_ROLE: &str = "${ca.roleName}";'' policy
|
||||
&& lib.hasInfix "bao write ssh-agents/roles/${ca.roleName} -" baoGrantHere.systemd.services.swarm-bao-agent-ssh-policy.script;
|
||||
}
|
||||
|
||||
# ── the UI's OIDC login ─────────────────────────────────────────────────
|
||||
{
|
||||
# What an `admins` login through the UI holds: the key tree, KV
|
||||
|
|
|
|||
Loading…
Reference in a new issue