Skip to content

re: refresh credentials when a proxy answers HTTP 401 - #3

Open
jun-mercury wants to merge 1 commit into
arianvp:claude/buck2-credential-helpers-tulz2jfrom
mercury:labpriv-44/arian-pr1-http-401
Open

jun-mercury wants to merge 1 commit into
arianvp:claude/buck2-credential-helpers-tulz2jfrom
mercury:labpriv-44/arian-pr1-http-401

Conversation

@jun-mercury

Copy link
Copy Markdown

With this change, a credential helper's refresh also runs when an HTTP proxy in front of the remote refuses the token, not only when the gRPC server returns UNAUTHENTICATED. rpc_result now treats UNAUTHENTICATED, and INTERNAL or UNKNOWN whose message names HTTP status 401, as rejected credentials, so your existing invalidate-and-retry path runs.

Why

Namespace's ingress answers a bearer it doesn't accept with HTTP 401 and a plain-text body. tonic 0.14.6 decodes that body before it looks at the HTTP status, and returns INTERNAL with the status only in the message (codec/decode.rs). Its own mapping of 401 to UNAUTHENTICATED only runs at the end of a stream that decoded. We ported #1 onto MercuryTechnologies/buck2's bes-stack (mercury#51). There, a remote build on Namespace failed two minutes in, when the helper handed out a token the ingress refused:

Failed to make BatchReadBlobs request: protocol error: received message with invalid compression flag: 105 (valid flags are 0 and 1) while receiving response with status: 401 Unauthorized

The helper was never called again, and every retry carried the same cached token. The error message is matched as text because nothing structured carries the 401.

Proven and not

  • Proven on this branch:
    • Unit tests. A tower layer in front of a real tonic ActionCache server answers a refused bearer with HTTP 401 and a plain-text body.
      • With a helper configured, get_action_result sends the first token, gets the 401, runs the helper again and succeeds with the second token.
      • Without a helper, the call fails after one attempt.
    • With the new check reverted to the UNAUTHENTICATED-only guard, the helper test fails.
    • cargo test -p remote_execution passes (33 tests).
  • Proven on the bes-stack port only: the same check fixed the live Namespace run that had failed, and the 10-minute build succeeded after the refused token.
  • Not proven:
    • A live run of this branch.
    • Real token expiry. So far a corrupted token has stood in for an expired one.
    • Streams: the ByteStream and Execute/WaitExecution paths don't go through rpc_result, so a refusal on a stream still doesn't refresh.

Assisted-by: Claude:claude-opus-5-5

A credential helper's refresh never ran when Namespace refused a token. Its
ingress answers a bearer it does not accept with HTTP 401 and a plain-text
body, not with gRPC UNAUTHENTICATED. tonic 0.14.6 decodes that body before it
looks at the HTTP status and returns INTERNAL, with the status only in the
message (codec/decode.rs); its own 401-to-UNAUTHENTICATED mapping runs only at
the end of a stream that decoded. A remote build on Namespace failed two
minutes in on exactly this, on the bes-stack port of this change:
"Failed to make BatchReadBlobs request: protocol error: received message with
invalid compression flag: 105 (valid flags are 0 and 1) while receiving
response with status: 401 Unauthorized".

rpc_result now treats UNAUTHENTICATED, and INTERNAL or UNKNOWN whose message
names status 401, as rejected credentials, so the existing invalidate and
retry path runs. The message is matched because nothing structured carries
the 401.

Assisted-by: Claude:claude-opus-5-5
@jun-mercury

Copy link
Copy Markdown
Author

How we tested this, since the unit tests alone don't show it holds up against a real server.

The live runs used the same change on our bes-stack port (mercury#51), not this branch. The logic is the same status_rejects_credentials check, but this branch itself has only had the unit tests below.

Setup. Each run was a single buck2 build --remote-only --no-remote-cache of a chain of ten genrules that each sleep 60 s, so one build keeps making remote calls for about 10 minutes. Each run had its own daemon and HOME. credential_helper pointed at a small script that speaks the Bazel helper protocol, logs every call, and chooses the token and the expires it reports.

Runs on Namespace remote execution:

  1. Renewal before expiry. The helper reported an expiry 3 minutes out. buck2 called it again as each expiry passed, and the build finished green.
  2. Server rejection, simulated. At the first renewal the helper handed out a corrupted token and claimed it lasted an hour, so only the server's refusal could make buck2 ask again. Without the fix the build failed on the 401 quoted in Add credential helper support to the OSS remote execution client #1. With it, buck2 called the helper again within the second and the build finished green.
  3. Real expiry. We minted four revocable tokens with nsc bazel create-token --expires_in 4m/8m/12m/16m.
    • A control with the 4-minute token fixed in http_headers failed at its expiry with 401 Unauthorized, which shows Namespace enforces it.
    • A helper that served the earliest unexpired token finished green across three real expiries.
    • The same helper claiming each token lasted an hour also finished green, so the 401 refresh path works when a token really dies.

On this branch: cargo test -p remote_execution runs three new tests against a real tonic server, with a tower layer that answers a refused bearer with HTTP 401 and a plain-text body. With the helper, the call succeeds on the second token. Put the check back to UNAUTHENTICATED only and that test fails.

Not covered: a rejection in the middle of a ByteStream or Execute/WaitExecution stream. Those retry loops don't refresh, here or on our port.

Posted by Claude Code on behalf of Jun Matsushita.

@arianvp

arianvp commented Oct 9, 2026

Copy link
Copy Markdown
Owner

Is this not more a thing that should be fixed by Namespace? gRPC endpoint should serve gRPC; not 401s with non-gRPC content

jun-mercury added a commit to mercury/buck2 that referenced this pull request Nov 19, 2026
An ingress in front of the remote can answer a bearer it refuses with HTTP
401 and a plain-text body rather than gRPC UNAUTHENTICATED (LABPRIV-44, run
E, 2026-09-26). tonic 0.14 decodes that body before it reads the status and
returns INTERNAL with the 401 only in its message (codec/decode.rs), so the
refusal read as a retryable INTERNAL and every retry carried the same cached
token. `buck2_credential_helper::status_rejects_credentials` treats
UNAUTHENTICATED, and INTERNAL or UNKNOWN naming status 401, as refused
credentials; the RE client refreshes once per call on either, ahead of the
reconnect test, and no longer retries a refusal it could not refresh.

Ported from the lab line's 64289ce (cherry-picked from f17fb32,
arianvp#3) onto the previous commit's port to the re_grpc stack.
Execute and WaitExecution streams and ByteStream reads and writes keep their
own retry loops, as on the lab line; a unary call's refresh clears the
helper's shared cache they read from. The BES sink half is left out with the
previous commit's.

Change-Id: I243c4a2afe3da31a8d77813c53ed5b0b8b250f19
Assisted-by: Claude:claude-opus-5-5
arianvp pushed a commit that referenced this pull request Nov 24, 2026
…bounds

Summary:
# context
Follow-up to the diff below (tracked gap): composable function types in type-parameter bounds still leak `ComposableFunctionN`. No prod instance — 0 changed jars in the fleet sample — but a fail-closed violation in the same family.

# problem
The real plugin's `ComposableTypeTransformer.visitTypeParameter` remaps every module type parameter's supertypes. We didn't, so bounds kept `ComposableFunctionN` in the erasure (`host(L...ComposableFunction0;)I`) and in class and method Signatures.

# fix
- Emulation: `ComposableTypeRewriter.visitTypeParameter` remaps bounds.
- Native mirror, same diff: bounds counted and rewritten (`compose_types.rs`), function-type bound nullability (`type_parameters.rs`); `compose_type_bound.kt` moves from `refusals.d` to exact; 2 new probes.

# fleet parity
Full-universe Compose check (4,745 targets): stability rows 3,977 -> 1,048, applier 8,553 -> 8,533, 0 new rows, 0 unclassified; library and class ABI byte-identical 4,735/4,735 at every position (final re-gate, harness D133166871 V0.3 + D133182189 V0.2, independently reviewed: P1 FAIL unchanged; P2-P6, P8 and P10 PASS; P7 PASS; P9 FAIL declared intended omission (owner decision pending)). The table covers the stack through P6; the 7th adds the P7 run (73 bridge rows removed, 48 constructor-body-only changes under manifest; P7 PASS, library instruction-for-instruction match) and the 8th is fleet byte-identical (V0.4 PASS, 0 changed; direct byte comparison stands). Cumulative gate: FAIL, exit 1 -- 1 changed row (photorestyle `PlaybackStep`: test 1 -> 3 against backend 2, adding the matching known-stable bit while retaining an extra parameter-dependency bit (conservative direction: extra work, not an over-assertion); pre-existing #3 shape) and 1 inconclusive target (`composed`, unverifiable broken base). Reports: https://artifacts.internalmeta.com/a/4510517255827441 https://artifacts.internalmeta.com/a/2326985064801664 https://artifacts.internalmeta.com/a/1137597922586590 https://artifacts.internalmeta.com/a/2475838362823582 https://artifacts.internalmeta.com/a/2024183578261982

Reviewed By: jiawei-lyu

Differential Revision: D133126208

fbshipit-source-id: 05bb01f2a36377a2914ee20ed6de235624870ba0
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.

2 participants