Skip to content

feature: Test users: let automation log in without the GitHub device flow #616

Closed

Description

@aslom

Problem

Every login to the control plane goes through GitHub's device flow: a person reads a code off the
terminal and approves it in a browser. That is right for real users and impossible for anything
unattended.

The cost is already in the tree. deploy/k8s/smoke.sh:230-237 cannot log in, so it mints an API
token inside the control-plane pod using the signing key directly:

api="$(kc -n "$NS" exec deploy/moca-control-plane -c control-plane -- \
  node --import tsx --input-type=module -e \
  "import { makeSigner } from './src/token.ts';
   const s = makeSigner(readFileSync('/run/credentials/SH_SESSION_TOKEN_PRIVATE_KEY', 'utf8'));
   process.stdout.write(s.mint({ sub: 'smoke:1', tenant: 'smoke:1', roles: [], scope: ['api'], ttlSeconds: 900 }));")"

That works, but it is not a login:

  • no refresh family is issued, so POST /v1/auth/token and the whole rotation path
    (ADR-0039) go untested by the smoke;
  • identity.rolesFor() never runs, so an SH_ADMIN_SUBJECTS change is not exercised;
  • no login entry reaches the audit stream;
  • it needs kubectl exec into a running pod, so it cannot be driven by a client that only has the
    control plane's URL.

In other words the smoke authenticates in a shape no real user ever has.

What cannot be tested at all today is multi-user behaviour: N users each with their own stored
credential, each spending only their own, and the cross-user case where user A is refused user B's
session. Doing that now would take N GitHub accounts and N browsers. The MU1 claims that matter most
— per-subject credential issuance and session isolation — have no automated coverage above one user.

Proposed solution

Add an opt-in test-user identity path, off unless an operator asks for it.

When SH_TEST_USERS=N is set, the control plane serves logins for N synthetic subjects
test:1 … test:N. A test user is configured exactly like a GitHub user: the handler calls the
same deps.refresh.issue() and returns the same loginBody() as completeDeviceAuth
(packages/control-plane/src/handlers.ts:458-478), so downstream — token TTLs, refresh rotation and
renewal, rolesFor, the ownership index, the audit trail, credential storage — nothing can tell the
difference. Only the way identity is established changes.

Three pieces:

  1. TestUserProvider in packages/control-plane/src/identity.ts, behind the existing
    IdentityProvider seam that the file's own header already anticipates ("slice 2 adds a generic
    oidc provider"). Additive: deps.identity stays the GitHub provider, and the test provider is
    constructed only when SH_TEST_USERS > 0, so a deployment without the variable gains no new code
    path.
  2. POST /v1/auth/test-token taking {"index": N}, returning a device-login-shaped body.
  3. mocactl login --test-user N, which writes the same auth.json a device login writes
    (setAuth(rt, toCachedAuth(login, url))), so every later command and the automatic token renewal
    behave identically.

Fails closed

  • The handler answers 404 not_found unless SH_TEST_USERS > 0 — indistinguishable from an
    unrouted path, so a production deployment does not advertise that a test-login path exists.
  • SH_TEST_USERS set together with the sandbox profile is a boot refusal. That profile gates
    logins on enrolled GitHub subjects (sandbox-config.ts:117-127, GITHUB_SUBJECT_RE); a synthetic
    subject must not slip past it.
  • N is capped (1000), so a mistyped 100000 cannot mint a six-digit user space.
  • The control plane logs one line at boot naming the count, so an operator never has to wonder
    whether a cluster has test logins enabled.

Why not the alternatives

  • Pre-mint N tokens at boot — tokens exist before anyone asks for them, cannot be re-obtained
    after they expire, and have to be surfaced somewhere (a log or a Secret) that is worse than an
    endpoint.
  • MOCA_API_TOKEN override in mocactl — smallest change, but it skips the refresh machinery
    entirely, so test users would not be configured like GitHub users, which is the point.
  • Run a real second IdP (Dex) for tests — cleaner in the abstract, but adds a deployable to the
    test path to avoid code that is ~150 lines behind a seam that already exists. The same reasoning
    identity.ts records for not using Dex in slice 1.
  • Keep minting in-pod — the status quo; requires kubectl exec, and tests a shape no user has.

Test commands

Unit and type checks:

make lint && make typecheck && make test

Deploy with test users on (--target ocp-single, README §12; --target ocp works the same way):

export LOG_DIR="${TMPDIR:-/tmp}/kagenti/tdd/moca"; mkdir -p "$LOG_DIR"
SH_TEST_USERS=10 SH_GITHUB_CLIENT_ID=<client id> SH_SINGLE_NAMESPACE=<ns> \
  deploy/k8s/setup.sh --target ocp-single --skip-build \
  --image <registry>/moca:<tag> --sandbox-image <registry>/moca-remote-worker:<tag> \
  >"$LOG_DIR/setup.log" 2>&1; echo "EXIT:$?"

Check the gate before checking the feature — on a stack with SH_TEST_USERS unset this must be
404, or the gate is not doing its job:

kubectl -n <ns> port-forward svc/moca-control-plane 8090:8080 >/dev/null 2>&1 &
curl -s -o /dev/null -w '%{http_code}\n' -X POST -H 'Content-Type: application/json' \
  -d '{"index":1}' http://127.0.0.1:8090/v1/auth/test-token
# want 404 when SH_TEST_USERS is unset; 200 once it is set

Ten users end to end:

./deploy/k8s/test-users.py --users 10 --control-plane-url http://127.0.0.1:8090
./deploy/k8s/test-users.py --users 10 --api      # same claims, straight against the API

The audit stream must show ten distinct subjects, each with its own credential name (the audit
records names, never values):

kubectl -n <ns> exec redis-0 -- sh -c \
  'redis-cli --no-auth-warning -a "$REDISCLI_AUTH" XREVRANGE sh:cp:audit + - COUNT 40' |
  grep -A1 '^subject$' | grep '^test:' | sort -u | wc -l     # want 10

Regression — the device flow and the existing smoke must be untouched:

mocactl --control-plane-url http://127.0.0.1:8090 login      # still the device flow
K8S_LIVE_SMOKE=1 SMOKE_MODEL_URL=<endpoint> SMOKE_MODEL_KIND=bearer SMOKE_MODEL_TOKEN=<token> \
  deploy/k8s/smoke.sh --target ocp-single                    # still PASS=12 FAIL=0

The driver script

deploy/k8s/test-users.py, a PEP 723 uv run script (stdlib-only, so uv needs no PyPI egress in
a locked-down cluster). It prefers real mocactl commands over the API, so what it proves is
what a user's client actually does; --api switches to urllib against the same endpoints for the
same claims, and doubles as a reference for a non-CLI client.

./deploy/k8s/test-users.py --users 10              # N is configurable; 10 is the default
./deploy/k8s/test-users.py --users 3 --jobs 3 --json

Why one directory per user

mocactl keeps its login in a single auth.json holding exactly one CachedAuth
(packages/mocactl/src/config.ts:142), resolved from XDG_CONFIG_HOME
(resolvePaths, config.ts:25). So one process in one directory cannot hold ten logins — a
second login overwrites the first. The script therefore gives each user its own config dir:

XDG_CONFIG_HOME=$RUN/u03 mocactl login --test-user 3
XDG_CONFIG_HOME=$RUN/u03 mocactl run "..."

which is parallel-safe and needs no product change. (A --profile flag in mocactl would be the
nicer long-term answer; it is deliberately not part of this issue.)

What it does

Per user N, in its own config dir:

  1. login --test-user N → expect logged in as Test User N
  2. doctor → expect every check green
  3. credentials add test-user-N … → the user's own inference credential, secret via stdin
  4. run "<one simple operation>" → assert the reply (e.g. the sandbox hostname, or an arithmetic
    answer that proves a real turn ran)

Then the cross-user claims, which are the reason the script exists:

  1. user 2 attempts user 1's real session id → expect exit 1 and
    that session no longer exists, or is not yours (the message asserted in
    packages/mocactl/test/messages.test.ts:14)
  2. user 1 resumes that same session → succeeds, proving the id was real and the refusal in (5) was
    about ownership, not a dead session
  3. each user's sessions lists only their own
  4. the audit stream shows each turn spending its own user's credential name

It prints a per-user table plus a summary, and exits non-zero unless every claim passed.

Scope

  • In: dev/test logins, the endpoint, the CLI flag, the driver script, an ADR, and reference docs.
  • Out: any production use; changing the device flow; a mocactl --profile flag; replacing the
    smoke's in-pod mint (that can follow once this lands, and is the natural first consumer).

Follow-up this would enable

smoke.sh could drop the kubectl exec … mint entirely and log in as test:1 instead, which would
let it run against a control plane it can only reach over HTTP, and would put the refresh path under
test for the first time.


Assisted-By: Claude (Anthropic AI) [email protected]

Activity

  1. added 7 commits that reference this issue on Oct 26, 2026
    2c52de5
    d4ec0a4
    a6f2f73
    753098a
    8937c2f
    53d8764
    0c1ccd7
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Metadata

Metadata

Assignees

No one assigned

    Labels

    No labels
    No labels

    Type

    No type

    Projects

    No projects

      Milestone

      No milestone

      Relationships

      None yet

      Development

      No branches or pull requests

      Issue actions