1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
use super::*;
use crate::sbom::{
    discover::DiscoveredSbom,
    retrieve::{RetrievalContext, RetrievalError, RetrievedSbom, RetrievedVisitor},
    validation::{ValidatedSbom, ValidatedVisitor, ValidationContext, ValidationError},
};
use reqwest::header;

#[derive(Debug, thiserror::Error)]
pub enum SendRetrievedSbomError {
    #[error(transparent)]
    Store(#[from] SendError),
    #[error(transparent)]
    Retrieval(#[from] RetrievalError),
}

impl RetrievedVisitor for SendVisitor {
    type Error = SendRetrievedSbomError;
    type Context = ();

    async fn visit_context(&self, _: &RetrievalContext<'_>) -> Result<Self::Context, Self::Error> {
        Ok(())
    }

    async fn visit_sbom(
        &self,
        _context: &Self::Context,
        result: Result<RetrievedSbom, RetrievalError>,
    ) -> Result<(), Self::Error> {
        self.send_sbom(result?).await?;
        Ok(())
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SendValidatedSbomError {
    #[error(transparent)]
    Store(#[from] SendError),
    #[error(transparent)]
    Validation(#[from] ValidationError),
}

impl ValidatedVisitor for SendVisitor {
    type Error = SendValidatedSbomError;
    type Context = ();

    async fn visit_context(&self, _: &ValidationContext<'_>) -> Result<Self::Context, Self::Error> {
        Ok(())
    }

    async fn visit_sbom(
        &self,
        _context: &Self::Context,
        result: Result<ValidatedSbom, ValidationError>,
    ) -> Result<(), Self::Error> {
        self.send_sbom(result?.retrieved).await?;
        Ok(())
    }
}

impl SendVisitor {
    async fn send_sbom(&self, sbom: RetrievedSbom) -> Result<(), SendError> {
        log::debug!(
            "Sending: {} (modified: {:?})",
            sbom.url,
            sbom.metadata.last_modification
        );

        let RetrievedSbom {
            data,
            discovered: DiscoveredSbom { url, .. },
            ..
        } = sbom;

        let name = url
            .path_segments()
            .and_then(|p| p.last())
            .unwrap_or_else(|| url.path());

        if !(name.ends_with(".json") || name.ends_with(".json.bz2")) {
            log::warn!("Skipping unknown file: {name}");
            return Ok(());
        }

        let bzip2 = name.ends_with(".bz2");

        self.send(url.as_str(), data, |mut request| {
            request = request
                .query(&[("id", name)])
                .header(header::CONTENT_TYPE, "application/json");
            if bzip2 {
                request.header(header::CONTENT_ENCODING, "bzip2")
            } else {
                request
            }
        })
        .await
    }
}