Skip to content

feat: validate tokens and repository access up front#341

Merged
GuillaumeLagrange merged 5 commits into
mainfrom
cod-2628-cli-improve-token-validation-across-commands
May 12, 2026
Merged

feat: validate tokens and repository access up front#341
GuillaumeLagrange merged 5 commits into
mainfrom
cod-2628-cli-improve-token-validation-across-commands

Conversation

@GuillaumeLagrange
Copy link
Copy Markdown
Contributor

@GuillaumeLagrange GuillaumeLagrange commented May 7, 2026

Catch invalid or mis-scoped tokens before any benchmarks run, instead of
surfacing a 401 from the upload endpoint after a full suite has executed.

The local provider now validates the token and the repository's
CREATE_LOCAL_RUN access in a single GraphQL round-trip when resolving
from a -r override or a detected git remote. The project-repository
fallback is unchanged. auth status uses the same combined query so it
doesn't double-roundtrip when a remote is detected, and auth login
validates the token via session() before persisting it.

The bulk of the diff is a prerequisite refactor: the api_client is now
the single source of truth for the auth token. Previously the token
lived in two places — on the api_client for GraphQL Authorization
headers, and on OrchestratorConfig/ExecutorConfig for upload
Authorization headers — and the two could drift, notably in CI with
OIDC where set_oidc_token mutated config but the api_client kept its
original token. CodSpeedAPIClient now owns the token through token()
and set_token(), the trait method becomes
refresh_token(&mut CodSpeedAPIClient), and &mut api_client is
plumbed through cli/run/exec → Orchestrator → upload_all. The token
field is deleted from both configs.

Reviewers: the two commits are independently reviewable. The first
(ref: make api_client the single source of truth…) is a pure
refactor with no behavior change. The second (feat: validate tokens…) is the user-facing change.

Refs COD-2628

@codspeed-hq
Copy link
Copy Markdown

codspeed-hq Bot commented May 7, 2026

Merging this PR will not alter performance

⚠️ Unknown Walltime execution environment detected

Using the Walltime instrument on standard Hosted Runners will lead to inconsistent data.

For the most accurate results, we recommend using CodSpeed Macro Runners: bare-metal machines fine-tuned for performance measurement consistency.

✅ 4 untouched benchmarks
🆕 3 new benchmarks

Performance Changes

Mode Benchmark BASE HEAD Efficiency
🆕 Memory sleep 1 N/A 15.6 KB N/A
🆕 WallTime sleep 1 N/A 1 s N/A
🆕 Simulation sleep 1 N/A 126.3 µs N/A

Comparing cod-2628-cli-improve-token-validation-across-commands (58520c3) with main (fd8701d)

Open in CodSpeed

@GuillaumeLagrange GuillaumeLagrange force-pushed the cod-2628-cli-improve-token-validation-across-commands branch 2 times, most recently from 9d888b4 to 04f58f9 Compare May 7, 2026 14:04
@GuillaumeLagrange GuillaumeLagrange changed the title Improve token validation across commands feat: validate tokens and repository access up front May 7, 2026
Copy link
Copy Markdown
Member

@adriencaccia adriencaccia left a comment

Choose a reason for hiding this comment

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

I did not finish the review yet. Last two commits still left

Comment thread Cargo.toml Outdated
Comment thread src/run_environment/github_actions/provider.rs Outdated
Comment thread src/executor/orchestrator.rs Outdated
Comment thread src/cli/auth.rs Outdated
Comment thread src/run_environment/local/provider.rs
Comment thread src/run_environment/local/provider.rs
@GuillaumeLagrange GuillaumeLagrange force-pushed the cod-2628-cli-improve-token-validation-across-commands branch 2 times, most recently from 5c67790 to 38a123f Compare May 12, 2026 16:40
Comment thread Cargo.toml Outdated
Comment thread src/run_environment/local/provider.rs Outdated
Comment thread src/run_environment/local/provider.rs Outdated
Comment thread src/run_environment/local/provider.rs Outdated
GuillaumeLagrange and others added 2 commits May 12, 2026 10:15
Pin the dependency to a revision that surfaces partial `data` on
errored responses via `GraphQLError::data()`. Lets callers consume
populated sibling fields when a per-field error propagates up to a
nullable ancestor — used by the upcoming up-front token validation
path to read the session even when `repositoryOverview` errors with
REPOSITORY_NOT_FOUND.

Refs COD-2628
Co-Authored-By: Claude <noreply@anthropic.com>
Token state used to live in two places: the api_client carried it for
GraphQL Authorization headers, while OrchestratorConfig.token (and its
clone in ExecutorConfig.token) carried it for upload Authorization
headers. The two were filled from different paths and could drift —
notably in CI with OIDC, where set_oidc_token mutated config but the
api_client kept its original token.

Centralize on the api_client:
- CodSpeedAPIClient now exposes token() and set_token() and is the
  single mutation point for the credentials.
- RunEnvironmentProvider::set_oidc_token(&mut ExecutorConfig) becomes
  refresh_token(&mut CodSpeedAPIClient). GHA implements it; other
  providers inherit the no-op default.
- The Buildkite "token required" check moves from try_from(config) to
  check_oidc_configuration(api_client) — token presence is the
  api_client's authority now.
- Token resolution happens once at CLI entry in build_api_client(&cli):
  --token / CODSPEED_TOKEN takes precedence; the persisted CLI config is
  only loaded as fallback.
- token field deleted from OrchestratorConfig and ExecutorConfig. The
  uploader's Authorization header and the tokenless metadata flag both
  read from api_client.token().
- &mut CodSpeedAPIClient is plumbed through cli/run/exec → Orchestrator
  → upload_all so refresh_token can mutate in place.

Refs COD-2628
Co-Authored-By: Claude <noreply@anthropic.com>
@GuillaumeLagrange GuillaumeLagrange force-pushed the cod-2628-cli-improve-token-validation-across-commands branch 2 times, most recently from 3a9ed50 to 8ede8b0 Compare May 12, 2026 17:23
GuillaumeLagrange and others added 3 commits May 12, 2026 10:25
Previously, an invalid or mis-scoped token only surfaced as a 401 from
the upload endpoint after the full benchmark suite had run. The local
provider also had to make two separate GraphQL calls — one to verify
the token and one to look up the repository — without a clean error
shape for the "missing repo / valid token" case.

Combine session validation and repository lookup into a single
`session_and_repository_overview` query, and use it for the local
provider's resolution path:

- `LocalProvider` now validates the token and the repository's
  `CREATE_LOCAL_RUN` access in one round-trip when resolving from a
  `-r` override or from a detected git remote. The project-repository
  fallback is unchanged and still relies on
  `get_or_create_project_repository` to surface auth errors.
- `auth status` now renders the detected git remote alongside the
  authentication state, using the same combined query so we don't
  double-roundtrip when a remote is detected.
- `auth login` validates the token via `session()` before persisting,
  so a malformed or expired token is rejected up front instead of
  being written to disk.
- `REPOSITORY_NOT_FOUND` is folded into the success path
  (`repository_overview: None`) by deserializing the partial-data
  payload — relies on the gql_client partial-data fork.
- `CurrentUser.gql` and `GetRepository.gql` are deleted; replaced by
  `Session.gql` and `SessionAndRepositoryOverview.gql`.

Refs COD-2628
Co-Authored-By: Claude <noreply@anthropic.com>
@GuillaumeLagrange GuillaumeLagrange force-pushed the cod-2628-cli-improve-token-validation-across-commands branch from 8ede8b0 to 58520c3 Compare May 12, 2026 17:25
@GuillaumeLagrange GuillaumeLagrange merged commit 58520c3 into main May 12, 2026
21 checks passed
@GuillaumeLagrange GuillaumeLagrange deleted the cod-2628-cli-improve-token-validation-across-commands branch May 12, 2026 17:41
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