feat: remote crew sign-in via Crew's vault on the KAS bundle route

Merged
CR
1mo ago
+3082-112

Problem / Motivation

Goal: A remote crew (an EC2 instance signed in by kirocrew cloud login, or the aws_control crew container) that runs the KAS bundle route can be signed in, show who it is signed in as, and sign out through Crew's own vault, with no kiro-cli on the remote machine, while every other route keeps its kiro-cli sign-in exactly as before.

On the KAS bundle route (agent.acp_backend: kas with agent.kas_bundle: true) the engine's only credential is Crew's own vault and kiro-cli is not installed (#21843). Two remote sign-in surfaces still drove kiro-cli on the remote machine:

  • kirocrew cloud login|logout and the launch wizard ran kiro-cli login, kiro-cli whoami --format json and kiro-cli logout on the instance over SSM. On a bundle-route instance every one of those is "command not found": the box cannot be signed in, its identity cannot be read, and the launching machine's own identity (discover_local_identity) also came from kiro-cli whoami.
  • The aws_control crew container's supervisor wrote a sentinel row into kiro-cli's login store and asked kiro-cli whoami to accept it before every start. On a bundle-route task there is no kiro-cli to ask, so the task refuses to start.

Why it matters

The bundle route is the no-kiro-cli install that lets the desktop app drop the bundled kiro-cli-chat (#21843). Until a remote crew can sign in through the vault, turning the route on by default would leave every cloud instance and every crew container with a chat backend nobody can sign in. The relay route must keep working unchanged, because that is what every remote crew runs today.

Not a goal

  • The dashboard identity endpoint (#23615), kirocrew doctor (#23617) and the model picker (#23618): other items of #21843. This PR adds its own small auth/whoami.py mapping; it can collapse onto #23615's account_type_from_claims once both are on main.
  • Shipping the KAS engine inside the aws_control container image. The container branch is gated on the written config and requires an installed engine; the image still pins kiro-cli and ships no engine.
  • A Google/GitHub sign-in for a remote crew through the vault. The cloud login target names Builder ID or an Identity Center organisation, as before; the vault verb accepts those providers for a human at a shell.
  • Flipping agent.kas_bundle to default-on, or the live validation of the approval half of Builder ID / IdC / External IdP sign-in (recorded as open in kas-auth.md).
  • Any change to the kiro-cli commands the relay route runs: they are byte-for-byte the ones in cloud/login.py today, now inside the else branch.

What changed (motivation → approach → change)

The local kirocrew cloud command cannot read the remote config (it lives in the instance's config.json), so the box decides which program owns the sign-in.

New verbs kirocrew kas login | whoami | logout | route (cli_kas_login.py) drive the existing KasLoginService from a shell in kiro-cli's vocabulary: login prints Open <URL> and code: XXXX-XXXX, polls until approved, exits 0 once the vault holds the credential (already signed in over a usable identity); whoami --format json prints the kiro-cli whoami document (accountType, startUrl; never a token or e-mail), exit 1 when signed out; logout empties the vault; route prints kas-bundle or kiro-cli, and reports an unreadable config as an error (exit 1), never as kiro-cli.

Every remote script in cloud/login.py splices in cloud/login_vault.py's prelude, which runs the box's own kirocrew kas route. Only the exact answer kas-bundle takes the vault branch; any other answer runs the kiro-cli commands the scripts always ran. A box with neither program (no kiro-cli, probe not kas-bundle) gets a plain-language refusal with the probe's own error and the remedy, not kiro-cli: command not found. Both branches share one log, capture loop and parsers; a failed vault sign-in never degrades into kiro-cli's callback flow; cancel and replace stop both patterns; the vault logout kills no engine process (the pre-turn identity-change check retires them).

An OIDC sign-in records its start URL on the token and every refresh keeps it, so an Identity Center vault identity matches its organisation for the life of the sign-in. whoami/login treat a refresh-rejected slot as signed out, as doctor and the card do. Because kas logout is a second process over the gateway's vault, the store carries a sign-out generation under a store-wide flock: every sign-out bumps it, every login compares it before it saves (and its account-switch deletions) under the same lock; the poll handler answers that refusal as the begin's 409. discover_local_identity reads the launching machine's vault on the bundle route. The accountType mapping in auth/whoami.py duplicates #23615's; collapsing them is #23856. The bundle-route predicate's single spelling across the codebase is #23858.

The container supervisor decides after write_backend_config: on the bundle route it skips the kiro-cli store seed (the gateway's readiness gate names a missing engine per turn); elsewhere the seed runs as before.

Wiring and wording: cli_kas.py mounts the verbs; cli_parser_gateway.py lists them; cli_cloud.py, cli_parser_admin.py, cloud/wizard.py say "Kiro" instead of "kiro-cli"; dashboard/handlers/kas_login.py maps the poll's sign-out code to 409.

Backwards compatibility

Compatible: the kiro-cli route's remote commands are unchanged inside the else branch, a box without the new verb answers empty and takes that branch, kirocrew kas keeps install|status|remove, and KasToken.extra already round-trips unknown keys.

Removes nothing: every path, key and function on main stays; the container's kiro-cli seed still runs on every config that does not turn the bundle route on.

Tests

  • test/test_cli_kas_login.py: the parser; route spells the constant the remote scripts compare against; whoami exit codes and a JSON that parses through parse_whoami_output; logout empties every slot; login prints a prompt parse_login_output reads, forwards IdC start URL and region, answers already signed in over a usable identity, names a lapsed slot as the one it replaces, and reports expiry / store failure with exit 1. The login service is faked at its class seam.
  • test/test_cloud_login_vault.py: every remote script asks the box for its route, the vault verb is the if branch and the kiro-cli verb the else, the vault launch is one shape per target with shell-quoted flags, the callback script refuses on the vault route, cancel/replace stop both patterns; the local side reads a vault prompt like a kiro-cli one, never falls back to the callback flow on the vault route, and reads a vault whoami document as the remote identity; discover_local_identity on the bundle route. Then the generated scripts run under bash against stub kirocrew/kiro-cli programs, one isolated HOME per route (kas-bundle, kiro-cli, empty), asserting which program the box ran and the shared sentinels; a box with no kiro-cli whose kas route fails produces the readable refusal, and the local side reads it as unknown. kas route on an unreadable config prints the error and exits 1.
  • test/test_kas_login_signout_generation.py: two services over one store (the gateway and a shell): a sign-out in one refuses the other's pending login at persist time with signed_out_during_login and leaves the vault empty; a login with no sign-out in between persists; every sign-out moves the generation; a refresh keeps the recorded start URL; a refresh-rejected slot reads as signed out.
  • test/test_kas_auth_whoami.py: the accountType spelling per slot, the document's parse and target match for Builder ID and Identity Center, no e-mail and no secret in it, resolve_signed_in is the usable predicate, vault_owns_sign_in is positive on both halves, and the service records the start URL on Builder ID and IdC sign-ins.
  • container_tests/test_supervisor_kiro_login.py and test_supervisor_main.py: kas_bundle_route reads the written config and is positive on both halves (unreadable or absent config = relay route); run() seeds kiro-cli's store after the config is written and before the backend, and skips the seed on the bundle route while the backend still starts.

Determinism and side effects: no sleeps, no clocks beyond fixed dates, no ports; the bash tests run foreground scripts only with a controlled HOME/PATH whose first entry shadows kirocrew, kiro-cli, pkill and pgrep with logging stubs, so no real process or store is reached and no background login is started.

Manual verification

  • The generated scripts for check / whoami / device login (Builder ID and IdC) / callback / logout / cancel pass bash -n and were driven end to end against stub kirocrew and kiro-cli programs in three isolated HOMEs: with kas route answering kas-bundle the box ran kirocrew kas whoami, kirocrew kas login [--provider idc --start-url … --region …], kirocrew kas logout and refused the callback flow; answering kiro-cli or nothing, it ran kiro-cli whoami, kiro-cli login --use-device-flow […], kiro-cli login (callback) and kiro-cli logout, with the same sentinels on the local side.
  • kirocrew kas route printed kiro-cli on a default config and kas-bundle with {"agent": {"acp_backend": "kas", "kas_bundle": true}}; kas whoami --format json exited 1 signed out; kas logout was a clean no-op signed out.
  • Live issuer, no credentials: kirocrew kas login --provider builder_id in an isolated KIROCREW_HOME completed RegisterClient and StartDeviceAuthorization against oidc.us-east-1.amazonaws.com and printed the verification URL and user code, then was killed after 20 s with nobody approving. The approval half (CreateToken) needs a human's own Builder ID and was not run; kas-auth.md § Validation status says so.
  • Not run: a real EC2 instance on the bundle route over SSM (needs an instance and a human approval), and a bundle-route crew container (the image ships no engine).

Screenshots / video

N/A — CLI and remote-script change, no dashboard surface.

Related Issues

Part of #21843
Refs #21843 #15373 #23856 #23858

Checklist

  • At most two commits (one is the norm), with a Conventional Commits title (feat|fix|docs|style|refactor|perf|test|chore|ci|build|revert: ...)
  • Existing tests pass and new tests added for new functionality
  • Self-review completed; code follows project style guidelines
  • Documentation updated (if applicable)
  • No secrets, credentials, or internal references in the diff