From ccfd1073002ffd591decec45897ea4f976a4bc6d Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Wed, 11 Nov 2026 18:58:28 +0000 Subject: [PATCH 1/6] feat(cli)!: take only a v2 API key or a Trunk JWT (TRUNK-23149, TRUNK-23372) MIME-Version: 1.0 Content-Type: text/plain; charset=UTF-8 Content-Transfer-Encoding: 8bit analytics-cli 1.0.0 reads what `--token` / `TRUNK_API_TOKEN` holds before it calls Trunk: - `trunk_v2_...` is a v2 API key, sent as `Authorization: Bearer` for the gateway to exchange. - Three non-empty base64url segments is a Trunk JWT, sent on `x-trunk-token` (#1293's header) and never as a bearer. - Anything else is the organization token, refused before any call with how to create a v2 key (Settings → Developer → API Keys), the Upload test results permission it needs, and the 0.17.x pin. `upload` and `test` exit with an error, and `test` refuses before running the tests. The fork lane's `GITHUB_TOKEN` exchange is unchanged. On the three CLI endpoints, a 403 `INSUFFICIENT_PERMISSIONS` names the missing `upload_test_results` permission group and how to grant it, and a 401 for a JWT says it is expired or invalid (with its `exp` when past) and to re-run `trunk login --api-key` or pass an API key. The error report shows that explanation in place of the generic org-slug hint. A Trunk JWT lasts 15 minutes (auth-hydra's `ttl.access_token`), which a long `test` run or a plugin that looks up quarantine early and uploads late can outlive. The CLI does not refresh it; the 401 message and the docs say to mint it right before the upload step. `test_report` and the rspec gem apply the same rule: the gem asks the native extension (`RSpec::Trunk.token_refusal`) and stays off with a warning for an organization token. Telemetry is unchanged and still sends the token on `x-api-token`. Co-Authored-By: Claude Opus 5.5 --- .../actions/test_ruby_gem_uploads/action.yaml | 4 +- CONTRIBUTING.md | 2 + README.md | 13 + api/src/client/mod.rs | 104 ++++++-- api/src/client/v2.rs | 75 +++++- api/src/credential.rs | 139 +++++++++++ api/src/lib.rs | 1 + api/tests/v2_client.rs | 227 +++++++++++++++++- cli/src/summary_output/github.rs | 19 +- cli/src/summary_output/text.rs | 16 +- cli/src/test_command.rs | 5 +- cli/src/upload_command.rs | 25 +- cli/tests/common/command_builder.rs | 16 +- cli/tests/upload.rs | 141 ++++++++++- rspec-trunk-flaky-tests/README.md | 2 +- .../lib/trunk_spec_helper.rb | 16 +- .../test/namespace_spec.rb | 2 +- .../test/test_collection_spec.rb | 2 +- rspec-trunk-flaky-tests/test/token_spec.rb | 51 ++++ test_report/src/report.rs | 16 ++ test_report/tests/publish.rs | 10 +- test_report/tests/quarantine.rs | 70 +++++- test_report/tests/telemetry.rs | 2 +- 23 files changed, 884 insertions(+), 74 deletions(-) create mode 100644 api/src/credential.rs create mode 100644 rspec-trunk-flaky-tests/test/token_spec.rb diff --git a/.github/actions/test_ruby_gem_uploads/action.yaml b/.github/actions/test_ruby_gem_uploads/action.yaml index 74c278a2..0317a46f 100644 --- a/.github/actions/test_ruby_gem_uploads/action.yaml +++ b/.github/actions/test_ruby_gem_uploads/action.yaml @@ -255,7 +255,9 @@ runs: env: TRUNK_QUARANTINE_QUERY_FAILURE_EXIT: "true" TRUNK_ORG_URL_SLUG: fake-org - TRUNK_API_TOKEN: fake-token + # A fake v2 API key: an organization token would leave the gem off. + # trunk-ignore(checkov/CKV_SECRET_6) + TRUNK_API_TOKEN: trunk_v2_fake-token TRUNK_TEST_COLLECTION_ID: tc_fake TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS: "0" TRUNK_API_CLIENT_RETRY_COUNT: "0" diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 0a682efe..20f4461b 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,6 +55,8 @@ cargo build ./target/debug/trunk-analytics-cli upload --org-url-slug=trunk-io --token=${API_TOKEN} --test-collection-id=${TEST_COLLECTION_ID} --junit-paths=junit.xml ``` +`API_TOKEN` is a v2 API key (`trunk_v2_...`) or a Trunk JWT from `trunk login --api-key`; the CLI refuses an organization token before any call. See [Credentials](README.md#credentials). + You can generate sample junit files by running ```bash diff --git a/README.md b/README.md index 3ed9acaa..983c9c49 100644 --- a/README.md +++ b/README.md @@ -18,6 +18,19 @@ Rust CLI for uploading test output to [Trunk Flaky Tests][flaky-tests]. Please follow the instructions to [start uploading][app] or check out our [docs]. This is downloaded automatically using the [Trunk launcher][launcher] and when using the [analytics-uploader GitHub Action][uploader]. +## Credentials + +`--token` / `TRUNK_API_TOKEN` takes one of two credentials. The CLI reads which one before it calls Trunk: + +- **A v2 API key** (`trunk_v2_...`). Create one in the Trunk web app under Settings → Developer → API Keys, and give it the **Upload test results** (`upload_test_results`) permission. The CLI sends it as `Authorization: Bearer`. +- **A Trunk JWT**, such as `trunk login --api-key` prints. The CLI sends it on `x-trunk-token`. It lasts 15 minutes, so mint it in the step right before the upload, not at the start of the job: a long `trunk-analytics-cli test` run, or a test-runner plugin that looks up quarantined tests early and uploads at the end, can outlive it. The [analytics-uploader][uploader] v3 does this for you. When Trunk refuses an expired JWT, the CLI says so; it does not refresh it. + +analytics-cli 1.0.0 refuses any other token, the organization token included, before it calls Trunk. Pin analytics-cli 0.17.x to keep using the organization token. + +If Trunk answers `403` because the key lacks the Upload test results permission, edit the key on the API Keys screen to add it. + +`--allow-forked-pr-uploads` uploads with no token, only from a GitHub Actions run of a pull request from a fork: the CLI swaps the job's `GITHUB_TOKEN` for a Trunk login. + ## Development For more information about how to build and run the Rust CLI, please see [CONTRIBUTING.md][contributing]. diff --git a/api/src/client/mod.rs b/api/src/client/mod.rs index e5be9f96..1f86defc 100644 --- a/api/src/client/mod.rs +++ b/api/src/client/mod.rs @@ -10,6 +10,7 @@ use serde::de::DeserializeOwned; use tokio::fs; use crate::call_api::CallApi; +use crate::credential::{self, LegacyTokenRefused, TokenKind}; use crate::message; use crate::quarantine::{QuarantineLookup, QuarantinedTestSet}; @@ -53,6 +54,8 @@ pub struct ApiClient { /// Holds the run's credential: an API token from the start, or a fork pull request's Trunk /// login once it has one. trunk_api_v2_client: Option, + /// What `trunk_api_v2_client` presents, for the message when Trunk refuses it. + credential: Option, /// `--allow-forked-pr-uploads` with no token. credential_free: bool, /// A credential-free GitHub Actions fork PR run, which swaps its `GITHUB_TOKEN` for a login. @@ -75,11 +78,23 @@ pub struct GithubForkPullRequest { pub github_token: Option, } -/// What a v2 call presents. The gateway takes only an API key on `Authorization`, so a Trunk login +/// What a v2 call presents. The gateway takes only an API key on `Authorization`, so a Trunk JWT /// goes on `x-trunk-token` and the two are never sent together. pub(crate) enum V2Credential { ApiKey(String), - TrunkLogin(String), + Jwt(String), +} + +/// Where the run's credential came from. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub(crate) enum RunCredential { + ApiKey, + /// A Trunk JWT passed as the token, with its `exp` when it has one. + Jwt { + expires_at: Option>, + }, + /// A fork pull request's GitHub Actions login. + GithubActionsLogin, } impl V2Credential { @@ -96,10 +111,10 @@ impl V2Credential { format!("Bearer {token}"), "Trunk API token is not ASCII", ), - Self::TrunkLogin(jwt) => ( + Self::Jwt(jwt) => ( Self::TRUNK_TOKEN_HEADER, jwt.clone(), - "Trunk login is not ASCII", + "Trunk JWT is not ASCII", ), }; let mut value = HeaderValue::from_str(&value).map_err(|_| anyhow::Error::msg(error))?; @@ -130,6 +145,8 @@ impl ApiClient { const TRUNK_TELEMETRY_TIMEOUT: std::time::Duration = std::time::Duration::from_secs(1); const TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER: &'static str = "x-trunk-allow-forked-pr-uploads"; + /// Refuses a token that is neither a v2 API key nor a Trunk JWT with `LegacyTokenRefused`, + /// before any call. pub fn new( api_token: Option, allow_forked_pr_uploads: bool, @@ -138,7 +155,9 @@ impl ApiClient { ) -> anyhow::Result { let org_url_slug = String::from(org_url_slug.as_ref()); - let api_token = api_token.filter(|token| !token.trim().is_empty()); + let api_token = api_token + .map(|token| token.trim().to_owned()) + .filter(|token| !token.is_empty()); if api_token.is_none() && !allow_forked_pr_uploads { return Err(anyhow::anyhow!( "Either a Trunk API token or --allow-forked-pr-uploads is required." @@ -148,18 +167,31 @@ impl ApiClient { let api_host = get_api_host(); tracing::debug!("Using public api address {}", api_host); - let trunk_api_v2_client = api_token - .clone() - .map(|token| V2Credential::ApiKey(token).client()) - .transpose()?; + let (trunk_api_v2_client, telemetry_client, credential) = match api_token.clone() { + None => (None, None, None), + Some(token) => match TokenKind::of(&token) { + TokenKind::ApiKey => { + let v2 = V2Credential::ApiKey(token); + ( + Some(v2.client()?), + Some(v2.client_with_timeout(Self::TRUNK_TELEMETRY_TIMEOUT)?), + Some(RunCredential::ApiKey), + ) + } + TokenKind::Jwt => { + let expires_at = credential::jwt_expiry(&token); + let v2 = V2Credential::Jwt(token); + ( + Some(v2.client()?), + Some(v2.client_with_timeout(Self::TRUNK_TELEMETRY_TIMEOUT)?), + Some(RunCredential::Jwt { expires_at }), + ) + } + TokenKind::Legacy => return Err(anyhow::Error::new(LegacyTokenRefused)), + }, + }; let credential_free = allow_forked_pr_uploads && api_token.is_none(); - let telemetry_client = api_token - .clone() - .map(|token| { - V2Credential::ApiKey(token).client_with_timeout(Self::TRUNK_TELEMETRY_TIMEOUT) - }) - .transpose()?; let mut s3_client_default_headers = HeaderMap::new(); s3_client_default_headers.append( @@ -174,6 +206,7 @@ impl ApiClient { api_host, s3_client, trunk_api_v2_client, + credential, credential_free, github_fork_pull_request: None, github_fork_login_failure: None, @@ -346,8 +379,8 @@ pub(crate) enum CheckUnauthorized { } pub(crate) const UNAUTHORIZED_CONTEXT: &str = concat!( - "Your Trunk token may be incorrect - find it on the Trunk app ", - "(Settings -> Manage Organization -> Organization API Token -> View).", + "Your Trunk API key may be incorrect - find it in the Trunk app ", + "(Settings -> Developer -> API Keys).", ); /// The API's explanation of a client error, kept in the error chain so the error report can @@ -561,8 +594,13 @@ mod tests { let state = mock_server_builder.spawn_mock_server().await; - let mut api_client = - ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); + let mut api_client = ApiClient::new( + Some(String::from("trunk_v2_mock-token")), + false, + "mock-org", + None, + ) + .unwrap(); api_client.api_host.clone_from(&state.host); let error = api_client @@ -593,8 +631,13 @@ mod tests { let state = mock_server_builder.spawn_mock_server().await; - let mut api_client = - ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); + let mut api_client = ApiClient::new( + Some(String::from("trunk_v2_mock-token")), + false, + "mock-org", + None, + ) + .unwrap(); api_client.api_host.clone_from(&state.host); let error = api_client @@ -620,8 +663,13 @@ mod tests { let state = mock_server_builder.spawn_mock_server().await; - let mut api_client = - ApiClient::new(Some(String::from("mock-token")), false, "mock-org", None).unwrap(); + let mut api_client = ApiClient::new( + Some(String::from("trunk_v2_mock-token")), + false, + "mock-org", + None, + ) + .unwrap(); api_client.api_host.clone_from(&state.host); let error = api_client @@ -644,7 +692,15 @@ mod tests { // A blank token is treated as absent. assert!(ApiClient::new(Some(String::from(" ")), false, "mock-org", None).is_err()); - assert!(ApiClient::new(Some(String::from("token")), false, "mock-org", None).is_ok()); + assert!( + ApiClient::new( + Some(String::from("trunk_v2_token")), + false, + "mock-org", + None + ) + .is_ok() + ); } #[test] diff --git a/api/src/client/v2.rs b/api/src/client/v2.rs index fd093f46..9f16b64c 100644 --- a/api/src/client/v2.rs +++ b/api/src/client/v2.rs @@ -4,14 +4,17 @@ use base64::{Engine as _, engine::general_purpose::STANDARD}; use reqwest::{Client, StatusCode}; use super::{ - ApiClient, CheckUnauthorized, GithubForkPullRequest, StatusCodeHelp, V2Credential, - status_code_help, + ApiClient, CheckUnauthorized, ClientErrorExplanation, GithubForkPullRequest, RunCredential, + StatusCodeHelp, V2Credential, status_code_help, }; use crate::call_api::CallApi; use crate::message; use crate::quarantine::{QuarantineLookup, QuarantinedTestSet}; use crate::urls::TestCaseGuidScope; +const INSUFFICIENT_PERMISSIONS: &str = "INSUFFICIENT_PERMISSIONS"; +const MISSING_UPLOAD_PERMISSION: &str = "lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys."; + impl ApiClient { const QUARANTINE_STATUS_MAX_IDENTIFIERS: usize = 2_000; const CREDENTIAL_FREE_NOT_A_FORK_PR: &'static str = "--allow-forked-pr-uploads without a Trunk API token uploads only from a GitHub Actions run of a pull request from a fork; test results will not be uploaded."; @@ -29,7 +32,10 @@ impl ApiClient { return; }; match self.github_actions_login(pull_request).await { - Ok(client) => self.trunk_api_v2_client = Some(client), + Ok(client) => { + self.trunk_api_v2_client = Some(client); + self.credential = Some(RunCredential::GithubActionsLogin); + } Err(reason) => { tracing::warn!( "Could not log in to Trunk for this fork pull request ({reason}); test results will not be uploaded" @@ -88,11 +94,65 @@ impl ApiClient { .filter(|login| !login.jwt.is_empty()) .ok_or("Trunk's answer could not be read")?; tracing::debug!("Logged in to the Trunk API v2 until {}", login.expires_at); - V2Credential::TrunkLogin(login.jwt) + V2Credential::Jwt(login.jwt) .client() .map_err(|error| error.to_string()) } + /// Puts the CLI's own explanation on a 401 or a 403 the run's credential earned, in place of a + /// bare status: what is wrong with the key or JWT, and how to fix it. + fn explain_credential_refusal(&self, error: anyhow::Error) -> anyhow::Error { + let status = error + .root_cause() + .downcast_ref::() + .and_then(reqwest::Error::status); + let code = error + .downcast_ref::() + .and_then(|explanation| explanation.code.clone()); + let insufficient_permissions = code.as_deref() == Some(INSUFFICIENT_PERMISSIONS); + let message = match (status, self.credential) { + (Some(StatusCode::FORBIDDEN), Some(RunCredential::ApiKey)) + if insufficient_permissions => + { + format!("Trunk refused this API key: it {MISSING_UPLOAD_PERMISSION}") + } + (Some(StatusCode::FORBIDDEN), Some(RunCredential::Jwt { .. })) + if insufficient_permissions => + { + format!( + "Trunk refused this JWT: the API key it was minted from {MISSING_UPLOAD_PERMISSION}" + ) + } + (Some(StatusCode::FORBIDDEN), Some(RunCredential::GithubActionsLogin)) + if insufficient_permissions => + { + String::from( + "Trunk refused this fork pull request's upload: the test collection must have forked-PR uploads enabled.", + ) + } + (Some(StatusCode::UNAUTHORIZED), Some(RunCredential::Jwt { expires_at })) => { + let expired = match expires_at { + Some(expires_at) if expires_at <= chrono::Utc::now() => format!( + " (it expired at {})", + expires_at.to_rfc3339_opts(chrono::SecondsFormat::Secs, true) + ), + _ => String::new(), + }; + format!( + "Trunk did not accept this JWT: it is expired or invalid{expired}. A JWT from `trunk login --api-key` lasts 15 minutes, so re-run `trunk login --api-key` right before the upload step (trunk-io/analytics-uploader v3 does), or pass a v2 API key with --token or TRUNK_API_TOKEN instead." + ) + } + (Some(StatusCode::UNAUTHORIZED), Some(RunCredential::ApiKey)) => String::from( + "Trunk did not accept this API key: it may be mistyped, revoked or deleted. Check it in the Trunk web app under Settings → Developer → API Keys.", + ), + _ => return error, + }; + error.context(ClientErrorExplanation { + code, + message: Some(message), + }) + } + fn v2_client(&self) -> anyhow::Result<&Client> { if let Some(client) = &self.trunk_api_v2_client { return Ok(client); @@ -160,7 +220,8 @@ impl ApiClient { render_sender: self.render_sender.clone(), } .call_api() - .await?; + .await + .map_err(|error| self.explain_credential_refusal(error))?; let id_v2 = STANDARD.encode(serde_json::to_vec(&message::BundleUploadIdV2 { id: &upload.id, @@ -254,6 +315,7 @@ impl ApiClient { } .call_api() .await + .map_err(|error| self.explain_credential_refusal(error)) } pub(super) async fn get_all_quarantined_tests( @@ -310,7 +372,8 @@ impl ApiClient { render_sender: self.render_sender.clone(), } .call_api() - .await?; + .await + .map_err(|error| self.explain_credential_refusal(error))?; guids.extend(page.data.into_iter().map(|test| test.test_id)); // Every page carries the same scope, so the first page's is the list's. let (scope, quarantine_enabled) = scope.get_or_insert(( diff --git a/api/src/credential.rs b/api/src/credential.rs new file mode 100644 index 00000000..a12a14fc --- /dev/null +++ b/api/src/credential.rs @@ -0,0 +1,139 @@ +//! What `--token` / `TRUNK_API_TOKEN` holds. It is read before any call, so a token the API would +//! refuse, such as a pre-1.0 organization token, never reaches it. + +use std::fmt; + +use base64::{Engine as _, engine::general_purpose::URL_SAFE_NO_PAD}; +use chrono::{DateTime, Utc}; + +/// Every v2 API key starts with this; the gateway exchanges it for a Trunk JWT. +pub const API_KEY_PREFIX: &str = "trunk_v2_"; + +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub enum TokenKind { + /// A v2 API key, sent as `Authorization: Bearer`. + ApiKey, + /// A Trunk JWT, such as `trunk login --api-key` prints, sent on `x-trunk-token`. Sent as a + /// bearer it would reach the gateway's API key lookup and fail. + Jwt, + /// Anything else, which 1.0.0 reads as the organization token it no longer takes. + Legacy, +} + +impl TokenKind { + pub fn of(token: &str) -> Self { + let token = token.trim(); + if token.starts_with(API_KEY_PREFIX) { + Self::ApiKey + } else if is_jwt(token) { + Self::Jwt + } else { + Self::Legacy + } + } +} + +/// Three non-empty base64url segments: a JWS in compact form. An organization token has no dots. +fn is_jwt(token: &str) -> bool { + let segments: Vec<&str> = token.split('.').collect(); + segments.len() == 3 + && segments.iter().all(|segment| { + !segment.is_empty() + && segment + .bytes() + .all(|byte| byte.is_ascii_alphanumeric() || byte == b'-' || byte == b'_') + }) +} + +/// The JWT's `exp`, read without checking its signature, for the message when Trunk refuses it. +pub fn jwt_expiry(token: &str) -> Option> { + let payload = URL_SAFE_NO_PAD + .decode(token.trim().split('.').nth(1)?) + .ok()?; + let exp = serde_json::from_slice::(&payload) + .ok()? + .get("exp")? + .as_i64()?; + DateTime::from_timestamp(exp, 0) +} + +/// The run's token is neither a v2 API key nor a Trunk JWT. +#[derive(Debug, Clone, Copy, PartialEq, Eq)] +pub struct LegacyTokenRefused; + +impl fmt::Display for LegacyTokenRefused { + fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result { + f.write_str(LEGACY_TOKEN_MESSAGE) + } +} + +impl std::error::Error for LegacyTokenRefused {} + +pub const LEGACY_TOKEN_MESSAGE: &str = concat!( + "This Trunk token is not a v2 API key. analytics-cli 1.0.0 needs a v2 API key: create one in ", + "the Trunk web app under Settings → Developer → API Keys, give it the Upload test results ", + "permission, and pass it with --token or TRUNK_API_TOKEN. To keep using your organization ", + "token, pin analytics-cli 0.17.x.", +); + +#[cfg(test)] +mod tests { + use super::{TokenKind, jwt_expiry}; + + // {"alg":"RS256","typ":"JWT"}.{"sub":"org-token:1","exp":1762880400}.signature + const JWT: &str = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvcmctdG9rZW46MSIsImV4cCI6MTc2Mjg4MDQwMH0.c2lnbmF0dXJl"; + + #[test] + fn reads_a_v2_api_key_by_its_prefix() { + for token in [ + "trunk_v2_AbC123", + " trunk_v2_AbC123\n", + // The prefix decides, whatever follows it. + "trunk_v2_a.b.c", + ] { + assert_eq!(TokenKind::of(token), TokenKind::ApiKey, "{token:?}"); + } + } + + #[test] + fn reads_three_base64url_segments_as_a_jwt() { + for token in [JWT, "a.b.c", "A-_9.x.y_-", &format!(" {JWT} ")] { + assert_eq!(TokenKind::of(token), TokenKind::Jwt, "{token:?}"); + } + } + + #[test] + fn reads_anything_else_as_a_legacy_token() { + for token in [ + "", + " ", + "0123456789abcdef0123456789abcdef", + "8f14e45f-ceea-467a-9575-2b8a3e2c0a11", + "trunk_v1_AbC123", + "TRUNK_V2_AbC123", + // Dots, but not three segments. + "a.b", + "a.b.c.d", + "token.with.dots.in.it", + // Three segments, but one is empty or not base64url. + "a..c", + ".b.c", + "a.b.", + "a+b.c/d.e=", + "a b.c.d", + ] { + assert_eq!(TokenKind::of(token), TokenKind::Legacy, "{token:?}"); + } + } + + #[test] + fn reads_a_jwt_expiry_without_its_signature() { + assert_eq!( + jwt_expiry(JWT).map(|expiry| expiry.timestamp()), + Some(1_762_880_400) + ); + for token in ["a.b.c", "trunk_v2_AbC123", ""] { + assert_eq!(jwt_expiry(token), None, "{token:?}"); + } + } +} diff --git a/api/src/lib.rs b/api/src/lib.rs index 5f7a72f5..1c6ff2b1 100644 --- a/api/src/lib.rs +++ b/api/src/lib.rs @@ -1,5 +1,6 @@ mod call_api; pub mod client; +pub mod credential; pub mod message; pub mod otlp; pub mod quarantine; diff --git a/api/tests/v2_client.rs b/api/tests/v2_client.rs index 6839ef75..9244eb26 100644 --- a/api/tests/v2_client.rs +++ b/api/tests/v2_client.rs @@ -1,6 +1,7 @@ use std::collections::HashSet; -use api::client::{ApiClient, GithubForkPullRequest}; +use api::client::{ApiClient, ClientErrorExplanation, GithubForkPullRequest}; +use api::credential::LegacyTokenRefused; use api::message; use api::quarantine::{QuarantineLookup, QuarantinedTestSet}; use api::urls::TestCaseGuidScope; @@ -41,7 +42,7 @@ fn client(state: &SharedMockServerState, token: Option<&str>) -> ApiClient { } fn v2_client(state: &SharedMockServerState) -> ApiClient { - client(state, Some("org-key")) + client(state, Some("trunk_v2_org-key")) } fn uris(state: &SharedMockServerState) -> Vec { @@ -258,7 +259,7 @@ async fn creates_a_v2_upload() { ); assert_eq!( authorization(&state), - vec![Some(String::from("Bearer org-key"))] + vec![Some(String::from("Bearer trunk_v2_org-key"))] ); assert_eq!( bodies(&state), @@ -306,7 +307,7 @@ async fn looks_up_failures_in_batches_of_2000() { ); assert_eq!( authorization(&state), - vec![Some(String::from("Bearer org-key")); 2] + vec![Some(String::from("Bearer trunk_v2_org-key")); 2] ); } @@ -675,7 +676,8 @@ async fn a_fork_pr_run_without_github_token_makes_no_request() { #[tokio::test] async fn a_run_with_a_token_never_swaps() { let state = serve(vec![login_route((StatusCode::OK, LOGIN)), uploads_route()]).await; - let mut client = ApiClient::new(Some(String::from("org-key")), true, "acme", None).unwrap(); + let mut client = + ApiClient::new(Some(String::from("trunk_v2_org-key")), true, "acme", None).unwrap(); client.api_host.clone_from(&state.host); client.set_github_fork_pull_request(fork_pull_request(Some("ghs_job-token"))); @@ -697,8 +699,221 @@ async fn a_run_with_a_token_never_swaps() { .collect::>(), vec![( format!("/v2/test-collections/{SHORT_ID}/uploads"), - Some(String::from("Bearer org-key")), + Some(String::from("Bearer trunk_v2_org-key")), None )] ); } + +// {"alg":"RS256","typ":"JWT"}.{"sub":"org-token:1","exp":1762880400}.signature, which expired at +// 2025-11-11T17:00:00Z. +const EXPIRED_JWT: &str = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvcmctdG9rZW46MSIsImV4cCI6MTc2Mjg4MDQwMH0.c2lnbmF0dXJl"; + +fn credential_headers( + state: &SharedMockServerState, +) -> Vec<(String, Option, Option)> { + state + .route_requests + .lock() + .unwrap() + .iter() + .map(|request| { + ( + request.uri.split('?').next().unwrap().to_string(), + request.authorization.clone(), + request.trunk_token.clone(), + ) + }) + .collect() +} + +#[tokio::test] +async fn sends_a_jwt_on_x_trunk_token_and_never_as_a_bearer() { + let state = serve(vec![ + quarantine_status_route(true), + all_quarantined_tests_route(true), + uploads_route(), + ]) + .await; + let client = client(&state, Some(EXPIRED_JWT)); + + client + .get_quarantining_config(&quarantine_request(vec![failure(0)])) + .await + .unwrap(); + client + .get_quarantined_test_set(&quarantine_request(vec![])) + .await + .unwrap(); + client + .create_bundle_upload(&upload_request()) + .await + .unwrap(); + + let with_jwt = |path: String| (path, None, Some(String::from(EXPIRED_JWT))); + assert_eq!( + credential_headers(&state), + vec![ + with_jwt(format!("/v2/test-collections/{SHORT_ID}/quarantine-status")), + with_jwt(format!( + "/v2/test-collections/{SHORT_ID}/all-quarantined-tests" + )), + with_jwt(format!( + "/v2/test-collections/{SHORT_ID}/all-quarantined-tests" + )), + with_jwt(format!("/v2/test-collections/{SHORT_ID}/uploads")), + ] + ); +} + +#[tokio::test] +async fn sends_an_api_key_as_a_bearer_and_never_on_x_trunk_token() { + let state = serve(vec![quarantine_status_route(true), uploads_route()]).await; + // Surrounding whitespace, as a secret pasted with a newline has, is not sent. + let client = client(&state, Some(" trunk_v2_org-key\n")); + + client + .get_quarantining_config(&quarantine_request(vec![failure(0)])) + .await + .unwrap(); + client + .create_bundle_upload(&upload_request()) + .await + .unwrap(); + + let with_key = |path: String| (path, Some(String::from("Bearer trunk_v2_org-key")), None); + assert_eq!( + credential_headers(&state), + vec![ + with_key(format!("/v2/test-collections/{SHORT_ID}/quarantine-status")), + with_key(format!("/v2/test-collections/{SHORT_ID}/uploads")), + ] + ); +} + +#[test] +fn refuses_an_organization_token_before_any_call() { + for token in ["0123456789abcdef0123456789abcdef", "org.token", "a.b.c.d"] { + let error = ApiClient::new(Some(String::from(token)), false, "acme", None) + .err() + .unwrap_or_else(|| panic!("{token:?} must be refused")); + + assert_eq!( + error.downcast_ref::(), + Some(&LegacyTokenRefused), + "{token:?}" + ); + let message = error.to_string(); + for part in [ + "analytics-cli 1.0.0 needs a v2 API key", + "Settings → Developer → API Keys", + "Upload test results", + "pin analytics-cli 0.17.x", + ] { + assert!(message.contains(part), "{message}"); + } + } +} + +fn refusing_route( + route: &'static str, + status: StatusCode, + body: &'static str, +) -> (&'static str, MethodRouter) { + (route, post(move || async move { (status, body) })) +} + +const INSUFFICIENT_PERMISSIONS: &str = r#"{"error":{"code":"INSUFFICIENT_PERMISSIONS","message":"You do not have permission to upload test results in this organization"}}"#; + +/// Each CLI endpoint's error for `status` and `body`, called with `token`. +async fn refusals(token: &str, status: StatusCode, body: &'static str) -> Vec { + let state = serve(vec![ + refusing_route(UPLOADS_ROUTE, status, body), + refusing_route(STATUS_ROUTE, status, body), + refusing_route(LIST_ROUTE, status, body), + ]) + .await; + let client = client(&state, Some(token)); + vec![ + client + .create_bundle_upload(&upload_request()) + .await + .unwrap_err(), + client + .get_quarantining_config(&quarantine_request(vec![failure(0)])) + .await + .unwrap_err(), + client + .get_quarantined_test_set(&quarantine_request(vec![])) + .await + .unwrap_err(), + ] + .into_iter() + .map(|error| { + error + .downcast_ref::() + .and_then(|explanation| explanation.message.clone()) + .unwrap_or_default() + }) + .collect() +} + +#[tokio::test] +async fn names_the_missing_upload_permission_on_a_403() { + for (token, refused) in [ + ("trunk_v2_org-key", "Trunk refused this API key: it lacks"), + ( + EXPIRED_JWT, + "Trunk refused this JWT: the API key it was minted from lacks", + ), + ] { + for message in refusals(token, StatusCode::FORBIDDEN, INSUFFICIENT_PERMISSIONS).await { + assert!(message.starts_with(refused), "{message}"); + assert!( + message.contains( + "the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys." + ), + "{message}" + ); + } + } +} + +#[tokio::test] +async fn keeps_the_api_explanation_of_any_other_403() { + let body = r#"{"error":{"code":"SOMETHING_ELSE","message":"Not for you."}}"#; + assert_eq!( + refusals("trunk_v2_org-key", StatusCode::FORBIDDEN, body).await, + vec![String::from("Not for you."); 3] + ); +} + +#[tokio::test] +async fn says_a_jwt_has_expired_on_a_401() { + // The edge refuses an expired JWT before public-api sees it, with no envelope. + for message in refusals(EXPIRED_JWT, StatusCode::UNAUTHORIZED, "Jwt is expired").await { + assert!( + message.starts_with( + "Trunk did not accept this JWT: it is expired or invalid (it expired at 2025-11-11T17:00:00Z)." + ), + "{message}" + ); + for part in [ + "re-run `trunk login --api-key` right before the upload step", + "pass a v2 API key with --token or TRUNK_API_TOKEN", + ] { + assert!(message.contains(part), "{message}"); + } + } +} + +#[tokio::test] +async fn says_an_api_key_was_not_accepted_on_a_401() { + let body = r#"{"error":{"code":"INVALID_TOKEN","message":"Invalid token"}}"#; + for message in refusals("trunk_v2_org-key", StatusCode::UNAUTHORIZED, body).await { + assert!( + message.starts_with("Trunk did not accept this API key"), + "{message}" + ); + } +} diff --git a/cli/src/summary_output/github.rs b/cli/src/summary_output/github.rs index 2aff36b8..74eef273 100644 --- a/cli/src/summary_output/github.rs +++ b/cli/src/summary_output/github.rs @@ -163,13 +163,18 @@ fn error_message(error: &ErrorSummary) -> String { } ErrorKind::Unauthorized => { lines.extend(error.message.clone()); - lines.push(UNAUTHORIZED_CONTEXT.to_string()); - lines.extend( - error - .settings_url - .as_ref() - .map(|url| format!("{UNAUTHORIZED_HINT} {url}")), - ); + match &error.explanation { + Some(explanation) => lines.push(explanation.clone()), + None => { + lines.push(UNAUTHORIZED_CONTEXT.to_string()); + lines.extend( + error + .settings_url + .as_ref() + .map(|url| format!("{UNAUTHORIZED_HINT} {url}")), + ); + } + } } ErrorKind::GitRepository | ErrorKind::Interrupted | ErrorKind::Other => { match &error.message { diff --git a/cli/src/summary_output/text.rs b/cli/src/summary_output/text.rs index 9914ab49..7147971a 100644 --- a/cli/src/summary_output/text.rs +++ b/cli/src/summary_output/text.rs @@ -110,16 +110,24 @@ pub fn render_error(error: &ErrorSummary) -> anyhow::Result> { return Ok(lines); } ErrorKind::Unauthorized => { + lines.extend([ + Line::from_iter([Span::new_unstyled_lossy( + error.message.as_deref().unwrap_or_default(), + )]), + Line::default(), + ]); + // The CLI's explanation of a refused key or JWT says what to fix, where the generic + // hint would point at the org slug. + if let Some(explanation) = &error.explanation { + lines.push(Line::from_iter([Span::new_unstyled_lossy(explanation)])); + return Ok(lines); + } let mut hint = Line::from_iter([Span::new_unstyled_lossy(format!( "{UNAUTHORIZED_HINT} {}", error.settings_url.as_deref().unwrap_or_default() ))]); hint.pad_left(2); lines.extend([ - Line::from_iter([Span::new_unstyled_lossy( - error.message.as_deref().unwrap_or_default(), - )]), - Line::default(), Line::from_iter([Span::new_unstyled(UNAUTHORIZED_CONTEXT)?]), hint, ]); diff --git a/cli/src/test_command.rs b/cli/src/test_command.rs index 7d7d8ef3..dfa2b2f5 100644 --- a/cli/src/test_command.rs +++ b/cli/src/test_command.rs @@ -17,7 +17,9 @@ use crate::{ context::{gather_debug_props, gather_initial_test_context}, error_report::InterruptingError, exit_code::ExitCode, - upload_command::{RunUploadOptions, UploadArgs, UploadRunResult, run_upload}, + upload_command::{ + RunUploadOptions, UploadArgs, UploadRunResult, refuse_a_legacy_token, run_upload, + }, }; #[derive(Args, Clone, Debug)] @@ -67,6 +69,7 @@ pub async fn run_test( }: TestArgs, render_sender: Sender, ) -> anyhow::Result { + refuse_a_legacy_token(upload_args.token.as_deref())?; let token = upload_args.token.clone().unwrap_or_default(); let mut test_run_result = run_test_command(&command, render_sender.clone()).await?; let test_context = gather_initial_test_context( diff --git a/cli/src/upload_command.rs b/cli/src/upload_command.rs index c6ffa365..233c4e99 100644 --- a/cli/src/upload_command.rs +++ b/cli/src/upload_command.rs @@ -4,6 +4,7 @@ use std::path::PathBuf; use std::sync::mpsc::Sender; use api::client::{ApiClient, ApiErrorEndpoint, GithubForkPullRequest}; +use api::credential::{LegacyTokenRefused, TokenKind}; use api::urls::TestCaseGuidScope; use bundle::{BundleMeta, BundlerUtil, QuarantineResolutionMode, unzip_tarball}; use clap::{ArgAction, Args}; @@ -101,7 +102,7 @@ pub struct UploadArgs { long, required_unless_present = "allow_forked_pr_uploads", env = constants::TRUNK_API_TOKEN_ENV, - help = "Organization token. Defaults to TRUNK_API_TOKEN env var." + help = "A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, from Settings → Developer → API Keys in the Trunk web app; or a Trunk JWT from `trunk login --api-key`, which lasts 15 minutes, so mint it right before this step. The organization token is refused: pin analytics-cli 0.17.x to keep using it. Defaults to TRUNK_API_TOKEN env var." )] pub token: Option, /// Opting in explicitly is what keeps a job whose token secret failed to interpolate failing @@ -516,7 +517,8 @@ pub async fn run_upload( upload_args.allow_forked_pr_uploads, &upload_args.org_url_slug, render_sender, - )?; + ) + .map_err(refusal_interrupts)?; if upload_args.allow_forked_pr_uploads && let Some(pull_request) = env::var("GITHUB_EVENT_PATH") .ok() @@ -751,6 +753,25 @@ pub async fn run_upload( }) } +/// Refuses a token that is neither a v2 API key nor a Trunk JWT, before the run calls Trunk or, for +/// `test`, runs its tests. An InterruptingError, since any other error fails open: a green step +/// whose results go nowhere. +pub fn refuse_a_legacy_token(token: Option<&str>) -> anyhow::Result<()> { + match token.filter(|token| !token.trim().is_empty()) { + Some(token) if TokenKind::of(token) == TokenKind::Legacy => { + Err(refusal_interrupts(anyhow::Error::new(LegacyTokenRefused))) + } + _ => Ok(()), + } +} + +fn refusal_interrupts(error: anyhow::Error) -> anyhow::Error { + match error.downcast_ref::() { + Some(refusal) => anyhow::anyhow!(InterruptingError::new(refusal.to_string())), + None => error, + } +} + pub fn missing_test_collection_id_message(caller: UploadCaller) -> String { let how_to_pass = match caller { UploadCaller::Cli => String::from("Pass --test-collection-id"), diff --git a/cli/tests/common/command_builder.rs b/cli/tests/common/command_builder.rs index 962bb4fe..62764584 100644 --- a/cli/tests/common/command_builder.rs +++ b/cli/tests/common/command_builder.rs @@ -6,6 +6,8 @@ use constants::{ }; const DEFAULT_JUNIT_PATHS: &str = "./*"; +/// A v2 API key, which `--token` takes unless a test passes another with `token`. +pub const DEFAULT_TOKEN: &str = "trunk_v2_test-token"; /// Set through the environment, so a test passing `--test-collection-id` overrides it. pub const DEFAULT_TEST_COLLECTION_ID: &str = "dEfault1"; @@ -30,6 +32,7 @@ pub struct UploadArgs { validation_report: Option, dry_run: bool, forked_pr: bool, + token: String, } impl UploadArgs { @@ -55,6 +58,7 @@ impl UploadArgs { validation_report: None, dry_run: false, forked_pr: false, + token: String::from(DEFAULT_TOKEN), } } @@ -62,7 +66,7 @@ impl UploadArgs { let auth_args = if self.forked_pr { vec![String::from("--allow-forked-pr-uploads")] } else { - vec![String::from("--token"), String::from("test-token")] + vec![String::from("--token"), self.token.clone()] }; vec![String::from("--org-url-slug"), String::from("test-org")] .into_iter() @@ -572,6 +576,16 @@ impl<'b> CommandBuilder<'b> { self } + /// Passes `token` with `--token` in place of `DEFAULT_TOKEN`. + pub fn token(&mut self, token: &str) -> &mut Self { + if let CommandType::Upload { upload_args, .. } | CommandType::Test { upload_args, .. } = + &mut self.command_type + { + upload_args.token = String::from(token); + } + self + } + pub fn repo_url(&mut self, new_value: &str) -> &mut Self { self.command_type.repo_url(new_value); self diff --git a/cli/tests/upload.rs b/cli/tests/upload.rs index 22761b3e..c545681f 100644 --- a/cli/tests/upload.rs +++ b/cli/tests/upload.rs @@ -299,7 +299,7 @@ async fn upload_bundle() { debug_props.command_line.ends_with( &expected_command_args .join(" ") - .replace("test-token", "") + .replace("trunk_v2_test-token", "") .replace("--token", "") .trim() ) @@ -430,7 +430,7 @@ fn requires_a_test_collection_id(#[case] subcommand: &str) { "--org-url-slug", "test-org", "--token", - "test-token", + "trunk_v2_test-token", ]); if subcommand == "test" { command.arg("true"); @@ -459,7 +459,7 @@ fn refuses_a_blank_test_collection_id( "--org-url-slug", "test-org", "--token", - "test-token", + "trunk_v2_test-token", ]) .args(args) .assert() @@ -3534,7 +3534,7 @@ async fn upload_names_the_collection_and_sends_the_token_as_a_bearer() { for endpoint in ["quarantineStatus", "uploads"] { assert_eq!( credential_headers(&state, endpoint), - (Some(String::from("Bearer test-token")), None, None), + (Some(String::from("Bearer trunk_v2_test-token")), None, None), "{endpoint}" ); } @@ -3567,6 +3567,137 @@ async fn upload_names_the_collection_and_sends_the_token_as_a_bearer() { ); } +// {"alg":"RS256","typ":"JWT"}.{"sub":"org-token:1","exp":1762880400}.signature, which expired at +// 2025-11-11T17:00:00Z. +const TRUNK_JWT: &str = "eyJhbGciOiJSUzI1NiIsInR5cCI6IkpXVCJ9.eyJzdWIiOiJvcmctdG9rZW46MSIsImV4cCI6MTc2Mjg4MDQwMH0.c2lnbmF0dXJl"; + +// NOTE: must be multi threaded to start a mock server +#[tokio::test(flavor = "multi_thread")] +async fn upload_sends_a_trunk_jwt_on_x_trunk_token() { + let temp_dir = tempdir().unwrap(); + generate_mock_git_repo(&temp_dir); + generate_mock_valid_junit_xmls_with_failures(&temp_dir); + + let mut mock_server_builder = MockServerBuilder::new(); + mock_server_builder.set_quarantine_status_handler(quarantine_all_handler); + let state = mock_server_builder.spawn_mock_server().await; + + CommandBuilder::upload(temp_dir.path(), state.host.clone()) + .token(TRUNK_JWT) + .command() + .assert() + // every failure is quarantined + .success(); + + for endpoint in ["quarantineStatus", "uploads"] { + assert_eq!( + credential_headers(&state, endpoint), + (None, Some(String::from(TRUNK_JWT)), None), + "{endpoint}" + ); + } +} + +#[rstest::rstest] +#[case::hex("0123456789abcdef0123456789abcdef")] +#[case::dots_but_not_a_jwt("org.token")] +#[tokio::test(flavor = "multi_thread")] +async fn refuses_an_organization_token_before_any_call( + #[case] token: &str, + #[values("upload", "test")] subcommand: &str, +) { + let temp_dir = tempdir().unwrap(); + generate_mock_git_repo(&temp_dir); + generate_mock_valid_junit_xmls(&temp_dir); + let state = MockServerBuilder::new().spawn_mock_server().await; + + let mut command_builder = match subcommand { + "upload" => CommandBuilder::upload(temp_dir.path(), state.host.clone()), + _ => CommandBuilder::test( + temp_dir.path(), + state.host.clone(), + vec![ + String::from("bash"), + String::from("-c"), + String::from("touch tests-ran"), + ], + ), + }; + let assert = command_builder + .token(token) + .command() + .assert() + .code(ExitCode::Error.code()); + + let output = assert.get_output(); + let output = format!( + "{}{}", + String::from_utf8_lossy(&output.stdout), + String::from_utf8_lossy(&output.stderr) + ); + for part in [ + "This Trunk token is not a v2 API key. analytics-cli 1.0.0 needs a v2 API key", + "Settings → Developer → API Keys", + "Upload test results", + "pin analytics-cli 0.17.x", + ] { + assert!(output.contains(part), "{part:?} missing from {output}"); + } + assert!(state.requests.lock().unwrap().is_empty()); + assert!(!temp_dir.path().join("tests-ran").exists()); +} + +/// Serves `status` and `body` from every CLI endpoint. +fn refusing_mock_server(status: StatusCode, body: &'static str) -> MockServerBuilder { + let mut mock_server_builder = MockServerBuilder::new(); + mock_server_builder.set_quarantine_status_handler(move || async move { (status, body) }); + mock_server_builder.set_create_test_upload_handler(move || async move { (status, body) }); + mock_server_builder +} + +// NOTE: must be multi threaded to start a mock server +#[tokio::test(flavor = "multi_thread")] +async fn upload_names_the_missing_upload_permission_on_a_403() { + let temp_dir = tempdir().unwrap(); + generate_mock_git_repo(&temp_dir); + generate_mock_valid_junit_xmls(&temp_dir); + let state = refusing_mock_server( + StatusCode::FORBIDDEN, + r#"{"error":{"code":"INSUFFICIENT_PERMISSIONS","message":"You do not have permission to upload test results in this organization"}}"#, + ) + .spawn_mock_server() + .await; + + CommandBuilder::upload(temp_dir.path(), state.host.clone()) + .command() + .assert() + .code(ExitCode::Error.code()) + .stdout(predicate::str::contains( + "Trunk refused this API key: it lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys.", + )); +} + +// NOTE: must be multi threaded to start a mock server +#[tokio::test(flavor = "multi_thread")] +async fn upload_says_a_trunk_jwt_has_expired_on_a_401() { + let temp_dir = tempdir().unwrap(); + generate_mock_git_repo(&temp_dir); + generate_mock_valid_junit_xmls(&temp_dir); + let state = refusing_mock_server(StatusCode::UNAUTHORIZED, "Jwt is expired") + .spawn_mock_server() + .await; + + CommandBuilder::upload(temp_dir.path(), state.host.clone()) + .token(TRUNK_JWT) + .command() + .assert() + .code(ExitCode::Error.code()) + .stdout(predicate::str::contains( + "Trunk did not accept this JWT: it is expired or invalid (it expired at 2025-11-11T17:00:00Z).", + )) + .stdout(predicate::str::contains("re-run `trunk login --api-key`")); +} + const GITHUB_ACTIONS_LOGIN: &str = r#"{"jwt":"trunk-jwt","expiresAt":"2026-11-06T17:15:00Z","organization":{"id":"o","slug":"acme","name":"Acme"},"repository":{"id":"r","name":"acme/web"}}"#; const FORK_HEAD_SHA: &str = "0123456789abcdef0123456789abcdef01234567"; const GITHUB_TOKEN: &str = "ghs_job-token"; @@ -3772,7 +3903,7 @@ async fn upload_with_a_token_and_allow_forked_pr_uploads_never_swaps() { assert!(logins.lock().unwrap().is_empty()); assert_eq!( credential_headers(&state, "uploads"), - (Some(String::from("Bearer test-token")), None, None) + (Some(String::from("Bearer trunk_v2_test-token")), None, None) ); } diff --git a/rspec-trunk-flaky-tests/README.md b/rspec-trunk-flaky-tests/README.md index c570b2c9..068889f1 100644 --- a/rspec-trunk-flaky-tests/README.md +++ b/rspec-trunk-flaky-tests/README.md @@ -106,7 +106,7 @@ require 'trunk_spec_helper' ### Environment Variables -For a complete list of environment variables that the gem accepts, see [`lib/trunk_spec_helper.rb`](lib/trunk_spec_helper.rb). The gem uses the same environment variables as the Trunk Analytics CLI for configuration overrides. `TRUNK_ORG_URL_SLUG` and `TRUNK_API_TOKEN` must be set to activate the plugin, and `TRUNK_TEST_COLLECTION_ID` too: with the first two set and no test collection, the plugin prints a warning and stays off. +For a complete list of environment variables that the gem accepts, see [`lib/trunk_spec_helper.rb`](lib/trunk_spec_helper.rb). The gem uses the same environment variables as the Trunk Analytics CLI for configuration overrides. `TRUNK_ORG_URL_SLUG` and `TRUNK_API_TOKEN` must be set to activate the plugin, and `TRUNK_TEST_COLLECTION_ID` too: with the first two set and no test collection, the plugin prints a warning and stays off. `TRUNK_API_TOKEN` must be a v2 API key with the Upload test results permission, or a Trunk JWT from `trunk login --api-key` minted right before the run (it lasts 15 minutes); with an organization token, the plugin prints a warning and stays off. See [Credentials](../README.md#credentials). #### `TRUNK_QUARANTINE_QUERY_FAILURE_EXIT` diff --git a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb index eb69bf20..e46bf527 100644 --- a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb +++ b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb @@ -7,7 +7,8 @@ # # Required environment variables: # TRUNK_ORG_URL_SLUG - Your organization's URL slug -# TRUNK_API_TOKEN - Your API token for authentication +# TRUNK_API_TOKEN - A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, +# or a Trunk JWT from `trunk login --api-key`; the organization token is refused # TRUNK_TEST_COLLECTION_ID - The ID of the test collection to upload to # # Optional environment variables for repository metadata: @@ -106,7 +107,18 @@ def install(config, run) def disabled? knapsack_detector_mode? || ENV['DISABLE_RSPEC_TRUNK_FLAKY_TESTS'] == 'true' || - ENV['TRUNK_ORG_URL_SLUG'].nil? || ENV['TRUNK_API_TOKEN'].nil? || missing_test_collection? + ENV['TRUNK_ORG_URL_SLUG'].nil? || ENV['TRUNK_API_TOKEN'].nil? || missing_test_collection? || + refused_token? + end + + # 1.0.0 takes a v2 API key or a Trunk JWT, never the organization token. Warns for the same + # reason as missing_test_collection?. + def refused_token? + refusal = RSpec::Trunk.token_refusal(ENV.fetch('TRUNK_API_TOKEN')) + return false unless refusal + + warn("Trunk Flaky Tests is disabled: #{refusal}") + true end # Warns rather than staying silent like the checks before it: a run with a token and a slug diff --git a/rspec-trunk-flaky-tests/test/namespace_spec.rb b/rspec-trunk-flaky-tests/test/namespace_spec.rb index cbe3ae2e..dd2aab86 100644 --- a/rspec-trunk-flaky-tests/test/namespace_spec.rb +++ b/rspec-trunk-flaky-tests/test/namespace_spec.rb @@ -69,7 +69,7 @@ it 'defines nothing outside RSpec::Trunk' do env = { 'TRUNK_ORG_URL_SLUG' => 'namespace-spec', - 'TRUNK_API_TOKEN' => 'namespace-spec', + 'TRUNK_API_TOKEN' => 'trunk_v2_namespace-spec', 'TRUNK_TEST_COLLECTION_ID' => 'namespace-spec', 'DISABLE_RSPEC_TRUNK_FLAKY_TESTS' => nil } diff --git a/rspec-trunk-flaky-tests/test/test_collection_spec.rb b/rspec-trunk-flaky-tests/test/test_collection_spec.rb index 6007fd68..13693c41 100644 --- a/rspec-trunk-flaky-tests/test/test_collection_spec.rb +++ b/rspec-trunk-flaky-tests/test/test_collection_spec.rb @@ -19,7 +19,7 @@ def load_gem(test_collection_id) env = { 'TRUNK_ORG_URL_SLUG' => 'test-collection-spec', - 'TRUNK_API_TOKEN' => 'test-collection-spec', + 'TRUNK_API_TOKEN' => 'trunk_v2_test-collection-spec', 'TRUNK_TEST_COLLECTION_ID' => test_collection_id, 'DISABLE_RSPEC_TRUNK_FLAKY_TESTS' => nil } diff --git a/rspec-trunk-flaky-tests/test/token_spec.rb b/rspec-trunk-flaky-tests/test/token_spec.rb new file mode 100644 index 00000000..db56fd80 --- /dev/null +++ b/rspec-trunk-flaky-tests/test/token_spec.rb @@ -0,0 +1,51 @@ +# frozen_string_literal: true + +require 'open3' +require 'rbconfig' +require_relative '../spec/spec_helper' + +# trunk-ignore(rubocop/Metrics/BlockLength) +RSpec.describe 'TRUNK_API_TOKEN' do + let(:probe) do + <<~'RUBY' + require 'rspec_trunk_flaky_tests' + require 'trunk_spec_helper' + puts RSpec::Trunk.current_run ? 'enabled' : 'disabled' + RUBY + end + + let(:lib_dir) { File.dirname($LOAD_PATH.resolve_feature_path('trunk_spec_helper').last) } + + def load_gem(token) + env = { + 'TRUNK_ORG_URL_SLUG' => 'token-spec', + 'TRUNK_API_TOKEN' => token, + 'TRUNK_TEST_COLLECTION_ID' => 'aB3xY9kQ', + 'DISABLE_RSPEC_TRUNK_FLAKY_TESTS' => nil + } + Open3.capture3(env, RbConfig.ruby, '-I', lib_dir, '-e', probe) + end + + ['0123456789abcdef0123456789abcdef', 'a.b', ''].each do |token| + it "disables Trunk and says why for an organization token like #{token.inspect}" do + stdout, stderr, status = load_gem(token) + + expect(status).to be_success, stderr + expect(stdout.lines.last.strip).to eq('disabled') + expect(stderr).to include('Trunk Flaky Tests is disabled: This Trunk token is not a v2 API key.') + expect(stderr).to include('Settings → Developer → API Keys') + expect(stderr).to include('Upload test results') + expect(stderr).to include('pin analytics-cli 0.17.x') + end + end + + ['trunk_v2_AbC123', 'eyJhbGciOiJSUzI1NiJ9.eyJleHAiOjF9.c2ln'].each do |token| + it "enables Trunk for #{token.inspect}" do + stdout, stderr, status = load_gem(token) + + expect(status).to be_success, stderr + expect(stdout.lines.last.strip).to eq('enabled') + expect(stderr).not_to include('Trunk Flaky Tests is disabled') + end + end +end diff --git a/test_report/src/report.rs b/test_report/src/report.rs index e5e9d9b2..b4564c31 100644 --- a/test_report/src/report.rs +++ b/test_report/src/report.rs @@ -9,6 +9,7 @@ use std::{ use api::{ client::ApiClient, + credential::{LegacyTokenRefused, TokenKind}, message::{self, QuarantineResolutionMode}, urls::TestCaseGuidScope, }; @@ -45,6 +46,12 @@ pub fn quarantine_disk_cache_dir() -> PathBuf { .unwrap_or_else(|_| env::temp_dir().join(constants::CACHE_DIR)) } +/// Why the plugin refuses `token`, or `None` for a v2 API key or a Trunk JWT. The rspec gem asks +/// before it turns on, since this crate's logs reach its console only at error level. +pub fn token_refusal(token: String) -> Option { + (TokenKind::of(&token) == TokenKind::Legacy).then(|| LegacyTokenRefused.to_string()) +} + fn requested_test_collection_short_id() -> Option { env::var(constants::TRUNK_TEST_COLLECTION_ID_ENV) .ok() @@ -408,6 +415,10 @@ impl MutTestReport { tracing::warn!("Not checking quarantine status because TRUNK_API_TOKEN is empty"); return IsQuarantinedResult::default(); } + if let Some(refusal) = token_refusal(token.clone()) { + tracing::warn!("Not checking quarantine status: {refusal}"); + return IsQuarantinedResult::default(); + } if org_url_slug.is_empty() { tracing::warn!("Not checking quarantine status because TRUNK_ORG_URL_SLUG is empty"); return IsQuarantinedResult::default(); @@ -724,6 +735,10 @@ impl MutTestReport { tracing::warn!("Not publishing results because TRUNK_API_TOKEN is empty"); return false; } + if let Some(refusal) = token_refusal(token.clone()) { + tracing::error!("Not publishing results: {refusal}"); + return false; + } if org_url_slug.is_empty() { tracing::warn!("Not publishing results because TRUNK_ORG_URL_SLUG is empty"); return false; @@ -1084,6 +1099,7 @@ pub fn ruby_init(ruby: &magnus::Ruby, namespace: magnus::RModule) -> Result<(), "quarantine_lookup_error", magnus::method!(MutTestReport::quarantine_lookup_error, 0), )?; + namespace.define_module_function("token_refusal", magnus::function!(token_refusal, 1))?; Ok(()) } diff --git a/test_report/tests/publish.rs b/test_report/tests/publish.rs index fbae6a54..59fda03c 100644 --- a/test_report/tests/publish.rs +++ b/test_report/tests/publish.rs @@ -59,7 +59,7 @@ async fn publish_uploads_test_report() { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); env::set_var("CI", "1"); env::set_var("GITHUB_JOB", "test-job"); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "0"); } @@ -394,7 +394,7 @@ async fn publish_environment_variable_overrides() { unsafe { // Set all TRUNK_* environment variables to override defaults env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); // Don't set TRUNK_REPO_ROOT when using uncloned repo mode as they conflict env::set_var( @@ -533,7 +533,7 @@ async fn publish_variant_priority_constructor_over_env() { unsafe { // Set environment variables env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var( TRUNK_REPO_URL_ENV, @@ -603,7 +603,7 @@ fn set_up_summary_output_file(temp_dir: &TempDir, host: &str) -> PathBuf { let summary_path = temp_dir.path().join("summary.json"); unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "0"); env::set_var(TRUNK_SUMMARY_OUTPUT_FILE_ENV, &summary_path); @@ -744,7 +744,7 @@ async fn publish_writes_an_error_to_summary_output_file() { let state = MockServerBuilder::new().spawn_mock_server().await; unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var(TRUNK_REPO_ROOT_ENV, temp_dir.path()); env::set_var(TRUNK_SUMMARY_OUTPUT_FILE_ENV, &summary_path); diff --git a/test_report/tests/quarantine.rs b/test_report/tests/quarantine.rs index d778512e..db4abb39 100644 --- a/test_report/tests/quarantine.rs +++ b/test_report/tests/quarantine.rs @@ -128,7 +128,7 @@ async fn quarantine_variant_impacts_quarantining() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var(TRUNK_USE_UNCLONED_REPO_ENV, "false"); env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "0"); @@ -409,7 +409,7 @@ async fn quarantine_disk_cache() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); } let (test_1_is_quarantined, test_3_is_quarantined) = thread::spawn(|| { @@ -563,7 +563,7 @@ async fn quarantine_disabled_for_repo() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "0"); } @@ -629,7 +629,7 @@ async fn quarantine_lookup_failed_when_endpoint_fails() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_QUARANTINED_TESTS_DISK_CACHE_TTL_SECS_ENV, "0"); env::set_var(TRUNK_API_CLIENT_RETRY_COUNT_ENV, "0"); } @@ -736,7 +736,7 @@ async fn v2_matches_a_test_by_its_computed_test_id() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var( TRUNK_REPO_URL_ENV, @@ -785,7 +785,7 @@ async fn makes_no_calls_without_a_test_collection_id() { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); env::set_var( TRUNK_REPO_URL_ENV, @@ -818,3 +818,61 @@ async fn makes_no_calls_without_a_test_collection_id() { assert!(!published); assert!(state.requests.lock().unwrap().is_empty()); } + +#[tokio::test(flavor = "multi_thread")] +#[serial] +async fn makes_no_calls_with_an_organization_token() { + cleanup_env_vars(); + let temp_dir = tempdir().unwrap(); + setup_quarantine_disk_cache_dir(&temp_dir); + let _ = env::set_current_dir(&temp_dir); + + let state = MockServerBuilder::new().spawn_mock_server().await; + + unsafe { + env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, &state.host); + env::set_var(TRUNK_API_TOKEN_ENV, "0123456789abcdef0123456789abcdef"); + env::set_var(TRUNK_ORG_URL_SLUG_ENV, "test-org"); + env::set_var( + TRUNK_REPO_URL_ENV, + "https://github.com/test-org/test-repo.git", + ); + env::set_var(TRUNK_USE_UNCLONED_REPO_ENV, "true"); + env::set_var(TRUNK_REPO_HEAD_SHA_ENV, ""); + env::set_var(TRUNK_REPO_HEAD_BRANCH_ENV, ""); + env::set_var(TRUNK_REPO_HEAD_AUTHOR_NAME_ENV, ""); + } + + let (result, published) = thread::spawn(|| { + let test_report = MutTestReport::new("test".into(), "test-command".into(), None); + let result = test_report.is_quarantined( + None, + Some("quarantined".to_string()), + Some("test-parent-name".to_string()), + None, + None, + ); + (result, test_report.publish()) + }) + .join() + .unwrap(); + + assert!(!result.test_is_quarantined); + assert!(!result.quarantine_lookup_failed); + assert!(!published); + assert!(state.requests.lock().unwrap().is_empty()); +} + +#[test] +fn refuses_only_an_organization_token() { + use test_report::report::token_refusal; + + let refusal = token_refusal(String::from("0123456789abcdef0123456789abcdef")).unwrap(); + assert!( + refusal.contains("analytics-cli 1.0.0 needs a v2 API key"), + "{refusal}" + ); + for token in ["trunk_v2_AbC123", "eyJhbGciOiJSUzI1NiJ9.eyJleHAiOjF9.c2ln"] { + assert_eq!(token_refusal(String::from(token)), None, "{token}"); + } +} diff --git a/test_report/tests/telemetry.rs b/test_report/tests/telemetry.rs index 0189c312..c2a77740 100644 --- a/test_report/tests/telemetry.rs +++ b/test_report/tests/telemetry.rs @@ -32,7 +32,7 @@ fn assert_telemetry_quarantine_query_result(requests: &[RequestPayload], expecte fn set_mock_api_env(api_host: &str) { unsafe { env::set_var(TRUNK_PUBLIC_API_ADDRESS_ENV, api_host); - env::set_var(TRUNK_API_TOKEN_ENV, "test-token"); + env::set_var(TRUNK_API_TOKEN_ENV, "trunk_v2_test-token"); } } From 18022311f624ef04f1ffb05bebf758f7ad69de2c Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Thu, 12 Nov 2026 20:42:52 +0000 Subject: [PATCH 2/6] docs(cli): describe the JWT that `trunk flakytests upload` passes (TRUNK-23916) `trunk login --api-key` was canceled. A Trunk JWT now reaches analytics-cli from `trunk flakytests upload`, exchanged from a key stored with `trunk auth api-key set`, and can have about a minute left, so the help text, the expired-JWT message and the docs point there instead of telling users to mint a 15-minute JWT themselves. Co-Authored-By: Claude Opus 5.5 --- CONTRIBUTING.md | 2 +- README.md | 2 +- api/src/client/mod.rs | 5 ++--- api/src/client/v2.rs | 2 +- api/src/credential.rs | 2 +- api/tests/v2_client.rs | 2 +- cli/src/upload_command.rs | 2 +- cli/tests/upload.rs | 2 +- rspec-trunk-flaky-tests/README.md | 2 +- 9 files changed, 10 insertions(+), 11 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 20f4461b..05472bf4 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -55,7 +55,7 @@ cargo build ./target/debug/trunk-analytics-cli upload --org-url-slug=trunk-io --token=${API_TOKEN} --test-collection-id=${TEST_COLLECTION_ID} --junit-paths=junit.xml ``` -`API_TOKEN` is a v2 API key (`trunk_v2_...`) or a Trunk JWT from `trunk login --api-key`; the CLI refuses an organization token before any call. See [Credentials](README.md#credentials). +`API_TOKEN` is a v2 API key (`trunk_v2_...`) or a Trunk JWT, such as `trunk flakytests upload` passes; the CLI refuses an organization token before any call. See [Credentials](README.md#credentials). You can generate sample junit files by running diff --git a/README.md b/README.md index 983c9c49..9d280ce4 100644 --- a/README.md +++ b/README.md @@ -23,7 +23,7 @@ This is downloaded automatically using the [Trunk launcher][launcher] and when u `--token` / `TRUNK_API_TOKEN` takes one of two credentials. The CLI reads which one before it calls Trunk: - **A v2 API key** (`trunk_v2_...`). Create one in the Trunk web app under Settings → Developer → API Keys, and give it the **Upload test results** (`upload_test_results`) permission. The CLI sends it as `Authorization: Bearer`. -- **A Trunk JWT**, such as `trunk login --api-key` prints. The CLI sends it on `x-trunk-token`. It lasts 15 minutes, so mint it in the step right before the upload, not at the start of the job: a long `trunk-analytics-cli test` run, or a test-runner plugin that looks up quarantined tests early and uploads at the end, can outlive it. The [analytics-uploader][uploader] v3 does this for you. When Trunk refuses an expired JWT, the CLI says so; it does not refresh it. +- **A Trunk JWT**. The CLI sends it on `x-trunk-token`. With a key stored by `trunk auth api-key set`, `trunk flakytests upload` exchanges it for a JWT just before the upload and passes that in `TRUNK_API_TOKEN`, while `trunk flakytests test` passes the key itself. A JWT lasts at most 15 minutes, and the one `trunk flakytests upload` passes can have about a minute left, so use a JWT only for the upload itself: a long `trunk-analytics-cli test` run, or a test-runner plugin that looks up quarantined tests early and uploads at the end, can outlive it, so give those a v2 API key. When Trunk refuses an expired JWT, the CLI says so; it does not refresh it. analytics-cli 1.0.0 refuses any other token, the organization token included, before it calls Trunk. Pin analytics-cli 0.17.x to keep using the organization token. diff --git a/api/src/client/mod.rs b/api/src/client/mod.rs index 1f86defc..5d5d77aa 100644 --- a/api/src/client/mod.rs +++ b/api/src/client/mod.rs @@ -62,8 +62,8 @@ pub struct ApiClient { github_fork_pull_request: Option, /// Why a fork pull request's login failed, for the error every later call returns. github_fork_login_failure: Option, - /// The run's API token, for the CLI telemetry route; `None` without one (a fork pull request - /// has no token, and the gateway refuses its GitHub Actions login there). + /// The run's v2 API key or Trunk JWT, for the CLI telemetry route; `None` without one (a fork + /// pull request has neither, and the gateway refuses its GitHub Actions login there). telemetry_client: Option, org_url_slug: String, render_sender: Option>, @@ -192,7 +192,6 @@ impl ApiClient { }; let credential_free = allow_forked_pr_uploads && api_token.is_none(); - let mut s3_client_default_headers = HeaderMap::new(); s3_client_default_headers.append( header::CONTENT_TYPE, diff --git a/api/src/client/v2.rs b/api/src/client/v2.rs index 9f16b64c..38333ea1 100644 --- a/api/src/client/v2.rs +++ b/api/src/client/v2.rs @@ -139,7 +139,7 @@ impl ApiClient { _ => String::new(), }; format!( - "Trunk did not accept this JWT: it is expired or invalid{expired}. A JWT from `trunk login --api-key` lasts 15 minutes, so re-run `trunk login --api-key` right before the upload step (trunk-io/analytics-uploader v3 does), or pass a v2 API key with --token or TRUNK_API_TOKEN instead." + "Trunk did not accept this JWT: it is expired or invalid{expired}. A Trunk JWT lasts at most 15 minutes, and the one `trunk flakytests upload` passes can have about a minute left, so get a new one right before the upload, or pass a v2 API key with --token or TRUNK_API_TOKEN instead." ) } (Some(StatusCode::UNAUTHORIZED), Some(RunCredential::ApiKey)) => String::from( diff --git a/api/src/credential.rs b/api/src/credential.rs index a12a14fc..2aa70176 100644 --- a/api/src/credential.rs +++ b/api/src/credential.rs @@ -13,7 +13,7 @@ pub const API_KEY_PREFIX: &str = "trunk_v2_"; pub enum TokenKind { /// A v2 API key, sent as `Authorization: Bearer`. ApiKey, - /// A Trunk JWT, such as `trunk login --api-key` prints, sent on `x-trunk-token`. Sent as a + /// A Trunk JWT, such as `trunk flakytests upload` passes, sent on `x-trunk-token`. Sent as a /// bearer it would reach the gateway's API key lookup and fail. Jwt, /// Anything else, which 1.0.0 reads as the organization token it no longer takes. diff --git a/api/tests/v2_client.rs b/api/tests/v2_client.rs index 9244eb26..26ebabb4 100644 --- a/api/tests/v2_client.rs +++ b/api/tests/v2_client.rs @@ -899,7 +899,7 @@ async fn says_a_jwt_has_expired_on_a_401() { "{message}" ); for part in [ - "re-run `trunk login --api-key` right before the upload step", + "get a new one right before the upload", "pass a v2 API key with --token or TRUNK_API_TOKEN", ] { assert!(message.contains(part), "{message}"); diff --git a/cli/src/upload_command.rs b/cli/src/upload_command.rs index 233c4e99..180a47ae 100644 --- a/cli/src/upload_command.rs +++ b/cli/src/upload_command.rs @@ -102,7 +102,7 @@ pub struct UploadArgs { long, required_unless_present = "allow_forked_pr_uploads", env = constants::TRUNK_API_TOKEN_ENV, - help = "A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, from Settings → Developer → API Keys in the Trunk web app; or a Trunk JWT from `trunk login --api-key`, which lasts 15 minutes, so mint it right before this step. The organization token is refused: pin analytics-cli 0.17.x to keep using it. Defaults to TRUNK_API_TOKEN env var." + help = "A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, from Settings → Developer → API Keys in the Trunk web app; or a Trunk JWT, which `trunk flakytests upload` sets for you from a key stored with `trunk auth api-key set` and which can have about a minute left, so it suits only the upload itself. The organization token is refused: pin analytics-cli 0.17.x to keep using it. Defaults to TRUNK_API_TOKEN env var." )] pub token: Option, /// Opting in explicitly is what keeps a job whose token secret failed to interpolate failing diff --git a/cli/tests/upload.rs b/cli/tests/upload.rs index c545681f..38a8bbca 100644 --- a/cli/tests/upload.rs +++ b/cli/tests/upload.rs @@ -3695,7 +3695,7 @@ async fn upload_says_a_trunk_jwt_has_expired_on_a_401() { .stdout(predicate::str::contains( "Trunk did not accept this JWT: it is expired or invalid (it expired at 2025-11-11T17:00:00Z).", )) - .stdout(predicate::str::contains("re-run `trunk login --api-key`")); + .stdout(predicate::str::contains("get a new one right before the upload")); } const GITHUB_ACTIONS_LOGIN: &str = r#"{"jwt":"trunk-jwt","expiresAt":"2026-11-06T17:15:00Z","organization":{"id":"o","slug":"acme","name":"Acme"},"repository":{"id":"r","name":"acme/web"}}"#; diff --git a/rspec-trunk-flaky-tests/README.md b/rspec-trunk-flaky-tests/README.md index 068889f1..302bfb86 100644 --- a/rspec-trunk-flaky-tests/README.md +++ b/rspec-trunk-flaky-tests/README.md @@ -106,7 +106,7 @@ require 'trunk_spec_helper' ### Environment Variables -For a complete list of environment variables that the gem accepts, see [`lib/trunk_spec_helper.rb`](lib/trunk_spec_helper.rb). The gem uses the same environment variables as the Trunk Analytics CLI for configuration overrides. `TRUNK_ORG_URL_SLUG` and `TRUNK_API_TOKEN` must be set to activate the plugin, and `TRUNK_TEST_COLLECTION_ID` too: with the first two set and no test collection, the plugin prints a warning and stays off. `TRUNK_API_TOKEN` must be a v2 API key with the Upload test results permission, or a Trunk JWT from `trunk login --api-key` minted right before the run (it lasts 15 minutes); with an organization token, the plugin prints a warning and stays off. See [Credentials](../README.md#credentials). +For a complete list of environment variables that the gem accepts, see [`lib/trunk_spec_helper.rb`](lib/trunk_spec_helper.rb). The gem uses the same environment variables as the Trunk Analytics CLI for configuration overrides. `TRUNK_ORG_URL_SLUG` and `TRUNK_API_TOKEN` must be set to activate the plugin, and `TRUNK_TEST_COLLECTION_ID` too: with the first two set and no test collection, the plugin prints a warning and stays off. `TRUNK_API_TOKEN` must be a v2 API key with the Upload test results permission, or a Trunk JWT that outlasts the run (one lasts at most 15 minutes); with an organization token, the plugin prints a warning and stays off. See [Credentials](../README.md#credentials). #### `TRUNK_QUARANTINE_QUERY_FAILURE_EXIT` From a1dd4a8312cd07eb24b9f2a39ee1e7ee28fcf966 Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Fri, 13 Nov 2026 19:08:12 +0000 Subject: [PATCH 3/6] docs(rspec): drop the canceled `trunk login --api-key` from the token comment TRUNK-23371 is canceled; the JWT now comes from `trunk flakytests upload`, and a gem run should use a v2 API key (see the README's Credentials). Co-Authored-By: Claude Opus 5.5 --- rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb index e46bf527..297102ad 100644 --- a/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb +++ b/rspec-trunk-flaky-tests/lib/trunk_spec_helper.rb @@ -8,7 +8,7 @@ # Required environment variables: # TRUNK_ORG_URL_SLUG - Your organization's URL slug # TRUNK_API_TOKEN - A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, -# or a Trunk JWT from `trunk login --api-key`; the organization token is refused +# or a Trunk JWT that outlasts the run; the organization token is refused # TRUNK_TEST_COLLECTION_ID - The ID of the test collection to upload to # # Optional environment variables for repository metadata: From 639c348b12920aa37732d31454fdc28d319746a4 Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Mon, 16 Nov 2026 23:06:58 +0000 Subject: [PATCH 4/6] ci: run the smoke and gem tests with the v2 API key secrets (TRUNK-23149) analytics-cli 1.x refuses the organization token, so every step that runs the built CLI or the rspec gem now uses TRUNK_PROD_API_V2_TOKEN and TRUNK_STAGING_API_V2_TOKEN. The Dynamic CI filter steps keep TRUNK_PROD_ORG_API_TOKEN: they don't run analytics-cli. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/pull_request.yml | 6 +++--- .github/workflows/pyo3.yml | 8 ++++---- .github/workflows/release_cli.yml | 8 ++++---- .github/workflows/release_ruby_gem.yml | 6 +++--- .github/workflows/ruby.yml | 16 ++++++++-------- .github/workflows/smoke_test.yml | 8 ++++---- .github/workflows/smoke_test_main.yml | 12 ++++++------ 7 files changed, 32 insertions(+), 32 deletions(-) diff --git a/.github/workflows/pull_request.yml b/.github/workflows/pull_request.yml index 678e70b2..5e34152d 100644 --- a/.github/workflows/pull_request.yml +++ b/.github/workflows/pull_request.yml @@ -269,7 +269,7 @@ jobs: target/${{ matrix.platform.target }}/release/trunk-analytics-cli upload \ --junit-paths ${{ github.workspace }}/target/**/*junit.xml \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-process-exit-code ${{ steps.extract.outputs.test-step-outcome }} \ --test-collection-id 2tYJWMu7 \ --variant ${{ matrix.platform.target }} @@ -286,7 +286,7 @@ jobs: target/${{ matrix.platform.target }}/release/trunk-analytics-cli upload \ --junit-paths ${{ github.workspace }}/target/**/*junit.xml \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-process-exit-code ${{ steps.extract.outputs.test-step-outcome }} \ --test-collection-id Gtnr7BWl \ --variant ${{ matrix.platform.target }} @@ -302,7 +302,7 @@ jobs: target/${{ matrix.platform.target }}/release/trunk-analytics-cli upload \ --junit-paths ${{ github.workspace }}/target/**/*junit.xml \ --org-url-slug trunk \ - --token ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} \ --test-collection-id EWhNPVic \ --variant ${{ matrix.platform.target }} # analytics-cli-pr: https://app.trunk.io/trunk/flaky-tests/collections/EWhNPVic/monitors diff --git a/.github/workflows/pyo3.yml b/.github/workflows/pyo3.yml index 4639516a..3deb2c04 100644 --- a/.github/workflows/pyo3.yml +++ b/.github/workflows/pyo3.yml @@ -179,7 +179,7 @@ jobs: ./target/debug/trunk-analytics-cli upload \ --junit-paths context-py/junit.xml \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-collection-id 6E6WINjl \ --variant ${{ matrix.platform.target }} env: @@ -192,7 +192,7 @@ jobs: ./target/debug/trunk-analytics-cli upload \ --junit-paths context-py/junit.xml \ --org-url-slug trunk \ - --token ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} \ --test-collection-id 68IdP3pq \ --variant ${{ matrix.platform.target }} env: @@ -225,7 +225,7 @@ jobs: ./target/debug/trunk-analytics-cli upload \ --junit-paths context-py/junit.xml \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-collection-id 6E6WINjl \ --variant ${{ matrix.platform.target }} env: @@ -238,7 +238,7 @@ jobs: ./target/debug/trunk-analytics-cli upload \ --junit-paths context-py/junit.xml \ --org-url-slug trunk \ - --token ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} \ --test-collection-id 68IdP3pq \ --variant ${{ matrix.platform.target }} env: diff --git a/.github/workflows/release_cli.yml b/.github/workflows/release_cli.yml index 80d24030..b54cdf2d 100644 --- a/.github/workflows/release_cli.yml +++ b/.github/workflows/release_cli.yml @@ -264,16 +264,16 @@ jobs: uses: ./.github/actions/perform_smoke_test with: cli-binary-location: trunk-analytics-cli/trunk-analytics-cli - staging-api-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} - production-api-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + staging-api-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} + production-api-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} - name: Run Smoke Tests (Windows) if: ${{ matrix.type.name == 'windows' }} uses: ./.github/actions/perform_smoke_test with: cli-binary-location: trunk-analytics-cli/trunk-analytics-cli.exe - staging-api-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} - production-api-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + staging-api-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} + production-api-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} tag_and_release: name: Tag and Release [ ${{ github.event.inputs.release_tag || inputs.release_tag }} ] diff --git a/.github/workflows/release_ruby_gem.yml b/.github/workflows/release_ruby_gem.yml index cf9714db..19329ac9 100644 --- a/.github/workflows/release_ruby_gem.yml +++ b/.github/workflows/release_ruby_gem.yml @@ -134,7 +134,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: ${{ matrix.ruby-version }} - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org test-collection-id: T5yKSn9h @@ -147,7 +147,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: ${{ matrix.ruby-version }} - trunk-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk.io trunk-org-slug: trunk test-collection-id: BiBP2neA @@ -178,7 +178,7 @@ jobs: with: alpine: "true" ruby-version: ${{ matrix.ruby-version }} - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org test-collection-id: T5yKSn9h diff --git a/.github/workflows/ruby.yml b/.github/workflows/ruby.yml index e3993627..9dc975cc 100644 --- a/.github/workflows/ruby.yml +++ b/.github/workflows/ruby.yml @@ -68,7 +68,7 @@ jobs: run: | ./target/debug/trunk-analytics-cli upload \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-reports ./upload_bundle/trunk_output.bin \ --test-collection-id T5yKSn9h env: @@ -79,7 +79,7 @@ jobs: run: | ./target/debug/trunk-analytics-cli upload \ --org-url-slug trunk \ - --token ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} \ --test-reports ./upload_bundle/trunk_output.bin \ --test-collection-id BiBP2neA env: @@ -90,7 +90,7 @@ jobs: run: | ./target/debug/trunk-analytics-cli upload \ --org-url-slug trunk-staging-org \ - --token ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} \ --test-reports ./upload_bundle/trunk_output.bin \ --test-collection-id z2dzjLHm env: @@ -101,7 +101,7 @@ jobs: run: | ./target/debug/trunk-analytics-cli upload \ --org-url-slug trunk \ - --token ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} \ + --token ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} \ --test-reports ./upload_bundle/trunk_output.bin \ --test-collection-id 4B2wfoEU env: @@ -199,7 +199,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org test-collection-id: T5yKSn9h @@ -211,7 +211,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk.io trunk-org-slug: trunk test-collection-id: BiBP2neA @@ -223,7 +223,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org test-collection-id: z2dzjLHm @@ -235,7 +235,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk.io trunk-org-slug: trunk test-collection-id: 4B2wfoEU diff --git a/.github/workflows/smoke_test.yml b/.github/workflows/smoke_test.yml index 82adc4ce..3fff5e6e 100644 --- a/.github/workflows/smoke_test.yml +++ b/.github/workflows/smoke_test.yml @@ -122,8 +122,8 @@ jobs: uses: ./.github/actions/perform_smoke_test with: cli-binary-location: ${{ matrix.platform.binary-name }} - staging-api-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} - production-api-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + staging-api-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} + production-api-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} environment-type: ${{ (github.event.action == 'production-release' && 'production') || (github.event.action == 'staging-release' && 'staging') || 'both' }} download_ruby_gem: @@ -170,7 +170,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org # analytics-cli-ruby-gem: https://app.trunk-staging.io/trunk-staging-org/flaky-tests/collections/T5yKSn9h/monitors @@ -197,7 +197,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "3.4" - trunk-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk.io trunk-org-slug: trunk # analytics-cli-ruby-gem: https://app.trunk.io/trunk/flaky-tests/collections/BiBP2neA/monitors diff --git a/.github/workflows/smoke_test_main.yml b/.github/workflows/smoke_test_main.yml index 5cf467ff..203ad9aa 100644 --- a/.github/workflows/smoke_test_main.yml +++ b/.github/workflows/smoke_test_main.yml @@ -128,8 +128,8 @@ jobs: uses: ./.github/actions/perform_smoke_test with: cli-binary-location: ${{ matrix.platform.binary-name }} - staging-api-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} - production-api-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + staging-api-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} + production-api-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} environment-type: ${{ (github.event.action == 'production-release' && 'production') || (github.event.action == 'staging-release' && 'staging') || 'both' }} test-variants: "true" @@ -137,8 +137,8 @@ jobs: uses: ./.github/actions/perform_gitlab_smoke_test with: cli-binary-location: ${{ matrix.platform.binary-name }} - staging-api-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} - production-api-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + staging-api-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} + production-api-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} environment-type: ${{ (github.event.action == 'production-release' && 'production') || (github.event.action == 'staging-release' && 'staging') || 'both' }} build_ruby_gem: @@ -223,7 +223,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "4.0" - trunk-token: ${{ secrets.TRUNK_STAGING_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_STAGING_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk-staging.io trunk-org-slug: trunk-staging-org # analytics-cli-ruby-gem: https://app.trunk-staging.io/trunk-staging-org/flaky-tests/collections/T5yKSn9h/monitors @@ -253,7 +253,7 @@ jobs: uses: ./.github/actions/test_ruby_gem_uploads with: ruby-version: "4.0" - trunk-token: ${{ secrets.TRUNK_PROD_ORG_API_TOKEN }} + trunk-token: ${{ secrets.TRUNK_PROD_API_V2_TOKEN }} trunk-public-api-address: https://api.trunk.io trunk-org-slug: trunk # analytics-cli-ruby-gem: https://app.trunk.io/trunk/flaky-tests/collections/BiBP2neA/monitors From c60c650eaa682cc9cf4c7c3ce37ad7dad0dfd8d7 Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Tue, 17 Nov 2026 13:25:06 +0000 Subject: [PATCH 5/6] feat(cli): link to the API keys page and date the 0.17.x organization-token pin (TRUNK-23149) Review follow-up. Messages, the --token help and the README link to https://app.trunk.io/settings/developer/api-tokens, which the web app sends to the viewer's org. The organization token keeps working on 0.17.x until December 1, 2027. Co-Authored-By: Claude Opus 5.5 --- README.md | 4 ++-- api/src/client/mod.rs | 4 ++-- api/src/client/v2.rs | 4 ++-- api/src/credential.rs | 8 ++++---- api/tests/v2_client.rs | 6 +++--- cli/src/upload_command.rs | 2 +- cli/tests/upload.rs | 6 +++--- rspec-trunk-flaky-tests/test/token_spec.rb | 4 ++-- 8 files changed, 19 insertions(+), 19 deletions(-) diff --git a/README.md b/README.md index 9d280ce4..fe0800bd 100644 --- a/README.md +++ b/README.md @@ -22,10 +22,10 @@ This is downloaded automatically using the [Trunk launcher][launcher] and when u `--token` / `TRUNK_API_TOKEN` takes one of two credentials. The CLI reads which one before it calls Trunk: -- **A v2 API key** (`trunk_v2_...`). Create one in the Trunk web app under Settings → Developer → API Keys, and give it the **Upload test results** (`upload_test_results`) permission. The CLI sends it as `Authorization: Bearer`. +- **A v2 API key** (`trunk_v2_...`). Create one at https://app.trunk.io/settings/developer/api-tokens, and give it the **Upload test results** (`upload_test_results`) permission. The CLI sends it as `Authorization: Bearer`. - **A Trunk JWT**. The CLI sends it on `x-trunk-token`. With a key stored by `trunk auth api-key set`, `trunk flakytests upload` exchanges it for a JWT just before the upload and passes that in `TRUNK_API_TOKEN`, while `trunk flakytests test` passes the key itself. A JWT lasts at most 15 minutes, and the one `trunk flakytests upload` passes can have about a minute left, so use a JWT only for the upload itself: a long `trunk-analytics-cli test` run, or a test-runner plugin that looks up quarantined tests early and uploads at the end, can outlive it, so give those a v2 API key. When Trunk refuses an expired JWT, the CLI says so; it does not refresh it. -analytics-cli 1.0.0 refuses any other token, the organization token included, before it calls Trunk. Pin analytics-cli 0.17.x to keep using the organization token. +analytics-cli 1.0.0 refuses any other token, the organization token included, before it calls Trunk. Pin analytics-cli 0.17.x to keep using the organization token until December 1, 2027. If Trunk answers `403` because the key lacks the Upload test results permission, edit the key on the API Keys screen to add it. diff --git a/api/src/client/mod.rs b/api/src/client/mod.rs index 5d5d77aa..11d2ba93 100644 --- a/api/src/client/mod.rs +++ b/api/src/client/mod.rs @@ -378,8 +378,8 @@ pub(crate) enum CheckUnauthorized { } pub(crate) const UNAUTHORIZED_CONTEXT: &str = concat!( - "Your Trunk API key may be incorrect - find it in the Trunk app ", - "(Settings -> Developer -> API Keys).", + "Your Trunk API key may be incorrect - check it at ", + "https://app.trunk.io/settings/developer/api-tokens.", ); /// The API's explanation of a client error, kept in the error chain so the error report can diff --git a/api/src/client/v2.rs b/api/src/client/v2.rs index 38333ea1..a7ff7513 100644 --- a/api/src/client/v2.rs +++ b/api/src/client/v2.rs @@ -13,7 +13,7 @@ use crate::quarantine::{QuarantineLookup, QuarantinedTestSet}; use crate::urls::TestCaseGuidScope; const INSUFFICIENT_PERMISSIONS: &str = "INSUFFICIENT_PERMISSIONS"; -const MISSING_UPLOAD_PERMISSION: &str = "lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys."; +const MISSING_UPLOAD_PERMISSION: &str = "lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key at https://app.trunk.io/settings/developer/api-tokens."; impl ApiClient { const QUARANTINE_STATUS_MAX_IDENTIFIERS: usize = 2_000; @@ -143,7 +143,7 @@ impl ApiClient { ) } (Some(StatusCode::UNAUTHORIZED), Some(RunCredential::ApiKey)) => String::from( - "Trunk did not accept this API key: it may be mistyped, revoked or deleted. Check it in the Trunk web app under Settings → Developer → API Keys.", + "Trunk did not accept this API key: it may be mistyped, revoked or deleted. Check it at https://app.trunk.io/settings/developer/api-tokens.", ), _ => return error, }; diff --git a/api/src/credential.rs b/api/src/credential.rs index 2aa70176..a648ffbc 100644 --- a/api/src/credential.rs +++ b/api/src/credential.rs @@ -70,10 +70,10 @@ impl fmt::Display for LegacyTokenRefused { impl std::error::Error for LegacyTokenRefused {} pub const LEGACY_TOKEN_MESSAGE: &str = concat!( - "This Trunk token is not a v2 API key. analytics-cli 1.0.0 needs a v2 API key: create one in ", - "the Trunk web app under Settings → Developer → API Keys, give it the Upload test results ", - "permission, and pass it with --token or TRUNK_API_TOKEN. To keep using your organization ", - "token, pin analytics-cli 0.17.x.", + "This Trunk token is not a v2 API key. analytics-cli 1.0.0 needs a v2 API key: create one at ", + "https://app.trunk.io/settings/developer/api-tokens, give it the Upload test results permission, and pass it ", + "with --token or TRUNK_API_TOKEN. To keep using your organization token until December 1, 2027, ", + "pin analytics-cli 0.17.x.", ); #[cfg(test)] diff --git a/api/tests/v2_client.rs b/api/tests/v2_client.rs index 26ebabb4..4d0fa4d8 100644 --- a/api/tests/v2_client.rs +++ b/api/tests/v2_client.rs @@ -806,9 +806,9 @@ fn refuses_an_organization_token_before_any_call() { let message = error.to_string(); for part in [ "analytics-cli 1.0.0 needs a v2 API key", - "Settings → Developer → API Keys", + "https://app.trunk.io/settings/developer/api-tokens", "Upload test results", - "pin analytics-cli 0.17.x", + "until December 1, 2027, pin analytics-cli 0.17.x", ] { assert!(message.contains(part), "{message}"); } @@ -871,7 +871,7 @@ async fn names_the_missing_upload_permission_on_a_403() { assert!(message.starts_with(refused), "{message}"); assert!( message.contains( - "the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys." + "the Upload test results (`upload_test_results`) permission group. Grant it by editing the key at https://app.trunk.io/settings/developer/api-tokens." ), "{message}" ); diff --git a/cli/src/upload_command.rs b/cli/src/upload_command.rs index 180a47ae..857ce38c 100644 --- a/cli/src/upload_command.rs +++ b/cli/src/upload_command.rs @@ -102,7 +102,7 @@ pub struct UploadArgs { long, required_unless_present = "allow_forked_pr_uploads", env = constants::TRUNK_API_TOKEN_ENV, - help = "A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, from Settings → Developer → API Keys in the Trunk web app; or a Trunk JWT, which `trunk flakytests upload` sets for you from a key stored with `trunk auth api-key set` and which can have about a minute left, so it suits only the upload itself. The organization token is refused: pin analytics-cli 0.17.x to keep using it. Defaults to TRUNK_API_TOKEN env var." + help = "A Trunk v2 API key (trunk_v2_...) with the Upload test results permission, created at https://app.trunk.io/settings/developer/api-tokens; or a Trunk JWT, which `trunk flakytests upload` sets for you from a key stored with `trunk auth api-key set` and which can have about a minute left, so it suits only the upload itself. The organization token is refused: pin analytics-cli 0.17.x to keep using it until December 1, 2027. Defaults to TRUNK_API_TOKEN env var." )] pub token: Option, /// Opting in explicitly is what keeps a job whose token secret failed to interpolate failing diff --git a/cli/tests/upload.rs b/cli/tests/upload.rs index 38a8bbca..d1755b29 100644 --- a/cli/tests/upload.rs +++ b/cli/tests/upload.rs @@ -3637,9 +3637,9 @@ async fn refuses_an_organization_token_before_any_call( ); for part in [ "This Trunk token is not a v2 API key. analytics-cli 1.0.0 needs a v2 API key", - "Settings → Developer → API Keys", + "https://app.trunk.io/settings/developer/api-tokens", "Upload test results", - "pin analytics-cli 0.17.x", + "until December 1, 2027, pin analytics-cli 0.17.x", ] { assert!(output.contains(part), "{part:?} missing from {output}"); } @@ -3673,7 +3673,7 @@ async fn upload_names_the_missing_upload_permission_on_a_403() { .assert() .code(ExitCode::Error.code()) .stdout(predicate::str::contains( - "Trunk refused this API key: it lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key in the Trunk web app under Settings → Developer → API Keys.", + "Trunk refused this API key: it lacks the Upload test results (`upload_test_results`) permission group. Grant it by editing the key at https://app.trunk.io/settings/developer/api-tokens.", )); } diff --git a/rspec-trunk-flaky-tests/test/token_spec.rb b/rspec-trunk-flaky-tests/test/token_spec.rb index db56fd80..283445f4 100644 --- a/rspec-trunk-flaky-tests/test/token_spec.rb +++ b/rspec-trunk-flaky-tests/test/token_spec.rb @@ -33,9 +33,9 @@ def load_gem(token) expect(status).to be_success, stderr expect(stdout.lines.last.strip).to eq('disabled') expect(stderr).to include('Trunk Flaky Tests is disabled: This Trunk token is not a v2 API key.') - expect(stderr).to include('Settings → Developer → API Keys') + expect(stderr).to include('https://app.trunk.io/settings/developer/api-tokens') expect(stderr).to include('Upload test results') - expect(stderr).to include('pin analytics-cli 0.17.x') + expect(stderr).to include('until December 1, 2027, pin analytics-cli 0.17.x') end end From d7241dcd734a6c66ef77677cb57c16573ae6a83f Mon Sep 17 00:00:00 2001 From: Tyler Beebe Date: Tue, 17 Nov 2026 13:29:00 +0000 Subject: [PATCH 6/6] ci(release): install qemu-user-binfmt for the ppc64le and s390x smoke tests ubuntu-latest is now ubuntu-26.04, where qemu-user-static is a virtual package with no installation candidate, so apt-get exits 100 before the smoke test runs. qemu-user-binfmt provides the user-mode emulators and registers binfmt_misc on both 24.04 and 26.04. Co-Authored-By: Claude Opus 5.5 --- .github/workflows/release_cli.yml | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/.github/workflows/release_cli.yml b/.github/workflows/release_cli.yml index b54cdf2d..d73764d6 100644 --- a/.github/workflows/release_cli.yml +++ b/.github/workflows/release_cli.yml @@ -250,7 +250,7 @@ jobs: shell: bash run: | sudo apt-get update - sudo apt-get install -y qemu-user-static binfmt-support \ + sudo apt-get install -y qemu-user-binfmt \ libc6-${{ matrix.type.qemu-deb-arch }}-cross libgcc-s1-${{ matrix.type.qemu-deb-arch }}-cross echo "QEMU_LD_PREFIX=/usr/${{ matrix.type.qemu-triple }}" >> "$GITHUB_ENV" chmod +x trunk-analytics-cli/trunk-analytics-cli