feat(backend): support access by offline challenge codes replaces the master account
Platform v2 step 1.10 (APPS_PLATFORM D-16, section 6.5).
- POST /api/auth/support/challenge (public): an 8-character challenge, stored 15 min
(support_challenges, revision 0009; at most 5 open). POST /api/auth/support/verify
{challenge, code}: code = base32(HMAC(SUPPORT_SECRET, site_id + challenge))[:10];
single use, 5 wrong codes burn the challenge. Opens a 4 h support session
(client=support, synthetic superadmin) with a hard end: sessions.expires_at.
- core/support_codes.py (stdlib only) is the derivation; tools/support_code.py
(--init, --site-secret <SITE_ID>, <SITE_ID> <CHALLENGE>) uses it on the builder's
machine with the master key in %USERPROFILE%/.xenia/support_master_key.txt.
- MASTER_USERNAME/MASTER_PASSWORD removed (config, login, docker-compose.yml,
.env.example); SUPPORT_SECRET added; install.sh asks for it.
- e2e: backends get SUPPORT_SECRET from a test master key and log in with challenge
codes (lib/support.mjs, license_e2e.py); install.mjs expects the new prompt.
- Pack rule CS-15.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
This commit is contained in:
@@ -39,8 +39,8 @@ host eth0; DEFAULT_ROUTE=""; echo "DETECT no-d
|
||||
host wlan0; DEFAULT_ROUTE="default via 192.168.1.1 dev wlan0"; echo "DETECT wifi-only=$(detect_host_ip)"
|
||||
# full install.sh flows (real ip/hostname again, docker stubbed)
|
||||
rm /stub/ip /stub/hostname; unset XENIA_SYS_NET
|
||||
cp -r /t /a && cd /a && printf 'site-123\\nkey-abc\\n\\n\\n' | bash install.sh >/dev/null
|
||||
echo "FRESH env=$(grep -cE '^(SITE_ID=site-123|SITE_KEY=key-abc|HOST_IP=.+)$' .env) cert=$(test -s certs/cert.pem && echo yes) keymode=$(stat -c %a certs/key.pem) conf=$(test -s nginx-proxy/nginx.conf && echo yes)"
|
||||
cp -r /t /a && cd /a && printf 'site-123\\nkey-abc\\n\\n\\nsupp-0123abcd\\n' | bash install.sh >/dev/null
|
||||
echo "FRESH env=$(grep -cE '^(SITE_ID=site-123|SITE_KEY=key-abc|HOST_IP=.+|SUPPORT_SECRET=supp-0123abcd)$' .env) cert=$(test -s certs/cert.pem && echo yes) keymode=$(stat -c %a certs/key.pem) conf=$(test -s nginx-proxy/nginx.conf && echo yes)"
|
||||
cp -r /t /b && cd /b && mkdir -p certs && cp /a/certs/* certs/ && grep -v '^HOST_IP' .env.example > .env
|
||||
bash install.sh >/dev/null; echo "UPGRADE hostip=$(grep -c '^HOST_IP=.\\+' .env)"
|
||||
bash install.sh >/dev/null; echo "RERUN hostip_lines=$(grep -c '^HOST_IP=' .env)"
|
||||
@@ -54,7 +54,7 @@ r.check('HOST_IP: WireGuard full tunnel → physical NIC', val('DETECT wg-full-t
|
||||
r.check('HOST_IP: tunnel owns the default route → physical NIC', val('DETECT tunnel-owns-default') === '10.98.20.70')
|
||||
r.check('HOST_IP: no default route → physical NIC', val('DETECT no-default-route') === '10.98.20.70')
|
||||
r.check('HOST_IP: WiFi-only server', val('DETECT wifi-only') === '192.168.1.44')
|
||||
r.check('fresh install: .env filled (SITE_ID, SITE_KEY, HOST_IP)', val('env') === '3')
|
||||
r.check('fresh install: .env filled (SITE_ID, SITE_KEY, HOST_IP, SUPPORT_SECRET)', val('env') === '4')
|
||||
r.check('fresh install: self-signed cert, key 600, proxy config written', val('cert') === 'yes' && val('keymode') === '600' && val('conf') === 'yes')
|
||||
r.check('upgrade: missing HOST_IP backfilled', val('UPGRADE hostip') === '1')
|
||||
r.check('re-run: HOST_IP not duplicated', val('RERUN hostip_lines') === '1')
|
||||
|
||||
@@ -18,8 +18,6 @@ mkdirSync(OUT, { recursive: true })
|
||||
export const PYTHON = process.env.PYTHON || (process.platform === 'win32' ? 'python' : 'python3')
|
||||
// Playwright browser: system Edge by default; BROWSER_CHANNEL= (empty) uses Playwright's own Chromium
|
||||
export const BROWSER_CHANNEL = process.env.BROWSER_CHANNEL ?? 'msedge'
|
||||
// Break-glass login the test backends are started with (never a real account)
|
||||
export const MASTER = { username: 'e2e', password: 'e2e-pass-123' }
|
||||
// The Android emulator reaches the host machine at this address
|
||||
export const EMULATOR_HOST = '10.0.2.2'
|
||||
|
||||
|
||||
@@ -4,7 +4,8 @@
|
||||
import { execFileSync, spawn } from 'node:child_process'
|
||||
import { copyFileSync, existsSync, mkdirSync, mkdtempSync, openSync, readFileSync, renameSync, rmSync, writeFileSync } from 'node:fs'
|
||||
import path from 'node:path'
|
||||
import { BACKEND, CS, MASTER, OUT, PYTHON, WAITER, sleep } from './env.mjs'
|
||||
import { BACKEND, CS, OUT, PYTHON, WAITER, sleep } from './env.mjs'
|
||||
import { SUPPORT_MASTER, siteSecret, supportLogin } from './support.mjs'
|
||||
|
||||
/** Kill a process and its children (npx/shell wrappers on Windows). */
|
||||
export function killTree(child) {
|
||||
@@ -66,7 +67,7 @@ export async function startBackend({ port = 8055, siteId = 'test-site-1', siteKe
|
||||
SITE_ID: siteId, SITE_KEY: siteKey, CLOUD_URL: cloudUrl, SECRET_KEY: 'e2e-secret',
|
||||
// No cloud = a test backend: licensing off (every module). The license suite passes a cloudUrl.
|
||||
LICENSE_MODE: cloudUrl ? 'enforced' : 'off',
|
||||
MASTER_USERNAME: MASTER.username, MASTER_PASSWORD: MASTER.password, ...env,
|
||||
SUPPORT_SECRET: siteSecret(SUPPORT_MASTER, siteId), ...env,
|
||||
},
|
||||
})
|
||||
const url = `http://127.0.0.1:${port}`
|
||||
@@ -74,12 +75,8 @@ export async function startBackend({ port = 8055, siteId = 'test-site-1', siteKe
|
||||
return { url, dataDir, child, stop: async () => { killTree(child); await waitHttp(`${url}/api/system/health`, false).catch(() => {}) } }
|
||||
}
|
||||
|
||||
export async function login(baseUrl) {
|
||||
const r = await fetch(`${baseUrl}/api/auth/login`, {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify(MASTER),
|
||||
})
|
||||
return (await r.json()).access_token
|
||||
}
|
||||
/** A superadmin token for a test backend, through support access (challenge code). */
|
||||
export const login = baseUrl => supportLogin(baseUrl)
|
||||
|
||||
/** nginx:alpine serving a waiter build (`dist`) with the waiter's own nginx.conf, /api → host backend. */
|
||||
export async function serveWaiterBuild({ name, port, dist = path.join(WAITER, 'dist'), backendPort = 8055 }) {
|
||||
@@ -121,7 +118,7 @@ export function composeStack({ project, version, siteId, http, https, manager, e
|
||||
writeFileSync(envFile, Object.entries({
|
||||
REGISTRY: 'e2e', VERSION: version, SITE_ID: siteId, SITE_KEY: 'x', CLOUD_URL: '', SECRET_KEY: 'e2e-secret',
|
||||
LICENSE_MODE: 'off',
|
||||
HOST_IP: '10.0.2.2', TLS_PORT: https, HTTP_PORT: http, MASTER_USERNAME: MASTER.username, MASTER_PASSWORD: MASTER.password,
|
||||
HOST_IP: '10.0.2.2', TLS_PORT: https, HTTP_PORT: http, SUPPORT_SECRET: siteSecret(SUPPORT_MASTER, siteId),
|
||||
DATA_PATH: fwd(path.join(dir, 'data')), LOGO_PATH: fwd(path.join(dir, 'logo.png')), FISCAL_PATH: fwd(path.join(dir, 'fiscal')),
|
||||
...extraEnv,
|
||||
}).map(([k, v]) => `${k}=${v}`).join('\n') + '\n')
|
||||
|
||||
@@ -0,0 +1,49 @@
|
||||
// Support access for the test backends (APPS_PLATFORM D-16): the suites log in the way
|
||||
// the builder would, with a challenge code. Same algorithm as
|
||||
// local_backend/core/support_codes.py; local_backend/tests/test_support.py pins a known
|
||||
// vector (master "test-master-key-0123456789abcdef", site "site-A", challenge
|
||||
// "ABCD-EFGH" -> "E4MYPVWT3M"), checked here by selfCheck().
|
||||
import { createHmac } from 'node:crypto'
|
||||
|
||||
// The test master key the e2e backends' SUPPORT_SECRET is derived from (never a real key)
|
||||
export const SUPPORT_MASTER = 'e2e-support-master-key-0000000000'
|
||||
|
||||
const B32 = 'ABCDEFGHIJKLMNOPQRSTUVWXYZ234567'
|
||||
|
||||
function base32(buf) {
|
||||
let bits = 0, value = 0, out = ''
|
||||
for (const byte of buf) {
|
||||
value = (value << 8) | byte
|
||||
bits += 8
|
||||
while (bits >= 5) { out += B32[(value >>> (bits - 5)) & 31]; bits -= 5 }
|
||||
}
|
||||
if (bits > 0) out += B32[(value << (5 - bits)) & 31]
|
||||
return out
|
||||
}
|
||||
|
||||
export const siteSecret = (master, siteId) =>
|
||||
createHmac('sha256', master.trim()).update(`xenia-support-site:${siteId}`).digest('hex')
|
||||
|
||||
export function codeFor(secret, siteId, challenge) {
|
||||
const ch = challenge.toUpperCase().replace(/[^A-Z0-9]/g, '')
|
||||
return base32(createHmac('sha256', secret.trim()).update(`xenia-support:${siteId}:${ch}`).digest()).slice(0, 10)
|
||||
}
|
||||
|
||||
export function selfCheck() {
|
||||
const s = siteSecret('test-master-key-0123456789abcdef', 'site-A')
|
||||
if (codeFor(s, 'site-A', 'ABCD-EFGH') !== 'E4MYPVWT3M') throw new Error('support code algorithm drifted from core/support_codes.py')
|
||||
}
|
||||
|
||||
/** A support session on a test backend: challenge -> code -> token. */
|
||||
export async function supportLogin(baseUrl, master = SUPPORT_MASTER) {
|
||||
selfCheck()
|
||||
const c = await (await fetch(`${baseUrl}/api/auth/support/challenge`, { method: 'POST' })).json()
|
||||
if (!c.challenge) throw new Error(`support challenge failed: ${JSON.stringify(c)}`)
|
||||
const code = codeFor(siteSecret(master, c.site_id), c.site_id, c.challenge)
|
||||
const r = await fetch(`${baseUrl}/api/auth/support/verify`, {
|
||||
method: 'POST', headers: { 'Content-Type': 'application/json' }, body: JSON.stringify({ challenge: c.challenge, code }),
|
||||
})
|
||||
const body = await r.json()
|
||||
if (!body.access_token) throw new Error(`support verify failed: ${JSON.stringify(body)}`)
|
||||
return body.access_token
|
||||
}
|
||||
@@ -11,6 +11,7 @@ Starts the cloud on :8101 and the site on :8055, on throwaway databases.
|
||||
python license_e2e.py
|
||||
"""
|
||||
import base64
|
||||
import importlib.util
|
||||
import json
|
||||
import os
|
||||
import shutil
|
||||
@@ -33,7 +34,12 @@ BACKEND = CS / "local_backend"
|
||||
CLOUD = Path(os.environ.get("CLOUD_DIR", CS.parent / "cloud-service" / "cloud_backend"))
|
||||
OUT = E2E / "out"
|
||||
CLOUD_URL, SITE_URL = "http://127.0.0.1:8101", "http://127.0.0.1:8055"
|
||||
MASTER = {"username": "e2e", "password": "e2e-pass-123"}
|
||||
# Support access (D-16): the site gets a SUPPORT_SECRET derived from this test master key,
|
||||
# and the suite logs in with a challenge code, the way the builder would.
|
||||
SUPPORT_MASTER = "e2e-support-master-key-0000000000"
|
||||
_spec = importlib.util.spec_from_file_location("support_codes", BACKEND / "core" / "support_codes.py")
|
||||
codes = importlib.util.module_from_spec(_spec)
|
||||
_spec.loader.exec_module(codes)
|
||||
|
||||
key = Ed25519PrivateKey.generate()
|
||||
PRIV = base64.b64encode(key.private_bytes(serialization.Encoding.Raw, serialization.PrivateFormat.Raw, serialization.NoEncryption())).decode()
|
||||
@@ -116,7 +122,7 @@ def start_cloud():
|
||||
def start_site(site_id, site_key):
|
||||
env = {**os.environ, "DATABASE_URL": f"sqlite:///{(tmp / 'data' / 'pos.db').as_posix()}", "CLOUD_URL": CLOUD_URL,
|
||||
"SITE_ID": site_id, "SITE_KEY": site_key, "SECRET_KEY": "e2e-site", "LICENSE_MODE": "enforced",
|
||||
"MASTER_USERNAME": MASTER["username"], "MASTER_PASSWORD": MASTER["password"]}
|
||||
"SUPPORT_SECRET": codes.site_secret(SUPPORT_MASTER, site_id)}
|
||||
launcher = (f"import services.license as L; L.LICENSE_PUBLIC_KEY = {PUB!r}; "
|
||||
"import uvicorn; uvicorn.run('main:app', host='127.0.0.1', port=8055)")
|
||||
procs["site"] = subprocess.Popen([sys.executable, "-c", launcher], cwd=BACKEND, env=env,
|
||||
@@ -133,7 +139,9 @@ def stop(name):
|
||||
|
||||
|
||||
def status():
|
||||
_, tok = call(SITE_URL, "POST", "/api/auth/login", MASTER)
|
||||
_, ch = call(SITE_URL, "POST", "/api/auth/support/challenge", {})
|
||||
code = codes.code_for(codes.site_secret(SUPPORT_MASTER, ch["site_id"]), ch["site_id"], ch["challenge"])
|
||||
_, tok = call(SITE_URL, "POST", "/api/auth/support/verify", {"challenge": ch["challenge"], "code": code})
|
||||
return call(SITE_URL, "GET", "/api/system/status", token=tok["access_token"])[1], tok["access_token"]
|
||||
|
||||
|
||||
|
||||
Reference in New Issue
Block a user