|
| 1 | +use crate::aws_api::arn::AwsArn; |
| 2 | +use crate::aws_api::auth::{AwsRequestSigner, SystemClock}; |
| 3 | +use crate::aws_api::client::AwsClient; |
| 4 | +use crate::aws_api::error::Error; |
| 5 | +use http::header::CONTENT_TYPE; |
| 6 | +use http::{HeaderMap, HeaderValue, Method, Uri}; |
| 7 | +use serde::Deserialize; |
| 8 | +use serde_json::json; |
| 9 | +use std::collections::HashMap; |
| 10 | +use tracing::error; |
| 11 | + |
| 12 | +pub struct ParameterStore<'a> { |
| 13 | + client: &'a AwsClient, |
| 14 | + service_name: &'static str, |
| 15 | +} |
| 16 | + |
| 17 | +#[derive(Debug, Deserialize)] |
| 18 | +pub struct GetParametersResponse { |
| 19 | + /// The parameter object. |
| 20 | + #[serde(rename = "Parameters")] |
| 21 | + pub parameters: Vec<Parameter>, |
| 22 | + |
| 23 | + #[serde(rename = "InvalidParameters")] |
| 24 | + pub invalid_parameters: Vec<InvalidParameters>, |
| 25 | +} |
| 26 | + |
| 27 | +#[derive(Debug, Deserialize)] |
| 28 | +pub struct InvalidParameters { |
| 29 | + #[serde(rename = "Name")] |
| 30 | + pub name: String, |
| 31 | +} |
| 32 | + |
| 33 | +#[derive(Debug, Deserialize)] |
| 34 | +pub struct Parameter { |
| 35 | + /// The Amazon Resource Name (ARN) of the parameter. |
| 36 | + #[serde(rename = "ARN")] |
| 37 | + pub arn: Option<String>, |
| 38 | + |
| 39 | + /// The data type of the parameter, such as text, aws:ec2:image, or aws:tag-specification. |
| 40 | + #[serde(rename = "DataType")] |
| 41 | + pub data_type: Option<String>, |
| 42 | + |
| 43 | + /// The last modification date of the parameter. |
| 44 | + #[serde(rename = "LastModifiedDate")] |
| 45 | + pub last_modified_date: Option<f64>, |
| 46 | + |
| 47 | + /// The name of the parameter. |
| 48 | + #[serde(rename = "Name")] |
| 49 | + pub name: String, |
| 50 | + |
| 51 | + /// The unique identifier for the parameter version. |
| 52 | + #[serde(rename = "Selector")] |
| 53 | + pub selector: Option<String>, |
| 54 | + |
| 55 | + /// The parameter source. |
| 56 | + #[serde(rename = "SourceResult")] |
| 57 | + pub source_result: Option<String>, |
| 58 | + |
| 59 | + /// The parameter type. |
| 60 | + #[serde(rename = "Type")] |
| 61 | + pub type_: String, |
| 62 | + |
| 63 | + /// The parameter value. |
| 64 | + #[serde(rename = "Value")] |
| 65 | + pub value: String, |
| 66 | + |
| 67 | + /// The parameter version. |
| 68 | + #[serde(rename = "Version")] |
| 69 | + pub version: Option<i64>, |
| 70 | + |
| 71 | + /// Tags associated with the parameter. |
| 72 | + #[serde(rename = "Tags")] |
| 73 | + pub tags: Option<HashMap<String, String>>, |
| 74 | +} |
| 75 | + |
| 76 | +impl<'a> ParameterStore<'a> { |
| 77 | + pub(crate) fn new(client: &'a AwsClient) -> Self { |
| 78 | + Self { |
| 79 | + client, |
| 80 | + service_name: "ssm", |
| 81 | + } |
| 82 | + } |
| 83 | + |
| 84 | + pub async fn get_parameters( |
| 85 | + &self, |
| 86 | + param_arns: &[String], |
| 87 | + ) -> Result<HashMap<String, Parameter>, Error> { |
| 88 | + let mut arns_by_endpoint = HashMap::new(); |
| 89 | + for arn_str in param_arns { |
| 90 | + let arn = arn_str.parse::<AwsArn>()?; |
| 91 | + if arn.service != self.service_name { |
| 92 | + return Err(Error::ArnParseError(arn_str.clone())); |
| 93 | + } |
| 94 | + |
| 95 | + arns_by_endpoint |
| 96 | + .entry(arn.get_endpoint()) |
| 97 | + .or_insert_with(|| Vec::new()) |
| 98 | + .push(arn); |
| 99 | + } |
| 100 | + |
| 101 | + let mut res = HashMap::new(); |
| 102 | + for (endpoint, arns) in &arns_by_endpoint { |
| 103 | + let endpoint = endpoint.parse::<Uri>()?; |
| 104 | + |
| 105 | + let payload = json!({ |
| 106 | + "Names": arns.iter().map(|arn| arn.to_string()).collect::<Vec<String>>(), |
| 107 | + "WithDecryption": true, |
| 108 | + }); |
| 109 | + |
| 110 | + let payload_bytes = serde_json::to_vec(&payload)?; |
| 111 | + |
| 112 | + let mut hdrs = HeaderMap::new(); |
| 113 | + hdrs.insert( |
| 114 | + "X-Amz-Target", |
| 115 | + HeaderValue::from_static("AmazonSSM.GetParameters"), |
| 116 | + ); |
| 117 | + hdrs.insert( |
| 118 | + CONTENT_TYPE, |
| 119 | + HeaderValue::from_static("application/x-amz-json-1.1"), |
| 120 | + ); |
| 121 | + |
| 122 | + // Sign the request |
| 123 | + let signer = AwsRequestSigner::new( |
| 124 | + self.service_name, |
| 125 | + &arns[0].region, |
| 126 | + &self.client.config.aws_access_key_id, |
| 127 | + &self.client.config.aws_secret_access_key, |
| 128 | + self.client.config.aws_session_token.as_deref(), |
| 129 | + SystemClock, |
| 130 | + ); |
| 131 | + let signed_request = signer.sign(endpoint, Method::POST, hdrs, payload_bytes)?; |
| 132 | + |
| 133 | + // Send the request |
| 134 | + let response = self.client.perform(signed_request).await?; |
| 135 | + |
| 136 | + let result: GetParametersResponse = serde_json::from_slice(response.as_ref())?; |
| 137 | + |
| 138 | + if !result.invalid_parameters.is_empty() { |
| 139 | + return Err(Error::InvalidParameters( |
| 140 | + result |
| 141 | + .invalid_parameters |
| 142 | + .into_iter() |
| 143 | + .map(|i| i.name) |
| 144 | + .collect(), |
| 145 | + )); |
| 146 | + } |
| 147 | + |
| 148 | + for param in result.parameters { |
| 149 | + if param.arn.is_none() { |
| 150 | + error!(parameter = param.name, "Parameter was missing ARN"); |
| 151 | + return Err(Error::InvalidParameters(vec![arns.into_iter().map(|arn|arn.to_string()).collect()])) |
| 152 | + } |
| 153 | + |
| 154 | + let arn = param.arn.clone().unwrap(); |
| 155 | + res.insert(arn, param); |
| 156 | + } |
| 157 | + } |
| 158 | + |
| 159 | + Ok(res) |
| 160 | + } |
| 161 | +} |
| 162 | + |
| 163 | +#[cfg(test)] |
| 164 | +mod tests { |
| 165 | + use super::*; |
| 166 | + use crate::aws_api::config::AwsConfig; |
| 167 | + use crate::aws_api::test_util::init_crypto; |
| 168 | + |
| 169 | + #[tokio::test] |
| 170 | + async fn test_basic_paramstore_retrieval() { |
| 171 | + // TEST_PARAMSTORE_ARNS should be set to a comma-separated list of k=v pairs, |
| 172 | + // where k is an ARN of a secret and v is the secret value to test against. |
| 173 | + let test_paramstore_arns = std::env::var("TEST_PARAMSTORE_ARNS"); |
| 174 | + if !test_paramstore_arns.is_ok() { |
| 175 | + println!("Skipping test_basic_paramstore_retrieval due to unset envvar"); |
| 176 | + return; |
| 177 | + } |
| 178 | + |
| 179 | + let test_arns: Vec<(String, String)> = test_paramstore_arns |
| 180 | + .unwrap() |
| 181 | + .split(",") |
| 182 | + .filter(|s| !s.is_empty()) |
| 183 | + .filter_map(|pair| { |
| 184 | + let parts: Vec<&str> = pair.splitn(2, '=').collect(); |
| 185 | + if parts.len() == 2 { |
| 186 | + Some((parts[0].trim().to_string(), parts[1].trim().to_string())) |
| 187 | + } else { |
| 188 | + None // Skip malformed pairs that don't have an equals sign |
| 189 | + } |
| 190 | + }) |
| 191 | + .collect(); |
| 192 | + |
| 193 | + init_crypto(); |
| 194 | + |
| 195 | + let client = AwsClient::new(AwsConfig::from_env()).unwrap(); |
| 196 | + |
| 197 | + let ps = client.parameter_store(); |
| 198 | + |
| 199 | + let arn_values = test_arns.iter().map(|arn| arn.0.clone()).collect::<Vec<String>>(); |
| 200 | + let res = ps.get_parameters(&arn_values).await.unwrap(); |
| 201 | + |
| 202 | + for test_arn in &test_arns { |
| 203 | + let entry = res.get(&test_arn.0).unwrap(); |
| 204 | + |
| 205 | + assert_eq!(test_arn.1, entry.value); |
| 206 | + } |
| 207 | + } |
| 208 | +} |
0 commit comments