Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
98 changes: 74 additions & 24 deletions api/src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -4,7 +4,7 @@ use std::sync::mpsc::Sender;

use constants::{DEFAULT_ORIGIN, TRUNK_PUBLIC_API_ADDRESS_ENV};
use display::message::DisplayMessage;
use http::{HeaderValue, header::HeaderMap};
use http::{HeaderName, HeaderValue, header::HeaderMap};
use reqwest::{Client, Response, StatusCode, header};
use serde::de::DeserializeOwned;
use tokio::fs;
Expand Down Expand Up @@ -53,8 +53,13 @@ pub struct ApiClient {
pub telemetry_host: String,
s3_client: Client,
trunk_api_client: Client,
/// Only a client holding an API token can call v2, which takes it as a bearer credential.
/// Holds the run's v2 credential: an API token from the start, or a fork pull request's Trunk
/// login once it has one.
trunk_api_v2_client: Option<Client>,
/// `--allow-forked-pr-uploads` with no token and no public repo id.
credential_free: bool,
/// A credential-free GitHub Actions fork PR run, which on v2 swaps its `GITHUB_TOKEN` for a login.
github_fork_pull_request: Option<GithubForkPullRequest>,
telemetry_client: Client,
version_path_prefix: String,
org_url_slug: String,
Expand All @@ -63,6 +68,53 @@ pub struct ApiClient {
missing_test_collection: bool,
}

/// A GitHub Actions run of a pull request from a fork, as its event payload names it.
pub struct GithubForkPullRequest {
pub number: u64,
/// `pull_request.head.sha`, not `GITHUB_SHA`, which is the merge commit on a PR run.
pub head_sha: String,
/// The job's `GITHUB_TOKEN`, when the step was given it.
pub github_token: Option<String>,
}

/// What a v2 call presents. The gateway takes only an API key on `Authorization`, so a Trunk login
/// goes on `x-trunk-token` and the two are never sent together.
pub(crate) enum V2Credential {
ApiKey(String),
TrunkLogin(String),
}

impl V2Credential {
const TRUNK_TOKEN_HEADER: HeaderName = HeaderName::from_static("x-trunk-token");

fn client(&self) -> anyhow::Result<Client> {
let (name, value, error) = match self {
Self::ApiKey(token) => (
header::AUTHORIZATION,
format!("Bearer {token}"),
"Trunk API token is not ASCII",
),
Self::TrunkLogin(jwt) => (
Self::TRUNK_TOKEN_HEADER,
jwt.clone(),
"Trunk login is not ASCII",
),
};
let mut value = HeaderValue::from_str(&value).map_err(|_| anyhow::Error::msg(error))?;
value.set_sensitive(true);
let mut headers = HeaderMap::new();
headers.append(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
headers.append(name, value);
Ok(Client::builder()
.timeout(ApiClient::TRUNK_API_TIMEOUT)
.default_headers(headers)
.build()?)
}
}

pub fn get_api_host() -> String {
std::env::var(TRUNK_PUBLIC_API_ADDRESS_ENV)
.ok()
Expand Down Expand Up @@ -94,8 +146,8 @@ impl ApiClient {
// request. Fork PRs can't read repo secrets, so they rely on the public repo id.
let api_token = api_token.filter(|token| !token.trim().is_empty());
let public_repo_id = public_repo_id.filter(|id| !id.trim().is_empty());
// The forked-PR lane presents no credential at all: the collection id already in the
// request authorizes it, gated on that collection's opt-in server-side.
// The forked-PR lane presents no credential at all on v1: the collection id already in
// the request authorizes it, gated on that collection's opt-in server-side.
if api_token.is_none() && public_repo_id.is_none() && !allow_forked_pr_uploads {
return Err(anyhow::anyhow!(
"Either a Trunk API token, a public repo id, or --allow-forked-pr-uploads is required."
Expand Down Expand Up @@ -160,25 +212,11 @@ impl ApiClient {
.build()?;

let trunk_api_v2_client = api_token
.as_deref()
.map(|token| {
let mut headers = HeaderMap::new();
headers.append(
header::CONTENT_TYPE,
HeaderValue::from_static("application/json"),
);
headers.append(
header::AUTHORIZATION,
HeaderValue::from_str(&format!("Bearer {token}"))
.map_err(|_| anyhow::Error::msg("Trunk API token is not ASCII"))?,
);
Client::builder()
.timeout(Self::TRUNK_API_TIMEOUT)
.default_headers(headers)
.build()
.map_err(anyhow::Error::from)
})
.clone()
.map(|token| V2Credential::ApiKey(token).client())
.transpose()?;
let credential_free =
allow_forked_pr_uploads && api_token.is_none() && public_repo_id.is_none();

let mut telemetry_client_default_headers = HeaderMap::new();
telemetry_client_default_headers.append(
Expand Down Expand Up @@ -217,6 +255,8 @@ impl ApiClient {
s3_client,
trunk_api_client,
trunk_api_v2_client,
credential_free,
github_fork_pull_request: None,
telemetry_client,
version_path_prefix,
org_url_slug,
Expand All @@ -240,14 +280,22 @@ impl ApiClient {
self.upload_api_version
}

/// Lets a credential-free run of a GitHub Actions fork PR ask for v2. Ignored for any other run.
pub fn set_github_fork_pull_request(&mut self, pull_request: GithubForkPullRequest) {
if self.credential_free {
self.github_fork_pull_request = Some(pull_request);
}
}

/// Which API the run's quarantine lookup and upload use. Every later call follows it.
pub fn set_upload_api_version(&mut self, upload_api_version: UploadApiVersion) {
self.upload_api_version = upload_api_version;
}

/// Asks Trunk whether this run uses v2. Anything but an explicit `v2`, a failed call
/// included, is v1, so this never fails a run. A timeout, a connection error or a 5xx is
/// retried once; any other answer is final.
/// retried once; any other answer is final. Only a run with an API token or a credential-free
/// GitHub Actions fork PR run asks.
///
/// A run that names no collection asks too, by its repository: Trunk may answer `v2` with the
/// collection it inferred. A `v2` that names none is v1, since every v2 route needs one. A `v1`
Expand All @@ -265,7 +313,9 @@ impl ApiClient {
.as_ref()
.and_then(message::named_repo)
.is_some();
let decision = if self.trunk_api_v2_client.is_none() || (requested.is_none() && !has_repo) {
let can_use_v2 =
self.trunk_api_v2_client.is_some() || self.github_fork_pull_request.is_some();
let decision = if !can_use_v2 || (requested.is_none() && !has_repo) {
message::UploadApiDecision::default()
} else {
match self.request_upload_api_version(request).await {
Expand Down
86 changes: 84 additions & 2 deletions api/src/client/v2.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,16 +3,98 @@ use std::collections::HashSet;
use base64::{Engine as _, engine::general_purpose::STANDARD};
use reqwest::{Client, StatusCode};

use super::{ApiClient, CheckNotFound, CheckUnauthorized, StatusCodeHelp, status_code_help};
use super::{
ApiClient, CheckNotFound, CheckUnauthorized, GithubForkPullRequest, StatusCodeHelp,
V2Credential, status_code_help,
};
use crate::call_api::CallApi;
use crate::message;
use crate::message::{self, UploadApiVersion};
use crate::quarantine::{QuarantineLookup, QuarantinedTestSet, QuarantinedTests};
use crate::urls::TestCaseGuidScope;

impl ApiClient {
const QUARANTINE_STATUS_MAX_IDENTIFIERS: usize = 2_000;
const ALL_QUARANTINED_TESTS_PAGE_SIZE: usize = 25_000;

/// Sets the API the run uses, taking the decision route's answer as given. The one exception is a
/// GitHub Actions fork PR run, which holds no v2 credential: on v2 it swaps the job's
/// `GITHUB_TOKEN` for a Trunk login, and stays on v1's collection lane if that fails.
pub async fn use_upload_api_version(&mut self, version: UploadApiVersion) {
self.upload_api_version = match &self.github_fork_pull_request {
Some(pull_request)
if version == UploadApiVersion::V2 && self.trunk_api_v2_client.is_none() =>
{
match self.log_in_github_fork_pull_request(pull_request).await {
Ok(client) => {
self.trunk_api_v2_client = Some(client);
UploadApiVersion::V2
}
Err(reason) => {
tracing::warn!(
"Could not log in to the Trunk API v2 for this fork pull request ({reason}); uploading with Trunk API v1"
);
UploadApiVersion::V1
}
}
}
_ => version,
};
}

/// `POST /v2/auth/github-actions/login` with the job's `GITHUB_TOKEN`. The error is a reason
/// fit to log: it never holds the token, the login, or a parse error that could quote them.
async fn log_in_github_fork_pull_request(
&self,
pull_request: &GithubForkPullRequest,
) -> Result<Client, String> {
let github_token = pull_request
Comment thread
acatxnamedvirtue marked this conversation as resolved.
Resolved
.github_token
.as_deref()
.filter(|token| !token.trim().is_empty())
.ok_or("GITHUB_TOKEN is not set")?;
let mut url = url::Url::parse(&self.api_host).map_err(|error| error.to_string())?;
url.path_segments_mut()
.map_err(|_| format!("invalid Trunk API address {}", self.api_host))?
.pop_if_empty()
.extend(["v2", "auth", "github-actions", "login"]);
let response = Client::builder()
.timeout(Self::TRUNK_API_TIMEOUT)
.build()
.map_err(|error| error.to_string())?
.post(url)
.header(Self::TRUNK_ALLOW_FORKED_PR_UPLOADS_HEADER, "true")
.json(&message::GithubActionsForkLoginRequest {
github_token,
pull_request_number: pull_request.number,
sha: &pull_request.head_sha,
})
.send()
.await
.map_err(|error| format!("the request failed: {}", error.without_url()))?;
let status = response.status();
if status != StatusCode::OK {
let code = response
.json::<message::GithubActionsLoginRefusal>()
.await
.ok()
.and_then(|refusal| refusal.error.code);
return Err(match code {
Some(code) => format!("Trunk answered {status}, {code}"),
None => format!("Trunk answered {status}"),
});
}
let login = response
.json::<message::GithubActionsLogin>()
.await
.ok()
.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)
.client()
.map_err(|error| error.to_string())
}

fn v2_client(&self) -> anyhow::Result<&Client> {
self.trunk_api_v2_client
.as_ref()
Expand Down
22 changes: 22 additions & 0 deletions api/src/message.rs
Original file line number Diff line number Diff line change
Expand Up @@ -129,6 +129,28 @@ pub struct GetUploadApiVersionResponse {
pub missing_test_collection: bool,
}

/// The fork lane's body for `POST /v2/auth/github-actions/login`. It holds the job's
/// `GITHUB_TOKEN`, so it has no `Debug`.
#[derive(Serialize)]
#[serde(rename_all = "camelCase")]
pub struct GithubActionsForkLoginRequest<'a> {
pub github_token: &'a str,
pub pull_request_number: u64,
pub sha: &'a str,
}

#[derive(Deserialize)]
#[serde(rename_all = "camelCase")]
pub struct GithubActionsLogin {
pub jwt: String,
pub expires_at: String,
}

#[derive(Debug, Deserialize)]
pub struct GithubActionsLoginRefusal {
pub error: ClientErrorBody,
}

/// Which API a run uses and, on v2 for a run that named no test collection, the collection Trunk
/// inferred from its repository.
#[derive(Debug, Clone, PartialEq, Eq, Default)]
Expand Down
Loading
Loading