-
Notifications
You must be signed in to change notification settings - Fork 26
feat(run-environment): support CircleCI for GitHub repositories #482
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
Open
fargito
wants to merge
3
commits into
main
Choose a base branch
from
feat/circleci-oidc-upload-auth
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
+811
−121
Open
Changes from all commits
Commits
Show all changes
3 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.
Oops, something went wrong.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,105 @@ | ||
| use console::style; | ||
| use log::*; | ||
| use simplelog::SharedLogger; | ||
| use std::{env, io::Write}; | ||
|
|
||
| use crate::{ | ||
| logger::{GroupEvent, get_announcement_event, get_group_event, get_json_event}, | ||
| run_environment::logger::should_provider_logger_handle_record, | ||
| }; | ||
|
|
||
| /// A logger that prints logs in the format expected by CircleCI | ||
| /// | ||
| /// CircleCI has no collapsible section markers, so groups are printed as plain | ||
| /// headers. | ||
| pub struct CircleCILogger { | ||
| log_level: LevelFilter, | ||
| } | ||
|
|
||
| impl CircleCILogger { | ||
| pub fn new() -> Self { | ||
| // force activation of colors: CircleCI renders ANSI sequences in its UI, but | ||
| // the output is not a TTY so colors would be disabled by default. | ||
| console::set_colors_enabled(true); | ||
|
|
||
| let log_level = env::var("CODSPEED_LOG") | ||
| .ok() | ||
| .and_then(|log_level| log_level.parse::<log::LevelFilter>().ok()) | ||
| .unwrap_or(log::LevelFilter::Info); | ||
| Self { log_level } | ||
| } | ||
| } | ||
|
|
||
| impl Log for CircleCILogger { | ||
| fn enabled(&self, _metadata: &Metadata) -> bool { | ||
| true | ||
| } | ||
|
|
||
| fn log(&self, record: &Record) { | ||
| if !should_provider_logger_handle_record(record) { | ||
| return; | ||
| } | ||
|
|
||
| let level = record.level(); | ||
| let message = record.args(); | ||
|
|
||
| if let Some(group_event) = get_group_event(record) { | ||
| match group_event { | ||
| GroupEvent::Start(name) | GroupEvent::StartOpened(name) => { | ||
| println!("{}", style(name).cyan().bold()); | ||
| } | ||
| GroupEvent::End => {} | ||
| } | ||
| return; | ||
| } | ||
|
|
||
| if get_json_event(record).is_some() { | ||
| return; | ||
| } | ||
|
|
||
| if let Some(announcement) = get_announcement_event(record) { | ||
| println!("{}", style(announcement).green()); | ||
| return; | ||
| } | ||
|
|
||
| if level > self.log_level { | ||
| return; | ||
| } | ||
|
|
||
| match level { | ||
| Level::Error => { | ||
| println!("{}", style(message).red()); | ||
| } | ||
| Level::Warn => { | ||
| println!("{}", style(message).yellow()); | ||
| } | ||
| Level::Info => { | ||
| println!("{message}"); | ||
| } | ||
| Level::Debug => { | ||
| println!("{}", style(message).cyan()); | ||
| } | ||
| Level::Trace => { | ||
| println!("{}", style(message).magenta()); | ||
| } | ||
| } | ||
| } | ||
|
|
||
| fn flush(&self) { | ||
| std::io::stdout().flush().unwrap(); | ||
| } | ||
| } | ||
|
|
||
| impl SharedLogger for CircleCILogger { | ||
| fn level(&self) -> LevelFilter { | ||
| self.log_level | ||
| } | ||
|
|
||
| fn config(&self) -> Option<&simplelog::Config> { | ||
| None | ||
| } | ||
|
|
||
| fn as_log(self: Box<Self>) -> Box<dyn Log> { | ||
| Box::new(*self) | ||
| } | ||
| } | ||
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,5 @@ | ||
| mod logger; | ||
| mod oidc; | ||
| mod provider; | ||
|
|
||
| pub use provider::CircleCIProvider; |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,47 @@ | ||
| use std::process::Command; | ||
|
|
||
| use crate::prelude::*; | ||
|
|
||
| /// The CLI CircleCI makes available inside jobs. | ||
| const CIRCLECI_CLI: &str = "circleci"; | ||
|
|
||
| /// Mints an OIDC token for `audience`. | ||
| /// | ||
| /// The token CircleCI puts in `CIRCLE_OIDC_TOKEN` and `CIRCLE_OIDC_TOKEN_V2` cannot | ||
| /// be used instead: its audience is the id of the CircleCI organization, while | ||
| /// CodSpeed requires its own. Requesting the audience is what makes a token minted | ||
| /// for another integration unusable against CodSpeed, and vice versa. | ||
| /// | ||
| /// Errors carry what the CLI itself reported, as only the first error of a chain is | ||
| /// shown outside of debug logging: callers should add their advice to that message | ||
| /// rather than wrap it. | ||
| /// | ||
| /// <https://circleci.com/docs/guides/permissions-authentication/oidc-tokens-with-custom-claims/> | ||
| pub fn mint_token(audience: &str) -> Result<String> { | ||
| let claims = serde_json::json!({ "aud": audience }).to_string(); | ||
|
|
||
| let output = Command::new(CIRCLECI_CLI) | ||
| .args(["run", "oidc", "get", "--claims", &claims]) | ||
| .output() | ||
| .map_err(|error| anyhow!("Failed to run the `circleci` CLI: {error}"))?; | ||
|
|
||
| if !output.status.success() { | ||
| bail!( | ||
| "`circleci run oidc get` failed: {}", | ||
| String::from_utf8_lossy(&output.stderr).trim() | ||
| ); | ||
| } | ||
|
|
||
| // CircleCI does not mask tokens minted this way in the job output, so the token | ||
| // must not reach the logs, here or in the callers. | ||
| let token = String::from_utf8(output.stdout) | ||
| .map_err(|_| anyhow!("The OIDC token minted by CircleCI is not valid UTF-8"))? | ||
| .trim() | ||
| .to_string(); | ||
|
|
||
| if token.is_empty() { | ||
| bail!("`circleci run oidc get` returned an empty token"); | ||
| } | ||
|
|
||
| Ok(token) | ||
| } |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
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.
Can't we do better for logging?