Skip to content
Closed
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
28 changes: 28 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -116,6 +116,34 @@ Knowledge reads need `reviews:read` and `knowledge:read`. Implementation also ne
opens that GitHub organization's installation page; `--print` only prints its address.
Cloud trust and connection changes still require a signed-in administrator browser.

## Deployment reviews

Read a deployment review by its public key, `rev_` followed by 32 hexadecimal
digits, as its console address shows:

```sh
proofline findings --deployment rev_0190f3a00000700080000000000000d1 --wait
```

The command reads the deployment operation of the review API and refuses a
reply that is another review or not a deployment review. `--json`, `--wait` and
`--timeout` behave as they do for `--review`; `--sarif` is for a retained
pull-request review. Reads need `reviews:read`.

A deploy job holds itself to the review of the commit it deploys:

```sh
proofline ci deployment --commit "$DEPLOYED_SHA" --fail-on serious
```

`ci deployment` waits for the deployment review whose head is that full commit
SHA, defaulting to `COMMIT_SHA` and then the GitHub Actions job's commit. It
authenticates as `ci review` does, with `PROOFLINE_TOKEN` or a GitHub Actions
job granted `id-token: write`. It only reads: the review API has no operation
that requests a deployment review, and a pull-request review of the same commit
is not its answer. Exit codes match `ci review`: 0 complete, 2 not complete, 3 an
open finding at or above `--fail-on`, 4 no answer before `--timeout`.

## Windows

Official releases include `proofline-x86_64-pc-windows-msvc.tar.gz` with `proofline.exe`.
Expand Down
16 changes: 16 additions & 0 deletions src/api.rs
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,7 @@ use serde::de::DeserializeOwned;
use url::Url;
use uuid::Uuid;

use crate::deployment::PublicKey;
use crate::model::{
CommitReviewRequest, CommitReviewResponse, CoverageUploadRequest, CoverageUploadResponse,
FileReviewRequest, FileReviewResponse, PatchReviewRequest, PatchReviewResponse,
Expand Down Expand Up @@ -476,6 +477,21 @@ impl Api {
Ok((raw, typed))
}

/// Fetches a deployment review by its public key, as the raw document and
/// its typed reading. The caller holds the result to the key.
pub async fn deployment_review(
&self,
key: PublicKey,
) -> Result<(serde_json::Value, ReviewDetail), ApiError> {
let key = key.to_string();
let raw = self
.get(self.url(&["reviews", "deployments", &key]))
.await?;
let typed = serde_json::from_value(raw.clone())
.map_err(|error| self.http.protocol(error.to_string()))?;
Ok((raw, typed))
}

/// One page of the review collection, newest first.
pub async fn reviews(
&self,
Expand Down
160 changes: 160 additions & 0 deletions src/deployment.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,160 @@
//! A deployment review's public address. The review API reads a deployment
//! review by its `public_key`: `rev_` followed by the review's identifier
//! without hyphens. The key is parsed before any request, and the review the
//! server returns is held to it, so a command never reports another review
//! as the deployment the caller named.

use std::fmt;
use std::str::FromStr;

use uuid::Uuid;

use crate::model::ReviewDetail;

const PREFIX: &str = "rev_";
/// A review identifier without its hyphens.
const DIGITS: usize = 32;
/// The `kind` of a deployment review.
pub const KIND: &str = "DEPLOYMENT";

/// The stable public address of one review.
#[derive(Debug, Clone, Copy, PartialEq, Eq)]
pub struct PublicKey(Uuid);

impl PublicKey {
/// The review this key addresses.
pub const fn review_id(self) -> Uuid {
self.0
}
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
#[error("a deployment review key is `rev_` followed by 32 lowercase hexadecimal digits")]
pub struct InvalidPublicKey;

impl FromStr for PublicKey {
type Err = InvalidPublicKey;

/// Only the canonical form: the key becomes a path segment, and a second
/// spelling of one review would not be the address the server published.
fn from_str(raw: &str) -> Result<Self, Self::Err> {
let digits = raw.strip_prefix(PREFIX).ok_or(InvalidPublicKey)?;
if digits.len() != DIGITS
|| !digits
.bytes()
.all(|byte| matches!(byte, b'0'..=b'9' | b'a'..=b'f'))
{
return Err(InvalidPublicKey);
}
Uuid::try_parse(digits)
.map(Self)
.map_err(|_| InvalidPublicKey)
}
}

impl fmt::Display for PublicKey {
fn fmt(&self, f: &mut fmt::Formatter<'_>) -> fmt::Result {
write!(f, "{PREFIX}{}", self.0.simple())
}
}

#[derive(Debug, PartialEq, Eq, thiserror::Error)]
pub enum DeploymentError {
#[error("requested deployment review {requested}, but the server returned review {returned}")]
WrongReview {
requested: PublicKey,
returned: Uuid,
},
#[error("review {review} has kind {kind}, not DEPLOYMENT; read it with `findings --review`")]
NotADeployment { review: Uuid, kind: String },
}

/// Holds the returned review to the key that asked for it.
pub fn validate(detail: &ReviewDetail, requested: PublicKey) -> Result<(), DeploymentError> {
if detail.id != requested.review_id() {
return Err(DeploymentError::WrongReview {
requested,
returned: detail.id,
});
}
if detail.kind != KIND {
return Err(DeploymentError::NotADeployment {
review: detail.id,
kind: crate::text::one_line(&detail.kind),
});
}
Ok(())
}

#[cfg(test)]
mod tests {
use super::*;

const REVIEW: &str = "0190f3a0-0000-7000-8000-0000000000d1";
const KEY: &str = "rev_0190f3a00000700080000000000000d1";

fn key() -> String {
KEY.to_owned()
}

fn detail(id: &str, kind: &str) -> ReviewDetail {
serde_json::from_value(serde_json::json!({
"id": id,
"kind": kind,
"status": "COMPLETED",
"assessment_count": 0,
"assessments": [],
}))
.unwrap()
}

#[test]
fn a_key_names_its_review_and_prints_as_it_was_written() {
let parsed: PublicKey = key().parse().unwrap();
assert_eq!(parsed.review_id(), Uuid::parse_str(REVIEW).unwrap());
assert_eq!(parsed.to_string(), key());
}

#[test]
fn only_the_canonical_spelling_is_a_key() {
for refused in [
"",
REVIEW,
// No prefix, another prefix, or the hyphenated identifier.
"0190f3a00000700080000000000000d1",
"REV_0190f3a00000700080000000000000d1",
"rev_0190f3a0-0000-7000-8000-0000000000d1",
// Uppercase digits, one digit short and one digit long.
"rev_0190F3A00000700080000000000000D1",
"rev_0190f3a00000700080000000000000d",
"rev_0190f3a00000700080000000000000d10",
// Thirty-two characters that are not all digits.
"rev_0190f3a0000070008000000/../../x1",
" rev_0190f3a00000700080000000000000d1",
] {
assert_eq!(
refused.parse::<PublicKey>(),
Err(InvalidPublicKey),
"{refused:?}"
);
}
}

#[test]
fn a_returned_review_must_be_the_deployment_that_was_named() {
let requested: PublicKey = key().parse().unwrap();
assert_eq!(validate(&detail(REVIEW, KIND), requested), Ok(()));
let other = "0190f3a0-0000-7000-8000-0000000000d2";
assert_eq!(
validate(&detail(other, KIND), requested),
Err(DeploymentError::WrongReview {
requested,
returned: Uuid::parse_str(other).unwrap(),
})
);
// Untrusted server text is contained before it reaches the terminal.
let refused = validate(&detail(REVIEW, "PULL_\u{1b}[31mREQUEST"), requested).unwrap_err();
assert!(matches!(refused, DeploymentError::NotADeployment { .. }));
assert!(!refused.to_string().contains('\u{1b}'));
}
}
Loading