-
Notifications
You must be signed in to change notification settings - Fork 7
/
ssm.rs
197 lines (191 loc) · 6.88 KB
/
ssm.rs
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
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
use crate::log;
use anyhow::Result;
use futures::future::join_all;
use std::collections::HashMap;
use tokio::{spawn, task::JoinHandle};
use aws_sdk_ssm::config::BehaviorVersion;
pub async fn get_envs(env_vars: HashMap<String, String>) -> Result<HashMap<String, String>> {
let sdk_config = aws_config::load_defaults(BehaviorVersion::v2024_03_28()).await;
let ssm_client: aws_sdk_ssm::Client = aws_sdk_ssm::Client::new(&sdk_config);
let mut results: HashMap<String, String> = HashMap::new();
let mut handles: Vec<JoinHandle<Result<HashMap<String, String>>>> = Vec::new();
for (name, path) in env_vars {
if path.starts_with("x-crypteia-ssm:") {
let ssm_clone = ssm_client.clone();
handles.push(spawn(async move {
ssm_get_parameter(&ssm_clone, name, path).await
}));
} else if path.starts_with("x-crypteia-ssm-path:") {
let ssm_clone = ssm_client.clone();
handles.push(spawn(async move {
ssm_get_parameters_by_path(&ssm_clone, name, path).await
}));
}
}
let tasks = join_all(handles).await;
for task in tasks {
match task {
Ok(result) => match result {
Ok(parameter) => {
parameter.into_iter().for_each(|(key, value)| {
results.insert(key, value);
});
}
Err(error) => log::cloudwatch_metric("ssm", "error", true, Some(error.to_string())),
},
Err(error) => log::cloudwatch_metric("ssm", "error", true, Some(error.to_string())),
}
}
Ok(results)
}
async fn ssm_get_parameter(
ssm: &aws_sdk_ssm::Client,
name: String,
path: String,
) -> Result<HashMap<String, String>> {
let mut items: HashMap<String, String> = HashMap::new();
let response = ssm
.get_parameter()
.name(path.replace("x-crypteia-ssm:", ""))
.with_decryption(true)
.send()
.await;
match response {
Ok(response) => {
if let Some(parameter) = response.parameter {
items.insert(name, parameter.value.unwrap());
}
}
Err(error) => {
log::cloudwatch_metric(
"ssm",
"error",
true,
Some(format!(
"Error calling ssm:GetParameter. Environment variable: {} Path: {} Error: {:?}",
name, path, error.into_service_error().meta()
)),
);
}
}
Ok(items)
}
async fn ssm_get_parameters_by_path(
ssm: &aws_sdk_ssm::Client,
name: String,
path: String,
) -> Result<HashMap<String, String>> {
let mut items: HashMap<String, String> = HashMap::new();
let mut token: Option<String> = None;
loop {
let ssm_path = path.replace("x-crypteia-ssm-path:", "");
let response = ssm
.get_parameters_by_path()
.path(ssm_path.clone())
.recursive(true)
.with_decryption(true)
.set_next_token(token.clone())
.send()
.await;
match response {
Ok(response) => {
if let Some(parameters) = response.parameters {
for parameter in parameters {
let path_prefix = parameter.name.unwrap();
let ssm_clone = ssm_path.clone();
let ssm_path_replace = ssm_clone + "/";
let env_name = path_prefix.replace(ssm_path_replace.as_str(), "");
items.insert(env_name, parameter.value.unwrap());
}
}
if response.next_token == None {
break;
}
token = response.next_token;
}
Err(error) => {
log::cloudwatch_metric(
"ssm",
"error",
true,
Some(format!(
"Error calling ssm:GetParametersByPath. Environment variable: {} Path: {} Error: {:?}",
name, path, error.into_service_error().meta()
)),
);
break;
}
}
}
Ok(items)
}
#[cfg(test)]
mod test {
use super::*;
use anyhow::Result;
use aws_sdk_ssm::types::ParameterType;
use std::collections::HashMap;
#[tokio::test]
async fn should_parse() -> Result<()> {
let sdk_config = aws_config::load_defaults(BehaviorVersion::v2024_03_28()).await;
let ssm_client = aws_sdk_ssm::Client::new(&sdk_config);
ssm_client
.put_parameter()
.name("/crypteia/v5/myapp/SECRET".to_owned())
.value("1A2B3C4D5E6F".to_owned())
.r#type(ParameterType::SecureString)
.overwrite(true)
.send()
.await?;
ssm_client
.put_parameter()
.name("/crypteia/v5/myapp/access-key".to_owned())
.value("G7H8I9J0K1L2".to_owned())
.r#type(ParameterType::SecureString)
.overwrite(true)
.send()
.await?;
ssm_client
.put_parameter()
.name("/crypteia/v5/myapp/envs/DB_URL".to_owned())
.value("mysql2://u:p@host:3306".to_owned())
.r#type(ParameterType::SecureString)
.overwrite(true)
.send()
.await?;
ssm_client
.put_parameter()
.name("/crypteia/v5/myapp/envs/NR_KEY".to_owned())
.value("z6y5x4w3v2u1".to_owned())
.r#type(ParameterType::SecureString)
.overwrite(true)
.send()
.await?;
let env_vars: HashMap<String, String> = HashMap::from([
("EXISTING".to_string(), "existingvalue".to_string()),
(
"SECRET".to_string(),
"x-crypteia-ssm:/crypteia/v5/myapp/SECRET".to_string(),
),
(
"ACCESS_KEY".to_string(),
"x-crypteia-ssm:/crypteia/v5/myapp/access-key".to_string(),
),
(
"X_CRYPTEIA_SSM".to_string(),
"x-crypteia-ssm-path:/crypteia/v5/myapp/envs".to_string(),
),
("DB_URL".to_string(), "x-crypteia".to_string()),
("NR_KEY".to_string(), "x-crypteia".to_string()),
]);
let expected: HashMap<String, String> = HashMap::from([
("SECRET".to_string(), "1A2B3C4D5E6F".to_string()),
("ACCESS_KEY".to_string(), "G7H8I9J0K1L2".to_string()),
("DB_URL".to_string(), "mysql2://u:p@host:3306".to_string()),
("NR_KEY".to_string(), "z6y5x4w3v2u1".to_string()),
]);
let results = get_envs(env_vars).await.expect("Should fetch parameters");
assert_eq!(results, expected);
Ok(())
}
}