csaf_walker/rolie/
mod.rs

1mod roliefeed;
2
3pub use roliefeed::*;
4
5use crate::source::HttpSourceError;
6use time::OffsetDateTime;
7use url::{ParseError, Url};
8use walker_common::fetcher::Json;
9use walker_common::{fetcher, fetcher::Fetcher};
10
11#[derive(Debug, thiserror::Error)]
12pub enum Error {
13    #[error("Fetch error: {0}")]
14    Fetcher(#[from] fetcher::Error),
15    #[error("URL error: {0}")]
16    Url(#[from] ParseError),
17    #[error("JSON parse error: {0}")]
18    Json(#[from] serde_json::Error),
19}
20
21impl From<Error> for HttpSourceError {
22    fn from(value: Error) -> Self {
23        match value {
24            Error::Fetcher(err) => Self::Fetcher(err),
25            Error::Url(err) => Self::Url(err),
26            Error::Json(err) => Self::Json(err),
27        }
28    }
29}
30
31#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)]
32pub struct SourceFile {
33    /// The relative or absolute file name
34    pub file: String,
35
36    /// The relative or absolute file name to the hash
37    pub digest: Option<String>,
38
39    /// The relative or absolute file name to the signature
40    pub signature: Option<String>,
41
42    /// The timestamp of the last change
43    #[serde(with = "time::serde::iso8601")]
44    pub timestamp: OffsetDateTime,
45}
46
47#[derive(Clone, Debug, PartialEq, Eq, serde::Deserialize)]
48pub struct RolieSource {
49    #[serde(default, skip_serializing_if = "Vec::is_empty")]
50    pub files: Vec<SourceFile>,
51}
52
53impl RolieSource {
54    pub async fn retrieve(fetcher: &Fetcher, base_url: Url) -> Result<Self, Error> {
55        let mut files = vec![];
56        let Json(result) = fetcher.fetch::<Json<RolieFeed>>(base_url).await?;
57        for entry in result.feed.entry {
58            files.push(find_file(entry));
59        }
60
61        log::debug!("found {:?} files", files.len());
62
63        Ok(Self { files })
64    }
65}
66
67fn find_file(entry: Entry) -> SourceFile {
68    let mut file = None;
69    let mut signature = None;
70    let mut digest = None;
71
72    for link in entry.link {
73        match &*link.rel {
74            "self" => file = Some(link.href),
75            "signature" => signature = Some(link.href),
76            "hash" => digest = Some(link.href),
77            _ => continue,
78        }
79    }
80
81    SourceFile {
82        file: file.unwrap_or(entry.content.src),
83        timestamp: entry.updated,
84        signature,
85        digest,
86    }
87}
88
89#[cfg(test)]
90mod test {
91    use super::*;
92    use time::macros::datetime;
93
94    #[test]
95    fn find_by_link() {
96        let result = find_file(Entry {
97            link: vec![
98                Link {
99                    rel: "self".to_string(),
100                    href: "https://example.com/foo/bar/1.json".to_string(),
101                },
102                Link {
103                    rel: "hash".to_string(),
104                    href: "https://example.com/foo/bar/1.json.sha512".to_string(),
105                },
106                Link {
107                    rel: "signature".to_string(),
108                    href: "https://example.com/foo/bar/1.json.asc".to_string(),
109                },
110            ],
111            format: Format {
112                schema: "https://docs.oasis-open.org/csaf/csaf/v2.0/csaf_json_schema.json"
113                    .to_string(),
114                version: "2.0".to_string(),
115            },
116            id: "1".to_string(),
117            published: datetime!(2025-01-01 00:00:00 UTC ),
118            title: "Example entry".to_string(),
119            updated: datetime!(2025-01-02 00:00:00 UTC ),
120            content: Content {
121                src: "https://example.com/foo/bar/1.json".to_string(),
122                content_type: "application/json".to_string(),
123            },
124        });
125
126        assert_eq!(
127            result,
128            SourceFile {
129                file: "https://example.com/foo/bar/1.json".to_string(),
130                digest: Some("https://example.com/foo/bar/1.json.sha512".to_string()),
131                signature: Some("https://example.com/foo/bar/1.json.asc".to_string()),
132                timestamp: datetime!(2025-01-02 00:00:00 UTC ),
133            }
134        );
135    }
136}