walker_extras/visitors/send/
csaf.rs

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
use super::*;
use crate::csaf::{
    retrieve::{RetrievalContext, RetrievedAdvisory, RetrievedVisitor},
    validation::{ValidatedAdvisory, ValidatedVisitor, ValidationContext, ValidationError},
};
use csaf_walker::{discover::DiscoveredAdvisory, source::Source};
use walker_common::retrieve::RetrievalError;

#[derive(Debug, thiserror::Error)]
pub enum SendRetrievedAdvisoryError<S: Source> {
    #[error(transparent)]
    Store(#[from] SendError),
    #[error(transparent)]
    Retrieval(#[from] RetrievalError<DiscoveredAdvisory, S>),
}

impl<S: Source> RetrievedVisitor<S> for SendVisitor {
    type Error = SendRetrievedAdvisoryError<S>;
    type Context = ();

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

    async fn visit_advisory(
        &self,
        _context: &Self::Context,
        result: Result<RetrievedAdvisory, RetrievalError<DiscoveredAdvisory, S>>,
    ) -> Result<(), Self::Error> {
        self.send_retrieved_advisory(result?).await?;
        Ok(())
    }
}

#[derive(Debug, thiserror::Error)]
pub enum SendValidatedAdvisoryError<S: Source> {
    #[error(transparent)]
    Store(#[from] SendError),
    #[error(transparent)]
    Validation(#[from] ValidationError<S>),
}

impl<S: Source> ValidatedVisitor<S> for SendVisitor {
    type Error = SendValidatedAdvisoryError<S>;
    type Context = ();

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

    async fn visit_advisory(
        &self,
        _context: &Self::Context,
        result: Result<ValidatedAdvisory, ValidationError<S>>,
    ) -> Result<(), Self::Error> {
        self.send_retrieved_advisory(result?.retrieved).await?;
        Ok(())
    }
}

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

        let RetrievedAdvisory {
            data,
            discovered: DiscoveredAdvisory { url, .. },
            ..
        } = advisory;

        self.send_advisory(url.as_str(), data).await
    }

    pub async fn send_advisory(&self, name: &str, data: Bytes) -> Result<(), SendError> {
        self.send(name, data, |request| {
            request.header(header::CONTENT_TYPE, "application/json")
        })
        .await
    }
}