feat(offers): an open offer names the version accepting delivers (#1028) #1068
No files matched your search
@@ -6,8 +6,9 @@
|
||||
//! supplier SBOM and a scan starts. Publishing delivers to every accepted
|
||||
//! release of the target, accepting delivers the latest version at once. The
|
||||
//! connection is asked again right before each write into the customer's
|
||||
//! workspace, and a version with the content last delivered is not delivered
|
||||
//! again.
|
||||
//! workspace, and the version last delivered is not delivered again. (Each
|
||||
//! publication is its own document, with its own serial number and time,
|
||||
//! so a new version is delivered even when its components are unchanged.)
|
||||
|
||||
use std::sync::Arc;
|
||||
|
||||
@@ -187,7 +188,9 @@ async fn attach(
|
||||
}
|
||||
|
||||
/// Deliver `version` of target `target_id`, just published by `supplier`
|
||||
/// (its database `db`), to every customer that accepted a release of it.
|
||||
/// (its database `db`), to every customer that accepted a release of it,
|
||||
/// and tell every customer whose offer is still open that it is now what
|
||||
/// accepting delivers.
|
||||
///
|
||||
/// # Returns
|
||||
///
|
||||
@@ -199,10 +202,10 @@ pub(super) async fn deliver_published(
|
||||
target_id: &str,
|
||||
version: &SbomVersion,
|
||||
) -> usize {
|
||||
let accepted: Vec<SbomRelease> = match db
|
||||
let live: Vec<SbomRelease> = match db
|
||||
.inner()
|
||||
.collection::<SbomRelease>(SbomRelease::COLLECTION)
|
||||
.find(doc! { "target_id": target_id, "ended_at": null, "answer": "accepted" })
|
||||
.find(doc! { "target_id": target_id, "ended_at": null })
|
||||
.await
|
||||
{
|
||||
Ok(cursor) => cursor.try_collect().await.unwrap_or_default(),
|
||||
@@ -212,10 +215,24 @@ pub(super) async fn deliver_published(
|
||||
}
|
||||
};
|
||||
let mut delivered = 0;
|
||||
for release in &accepted {
|
||||
if joined(agent, supplier, release).await
|
||||
&& deliver(agent, release, version).await == Outcome::Delivered
|
||||
for release in &live {
|
||||
let open = matches!(release.answer, None | Some(OfferState::Offered));
|
||||
if !(open || release.answer == Some(OfferState::Accepted))
|
||||
|| !joined(agent, supplier, release).await
|
||||
{
|
||||
continue;
|
||||
}
|
||||
if open {
|
||||
let id = release.id.to_hex();
|
||||
super::offers::announce(
|
||||
agent,
|
||||
&release.peer_tenant_id,
|
||||
&id,
|
||||
&version.label,
|
||||
version.components,
|
||||
)
|
||||
.await;
|
||||
} else if deliver(agent, release, version).await == Outcome::Delivered {
|
||||
delivered += 1;
|
||||
}
|
||||
}
|
||||
|
||||
@@ -69,7 +69,7 @@ pub struct SbomOffer {
|
||||
pub decided_by: Option<String>,
|
||||
#[serde(default)]
|
||||
pub ended_at: Option<BsonDateTime>,
|
||||
/// The content hash of the version last delivered, so the same content
|
||||
/// The content hash of the version last delivered, so the same version
|
||||
/// is never delivered twice.
|
||||
#[serde(default)]
|
||||
pub delivered_hash: Option<String>,
|
||||
@@ -78,6 +78,12 @@ pub struct SbomOffer {
|
||||
pub delivered_label: Option<String>,
|
||||
#[serde(default)]
|
||||
pub delivered_at: Option<BsonDateTime>,
|
||||
/// While the offer is open: the supplier's latest published version,
|
||||
/// what accepting delivers at once, and how many components it lists.
|
||||
#[serde(default)]
|
||||
pub offered_label: Option<String>,
|
||||
#[serde(default)]
|
||||
pub offered_components: Option<i64>,
|
||||
}
|
||||
|
||||
impl SbomOffer {
|
||||
@@ -86,7 +92,7 @@ impl SbomOffer {
|
||||
}
|
||||
|
||||
/// An offer as the customer reads it: nothing of the supplier's but its
|
||||
/// name and the product's.
|
||||
/// name, the product's, and the version it offers.
|
||||
#[derive(Debug, Clone, PartialEq, Serialize)]
|
||||
pub struct OfferView {
|
||||
pub id: String,
|
||||
@@ -101,6 +107,10 @@ pub struct OfferView {
|
||||
/// The version last delivered into the component, and when.
|
||||
pub delivered_label: Option<String>,
|
||||
pub delivered_at: Option<DateTime<Utc>>,
|
||||
/// While it is open: the version accepting delivers at once, and how
|
||||
/// many components it lists; `None` before the supplier published one.
|
||||
pub offered_label: Option<String>,
|
||||
pub offered_components: Option<i64>,
|
||||
}
|
||||
|
||||
impl From<SbomOffer> for OfferView {
|
||||
@@ -117,6 +127,8 @@ impl From<SbomOffer> for OfferView {
|
||||
ended_at: o.ended_at.map(BsonDateTime::to_chrono),
|
||||
delivered_label: o.delivered_label,
|
||||
delivered_at: o.delivered_at.map(BsonDateTime::to_chrono),
|
||||
offered_label: o.offered_label,
|
||||
offered_components: o.offered_components,
|
||||
}
|
||||
}
|
||||
}
|
||||
@@ -170,6 +182,28 @@ pub(super) async fn offer(
|
||||
Ok(())
|
||||
}
|
||||
|
||||
/// Tell `customer`'s open offer of release `release` that `label` (with
|
||||
/// `components` components) is now what accepting delivers. Best-effort:
|
||||
/// the next publication tells it again.
|
||||
pub(super) async fn announce(
|
||||
agent: &ComplianceAgent,
|
||||
customer: &str,
|
||||
release: &str,
|
||||
label: &str,
|
||||
components: i64,
|
||||
) {
|
||||
let db = agent.db_pool.for_existing_tenant_id(customer);
|
||||
let told = offers(&db)
|
||||
.update_one(
|
||||
doc! { "release_id": release, "state": "offered" },
|
||||
doc! { "$set": { "offered_label": label, "offered_components": components } },
|
||||
)
|
||||
.await;
|
||||
if let Err(e) = told {
|
||||
tracing::warn!(release, "new version not announced to the customer: {e}");
|
||||
}
|
||||
}
|
||||
|
||||
/// Mark the offer of release `release` in `customer`'s database ended: the
|
||||
/// supplier ended the release. Best-effort; a customer that has gone keeps
|
||||
/// nothing to mark.
|
||||
|
||||
@@ -80,8 +80,9 @@ pub struct SbomVersion {
|
||||
pub label: String,
|
||||
/// How many components it lists.
|
||||
pub components: i64,
|
||||
/// The SHA-256 of the document: a version with the same content is
|
||||
/// never delivered twice.
|
||||
/// The SHA-256 of the document: the same version is never delivered
|
||||
/// twice. Each document has its own serial number and time, so no two
|
||||
/// versions share it.
|
||||
pub content_hash: String,
|
||||
/// Where the document is, in the target's upload folder.
|
||||
pub stored_path: String,
|
||||
@@ -598,6 +599,12 @@ pub async fn create_release(
|
||||
delivered_at: None,
|
||||
};
|
||||
releases(&db).insert_one(&release).await.map_err(internal)?;
|
||||
// What accepting would deliver at once, if anything is published.
|
||||
let latest = versions(&db)
|
||||
.find_one(doc! { "target_id": &release.target_id })
|
||||
.sort(doc! { "created_at": -1, "_id": -1 })
|
||||
.await
|
||||
.map_err(internal)?;
|
||||
let offer = super::offers::SbomOffer {
|
||||
id: ObjectId::new(),
|
||||
release_id: release.id.to_hex(),
|
||||
@@ -615,6 +622,8 @@ pub async fn create_release(
|
||||
delivered_hash: None,
|
||||
delivered_label: None,
|
||||
delivered_at: None,
|
||||
offered_label: latest.as_ref().map(|v| v.label.clone()),
|
||||
offered_components: latest.as_ref().map(|v| v.components),
|
||||
};
|
||||
if let Err(status) = super::offers::offer(&agent, &release.peer_tenant_id, &offer).await {
|
||||
// Without its offer the release would wait for an answer that can
|
||||
|
||||
@@ -503,6 +503,8 @@ async fn an_offer_whose_connection_ended_cannot_be_accepted() {
|
||||
delivered_hash: None,
|
||||
delivered_label: None,
|
||||
delivered_at: None,
|
||||
offered_label: None,
|
||||
offered_components: None,
|
||||
};
|
||||
db.inner()
|
||||
.collection::<SbomOffer>(SbomOffer::COLLECTION)
|
||||
@@ -748,6 +750,14 @@ async fn accepted_releases_receive_each_published_version() {
|
||||
.await;
|
||||
let release = body["data"]["id"].as_str().unwrap().to_string();
|
||||
let offer = first_offer(&agent, "t-2").await;
|
||||
assert_eq!(
|
||||
(
|
||||
offer["offered_label"].as_str(),
|
||||
offer["offered_components"].as_i64()
|
||||
),
|
||||
(Some("1.4.0"), Some(1)),
|
||||
"the offer names what accepting delivers"
|
||||
);
|
||||
let accept = format!(
|
||||
"/api/v1/sbom-offers/{}/accept",
|
||||
offer["id"].as_str().unwrap()
|
||||
@@ -868,6 +878,56 @@ async fn accepted_releases_receive_each_published_version() {
|
||||
agent.db_pool.drop_tenant("t-2").await.ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn an_open_offer_learns_each_published_version() {
|
||||
use compliance_core::models::SbomEntry;
|
||||
use serde_json::json;
|
||||
let base = fake_registry().await;
|
||||
let Some(agent) = agent(&base).await else {
|
||||
return;
|
||||
};
|
||||
let product = stored_target(&agent, "t-1", "DR-4 Firmware", false).await;
|
||||
let releases = format!("/api/v1/targets/{product}/releases");
|
||||
let versions = format!("/api/v1/targets/{product}/versions");
|
||||
let (status, _) = release_call(
|
||||
agent.clone(),
|
||||
"t-1",
|
||||
&releases,
|
||||
json!({ "connection_id": "c-1" }),
|
||||
)
|
||||
.await;
|
||||
assert_eq!(status, StatusCode::CREATED);
|
||||
assert!(
|
||||
first_offer(&agent, "t-2").await["offered_label"].is_null(),
|
||||
"nothing published yet"
|
||||
);
|
||||
|
||||
agent
|
||||
.db_pool
|
||||
.for_tenant_id("t-1")
|
||||
.await
|
||||
.unwrap()
|
||||
.sbom_entries()
|
||||
.insert_one(SbomEntry::new(
|
||||
product.clone(),
|
||||
"lwip".into(),
|
||||
"2.1.2".into(),
|
||||
"generic".into(),
|
||||
))
|
||||
.await
|
||||
.unwrap();
|
||||
let (status, _) =
|
||||
release_call(agent.clone(), "t-1", &versions, json!({ "label": "1.5.0" })).await;
|
||||
assert_eq!(status, StatusCode::CREATED);
|
||||
|
||||
let offer = first_offer(&agent, "t-2").await;
|
||||
assert_eq!(offer["state"], "offered", "announced, not delivered");
|
||||
assert_eq!(offer["offered_label"], "1.5.0");
|
||||
assert!(offer["delivered_label"].is_null());
|
||||
agent.db_pool.drop_tenant("t-1").await.ok();
|
||||
agent.db_pool.drop_tenant("t-2").await.ok();
|
||||
}
|
||||
|
||||
#[tokio::test]
|
||||
async fn a_draft_without_components_says_why() {
|
||||
let base = fake_registry().await;
|
||||
|
||||
+7
-3
@@ -234,7 +234,10 @@ workspaces. The customer answers it, and the supplier sees the answer
|
||||
|
||||
- `GET /api/v1/sbom-offers` lists the offers, open ones first. Each names
|
||||
the supplier's workspace and its product, and nothing else of the
|
||||
supplier's.
|
||||
supplier's. An open offer also names the version accepting delivers at
|
||||
once and how many components it lists (`offered_label`,
|
||||
`offered_components`); each version the supplier publishes while it is
|
||||
open updates them.
|
||||
- `POST /api/v1/sbom-offers/{id}/accept` accepts an offer. With
|
||||
`{"target_id"}` it goes into an existing supplier component; otherwise
|
||||
into a new one with `{"name", "project_ids"}`, named after the product
|
||||
@@ -259,8 +262,9 @@ SBOM, as an upload does, sets the component's supplier version to its label,
|
||||
and starts a scan. The connection is checked again before each delivery, so
|
||||
nothing arrives after it or the release has ended. The customer sees the
|
||||
version last delivered on the offer (`delivered_label`, `delivered_at`), and
|
||||
the supplier sees it on the release. The same content is never delivered
|
||||
twice.
|
||||
the supplier sees it on the release. A version is never delivered twice.
|
||||
Each version is its own document, so a new version is delivered, and
|
||||
scanned, even when its components have not changed.
|
||||
|
||||
Deleting either side ends the offer. If the supplier deletes the released
|
||||
target, the customer's offer ends. If the customer deletes the component it
|
||||
|
||||
Reference in new issue
Block a user