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
198
use anyhow::{anyhow, Result};
use log::{error, info};
use rusoto_core::RusotoError;
use rusoto_ssm::GetParameterRequest;
use rusoto_ssm::PutParameterRequest;
use rusoto_ssm::{Ssm, SsmClient};
#[derive(Debug, Clone)]
pub struct Parameter {
pub name: String,
pub value: String,
pub description: String,
pub is_secure: bool,
}
impl Parameter {
pub fn new<S>(name: S, value: S, description: S, is_secure: bool) -> Parameter
where
S: Into<String>,
{
Parameter {
name: name.into(),
value: value.into(),
description: description.into(),
is_secure,
}
}
pub async fn update(&self, client: &SsmClient) -> Result<String> {
if self.needs_updating(client).await? {
info!("Parameter {} needs updating", self.name);
let parameter_request = self.to_put_parameter_request();
match client.put_parameter(parameter_request).await {
Ok(_parameter_result) => info!("Parameter {} successfully updated", self.name),
Err(error) => error!("Parameter {} failed to update: {}", self.name, error),
}
} else {
info!("Parameter {} does not need updating", self.name);
}
Ok(self.name.clone())
}
async fn needs_updating(&self, client: &SsmClient) -> Result<bool> {
match client.get_parameter(self.to_get_parameter_request()).await {
Ok(parameter_result) => match parameter_result.parameter {
Some(parameter) => match parameter.value {
Some(parameter_value) => {
info!(
"Found parameter {} with existing value: {}",
self.name, ¶meter_value
);
Ok(self.value != parameter_value)
}
None => Err(anyhow!("No value was found")),
},
None => Err(anyhow!("No parameter was returned")),
},
Err(error) => {
match error {
RusotoError::Credentials(credential_error) => error!(
"Error with credentials {}: {:?}",
self.name, credential_error.message
),
_ => error!("Could not retrieve parameter {}: {:?}", self.name, error),
};
Err(anyhow!("Failed while fetching parameter"))
}
}
}
fn to_get_parameter_request(&self) -> GetParameterRequest {
GetParameterRequest {
name: self.name.clone(),
with_decryption: Some(true),
}
}
fn to_put_parameter_request(&self) -> PutParameterRequest {
PutParameterRequest {
name: self.name.clone(),
value: self.value.clone(),
description: Some(self.description.clone()),
type_: if self.is_secure {
Some("SecureString".into())
} else {
Some("String".into())
},
overwrite: Some(true),
allowed_pattern: None,
key_id: None,
policies: None,
tags: None,
tier: None,
data_type: None,
}
}
}
#[cfg(test)]
mod tests {
use crate::parameter::Parameter;
#[test]
fn get_parameter_request_sets_with_decryption_true() {
let secure_parameter = Parameter::new("test_name", "test_value", "test_description", true);
let request = secure_parameter.to_get_parameter_request();
assert!(request.with_decryption.unwrap());
}
#[test]
fn get_parameter_request_sets_name() {
let secure_parameter = Parameter::new("test_name", "test_value", "test_description", true);
let request = secure_parameter.to_get_parameter_request();
assert_eq!(request.name, "test_name".to_string());
}
#[test]
fn put_parameter_request_sets_overwrite_true() {
let secure_parameter = Parameter::new("test_name", "test_value", "test_description", true);
let request = secure_parameter.to_put_parameter_request();
assert!(request.overwrite.unwrap());
}
#[test]
fn put_parameter_request_is_secure_sets_type_secure_string() {
let secure_parameter = Parameter::new("test_name", "test_value", "test_description", true);
let request = secure_parameter.to_put_parameter_request();
assert_eq!(request.type_, Some("SecureString".into()));
}
#[test]
fn put_parameter_request_is_not_secure_sets_type_string() {
let secure_parameter = Parameter::new("test_name", "test_value", "test_description", false);
let request = secure_parameter.to_put_parameter_request();
assert_eq!(request.type_, Some("String".into()));
}
}