walker_extras/visitors/send/
clap.rs

1use crate::visitors::SendVisitor;
2use reqwest::Url;
3use std::path::PathBuf;
4use walker_common::sender::{
5    provider::OpenIdTokenProviderConfigArguments, HttpSender, HttpSenderOptions,
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    /// Delay between retries
50    #[arg(
51        id = "sender-retry-delay",
52        long,
53        env = "SENDER_RETRY_DELAY",
54        default_value = "5s"
55    )]
56    pub retry_delay: humantime::Duration,
57
58    /// Custom query parameters
59    #[arg(
60        id = "sender-query-parameter",
61        long,
62        env = "SENDER_QUERY_PARAMETER",
63        value_delimiter = ','
64    )]
65    pub query: Vec<String>,
66
67    #[command(flatten)]
68    pub oidc: OpenIdTokenProviderConfigArguments,
69}
70
71impl SendArguments {
72    pub async fn into_visitor(self) -> Result<SendVisitor, anyhow::Error> {
73        let SendArguments {
74            target,
75            connect_timeout,
76            timeout,
77            additional_root_certificates,
78            tls_insecure,
79            retries,
80            retry_delay,
81            oidc,
82            query,
83        } = self;
84
85        let provider = oidc.into_provider().await?;
86        let sender = HttpSender::new(
87            provider,
88            HttpSenderOptions::default()
89                .connect_timeout(Some(connect_timeout.into()))
90                .timeout(Some(timeout.into()))
91                .tls_insecure(tls_insecure)
92                .additional_root_certificates(additional_root_certificates)
93                .query_parameters(query.into_iter().map(|entry| match entry.split_once('=') {
94                    Some((key, value)) => (key.to_string(), value.to_string()),
95                    None => (entry, "".to_string()),
96                })),
97        )
98        .await?;
99
100        Ok(SendVisitor {
101            url: target,
102            sender,
103            retries,
104            retry_delay: Some(retry_delay.into()),
105        })
106    }
107}