Skip to content

WIP: Trusted Publishing #11056

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Draft
wants to merge 25 commits into
base: main
Choose a base branch
from
Draft
Changes from all commits
Commits
Show all changes
25 commits
Select commit Hold shift + click to select a range
e9c82d0
diesel_helpers: Adjust `lower()` fn to also accept `NULL`
Turbo87 May 8, 2025
34fbba3
trustpub: Implement `load_jwks()` fn
Turbo87 May 7, 2025
609333c
trustpub: Add `GITHUB_ISSUER_URL` constant
Turbo87 May 7, 2025
aaff3cd
trustpub: Add `OidcKeyStore` trait
Turbo87 May 7, 2025
7fb7948
trustpub: Implement `OidcKeyStore` trait
Turbo87 May 7, 2025
7a7cbb9
trustpub: Add mock `OidcKeyStore` implementation
Turbo87 May 7, 2025
81261b0
trustpub: Add RSA keys for testing purposes
Turbo87 May 7, 2025
fe581b6
trustpub: Add `MockOidcKeyStore::with_test_key()` fn
Turbo87 May 8, 2025
0af18d3
trustpub: Implement `extract_workflow_filename()` fn
Turbo87 May 7, 2025
be6ef1c
trustpub: Implement `UnverifiedClaims::decode()` fn
Turbo87 May 7, 2025
eb2381c
trustpub: Implement `GitHubClaims` struct
Turbo87 May 7, 2025
9f21064
trustpub: Implement `FullGitHubClaims` struct for testing purposes
Turbo87 May 8, 2025
688f462
trustpub: Implement `AccessToken` struct
Turbo87 May 8, 2025
11623e3
database: Add `NewUsedJti` data access object
Turbo87 Apr 28, 2025
afb3438
database: Add `NewToken` data access object
Turbo87 Apr 28, 2025
a598135
config: Add `TRUSTPUB_AUDIENCE` setting
Turbo87 May 8, 2025
f06f2d5
App: Add `oidc_key_stores` hashmap
Turbo87 May 7, 2025
11c4dda
AppBuilder: Add `trustpub_providers()` fn
Turbo87 May 8, 2025
713e5fc
bin/server: Use `TRUSTPUB_PROVIDERS` env var to configure Trusted Pub…
Turbo87 May 8, 2025
3afc69d
tests/TestAppBuilder: Add `with_oidc_keystore()` fn
Turbo87 May 7, 2025
95c15f9
Implement `PUT /api/v1/trusted_publishing/tokens` API endpoint
Turbo87 May 8, 2025
203808c
tests/util: Change `MockTokenUser::token` to be optional
Turbo87 Apr 16, 2025
fc5582e
tests/util: Add `MockTokenUser::for_token()` fn
Turbo87 Apr 16, 2025
8dd7179
database/NewVersion: Make `published_by_email` argument of `save()` f…
Turbo87 May 7, 2025
b4310e3
WIP
Turbo87 May 8, 2025
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
52 changes: 52 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

4 changes: 3 additions & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
@@ -111,6 +111,7 @@ paste = "=1.0.15"
postgres-native-tls = "=0.5.1"
prometheus = { version = "=0.14.0", default-features = false }
rand = "=0.9.1"
regex = "=1.11.1"
reqwest = { version = "=0.12.15", features = ["gzip", "json"] }
rss = { version = "=2.0.12", default-features = false, features = ["atom"] }
secrecy = "=0.10.3"
@@ -145,11 +146,12 @@ crates_io_index = { path = "crates/crates_io_index", features = ["testing"] }
crates_io_tarball = { path = "crates/crates_io_tarball", features = ["builder"] }
crates_io_team_repo = { path = "crates/crates_io_team_repo", features = ["mock"] }
crates_io_test_db = { path = "crates/crates_io_test_db" }
crates_io_trustpub = { path = "crates/crates_io_trustpub", features = ["test-helpers"] }
claims = "=0.8.0"
diesel = { version = "=2.2.10", features = ["r2d2"] }
googletest = "=0.14.0"
insta = { version = "=1.43.1", features = ["glob", "json", "redactions"] }
regex = "=1.11.1"
jsonwebtoken = "=9.3.1"
sentry = { version = "=0.38.1", features = ["test"] }
tokio = "=1.45.0"
zip = { version = "=3.0.0", default-features = false, features = ["deflate"] }
3 changes: 2 additions & 1 deletion crates/crates_io_database/src/models/category.rs
Original file line number Diff line number Diff line change
@@ -3,6 +3,7 @@ use crate::schema::*;
use chrono::{DateTime, Utc};
use diesel::dsl;
use diesel::prelude::*;
use diesel::sql_types::Text;
use diesel_async::scoped_futures::ScopedFutureExt;
use diesel_async::{AsyncConnection, AsyncPgConnection, RunQueryDsl};
use futures_util::FutureExt;
@@ -19,7 +20,7 @@ pub struct Category {
pub created_at: DateTime<Utc>,
}

type WithSlug<'a> = dsl::Eq<categories::slug, crates_io_diesel_helpers::lower<&'a str>>;
type WithSlug<'a> = dsl::Eq<categories::slug, crates_io_diesel_helpers::lower<Text, &'a str>>;

#[derive(Associations, Insertable, Identifiable, Debug, Clone, Copy)]
#[diesel(
4 changes: 4 additions & 0 deletions crates/crates_io_database/src/models/trustpub/mod.rs
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
mod github_config;
mod token;
mod used_jti;

pub use self::github_config::{GitHubConfig, NewGitHubConfig};
pub use self::token::NewToken;
pub use self::used_jti::NewUsedJti;
22 changes: 22 additions & 0 deletions crates/crates_io_database/src/models/trustpub/token.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
use crate::schema::trustpub_tokens;
use chrono::{DateTime, Utc};
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};

#[derive(Debug, Insertable)]
#[diesel(table_name = trustpub_tokens, check_for_backend(diesel::pg::Pg))]
pub struct NewToken<'a> {
pub expires_at: DateTime<Utc>,
pub hashed_token: &'a [u8],
pub crate_ids: &'a [i32],
}

impl NewToken<'_> {
pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<()> {
self.insert_into(trustpub_tokens::table)
.execute(conn)
.await?;

Ok(())
}
}
24 changes: 24 additions & 0 deletions crates/crates_io_database/src/models/trustpub/used_jti.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,24 @@
use crate::schema::trustpub_used_jtis;
use chrono::{DateTime, Utc};
use diesel::prelude::*;
use diesel_async::{AsyncPgConnection, RunQueryDsl};

#[derive(Debug, Insertable)]
#[diesel(table_name = trustpub_used_jtis, check_for_backend(diesel::pg::Pg))]
pub struct NewUsedJti<'a> {
pub jti: &'a str,
pub expires_at: DateTime<Utc>,
}

impl<'a> NewUsedJti<'a> {
pub fn new(jti: &'a str, expires_at: DateTime<Utc>) -> Self {
Self { jti, expires_at }
}

pub async fn insert(&self, conn: &mut AsyncPgConnection) -> QueryResult<usize> {
diesel::insert_into(trustpub_used_jtis::table)
.values(self)
.execute(conn)
.await
}
}
20 changes: 11 additions & 9 deletions crates/crates_io_database/src/models/version.rs
Original file line number Diff line number Diff line change
@@ -91,7 +91,7 @@ pub struct NewVersion<'a> {
license: Option<&'a str>,
#[builder(default, name = "size")]
crate_size: i32,
published_by: i32,
published_by: Option<i32>,
checksum: &'a str,
links: Option<&'a str>,
rust_version: Option<&'a str>,
@@ -110,7 +110,7 @@ impl NewVersion<'_> {
pub async fn save(
&self,
conn: &mut AsyncPgConnection,
published_by_email: &str,
published_by_email: Option<&str>,
) -> QueryResult<Version> {
use diesel::insert_into;

@@ -122,13 +122,15 @@ impl NewVersion<'_> {
.get_result(conn)
.await?;

insert_into(versions_published_by::table)
.values((
versions_published_by::version_id.eq(version.id),
versions_published_by::email.eq(published_by_email),
))
.execute(conn)
.await?;
if let Some(published_by_email) = published_by_email {
insert_into(versions_published_by::table)
.values((
versions_published_by::version_id.eq(version.id),
versions_published_by::email.eq(published_by_email),
))
.execute(conn)
.await?;
}

Ok(version)
}
2 changes: 1 addition & 1 deletion crates/crates_io_diesel_helpers/src/fns.rs
Original file line number Diff line number Diff line change
@@ -4,7 +4,7 @@ use diesel::sql_types::{Date, Double, Integer, Interval, SingleValue, Text, Time
define_sql_function!(#[aggregate] fn array_agg<T: SingleValue>(x: T) -> Array<T>);
define_sql_function!(fn canon_crate_name(x: Text) -> Text);
define_sql_function!(fn to_char(a: Date, b: Text) -> Text);
define_sql_function!(fn lower(x: Text) -> Text);
define_sql_function!(fn lower<T: SingleValue>(x: T) -> T);
define_sql_function!(fn date_part(x: Text, y: Timestamptz) -> Double);
define_sql_function! {
#[sql_name = "date_part"]
22 changes: 21 additions & 1 deletion crates/crates_io_trustpub/Cargo.toml
Original file line number Diff line number Diff line change
@@ -7,10 +7,30 @@ edition = "2024"
[lints]
workspace = true

[features]
test-helpers = ["dep:bon", "dep:mockall", "dep:serde_json"]

[dependencies]
anyhow = "=1.0.98"
async-trait = "=0.1.88"
bon = { version = "=3.6.3", optional = true }
chrono = { version = "=0.4.41", features = ["serde"] }
jsonwebtoken = "=9.3.1"
mockall = { version = "=0.13.1", optional = true }
rand = "=0.9.1"
reqwest = { version = "=0.12.15", features = ["gzip", "json"] }
regex = "=1.11.1"
secrecy = "=0.10.3"
serde = { version = "=1.0.219", features = ["derive"] }
serde_json = { version = "=1.0.140", optional = true }
sha2 = "=0.10.9"
thiserror = "=2.0.12"
tokio = { version = "=1.45.0", features = ["sync"] }

[dev-dependencies]
bon = "=3.6.3"
claims = "=0.8.0"
insta = "=1.43.1"
insta = { version = "=1.43.1", features = ["json", "redactions"] }
mockito = "=1.7.0"
serde_json = "=1.0.140"
tokio = { version = "=1.45.0", features = ["macros", "rt-multi-thread"] }
Loading
Loading