|
| 1 | +// Unless explicitly stated otherwise all files in this repository are licensed under the Apache-2.0 License. |
| 2 | +// This product includes software developed at Datadog (https://www.datadoghq.com/). |
| 3 | +// Copyright 2019-Present Datadog, Inc. |
| 4 | +use crate::datadog; |
| 5 | +use reqwest::header::{HeaderMap, HeaderValue}; |
| 6 | +use serde::{Deserialize, Serialize}; |
| 7 | + |
| 8 | +/// ListAwsScanOptionsError is a struct for typed errors of method [`AgentlessScanningAPI::list_aws_scan_options`] |
| 9 | +#[derive(Debug, Clone, Serialize, Deserialize)] |
| 10 | +#[serde(untagged)] |
| 11 | +pub enum ListAwsScanOptionsError { |
| 12 | + APIErrorResponse(crate::datadogV2::model::APIErrorResponse), |
| 13 | + UnknownValue(serde_json::Value), |
| 14 | +} |
| 15 | + |
| 16 | +/// Datadog Agentless Scanning provides visibility into risks and vulnerabilities |
| 17 | +/// within your hosts, running containers, and serverless functions—all without |
| 18 | +/// requiring teams to install Agents on every host or where Agents cannot be installed. |
| 19 | +/// Go to <https://www.datadoghq.com/blog/agentless-scanning/> to learn more |
| 20 | +#[derive(Debug, Clone)] |
| 21 | +pub struct AgentlessScanningAPI { |
| 22 | + config: datadog::Configuration, |
| 23 | + client: reqwest_middleware::ClientWithMiddleware, |
| 24 | +} |
| 25 | + |
| 26 | +impl Default for AgentlessScanningAPI { |
| 27 | + fn default() -> Self { |
| 28 | + Self::with_config(datadog::Configuration::default()) |
| 29 | + } |
| 30 | +} |
| 31 | + |
| 32 | +impl AgentlessScanningAPI { |
| 33 | + pub fn new() -> Self { |
| 34 | + Self::default() |
| 35 | + } |
| 36 | + pub fn with_config(config: datadog::Configuration) -> Self { |
| 37 | + let mut reqwest_client_builder = reqwest::Client::builder(); |
| 38 | + |
| 39 | + if let Some(proxy_url) = &config.proxy_url { |
| 40 | + let proxy = reqwest::Proxy::all(proxy_url).expect("Failed to parse proxy URL"); |
| 41 | + reqwest_client_builder = reqwest_client_builder.proxy(proxy); |
| 42 | + } |
| 43 | + |
| 44 | + let mut middleware_client_builder = |
| 45 | + reqwest_middleware::ClientBuilder::new(reqwest_client_builder.build().unwrap()); |
| 46 | + |
| 47 | + if config.enable_retry { |
| 48 | + struct RetryableStatus; |
| 49 | + impl reqwest_retry::RetryableStrategy for RetryableStatus { |
| 50 | + fn handle( |
| 51 | + &self, |
| 52 | + res: &Result<reqwest::Response, reqwest_middleware::Error>, |
| 53 | + ) -> Option<reqwest_retry::Retryable> { |
| 54 | + match res { |
| 55 | + Ok(success) => reqwest_retry::default_on_request_success(success), |
| 56 | + Err(_) => None, |
| 57 | + } |
| 58 | + } |
| 59 | + } |
| 60 | + let backoff_policy = reqwest_retry::policies::ExponentialBackoff::builder() |
| 61 | + .build_with_max_retries(config.max_retries); |
| 62 | + |
| 63 | + let retry_middleware = |
| 64 | + reqwest_retry::RetryTransientMiddleware::new_with_policy_and_strategy( |
| 65 | + backoff_policy, |
| 66 | + RetryableStatus, |
| 67 | + ); |
| 68 | + |
| 69 | + middleware_client_builder = middleware_client_builder.with(retry_middleware); |
| 70 | + } |
| 71 | + |
| 72 | + let client = middleware_client_builder.build(); |
| 73 | + |
| 74 | + Self { config, client } |
| 75 | + } |
| 76 | + |
| 77 | + pub fn with_client_and_config( |
| 78 | + config: datadog::Configuration, |
| 79 | + client: reqwest_middleware::ClientWithMiddleware, |
| 80 | + ) -> Self { |
| 81 | + Self { config, client } |
| 82 | + } |
| 83 | + |
| 84 | + /// Fetches the scan options configured for AWS accounts. |
| 85 | + pub async fn list_aws_scan_options( |
| 86 | + &self, |
| 87 | + ) -> Result< |
| 88 | + crate::datadogV2::model::AwsScanOptionsResponse, |
| 89 | + datadog::Error<ListAwsScanOptionsError>, |
| 90 | + > { |
| 91 | + match self.list_aws_scan_options_with_http_info().await { |
| 92 | + Ok(response_content) => { |
| 93 | + if let Some(e) = response_content.entity { |
| 94 | + Ok(e) |
| 95 | + } else { |
| 96 | + Err(datadog::Error::Serde(serde::de::Error::custom( |
| 97 | + "response content was None", |
| 98 | + ))) |
| 99 | + } |
| 100 | + } |
| 101 | + Err(err) => Err(err), |
| 102 | + } |
| 103 | + } |
| 104 | + |
| 105 | + /// Fetches the scan options configured for AWS accounts. |
| 106 | + pub async fn list_aws_scan_options_with_http_info( |
| 107 | + &self, |
| 108 | + ) -> Result< |
| 109 | + datadog::ResponseContent<crate::datadogV2::model::AwsScanOptionsResponse>, |
| 110 | + datadog::Error<ListAwsScanOptionsError>, |
| 111 | + > { |
| 112 | + let local_configuration = &self.config; |
| 113 | + let operation_id = "v2.list_aws_scan_options"; |
| 114 | + |
| 115 | + let local_client = &self.client; |
| 116 | + |
| 117 | + let local_uri_str = format!( |
| 118 | + "{}/api/v2/agentless_scanning/accounts/aws", |
| 119 | + local_configuration.get_operation_host(operation_id) |
| 120 | + ); |
| 121 | + let mut local_req_builder = |
| 122 | + local_client.request(reqwest::Method::GET, local_uri_str.as_str()); |
| 123 | + |
| 124 | + // build headers |
| 125 | + let mut headers = HeaderMap::new(); |
| 126 | + headers.insert("Accept", HeaderValue::from_static("application/json")); |
| 127 | + |
| 128 | + // build user agent |
| 129 | + match HeaderValue::from_str(local_configuration.user_agent.as_str()) { |
| 130 | + Ok(user_agent) => headers.insert(reqwest::header::USER_AGENT, user_agent), |
| 131 | + Err(e) => { |
| 132 | + log::warn!("Failed to parse user agent header: {e}, falling back to default"); |
| 133 | + headers.insert( |
| 134 | + reqwest::header::USER_AGENT, |
| 135 | + HeaderValue::from_static(datadog::DEFAULT_USER_AGENT.as_str()), |
| 136 | + ) |
| 137 | + } |
| 138 | + }; |
| 139 | + |
| 140 | + // build auth |
| 141 | + if let Some(local_key) = local_configuration.auth_keys.get("apiKeyAuth") { |
| 142 | + headers.insert( |
| 143 | + "DD-API-KEY", |
| 144 | + HeaderValue::from_str(local_key.key.as_str()) |
| 145 | + .expect("failed to parse DD-API-KEY header"), |
| 146 | + ); |
| 147 | + }; |
| 148 | + if let Some(local_key) = local_configuration.auth_keys.get("appKeyAuth") { |
| 149 | + headers.insert( |
| 150 | + "DD-APPLICATION-KEY", |
| 151 | + HeaderValue::from_str(local_key.key.as_str()) |
| 152 | + .expect("failed to parse DD-APPLICATION-KEY header"), |
| 153 | + ); |
| 154 | + }; |
| 155 | + |
| 156 | + local_req_builder = local_req_builder.headers(headers); |
| 157 | + let local_req = local_req_builder.build()?; |
| 158 | + log::debug!("request content: {:?}", local_req.body()); |
| 159 | + let local_resp = local_client.execute(local_req).await?; |
| 160 | + |
| 161 | + let local_status = local_resp.status(); |
| 162 | + let local_content = local_resp.text().await?; |
| 163 | + log::debug!("response content: {}", local_content); |
| 164 | + |
| 165 | + if !local_status.is_client_error() && !local_status.is_server_error() { |
| 166 | + match serde_json::from_str::<crate::datadogV2::model::AwsScanOptionsResponse>( |
| 167 | + &local_content, |
| 168 | + ) { |
| 169 | + Ok(e) => { |
| 170 | + return Ok(datadog::ResponseContent { |
| 171 | + status: local_status, |
| 172 | + content: local_content, |
| 173 | + entity: Some(e), |
| 174 | + }) |
| 175 | + } |
| 176 | + Err(e) => return Err(datadog::Error::Serde(e)), |
| 177 | + }; |
| 178 | + } else { |
| 179 | + let local_entity: Option<ListAwsScanOptionsError> = |
| 180 | + serde_json::from_str(&local_content).ok(); |
| 181 | + let local_error = datadog::ResponseContent { |
| 182 | + status: local_status, |
| 183 | + content: local_content, |
| 184 | + entity: local_entity, |
| 185 | + }; |
| 186 | + Err(datadog::Error::ResponseError(local_error)) |
| 187 | + } |
| 188 | + } |
| 189 | +} |
0 commit comments