-
Notifications
You must be signed in to change notification settings - Fork 328
Sync crates.io Trusted Publishing configs #2078
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
Changes from all commits
4b0a0a6
05e4a41
5ec0d5d
a55bca9
9691eb6
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
|
|
@@ -430,3 +430,16 @@ allowed-merge-teams = ["awesome-team"] | |
| # (optional) | ||
| merge-bots = ["homu"] | ||
| ``` | ||
|
|
||
| ### Crates.io trusted publishing | ||
| Configure crates.io Trusted Publishing for crates published from a given repository from GitHub Actions. | ||
|
|
||
| ```toml | ||
| [[crates-io-publishing]] | ||
| # Crates that will be published with the given workflow file from this repository (required) | ||
| crates = ["regex"] | ||
| # Name of a GitHub Actions workflow file that will publish the crate (required) | ||
| workflow-filename = "ci.yml" | ||
| # GitHub Actions environment that has to be used for the publishing (required) | ||
| environment = "deploy" | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Only people with maintain+ access on a repo can create environments. We'd need a way to let people create environments without infra-admins involvement. Also, migrating all environments to be managed by the team repo might not be trivial, as a lot of environments nowadays are created by Terraform.
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Ah, I see, this answers my question above. So what would you prefer? Make environments optional for now? Or make them required and involve infra admins in their creation?
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think it would be better now to require them, and eventually integrate them in the team repo.
Contributor
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. What do environments do in this case? Why would they be required?
Member
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. Environments can be used to ensure that the publish happens only from a specific branch or workflow job. That is useful to ensure that unexpected parts of CI (e.g. those that execute untrusted code at build time) are not allowed to publish. |
||
| ``` | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,172 @@ | ||
| use crate::crates_io::CratesIoPublishingConfig; | ||
| use crate::utils::ResponseExt; | ||
| use anyhow::{Context, anyhow}; | ||
| use log::debug; | ||
| use reqwest::blocking::Client; | ||
| use reqwest::header; | ||
| use reqwest::header::{HeaderMap, HeaderValue}; | ||
| use secrecy::{ExposeSecret, SecretString}; | ||
| use serde::Serialize; | ||
| use std::fmt::{Display, Formatter}; | ||
|
|
||
| // OpenAPI spec: https://crates.io/api/openapi.json | ||
| const CRATES_IO_BASE_URL: &str = "https://crates.io/api/v1"; | ||
|
|
||
| /// Access to the Zulip API | ||
| #[derive(Clone)] | ||
| pub(crate) struct CratesIoApi { | ||
| client: Client, | ||
| token: SecretString, | ||
| dry_run: bool, | ||
| } | ||
|
|
||
| impl CratesIoApi { | ||
| pub(crate) fn new(token: SecretString, dry_run: bool) -> Self { | ||
| let mut map = HeaderMap::default(); | ||
| map.insert( | ||
| header::USER_AGENT, | ||
| HeaderValue::from_static(crate::USER_AGENT), | ||
| ); | ||
|
|
||
| Self { | ||
| client: reqwest::blocking::ClientBuilder::default() | ||
| .default_headers(map) | ||
| .build() | ||
| .unwrap(), | ||
| token, | ||
| dry_run, | ||
| } | ||
| } | ||
|
|
||
| /// List existing trusted publishing configurations for a given crate. | ||
| pub(crate) fn list_trusted_publishing_github_configs( | ||
| &self, | ||
| krate: &str, | ||
| ) -> anyhow::Result<Vec<TrustedPublishingGitHubConfig>> { | ||
| #[derive(serde::Deserialize)] | ||
| struct GetTrustedPublishing { | ||
| github_configs: Vec<TrustedPublishingGitHubConfig>, | ||
| } | ||
|
|
||
| let response: GetTrustedPublishing = self | ||
| .req::<()>( | ||
| reqwest::Method::GET, | ||
| &format!("/trusted_publishing/github_configs?crate={krate}"), | ||
| None, | ||
| )? | ||
| .error_for_status()? | ||
| .json_annotated()?; | ||
|
|
||
| Ok(response.github_configs) | ||
| } | ||
|
|
||
| /// Create a new trusted publishing configuration for a given crate. | ||
| pub(crate) fn create_trusted_publishing_github_config( | ||
| &self, | ||
| config: &CratesIoPublishingConfig, | ||
| ) -> anyhow::Result<()> { | ||
| debug!( | ||
| "Creating trusted publishing config for '{}' in repo '{}/{}', workflow file '{}' and environment '{}'", | ||
| config.krate.0, | ||
| config.repo_org, | ||
| config.repo_name, | ||
| config.workflow_file, | ||
| config.environment | ||
| ); | ||
|
|
||
| if self.dry_run { | ||
| return Ok(()); | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| struct TrustedPublishingGitHubConfigCreate<'a> { | ||
| repository_owner: &'a str, | ||
| repository_name: &'a str, | ||
| #[serde(rename = "crate")] | ||
| krate: &'a str, | ||
| workflow_filename: &'a str, | ||
| environment: Option<&'a str>, | ||
| } | ||
|
|
||
| #[derive(serde::Serialize)] | ||
| struct CreateTrustedPublishing<'a> { | ||
| github_config: TrustedPublishingGitHubConfigCreate<'a>, | ||
| } | ||
|
|
||
| let request = CreateTrustedPublishing { | ||
| github_config: TrustedPublishingGitHubConfigCreate { | ||
| repository_owner: &config.repo_org, | ||
| repository_name: &config.repo_name, | ||
| krate: &config.krate.0, | ||
| workflow_filename: &config.workflow_file, | ||
| environment: Some(&config.environment), | ||
| }, | ||
| }; | ||
|
|
||
| self.req( | ||
| reqwest::Method::POST, | ||
| "/trusted_publishing/github_configs", | ||
| Some(&request), | ||
| )? | ||
| .error_for_status() | ||
| .with_context(|| anyhow!("Cannot created trusted publishing config {config:?}"))?; | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Delete a trusted publishing configuration with the given ID. | ||
| pub(crate) fn delete_trusted_publishing_github_config( | ||
| &self, | ||
| id: TrustedPublishingId, | ||
| ) -> anyhow::Result<()> { | ||
| debug!("Deleting trusted publishing with ID {id}"); | ||
|
|
||
| if !self.dry_run { | ||
| self.req::<()>( | ||
| reqwest::Method::DELETE, | ||
| &format!("/trusted_publishing/github_configs/{}", id.0), | ||
| None, | ||
| )? | ||
| .error_for_status() | ||
| .with_context(|| anyhow!("Cannot delete trusted publishing config with ID {id}"))?; | ||
| } | ||
|
|
||
| Ok(()) | ||
| } | ||
|
|
||
| /// Perform a request against the crates.io API | ||
| fn req<T: Serialize>( | ||
| &self, | ||
| method: reqwest::Method, | ||
| path: &str, | ||
| data: Option<&T>, | ||
| ) -> anyhow::Result<reqwest::blocking::Response> { | ||
| let mut req = self | ||
| .client | ||
| .request(method, format!("{CRATES_IO_BASE_URL}{path}")) | ||
| .bearer_auth(self.token.expose_secret()); | ||
| if let Some(data) = data { | ||
| req = req.json(data); | ||
| } | ||
|
|
||
| Ok(req.send()?) | ||
| } | ||
| } | ||
|
|
||
| #[derive(serde::Deserialize, Copy, Clone, Debug, PartialEq, Eq, PartialOrd, Ord)] | ||
| pub struct TrustedPublishingId(u64); | ||
|
|
||
| impl Display for TrustedPublishingId { | ||
| fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result { | ||
| self.0.fmt(f) | ||
| } | ||
| } | ||
|
|
||
| #[derive(serde::Deserialize, Debug)] | ||
| pub(crate) struct TrustedPublishingGitHubConfig { | ||
| pub(crate) id: TrustedPublishingId, | ||
| pub(crate) repository_owner: String, | ||
| pub(crate) repository_name: String, | ||
| pub(crate) workflow_filename: String, | ||
| pub(crate) environment: Option<String>, | ||
| } |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
I would personally enforce on the sync-team side that an environment is always configured when publishing. I don't see any case when we would want all workflows on a repository to publish crates.
Uh oh!
There was an error while loading. Please reload this page.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
This might make it a bit harder to deploy TP though, as creating an environment will require an infra-admin for each repo? Or are they auto-created if you just specify them in the YML workflow file? 🤔
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
They are not auto-created :(