Skip to main content

walker_extras/visitors/send/
clap.rs

1use crate::visitors::SendVisitor;
2use reqwest::Url;
3use std::path::PathBuf;
4use walker_common::sender::{
5    HttpSender, HttpSenderOptions, provider::OpenIdTokenProviderConfigArguments,
6};
7
8#[derive(Debug, clap::Parser)]
9#[command(next_help_heading = "Sending")]
10pub struct SendArguments {
11    /// Target to send to
12    pub target: Url,
13
14    /// Sender connect timeout
15    #[arg(
16        id = "sender-connect-timeout",
17        long,
18        env = "SENDER_CONNECT_TIMEOUT",
19        default_value = "15s"
20    )]
21    pub connect_timeout: humantime::Duration,
22
23    /// Sender request timeout
24    #[arg(
25        id = "sender-timeout",
26        long,
27        env = "SENDER_TIMEOUT",
28        default_value = "5m"
29    )]
30    pub timeout: humantime::Duration,
31
32    /// Additional root certificates
33    #[arg(id = "sender-tls-ca-certificate", long)]
34    pub additional_root_certificates: Vec<PathBuf>,
35
36    /// Allow using TLS in an insecure mode when contacting the target (DANGER!)
37    #[arg(id = "sender-tls-insecure", long)]
38    pub tls_insecure: bool,
39
40    /// Number of retries in case of temporary failures
41    #[arg(
42        id = "sender-retries",
43        long,
44        env = "SENDER_RETRIES",
45        default_value = "0"
46    )]
47    pub retries: usize,
48
49    /// Minimum delay between retries
50    #[arg(
51        id = "sender-min-delay",
52        long,
53        env = "SENDER_MIN_DELAY",
54        default_value = "1s"
55    )]
56    pub min_delay: humantime::Duration,
57
58    /// Maximum delay between retries
59    #[arg(
60        id = "sender-max-delay",
61        long,
62        env = "SENDER_MAX_DELAY",
63        default_value = "60s"
64    )]
65    pub max_delay: humantime::Duration,
66
67    /// Custom query parameters
68    #[arg(
69        id = "sender-query-parameter",
70        long,
71        env = "SENDER_QUERY_PARAMETER",
72        value_delimiter = ','
73    )]
74    pub query: Vec<String>,
75
76    #[command(flatten)]
77    pub oidc: OpenIdTokenProviderConfigArguments,
78}
79
80impl SendArguments {
81    pub async fn into_visitor(self) -> Result<SendVisitor, anyhow::Error> {
82        let SendArguments {
83            target,
84            connect_timeout,
85            timeout,
86            additional_root_certificates,
87            tls_insecure,
88            retries,
89            min_delay,
90            max_delay,
91            oidc,
92            query,
93        } = self;
94
95        let provider = oidc.into_provider().await?;
96        let sender = HttpSender::new(
97            provider,
98            HttpSenderOptions::default()
99                .connect_timeout(Some(connect_timeout.into()))
100                .timeout(Some(timeout.into()))
101                .tls_insecure(tls_insecure)
102                .additional_root_certificates(additional_root_certificates)
103                .query_parameters(query.into_iter().map(|entry| match entry.split_once('=') {
104                    Some((key, value)) => (key.to_string(), value.to_string()),
105                    None => (entry, "".to_string()),
106                })),
107        )
108        .await?;
109
110        Ok(SendVisitor::new(target, sender)
111            .retries(retries)
112            .min_delay(min_delay)
113            .max_delay(max_delay))
114    }
115}