swarmctl login: find the sso provider from the gateway's published metadata
This commit is contained in:
parent
c1851f37cc
commit
cd040afa0e
3 changed files with 421 additions and 16 deletions
|
|
@ -32,6 +32,17 @@ let
|
|||
auth_request_set $target_url $scheme://$http_host$request_uri;
|
||||
'';
|
||||
loginRedirect = "https://${autheliaCfg.domain}/?rd=$target_url";
|
||||
|
||||
# What `swarmctl login` reads to find the swarm's SSO provider (RFC 9728): the
|
||||
# `/api/` 401 names this document in its challenge, and the document names the
|
||||
# authorization server. `resource` is the audience a `swarmctl` token carries
|
||||
# (`swarmctl/src/remote.rs`, `swarm-authelia.nix`).
|
||||
protectedResourcePath = "/.well-known/oauth-protected-resource/api/";
|
||||
protectedResource = builtins.toJSON {
|
||||
resource = "https://${apex}/api/";
|
||||
authorization_servers = [ "https://${autheliaCfg.domain}" ];
|
||||
};
|
||||
challenge = ''Bearer realm="swarm", resource_metadata="https://${apex}${protectedResourcePath}"'';
|
||||
swarmAuthRequest = ''
|
||||
${authRequest}
|
||||
error_page 401 =302 ${loginRedirect};
|
||||
|
|
@ -334,15 +345,23 @@ in
|
|||
${gatewayCfg.lib.securityHeaders}
|
||||
if ($http_authorization) {
|
||||
${gatewayCfg.lib.securityHeaders}
|
||||
add_header WWW-Authenticate 'Bearer realm="swarm"' always;
|
||||
add_header WWW-Authenticate '${challenge}' always;
|
||||
return 401;
|
||||
}
|
||||
if ($http_accept ~* "text/html") {
|
||||
return 302 ${loginRedirect};
|
||||
}
|
||||
add_header WWW-Authenticate 'Bearer realm="swarm"' always;
|
||||
add_header WWW-Authenticate '${challenge}' always;
|
||||
return 401;
|
||||
'';
|
||||
# The document the challenge names. Public by design: a client reads it
|
||||
# before it has a login, and it holds only the names of the swarm's own
|
||||
# API and its SSO provider.
|
||||
"= ${protectedResourcePath}".extraConfig = ''
|
||||
${gatewayCfg.lib.securityHeaders}
|
||||
default_type application/json;
|
||||
return 200 '${protectedResource}';
|
||||
'';
|
||||
# ⚠️ THE ONE PROXIED LOCATION ON THIS VHOST WITH NO `auth_request`,
|
||||
# and that is deliberate rather than an omission.
|
||||
#
|
||||
|
|
|
|||
|
|
@ -729,16 +729,37 @@ let
|
|||
api = l."/api/".extraConfig;
|
||||
refused = l."@swarm_api_unauthorized".extraConfig;
|
||||
sec = allLocal.services.hyperhive.gateway.lib.securityHeaders;
|
||||
challenge = ''Bearer realm="swarm", resource_metadata="https://t.local/.well-known/oauth-protected-resource/api/"'';
|
||||
in
|
||||
lib.hasInfix "error_page 401 = @swarm_api_unauthorized;" api
|
||||
&& !(lib.hasInfix "=302" api)
|
||||
&& lib.hasInfix ''proxy_set_header Authorization "";'' api
|
||||
&& lib.hasInfix "if ($http_authorization) {\n ${sec}\n add_header WWW-Authenticate 'Bearer realm=\"swarm\"' always;\n return 401;\n}" refused
|
||||
&& lib.hasInfix "if ($http_authorization) {\n ${sec}\n add_header WWW-Authenticate '${challenge}' always;\n return 401;\n}" refused
|
||||
&& lib.hasInfix "if ($http_accept ~* \"text/html\") {\n return 302 https://auth.t.local/?rd=$target_url;\n}" refused
|
||||
&& lib.hasPrefix sec refused
|
||||
&& lib.hasInfix "}\nadd_header WWW-Authenticate 'Bearer realm=\"swarm\"' always;\nreturn 401;" refused
|
||||
&& lib.hasInfix "}\nadd_header WWW-Authenticate '${challenge}' always;\nreturn 401;" refused
|
||||
&& lib.hasInfix "error_page 401 =302 https://auth.t.local/" l."/".extraConfig;
|
||||
}
|
||||
{
|
||||
# `swarmctl login` finds the SSO provider from the `/api/` challenge's
|
||||
# `resource_metadata` document. The document is served with no
|
||||
# `auth_request`, as JSON naming the API as `resource` and authelia as
|
||||
# its authorization server. The `/` clause is the control: the page
|
||||
# route carries an `auth_request`, so a pass means the two differ.
|
||||
# Turned red by dropping `authorization_servers` from `protectedResource`
|
||||
# in `swarm-ui.nix`.
|
||||
name = "the swarm vhost publishes its API's authorization server without a login";
|
||||
ok =
|
||||
let
|
||||
l = allLocal.services.nginx.virtualHosts."t.local".locations;
|
||||
doc = l."= /.well-known/oauth-protected-resource/api/".extraConfig;
|
||||
in
|
||||
lib.hasInfix "default_type application/json;" doc
|
||||
&& lib.hasInfix ''"resource":"https://t.local/api/"'' doc
|
||||
&& lib.hasInfix ''"authorization_servers":["https://auth.t.local"]'' doc
|
||||
&& !(lib.hasInfix "auth_request" doc)
|
||||
&& lib.hasInfix "auth_request" l."/".extraConfig;
|
||||
}
|
||||
{
|
||||
# authelia 4.39.20 refuses a bearer-scoped public client missing any of
|
||||
# these at startup, after a green deploy; and a public client must not
|
||||
|
|
|
|||
|
|
@ -333,6 +333,36 @@ with subtest(
|
|||
assert status != 0 and f"not logged in to {SWARM_URL}" in out, (status, out)
|
||||
machine.succeed(f"{VIA_SOCKET} swarmctl schedule list")
|
||||
|
||||
with subtest("swarmctl login discovers the swarm's SSO provider from the gateway"):
|
||||
# The swarm's own CA signs the gateway's leaf. A VM has no browser, so the login runs to the
|
||||
# point where it prints the authorization URL and waits for the answer, which the timeout ends.
|
||||
# Reaching that URL takes the gateway's 401 challenge, its published metadata, and authelia
|
||||
# accepting the pushed authorization request.
|
||||
ca = "/var/lib/hive-tls/swarm-services-root.pem"
|
||||
auth = f"https://auth.{SWARM_URL.removeprefix('https://')}"
|
||||
challenge = machine.succeed(
|
||||
f"curl --silent --cacert {ca} -D - -o /dev/null {SWARM_URL}/api/"
|
||||
)
|
||||
assert challenge.startswith("HTTP/") and " 401" in challenge.splitlines()[0], (
|
||||
challenge
|
||||
)
|
||||
assert (
|
||||
f'resource_metadata="{SWARM_URL}/.well-known/oauth-protected-resource/api/"'
|
||||
in challenge
|
||||
), challenge
|
||||
metadata = machine.succeed(
|
||||
f"curl --fail --silent --cacert {ca} {SWARM_URL}/.well-known/oauth-protected-resource/api/"
|
||||
)
|
||||
assert json.loads(metadata) == {
|
||||
"resource": f"{SWARM_URL}/api/",
|
||||
"authorization_servers": [auth],
|
||||
}, metadata
|
||||
status, out = machine.execute(
|
||||
f"timeout 30 swarmctl --swarm {SWARM_URL} --ca-file {ca} login --callback-port 8765 2>&1"
|
||||
)
|
||||
assert status == 124, (status, out)
|
||||
assert f"{auth}/api/oidc/authorization?client_id=swarmctl&request_uri=" in out, out
|
||||
|
||||
with subtest("the agents are deployed and need a login"):
|
||||
settle(
|
||||
f"curl --fail --silent --unix-socket {SOCKET} {API}/api/agents/status "
|
||||
|
|
|
|||
Loading…
Reference in a new issue