Skip to content

feat: per-user inference credential headers and sandbox research tooling (#368 phase 1) - #372

Merged
pdettori merged 8 commits into
rossoctl:mainfrom
pdettori:feat/368-inference-creds-sandbox-tools
Sep 30, 2026
Merged

pdettori merged 8 commits into
rossoctl:mainfrom
pdettori:feat/368-inference-creds-sandbox-tools

Conversation

@pdettori

Copy link
Copy Markdown
Member

Phase 1 of #368 (VM demo C, epic #370): the parts that don't depend on #366. Closes #362 item 3.

Summary

A raw Anthropic API key now works as a per-user inference credential.

  • applyModelGateway always sent a caller's credential as Authorization: Bearer and stripped x-api-key. api.anthropic.com reads API keys only from x-api-key, so a sk-ant-api… key got an opaque 401.
  • This was confirmed with a wire-level test driving pi-ai's real client, and with a fake-key probe (401 invalid x-api-key on the Bearer path).
  • The credential's existing binding now chooses the header:
    • Control plane: for consumer inference, inferenceAuthHeader(binding, field) accepts exactly Authorization: Bearer {f} and x-api-key: {f}.
    • Exchange: returns authHeader: 'x-api-key' in direct mode only. Absent means Bearer, the pre-VM demo C: per-user inference credentials and a curl/git research turn in the P6 container sandbox #368 wire shape.
    • knative-server: turn-auth threads it into UpstreamCredential.header and refuses unknown values.
    • Harness: applyModelGateway sends it. The caller's key lives only in per-model headers, never in process.env (MI1 §5 R2).
  • In placeholder mode the harness keeps sending Bearer <placeholder>, and the injector picks the upstream header.

Shapes that would 401 are refused with a message naming the fix, never mid-turn.

  • At PUT /v1/credentials, the control plane refuses:
    • a binding that is neither of the two headers;
    • sk-ant-api… on Bearer;
    • sk-ant-oat… (pi-ai adds the OAuth betas only when the token is its own apiKey);
    • Bearer to an api.anthropic.com endpoint;
    • api.anthropic.com with a path (/v1 would become /v1/v1/messages).
  • At exchange (direct mode), credential_required is returned for:
    • a stored binding that can't be sent;
    • Bearer whose resolved endpoint (including SH_DEFAULT_INFERENCE_ENDPOINT) is api.anthropic.com;
    • a raw Anthropic key whose resolved endpoint is anywhere else. That would be a misdirected secret.
  • mocactl mirrors the PUT checks before submitting. The credential step names the two working shapes: a gateway token as kind bearer, or an Anthropic API key as kind api-key with endpoint https://api.anthropic.com.
  • The compose smoke stores a raw ANTHROPIC_API_KEY as kind api-key.

The sandbox image guarantees its research tooling.

  • curl-minimal and ca-certificates were inherited from ubi-minimal and are now installed explicitly, in both remote-worker/Dockerfile and Dockerfile.runtime. No size change: 258.34 MB vs 258.42 MB.
  • HOME=/home/sandbox, created like /workspace. Before this, HOME was whatever the runtime picked for a uid with no passwd entry: podman used the WORKDIR, so dotfiles landed in the shared /workspace; Docker and containerd use /, where git config --global fails.
  • A static Go test pins both Dockerfiles.

Behaviour changes to note

  • An existing inference credential stored as kind api-key (default binding X-API-Key) now goes as x-api-key in direct mode, where it used to go as Bearer. LiteLLM accepts either. A gateway that reads only Bearer needs the credential re-stored as kind bearer.
  • Bearer credentials, oauth2-token credentials and control planes that omit authHeader are unchanged.

How MI1 S2 absorbs this

  • inferenceAuthHeader and the write-time refusals describe the credential, not the delivery mode, so they stay.
  • The grant carries the binding to moca-egress, which injects the real secret under that header.
  • The harness's direct-mode x-api-key branch is deleted along with direct mode.

Deferred to phase 2 (after #366 merges)

Known minors, not fixed here:

  • The x-api-key path to api.anthropic.com still disables the prompt-caching compat flags.
  • A deployment default of https://api.anthropic.com/v1 isn't path-checked.
  • The operator-fallback refusal message is user-facing rather than operator-facing.

Test plan

  • harness/test/raw-anthropic-key-wire.test.ts: pi-ai's client against a local server. It sees x-api-key and no Authorization for an x-api-key credential, and Bearer for a gateway token.
  • New unit tests:
    • packages/control-plane/test/inference-shape.test.ts
    • packages/control-plane/test/exchange-auth-header.test.ts
    • packages/knative-server/test/turn-auth-header.test.ts
    • mocactl credential-form.test.ts
    • remote-worker/cmd/worker/research_tooling_test.go, mutation-checked
  • make lint, make typecheck and make test are green.
  • podman build of remote-worker/Dockerfile, then in the running image:
    • HOME=/home/sandbox, even with --workdir /tmp;
    • git config --global works as uid 1001 and as an arbitrary gid-0 uid;
    • curl https://example.com returns 200, and git clone works.
  • Live turn with a real Anthropic API key (phase 2, on the VM).

🤖 Generated with Claude Code

@mrsabath mrsabath left a comment

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Four-tier credential-header change, and the central factual claim checks out: api-key's default binding is X-API-Key: {key} (credential-store.ts:95), which inferenceAuthHeader maps to x-api-key, and the exchange's authHeader is threaded to turn-auth -> UpstreamCredential.header -> applyModelGateway, with each tier refusing unknown values rather than defaulting to one.

The "PUT cannot see the deployment default" justification for re-checking the endpoint at exchange time is real, not defensive duplication: baseUrl = endpoint ?? deps.config.defaultInferenceEndpoint (exchange.ts:119) resolves after PUT has already run, so the two checks genuinely see different values.

Cross-tier consistency verified in both directions. mocactl's KNOWN_KINDS field names match the server's KINDS registry (api-key -> key on both sides), so the mirrored form checks cannot drift from the authoritative ones on field naming. Secret hygiene is consistent too: every refusal path has a test asserting the value is not echoed, and MI1 R2 (the caller's key never entering process.env) gets a direct assertion rather than an implied one.

Two non-blocking notes inline. The operator-fallback one is a message-attribution problem, not a behavioural one.

On the failing proto check

Pre-existing on main, not caused by this PR. Identical job and identical step 10 ("Build and test remote-worker") on the same base commit 73f1bf41 that #371 sits on, where it is already failing on a push to main.

Worth one caveat I would not apply to #371: this PR does change remote-worker/ (both Dockerfiles, plus a new Go test), so it is not a priori unrelated to that job the way a TypeScript-only PR would be. The failure predates the branch, so it is not caused by it — but once main is green again, this is the PR worth re-checking rather than assuming clean.

Areas reviewed: TypeScript (harness, control-plane, knative-server, mocactl), Shell, Dockerfile, Go, CI
Commits: 7, all signed off, DCO passing
CI status: 12 pass, 1 fail (inherited from main)

One process note, not a review finding: the test plan's "live turn with a real Anthropic API key" is unchecked and deferred to phase 2, so the end-to-end path this PR exists to enable is verified against a local wire-level stand-in rather than api.anthropic.com. Stated openly in the description and reasonable for phase 1 — flagging only so it is not lost between phases.

Comment thread packages/control-plane/src/exchange.ts Outdated
rec.sessionId,
);
}
if (secretValue.startsWith('sk-ant-api') && host !== 'api.anthropic.com') {

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This block sits at function scope, after the stored and operator-fallback branches merge, so secretValue and credentialName can both come from the fallback.

An operator who sets operatorInferenceToken to a raw sk-ant-api... key -- and the config comment calls it "the operator's own key" (handlers.ts:23) -- together with a gateway defaultInferenceEndpoint gets this handed to the end user:

credential 'operator-fallback' is an Anthropic API key, but its endpoint resolves to litellm.internal: set its endpoint to https://api.anthropic.com

The refusal is correct -- that key would 401 at the gateway, and refusing beats misdirecting the secret. But it is delivered to a user who does not own operator-fallback and cannot re-store it, and "set its endpoint" is unactionable for them.

The description lists "the operator-fallback refusal message is user-facing rather than operator-facing" as a known minor, but under the Bearer check above; this new check has the same shape. Worth either scoping both to if (stored) or branching the message on usedOperatorFallback so the operator-side case names the env var instead.

Note that exchange-auth-header.test.ts's "keeps the operator fallback on Bearer" case uses op-gw-token, which trips neither check -- so the suite does not currently cover this path.

Suggestion, not blocking.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Fixed in fa6e555. Good catch: both direct-mode checks see the fallback's token, not only the new one.

When usedOperatorFallback is the source, the exchange now:

  • refuses with credential_unavailable (a deployment fault) instead of credential_required;
  • uses a message that names SH_OPERATOR_INFERENCE_TOKEN / SH_DEFAULT_INFERENCE_ENDPOINT and tells the caller the operator has to fix it, or that they can store their own credential.

One consequence I hit while doing this: turn-auth's PASSTHROUGH admits caller-attributable codes only, so the data plane shows the caller a generic control plane returned 503. That's the right attribution, but it means the operator-facing text reached nobody. So the refusal also logs [control-plane] operator fallback misconfigured: …. The log line names the settings, never the token.

There are two new tests, one for each mismatch: a raw sk-ant-api operator key on a gateway default, and a Bearer operator token on an api.anthropic.com default. Each asserts the code, the message, the log, and that no token is echoed. That covers the path the op-gw-token case missed.

Comment thread harness/src/run-turn.ts
Authorization: `Bearer ${authToken}`,
'x-api-key': null, // strip x-api-key when using gateway Bearer auth
} as unknown as Record<string, string>,
headers: (authHeader === 'x-api-key'

Copy link
Copy Markdown
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

authHeader is computed unconditionally above, but this header block is gated on authToken being truthy. For { mode: 'direct', value: '', header: 'x-api-key' } with no deployment token, authToken is falsy and no auth header is set at all -- the request goes upstream unauthenticated rather than failing loudly.

Unreachable today: the exchange refuses before it can return an empty secretValue (exchange.ts, the if (!secretValue) throw). The new model-gateway.test.ts case covering an empty value with a deployment token exercises the adjacent path, which is the one that actually matters.

Raising it only because the guarantee lives one tier away, in a function that otherwise validates its own inputs.

Nit.

Copy link
Copy Markdown
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Leaving this one as is, deliberately.

An empty caller value falling through the chain predates this PR. It's pinned by model-gateway.test.ts's "treats an empty value as 'not set', like every other term in the chain" case, so that "" means unset uniformly across config, upstream credential and env.

What this PR adds is that the fall-through doesn't carry the header along. upstream is undefined for an empty value, so authHeader is 'authorization', and the new "never applies a header choice to the deployment token" case pins that.

As you say, the no-token-at-all request is unreachable from the exchange, which throws on an empty secretValue. Making applyModelGateway fail loud on an empty caller value would reverse a documented contract for every caller, not just this path. I'd rather do that as its own change if we want it.

An inference credential goes upstream as Authorization: Bearer or as
x-api-key, per its binding. Refuse at PUT any other binding, a raw
Anthropic key on Bearer, an Anthropic OAuth token, Bearer to
api.anthropic.com, and an api.anthropic.com endpoint with a path.

Refs rossoctl#368, rossoctl#362

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
applyModelGateway sent every per-caller credential as Authorization:
Bearer and stripped x-api-key, so a raw Anthropic API key 401'd at
api.anthropic.com. UpstreamCredential now names its header; absent
still means Bearer. A wire-level test drives pi-ai's client against a
local server. The caller's key stays out of the environment (MI1 R2).

Refs rossoctl#368, rossoctl#362

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…ta plane

The exchange names the header a credential travels in (authHeader,
present only for x-api-key), and turn-auth threads it into the
upstream credential. The exchange refuses with credential_required:
- a stored binding the inference path cannot send;
- a Bearer credential whose resolved endpoint is api.anthropic.com
  (PUT cannot see the deployment default);
- an x-api-key credential behind a Bearer-only injector.
The compose smoke stores a raw Anthropic key as kind api-key.

Refs rossoctl#368

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
… rest

The credential step names the two shapes that reach a model: a gateway
token as kind bearer, or an Anthropic API key as kind api-key to
https://api.anthropic.com. The form refuses, before submitting, the
shapes the control plane refuses at PUT.

Refs rossoctl#368

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…he sandbox

curl-minimal and ca-certificates were inherited from ubi-minimal, never
installed; they are now explicit (no size change). HOME was whatever
the runtime chose for a uid with no passwd entry: podman's WORKDIR,
Docker's /. It is now /home/sandbox, created like /workspace, so
git config --global works under any runtime. A static test pins both
Dockerfiles.

Refs rossoctl#368

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…ed raw keys

Final-review fixes for rossoctl#368:
- In placeholder mode the exchange never returns authHeader: the
  harness sends Bearer <placeholder> as before, and the injector picks
  the upstream header (AB1 inject_header). This replaces the refusal of
  x-api-key credentials there, which broke api-key credentials that
  worked before rossoctl#368.
- In direct mode, a raw Anthropic key whose endpoint resolves anywhere
  but api.anthropic.com (e.g. an endpoint-less key on a gateway
  default) is refused with credential_required, instead of being sent
  to the gateway and 401ing.
- UpstreamCredential's doc comment is back above the type, and no
  longer claims every pre-rossoctl#368 credential is Bearer.

Refs rossoctl#368

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
…rator

Both direct-mode endpoint checks in the exchange also see the operator
fallback's token. The refusal was right, but it told the caller to
re-store 'operator-fallback', which they cannot do. When the fallback
is the source, the exchange now:
- refuses with credential_unavailable (a deployment fault) rather than
  credential_required;
- names SH_OPERATOR_INFERENCE_TOKEN and SH_DEFAULT_INFERENCE_ENDPOINT;
- logs the reason on the control plane. The data plane shows the caller
  only a generic 503, so the log is how the operator finds out.
Neither the message nor the log carries the token.

Addresses PR rossoctl#372 review.

Refs rossoctl#368

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
@pdettori
pdettori force-pushed the feat/368-inference-creds-sandbox-tools branch from 062f027 to fa6e555 Compare September 30, 2026 18:34
@pdettori
pdettori merged commit 24c6f5e into rossoctl:main Sep 30, 2026
13 checks passed
@pdettori
pdettori deleted the feat/368-inference-creds-sandbox-tools branch September 30, 2026 18:40
pdettori added a commit to pdettori/moca that referenced this pull request Oct 1, 2026
Performed 2026-10-01 on the shared KVM rig at main @ 03d2ee8: every act
held. One person played both users with two GitHub accounts on one
laptop; two people on two laptops is not yet tried.

The run's outputs replace the illustrative ones, and Act 5 is filled
in. Step fixes it found: pull the sandbox image in 0a (setup-vm.sh
never pulls; the rig's predated rossoctl#372), approve user 2's device code in
a private window, list session ids in 3a (the TUI titles both users'
sessions by the same prompt), point 4c at the hostname and kernel
suffix (host and guest are both 6.18.44), and the records' order in 0d.
Fix list items 4-9 are new.

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
pdettori added a commit that referenced this pull request Oct 8, 2026
…#415)

An agent's first `git commit` in a sandbox failed with exit 128 ("Please
tell me who you are") until it configured an identity itself -- observed
live on the VM demo (docs/demos/vm-multi-user-demo.md, fix list 5), on both
P4 sessions and on the container tier. Each workaround cost a tool call and
a model round, in front of an audience.

Fix both leaf images the way the issue proposes: a system gitconfig
(/etc/gitconfig, user.name "MOCA sandbox" / user.email
[email protected]), which applies to ANY uid the images can run as --
including the arbitrary gid-0 uid under nonroot-v2 that #372's writable
HOME prepares but cannot pre-configure -- and which a user's or agent's own
higher-precedence config still overrides. The P4 rootfs inherits it for
free, being exported from this image by build-rootfs.sh.

Guards, per the issue:

- research_tooling_test.go: a static pin of the exact RUN instruction (and
  its position before the USER drop), plus a runtime check that extracts
  the printf's format string, installs it as a scratch environment's ONLY
  git config, and runs a real no-prior-config commit -- so a gitconfig the
  line still prints but git silently ignores fails CI, not the next demo.
- build-snapshot.sh: check_git_identity runs a real empty commit (with
  user.useConfigOnly=true, so an unconfigured git fails instead of
  auto-guessing) in the booted guest of both VMM arms, before quiescing --
  a rootfs built from an image that lost its gitconfig now fails the
  snapshot build, not the demo. build-snapshot.test.sh pins the gate's
  contract (existence, both arms, probe->check->quiesce order,
  useConfigOnly, real commit, #415 pointer).

Verified: go test ./... in remote-worker (including mutation tests on the
new guards: commented-out RUN, RUN-after-USER, and a typo'd section header
all fail), hadolint clean on both Dockerfiles, shellcheck -S warning clean,
build-snapshot.test.sh and build-rootfs.test.sh PASS.

Co-Authored-By: Claude Code <[email protected]>
Signed-off-by: Paolo Dettori <[email protected]>
cwiklik added a commit that referenced this pull request Oct 29, 2026
`podman run` resolves an image reference only when the host has no copy of
it, so a mutable `:latest` pulled once is never re-resolved -- every later
`setup-vm.sh` run starts the sandboxes on it. On the 2026-10-01 KVM rig that
copy predated #372: HOME was the shared /workspace rather than /home/sandbox,
and nothing in the bring-up's output hinted the containers were not what main
builds. `build-rootfs.sh` had the same bug in a worse place: `podman image
exists || podman pull` pulled only on a host with no copy, and a snapshot is
built once and run for a long time, so a stale rootfs outlived the image it
was meant to track.

Both now pull before use, fail closed on a failed pull, and take
SH_SKIP_PULL=1 as the documented override for an offline host or a locally
built image -- the convention #349 established for `deploy/compose/install.sh`.
setup-vm.sh pulls before start_sandboxes, because `podman run --replace`
recreates each container from whatever the reference resolves to at that
moment. Both also log the resolved image id and creation time rather than the
tag asked for: `:latest` is the same string on a current and a pre-#372 host,
so the id is what answers "is this host on the current image?".

Tests: setup-vm.test.sh gains the pull, the fail-closed refusal, the skip path
and an uninspectable image warning, plus a main() ordering assertion that the
pull precedes the first container; build-rootfs.test.sh asserts the image is
re-resolved every build (and that the `image exists` short-circuit is gone,
not merely reordered), that a failed pull refuses before exporting, and that
SH_SKIP_PULL builds from the local copy but refuses when there is none.

Fixes #414

Assisted-By: Claude (Anthropic AI) <[email protected]>
Signed-off-by: cwiklik <[email protected]>
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Labels

None yet

Projects

None yet

Development

Successfully merging this pull request may close these issues.

MU1 follow-ups from #348: VM control plane on Vault, dev identity provider, inference-credential binding

2 participants