From b3f73a325d382d31fa94548363934770cc515170 Mon Sep 17 00:00:00 2001 From: John Coffey Date: Wed, 18 Nov 2026 14:52:52 -0700 Subject: [PATCH] Lab: the chart's webmail and console test frontends-test.sh installs them (values-frontends.yaml) and runs signin.py, which starts a webmail sign-in on one replica, signs in on the server, finishes on the other replica and uses the session on both; and console-signin.py, which loads the console, signs in as inbuxa-admin with PKCE to its callback, exchanges the code and calls the server from the console's origin. --- lab/chart/console-signin.py | 85 +++++++++++++++++++++++++++ lab/chart/frontends-test.sh | 47 +++++++++++++++ lab/chart/signin.py | 100 ++++++++++++++++++++++++++++++++ lab/chart/values-frontends.yaml | 18 ++++++ 4 files changed, 250 insertions(+) create mode 100755 lab/chart/console-signin.py create mode 100755 lab/chart/frontends-test.sh create mode 100755 lab/chart/signin.py create mode 100644 lab/chart/values-frontends.yaml diff --git a/lab/chart/console-signin.py b/lab/chart/console-signin.py new file mode 100755 index 0000000..f251fb9 --- /dev/null +++ b/lab/chart/console-signin.py @@ -0,0 +1,85 @@ +#!/usr/bin/env python3 +"""The console's sign-in, as its browser does it. + +Loads the console and checks it points at the server; signs in on the +server's page as the public client inbuxa-admin with PKCE, back to the +console's own callback; exchanges the code; and calls the server from the +console's origin, which CORS has to allow. URLs are port-forwards; the +public addresses are names only. + + console-signin.py +""" +import base64 +import hashlib +import http.client +import json +import secrets +import sys +import urllib.parse + +console, server, console_url, server_url, account, password_file = sys.argv[1:7] +password = open(password_file).read().strip() + + +def request(base, method, path, headers=None, body=None): + u = urllib.parse.urlsplit(base) + conn = http.client.HTTPConnection(u.hostname, u.port, timeout=30) + conn.request(method, path, body=body, headers=headers or {}) + res = conn.getresponse() + data = res.read() + conn.close() + return res, data + + +def fail(msg): + print("!!", msg) + sys.exit(1) + + +# 1. The console, pointed at the server +res, page = request(console, "GET", "/") +csp = res.getheader("content-security-policy") or "" +if res.status != 200 or f'content="{server_url}"' not in page.decode(): + fail(f"console page: {res.status}, api-base-url not {server_url}") +if f"connect-src 'self' {server_url}" not in csp: + fail(f"console CSP doesn't let it reach the server: {csp}") +print(f"console: served, api-base-url and connect-src name {server_url}") + +# 2. The server's page, for the console's client and callback +verifier = secrets.token_urlsafe(48) +challenge = base64.urlsafe_b64encode(hashlib.sha256(verifier.encode()).digest()).rstrip(b"=").decode() +redirect = f"{console_url}/oauth/callback" +res, data = request(server, "POST", "/api/auth", {"content-type": "application/json"}, json.dumps({ + "type": "authCode", "accountName": account, "accountSecret": password, + "clientId": "inbuxa-admin", "redirectUri": redirect, + "codeChallenge": challenge, "codeChallengeMethod": "S256", "state": "lab", +})) +answer = json.loads(data or b"{}") +code = answer.get("client_code") or answer.get("clientCode") +if res.status != 200 or not code: + fail(f"sign-in as inbuxa-admin to {redirect}: {res.status} {answer.get('type')}") +print(f"server: signed {account} in for inbuxa-admin, back to {redirect}") + +# 3. The code for a token, as the console's page exchanges it +res, data = request(server, "GET", "/.well-known/oauth-authorization-server") +token_path = urllib.parse.urlsplit(json.loads(data)["token_endpoint"]).path +res, data = request(server, "POST", token_path, {"content-type": "application/x-www-form-urlencoded"}, + urllib.parse.urlencode({"grant_type": "authorization_code", "code": code, + "redirect_uri": redirect, "client_id": "inbuxa-admin", + "code_verifier": verifier})) +token = json.loads(data or b"{}").get("access_token") +if res.status != 200 or not token: + fail(f"token exchange: {res.status} {data[:200]!r}") +print("server: exchanged the code for a token") + +# 4. From the console's origin: the preflight, then the call +res, _ = request(server, "OPTIONS", "/jmap/session", { + "origin": console_url, "access-control-request-method": "GET", + "access-control-request-headers": "authorization"}) +allowed = res.getheader("access-control-allow-origin") +if allowed not in (console_url, "*"): + fail(f"preflight from {console_url}: {res.status}, allow-origin {allowed}") +res, data = request(server, "GET", "/jmap/session", {"authorization": f"Bearer {token}", "origin": console_url}) +if res.status != 200 or res.getheader("access-control-allow-origin") not in (console_url, "*"): + fail(f"JMAP session from {console_url}: {res.status}") +print(f"server: allows {console_url}; the token opens {json.loads(data).get('username')}'s session") diff --git a/lab/chart/frontends-test.sh b/lab/chart/frontends-test.sh new file mode 100755 index 0000000..bc550c9 --- /dev/null +++ b/lab/chart/frontends-test.sh @@ -0,0 +1,47 @@ +#!/usr/bin/env bash +# The chart's webmail and console (K-C3) on the lab's seeded install: +# installs them (values-frontends.yaml), then +# +# - signs in to the webmail starting on one replica and finishing on the +# other, and uses the session on both (signin.py) +# - signs in to the console as its browser would (console-signin.py) +# +# SERVER_TAG=plan lab/chart/frontends-test.sh +set -euo pipefail + +cd "$(dirname "$0")" +SERVER_TAG="${SERVER_TAG:?the server image tag in kind}" +K=(kubectl --context kind-inbuxa-lab -n inbuxa) +CHART="${CHART:-../../../inbuxa-charts/charts/inbuxa}" +STATE="${STATE:-$HOME/.cache/inbuxa-lab}" +PIDS=() +cleanup() { for p in "${PIDS[@]}"; do kill "$p" 2>/dev/null || true; done; } +trap cleanup EXIT +forward() { # + "${K[@]}" port-forward "$2" "$1:8080" >/dev/null 2>&1 & + PIDS+=($!) +} + +echo "== install the webmail and the console" +helm --kube-context kind-inbuxa-lab -n inbuxa upgrade inbuxa "$CHART" \ + -f values-lab.yaml -f values-frontends.yaml --set server.image.tag="$SERVER_TAG" \ + --wait --timeout 10m >/dev/null +"${K[@]}" get pods -l 'app.kubernetes.io/component in (webmail,console)' --no-headers + +mapfile -t webmail < <("${K[@]}" get pods -l app.kubernetes.io/component=webmail -o name | sort) +[ "${#webmail[@]}" -ge 2 ] || { echo "!! fewer than two webmail pods"; exit 1; } +forward 18081 "${webmail[0]}" +forward 18082 "${webmail[1]}" +forward 18080 svc/inbuxa-http +forward 18083 svc/inbuxa-console +sleep 3 + +echo "== webmail: start on ${webmail[0]#pod/}, finish on ${webmail[1]#pod/}" +python3 signin.py http://127.0.0.1:18081 http://127.0.0.1:18082 http://127.0.0.1:18080 \ + alice@lab.test "$STATE/alice.password" + +echo "== console" +python3 console-signin.py http://127.0.0.1:18083 http://127.0.0.1:18080 \ + https://admin.lab.test https://mail.lab.test admin@lab.test "$STATE/admin.password" + +echo "== both pass" diff --git a/lab/chart/signin.py b/lab/chart/signin.py new file mode 100755 index 0000000..7768f1c --- /dev/null +++ b/lab/chart/signin.py @@ -0,0 +1,100 @@ +#!/usr/bin/env python3 +"""A browser signing in to the webmail across two replicas. + +Starts the sign-in on webmail replica A, signs in on the server's page (its +API, as the page itself does), comes back to replica B, then uses the +session on both: B and A each fetch the account's session from the server, +and each runs a JMAP Mailbox/get. Every URL is a port-forward; the public +addresses are names only. + + signin.py +""" +import http.client +import json +import sys +import urllib.parse + +a, b, server, account, password_file = sys.argv[1:6] +password = open(password_file).read().strip() + + +def request(base, method, path, headers=None, body=None): + u = urllib.parse.urlsplit(base) + conn = http.client.HTTPConnection(u.hostname, u.port, timeout=30) + conn.request(method, path, body=body, headers=headers or {}) + res = conn.getresponse() + data = res.read() + conn.close() + return res, data + + +def cookie(res, name): + for header, value in res.getheaders(): + if header.lower() == "set-cookie" and value.startswith(name + "="): + return value.split(";", 1)[0].split("=", 1)[1] + return None + + +def fail(msg): + print("!!", msg) + sys.exit(1) + + +# 1. Replica A sends the browser to the server's page +res, _ = request(a, "GET", "/api/auth/oauth/start?username=" + urllib.parse.quote(account)) +if res.status != 302: + fail(f"start on A: {res.status}") +to = urllib.parse.urlsplit(res.getheader("location")) +q = dict(urllib.parse.parse_qsl(to.query)) +bound = cookie(res, "ihm_session_signin") +if not bound: + fail("start on A set no state cookie") +print(f"A: sends the browser to {to.scheme}://{to.netloc}{to.path} as {q['client_id']}, " + f"state cookie {len(bound)} bytes") + +# 2. The server's page: the account's password, for this client and redirect +res, data = request(server, "POST", "/api/auth", {"content-type": "application/json"}, json.dumps({ + "type": "authCode", + "accountName": account, + "accountSecret": password, + "clientId": q["client_id"], + "redirectUri": q["redirect_uri"], + "scope": q.get("scope"), + "codeChallenge": q["code_challenge"], + "codeChallengeMethod": q["code_challenge_method"], + "state": q["state"], +})) +answer = json.loads(data or b"{}") +code = answer.get("client_code") or answer.get("clientCode") +if res.status != 200 or not code: + fail(f"server sign-in: {res.status} {answer.get('type')}") +print(f"server: signed {account} in, code issued for {q['redirect_uri']}") + +# 3. The browser comes back to replica B +res, _ = request(b, "GET", "/api/auth/callback?" + urllib.parse.urlencode({"code": code, "state": q["state"]}), + {"cookie": f"ihm_session_signin={bound}"}) +where = res.getheader("location") +session = cookie(res, "ihm_session") +if res.status != 302 or where != "/" or not session: + fail(f"callback on B: {res.status} -> {where}") +print(f"B: finished the sign-in A started, session cookie {len(session)} bytes") + +# 4. The session works on both +for name, base in (("B", b), ("A", a)): + res, data = request(base, "GET", "/api/auth/session", {"cookie": f"ihm_session={session}"}) + if res.status != 200: + fail(f"session on {name}: {res.status} {data[:200]!r}") + mail_account = json.loads(data).get("primaryAccounts", {}).get("urn:ietf:params:jmap:mail") + if not mail_account: + fail(f"session on {name} names no mail account") + res, data = request(base, "POST", "/api/jmap", { + "cookie": f"ihm_session={session}", + "content-type": "application/json", + "x-requested-with": "ihasmail", + }, json.dumps({"using": ["urn:ietf:params:jmap:core", "urn:ietf:params:jmap:mail"], + "methodCalls": [["Mailbox/get", {"accountId": mail_account, "properties": ["name"]}, "0"]]})) + body = json.loads(data or b"{}") + calls = body.get("methodResponses") or [] + if res.status != 200 or not calls or calls[0][0] != "Mailbox/get": + fail(f"JMAP on {name}: {res.status} {str(body)[:300]}") + print(f"{name}: session and JMAP work ({len(calls[0][1].get('list', []))} mailboxes)") diff --git a/lab/chart/values-frontends.yaml b/lab/chart/values-frontends.yaml new file mode 100644 index 0000000..07b2ccd --- /dev/null +++ b/lab/chart/values-frontends.yaml @@ -0,0 +1,18 @@ +# The webmail and the console, on top of values-lab.yaml. The addresses are +# names only: the tests reach the pods through port-forwards. +server: + publicUrl: https://mail.lab.test +webmail: + enabled: true + image: + repository: inbuxa-lab/webmail + tag: kc3 + pullPolicy: Never + publicUrl: https://webmail.lab.test +console: + enabled: true + image: + repository: inbuxa-lab/console + tag: kc3 + pullPolicy: Never + publicUrl: https://admin.lab.test -- 2.54.0