Skip to content
Open
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
2 changes: 1 addition & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -41,7 +41,7 @@ In GitHub Actions, `proofline ci review --annotations` prints unresolved finding

`proofline scan --ref main --budget 200` scans the whole repository at one commit, not a pull request's change. GitHub resolves the ref once, when the request arrives, and the scan records that commit; `--commit SHA` names an exact full SHA instead, and you give one of the two. The command waits for the scan review and prints it like `proofline review`, while `--no-wait` prints its link and `--json` prints JSON.

`--budget` is required and has no default: it is the most credits the whole scan may spend, capped by the organization's on-demand scan limit, and a limit of 0 turns on-demand scans off. Results stay in Proofline and are never posted to GitHub. The token needs `scans:request` and live GitHub write access to the repository. The default sign-in omits that scope, so run `proofline login --scope reviews:read --scope findings:read --scope reviews:request --scope scans:request`; `--scope` replaces the defaults, so list every scope you want.
`--budget` is required and has no default: it is the most credits the whole scan may spend, capped by the organization's on-demand scan limit, and a limit of 0 turns on-demand scans off. Results stay in Proofline and are never posted to GitHub. The token needs `scans:request` and live GitHub write access to the repository. The default sign-in omits that scope, so run `proofline login --add-scope scans:request`. `--add-scope` asks for the scopes of the sign-in stored for that console, even an expired one, plus the scopes you name, and asks for the defaults plus them when none is stored; it prints the full list before the browser opens. `--scope` instead replaces the defaults, so list every scope you want with it, and the two cannot be combined. The approval page can still narrow what you get.

The CLI prints the request ID before it sends anything and never repeats the request. When the outcome is unknown (the connection failed, the server failed, or its answer was unreadable) the command exits 4; repeat it with `--request-id ID` to read the first resolution instead of paying for a second scan. If GitHub cannot resolve the ref, nothing starts and nothing is spent, but repeating that ID returns the same answer, so omit `--request-id` to ask again; with `--json` the resolution is still printed. Other exit codes match `proofline review`: 0 for a complete result, 2 for an incomplete or unfinished one and 1 for a refusal or error.

Expand Down
11 changes: 11 additions & 0 deletions src/credentials.rs
Original file line number Diff line number Diff line change
Expand Up @@ -147,6 +147,17 @@ pub async fn probe(origin: &Origin) -> Result<(), CredentialError> {
}
}

/// The scopes of the sign-in stored for `origin`, even when it has expired.
/// An unreadable entry counts as none, as it does for [`probe`], because the
/// next sign-in replaces it.
pub async fn stored_scope(origin: &Origin) -> Result<Option<String>, CredentialError> {
match read(origin).await {
Ok(stored) => Ok(stored.and_then(|stored| stored.scope)),
Err(CredentialError::Corrupt) => Ok(None),
Err(error) => Err(error),
}
}

pub async fn store(origin: &Origin, credential: &Credential) -> Result<(), CredentialError> {
let stored = Stored {
token: credential.token.expose_secret().to_owned(),
Expand Down
82 changes: 81 additions & 1 deletion src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -222,6 +222,10 @@ struct LoginArgs {
/// narrow them.
#[arg(long = "scope", value_name = "SCOPE", default_values_t = DEFAULT_SCOPES.map(str::to_owned))]
scopes: Vec<String>,
/// Scope to ask for on top of the scopes of the sign-in stored for this
/// console, or of the defaults when none is stored; repeat for several.
#[arg(long = "add-scope", value_name = "SCOPE", conflicts_with = "scopes")]
add_scopes: Vec<String>,
/// Print the approval address instead of opening a browser.
#[arg(long)]
no_browser: bool,
Expand Down Expand Up @@ -635,7 +639,15 @@ async fn login(
credentials::probe(http.origin())
.await
.context("could not read the OS keychain, so a sign-in could not be kept")?;
let challenge = device::start(http, &args.scopes).await?;
let scopes = if args.add_scopes.is_empty() {
args.scopes.clone()
} else {
let held = credentials::stored_scope(http.origin()).await?;
let scopes = with_added_scopes(held.as_deref(), &args.add_scopes);
say(&format!("Asking for {}.", scopes.join(" ")));
scopes
};
let challenge = device::start(http, &scopes).await?;
let code = challenge.user_code.expose_secret();
say(&format!("Your sign-in code is {code}."));
let opened = !args.no_browser
Expand Down Expand Up @@ -674,6 +686,26 @@ async fn login(
Ok(())
}

/// The scopes a sign-in with `--add-scope` asks for: the space-separated
/// scopes `held` by the stored sign-in, or the defaults when it holds none,
/// followed by each added scope that is not already among them.
fn with_added_scopes(held: Option<&str>, added: &[String]) -> Vec<String> {
let mut scopes: Vec<String> = held
.unwrap_or_default()
.split_whitespace()
.map(str::to_owned)
.collect();
if scopes.is_empty() {
scopes = DEFAULT_SCOPES.map(str::to_owned).to_vec();
}
for scope in added {
if !scopes.contains(scope) {
scopes.push(scope.clone());
}
}
scopes
}

async fn logout(origin: &Origin, environment: bool) -> anyhow::Result<()> {
if credentials::remove(origin).await? {
say(&format!(
Expand Down Expand Up @@ -1971,4 +2003,52 @@ mod tests {
Verdict::NotComplete
);
}

#[test]
fn added_scopes_keep_the_stored_sign_in_scopes() {
let added = ["scans:request".to_owned(), "reviews:read".to_owned()];
assert_eq!(
with_added_scopes(Some("reviews:read analytics:read\tsetup:write"), &added),
[
"reviews:read",
"analytics:read",
"setup:write",
"scans:request"
]
);
}

#[test]
fn added_scopes_start_from_the_defaults_without_a_stored_scope() {
let added = ["scans:request".to_owned(), "scans:request".to_owned()];
let expected = [
"reviews:read",
"findings:read",
"reviews:request",
"scans:request",
];
assert_eq!(with_added_scopes(None, &added), expected);
assert_eq!(with_added_scopes(Some(" "), &added), expected);
}

#[test]
fn add_scope_cannot_be_combined_with_scope() {
let parse = |args: &[&str]| Cli::try_parse_from(["proofline", "login"].iter().chain(args));
let Ok(Cli {
command: Command::Login(login),
..
}) = parse(&[
"--add-scope",
"scans:request",
"--add-scope",
"analytics:read",
])
else {
panic!("--add-scope alone should parse");
};
assert_eq!(login.add_scopes, ["scans:request", "analytics:read"]);
let error = parse(&["--add-scope", "scans:request", "--scope", "reviews:read"])
.expect_err("--scope and --add-scope conflict");
assert_eq!(error.kind(), clap::error::ErrorKind::ArgumentConflict);
}
}
2 changes: 1 addition & 1 deletion src/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -86,7 +86,7 @@ pub(crate) async fn run(
Ok(response) => response,
Err(error) if error.status() == Some(403) => {
return Err(anyhow::Error::new(error).context(
"this sign-in cannot request scans; run `proofline login --scope reviews:read --scope findings:read --scope reviews:request --scope scans:request`, and have the token's repositories include this one",
"this sign-in cannot request scans; run `proofline login --add-scope scans:request`, and have the token's repositories include this one",
));
}
// The scan may have started, so the caller gets the one retry that
Expand Down
2 changes: 1 addition & 1 deletion tests/scan.rs
Original file line number Diff line number Diff line change
Expand Up @@ -282,7 +282,7 @@ async fn a_token_without_the_scan_scope_is_told_which_login_to_run() {
let (origin, _) = serve(refused(StatusCode::FORBIDDEN, "missing scope")).await;
let output = cli(&origin, &["--ref", "main", "--budget", "5"]).await;
assert_eq!(output.status.code(), Some(1));
assert!(stderr(&output).contains("--scope scans:request"));
assert!(stderr(&output).contains("proofline login --add-scope scans:request"));
}

#[tokio::test]
Expand Down