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
use crate::visitors::SendVisitor;
use reqwest::Url;
use std::path::PathBuf;
use walker_common::sender::{
    provider::OpenIdTokenProviderConfigArguments, HttpSender, HttpSenderOptions,
};

#[derive(Debug, clap::Parser)]
#[command(next_help_heading = "Sending")]
pub struct SendArguments {
    /// Target to send to
    pub target: Url,

    /// Sender connect timeout
    #[arg(id = "sender-connect-timeout", long, default_value = "15s")]
    pub connect_timeout: humantime::Duration,

    /// Sender request timeout
    #[arg(id = "sender-timeout", long, default_value = "5m")]
    pub timeout: humantime::Duration,

    /// Additional root certificates
    #[arg(id = "sender-tls-ca-certificate", long)]
    pub additional_root_certificates: Vec<PathBuf>,

    /// Allow using TLS in an insecure mode when contacting the target (DANGER!)
    #[arg(id = "sender-tls-insecure", long)]
    pub tls_insecure: bool,

    /// Number of retries in case of temporary failures
    #[arg(id = "sender-retries", long, default_value = "0")]
    pub retries: usize,

    /// Delay between retries
    #[arg(id = "sender-retry-delay", long, default_value = "5s")]
    pub retry_delay: humantime::Duration,

    #[command(flatten)]
    pub oidc: OpenIdTokenProviderConfigArguments,
}

impl SendArguments {
    pub async fn into_visitor(self) -> Result<SendVisitor, anyhow::Error> {
        let SendArguments {
            target,
            connect_timeout,
            timeout,
            additional_root_certificates,
            tls_insecure,
            retries,
            retry_delay,
            oidc,
        } = self;

        let provider = oidc.into_provider().await?;
        let sender = HttpSender::new(
            provider,
            HttpSenderOptions::default()
                .connect_timeout(Some(connect_timeout.into()))
                .timeout(Some(timeout.into()))
                .tls_insecure(tls_insecure)
                .additional_root_certificates(additional_root_certificates),
        )
        .await?;

        Ok(SendVisitor {
            url: target,
            sender,
            retries,
            retry_delay: Some(retry_delay.into()),
        })
    }
}