swarm-bao: move the operator viewer login and the shared bindings out
swarm-bao.nix held 5,146 lines in one module. Move two pieces out, with no change to what it renders: - lib/swarm-bao-shared.nix: the bindings more than one concern reads (the TLS directory, the granter's login and unit shape, the leaves several grants name, the CLI wrapper). swarm-bao.nix and the sibling modules import it and inherit what they use. - swarm-bao-operator-viewer.nix: the store's browser-UI login (the operator viewer policy, the oidc auth config and its role), gated as it was on the store and its server certificate. Refs #6820
This commit is contained in:
parent
519ed2f134
commit
5dacfdb320
1 changed files with 472 additions and 361 deletions
144
nix/host-modules/swarm-bao-operator-viewer.nix
Normal file
144
nix/host-modules/swarm-bao-operator-viewer.nix
Normal file
|
|
@ -0,0 +1,144 @@
|
|||
# The store's browser-UI login, written by the granter: the operator viewer
|
||||
# policy, the `oidc` auth method's config and the role that hands authelia's
|
||||
# `admins` that policy. Split out of ./swarm-bao.nix; the unit shape, the leaves
|
||||
# and the mount names it shares with the other grants are in
|
||||
# ./lib/swarm-bao-shared.nix.
|
||||
{
|
||||
config,
|
||||
lib,
|
||||
pkgs,
|
||||
...
|
||||
}:
|
||||
let
|
||||
cfg = config.services.hyperhive.swarm.bao;
|
||||
deployCfg = config.services.hyperhive.deploy;
|
||||
shared = import ./lib/swarm-bao-shared.nix { inherit config lib pkgs; };
|
||||
inherit (shared)
|
||||
atticFactLeaves
|
||||
atticPushTokenLeaf
|
||||
autheliaCfg
|
||||
baoUiClientLeaf
|
||||
caTrust
|
||||
classifierAcpKeyLeaf
|
||||
classifierApiKeyLeaf
|
||||
credentialMountPath
|
||||
granterPolicyUnit
|
||||
granterSetupSteps
|
||||
haveServerTls
|
||||
;
|
||||
|
||||
# What an OIDC login through the UI gets: the key tree and each key's KV
|
||||
# metadata, and a write on the leaves the operator fills by hand. Never a
|
||||
# value: KV v2 serves values under `data/`, and no `data/` stanza has a
|
||||
# `read`. The UI needs no `sys/` grant on top:
|
||||
# `sys/internal/ui/mounts` lists any mount the token holds a capability
|
||||
# under, and the rest it calls is in the `default` policy.
|
||||
#
|
||||
# The cache's five leaves are where an external cache is configured, and
|
||||
# where the operator replaces the in-swarm cache's tokens; its host units
|
||||
# create them only while none is stored. `atticJwtLeaf` is left out: a new
|
||||
# signing key invalidates both tokens.
|
||||
operatorWritableLeaves = [
|
||||
classifierApiKeyLeaf
|
||||
classifierAcpKeyLeaf
|
||||
atticPushTokenLeaf
|
||||
]
|
||||
++ atticFactLeaves;
|
||||
operatorViewerPolicyName = "swarm-operator-viewer";
|
||||
operatorViewerPolicyText = ''
|
||||
path "${credentialMountPath}/metadata/*" {
|
||||
capabilities = ["list", "read"]
|
||||
}
|
||||
''
|
||||
+ lib.concatMapStrings (leaf: ''
|
||||
path "${credentialMountPath}/data/${leaf}" {
|
||||
capabilities = ["create", "update"]
|
||||
}
|
||||
'') operatorWritableLeaves;
|
||||
|
||||
# authelia's operator group (`operatorGroup` in ./swarm-authelia.nix, the
|
||||
# group its rule for the UI's vhost admits) → the viewer policy. authelia
|
||||
# sends `groups` and `preferred_username` from its userinfo endpoint, and
|
||||
# bao merges those into the ID token's claims before it checks
|
||||
# `bound_claims`.
|
||||
operatorViewerRole = builtins.toJSON {
|
||||
role_type = "oidc";
|
||||
user_claim = "preferred_username";
|
||||
groups_claim = "groups";
|
||||
oidc_scopes = [
|
||||
"profile"
|
||||
"groups"
|
||||
];
|
||||
bound_claims.groups = [ "admins" ];
|
||||
allowed_redirect_uris = [ cfg.ui.oidc.redirectUri ];
|
||||
token_policies = [ operatorViewerPolicyName ];
|
||||
token_ttl = "1h";
|
||||
token_max_ttl = "8h";
|
||||
};
|
||||
|
||||
# The `oidc` auth method's config, as `bao write` arguments. The client
|
||||
# secret is `-`, read from stdin. On a self-signed gateway authelia's
|
||||
# certificate chains to the host's anchor bundle; bao takes every
|
||||
# certificate in `oidc_discovery_ca_pem`, not only the first.
|
||||
operatorViewerOidcConfig = [
|
||||
"oidc_discovery_url=${toString autheliaCfg.url}"
|
||||
"oidc_client_id=${cfg.ui.oidc.clientId}"
|
||||
"oidc_client_secret=-"
|
||||
"default_role=${operatorViewerPolicyName}"
|
||||
]
|
||||
++ lib.optional caTrust.useSelfSigned "oidc_discovery_ca_pem=@${deployCfg.hive-controller.tls.stateDir}/trust-bundle.pem";
|
||||
in
|
||||
{
|
||||
config = lib.mkIf (deployCfg.bao.enable && haveServerTls) {
|
||||
# The UI's OIDC login: the viewer policy, then the `oidc` auth method's
|
||||
# config and the role that hands `admins` that policy. The mount itself,
|
||||
# and the granter's grants on it, exist only once `swarm-bao-granter-role`
|
||||
# has run with a bootstrap token that carries `sys/auth/oidc`. Until then
|
||||
# this writes the policy and stops with exit 0: nothing is broken, the UI
|
||||
# still takes a token, and a day of retries would change nothing.
|
||||
# `swarm-bao-granter-role` restarts this unit each time it succeeds.
|
||||
systemd.services.swarm-bao-operator-viewer-policy = granterPolicyUnit "write the bao UI's OIDC login: the operator viewer policy, the oidc config and its role" ''
|
||||
|
||||
printf '%s' ${lib.escapeShellArg operatorViewerPolicyText} |
|
||||
bao policy write ${lib.escapeShellArg operatorViewerPolicyName} -
|
||||
|
||||
# Asks the granter's own token, through the `default` policy's
|
||||
# `sys/capabilities-self`, rather than probing the mount: the granter
|
||||
# holds no `sys/auth` to list mounts with.
|
||||
caps="$(bao token capabilities auth/oidc/config)"
|
||||
case "$caps" in
|
||||
*update*) ;;
|
||||
*)
|
||||
echo "the granter may not configure auth/oidc yet (capabilities: $caps), so the UI's OIDC login stays off and token login is unchanged." >&2
|
||||
echo "one-time step, as root on this host (docs/1-getting-started/setup.md):" >&2
|
||||
${lib.concatMapStringsSep "\n" (l: "echo ${lib.escapeShellArg " ${l}"} >&2") granterSetupSteps}
|
||||
echo "swarm-bao-granter-role re-runs this unit once it succeeds." >&2
|
||||
exit 0
|
||||
;;
|
||||
esac
|
||||
|
||||
# Stored by authelia's mint, in its container, which
|
||||
# this boot does not wait on, so absence is retried.
|
||||
if ! secret="$(bao kv get -field=value ${lib.escapeShellArg "${credentialMountPath}/${baoUiClientLeaf}"} 2>"$err")"; then
|
||||
echo ${lib.escapeShellArg "the store did not return ${credentialMountPath}/${baoUiClientLeaf}: the UI's OIDC client secret is not published yet."} >&2
|
||||
cat "$err" >&2
|
||||
exit 1
|
||||
fi
|
||||
if [ -z "$secret" ]; then
|
||||
echo ${lib.escapeShellArg "the store returned an empty ${credentialMountPath}/${baoUiClientLeaf}."} >&2
|
||||
exit 1
|
||||
fi
|
||||
|
||||
# bao fetches authelia's discovery document on this write, so an
|
||||
# unreachable or untrusted authelia fails it and the unit retries.
|
||||
printf '%s' "$secret" |
|
||||
bao write auth/oidc/config ${
|
||||
lib.concatMapStringsSep " " lib.escapeShellArg operatorViewerOidcConfig
|
||||
}
|
||||
|
||||
# A JSON body on stdin, because `bound_claims` is a map.
|
||||
printf '%s' ${lib.escapeShellArg operatorViewerRole} |
|
||||
bao write auth/oidc/role/${lib.escapeShellArg operatorViewerPolicyName} -
|
||||
'';
|
||||
};
|
||||
}
|
||||
Loading…
Reference in a new issue