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
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
use std::borrow::{Borrow, Cow};
use std::collections::HashMap;
use std::time::Duration;
use lazy_static::lazy_static;
use log::{debug, info};
use rusoto_core::credential::AwsCredentials;
use rusoto_core::param::{Params, ServiceParams};
use rusoto_core::signature::{SignedRequest, SignedRequestPayload};
use rusoto_core::Region;
use serde::{Deserialize, Serialize};
#[derive(Serialize, Deserialize, Debug, Eq, PartialEq, Clone)]
pub struct AwsAuthIamPayload {
pub iam_http_request_method: String,
pub iam_request_url: String,
pub iam_request_body: String,
pub iam_request_headers: HashMap<String, Vec<String>>,
}
impl AwsAuthIamPayload {
pub fn new<R>(
credentials: &AwsCredentials,
region: Option<R>,
additional_headers: HashMap<&str, &str>,
) -> Self
where
R: Borrow<Region>,
{
info!("Building Login Payload for AWS authentication");
let region = region
.as_ref()
.map(|r| Cow::Borrowed(r.borrow()))
.unwrap_or_default();
let mut request = SignedRequest::new("POST", "sts", ®ion, "/");
let mut params = Params::new();
params.put("Action", "GetCallerIdentity");
params.put("Version", "2011-06-15");
request.set_payload(Some(
serde_urlencoded::to_string(¶ms).unwrap().into_bytes(),
));
request.set_content_type("application/x-www-form-urlencoded".to_owned());
for (header, value) in additional_headers.into_iter() {
request.add_header(header, value)
}
request.sign_with_plus(credentials, true);
let uri = format!(
"{}://{}{}",
request.scheme(),
request.hostname(),
request.canonical_path()
);
let payload = match request.payload {
Some(SignedRequestPayload::Buffer(ref buffer)) => base64::encode(buffer),
_ => unreachable!("Payload was set above"),
};
let headers = request
.headers
.iter()
.map(|(k, v)| {
let values = v
.iter()
.map(|v| unsafe { String::from_utf8_unchecked(v.to_vec()) })
.collect();
(k.to_string(), values)
})
.collect();
let result = Self {
iam_http_request_method: "POST".to_string(),
iam_request_url: base64::encode(&uri),
iam_request_body: payload,
iam_request_headers: headers,
};
debug!("AWS Payload: {:#?}", result);
result
}
}
#[allow(clippy::implicit_hasher)]
pub fn presigned_url<R>(
credentials: &AwsCredentials,
region: Option<R>,
additional_headers: HashMap<&str, &str>,
expires_in: Option<&Duration>,
) -> String
where
R: Borrow<Region>,
{
lazy_static! {
static ref DEFAULT_EXPIRES: Duration = Duration::from_secs(60);
}
info!("Building pre-signed URL for AWS authentication");
let region = region
.as_ref()
.map(|r| Cow::Borrowed(r.borrow()))
.unwrap_or_default();
let mut request = SignedRequest::new("GET", "sts", ®ion, "/");
let mut params = Params::new();
params.put("Action", "GetCallerIdentity");
params.put("Version", "2011-06-15");
request.set_params(params);
for (header, value) in additional_headers.into_iter() {
request.add_header(header, value)
}
request.generate_presigned_url(credentials, expires_in.unwrap_or(&DEFAULT_EXPIRES), true)
}
#[cfg(test)]
pub(crate) mod tests {
use super::*;
use rusoto_core::credential::ProvideAwsCredentials;
pub(crate) async fn credentials() -> Result<AwsCredentials, crate::Error> {
let provider = rusoto_mock::MockCredentialsProvider;
Ok(provider.credentials().await?)
}
pub(crate) async fn post_aws_iam_payload(
region: Option<Region>,
header: HashMap<&str, &str>,
) -> Result<AwsAuthIamPayload, crate::Error> {
let cred = credentials().await?;
Ok(AwsAuthIamPayload::new(&cred, region, header))
}
pub(crate) async fn get_presigned_url(
region: Option<Region>,
header: HashMap<&str, &str>,
) -> Result<String, crate::Error> {
let cred = credentials().await?;
Ok(presigned_url(&cred, region, header, None))
}
#[tokio::test(threaded_scheduler)]
async fn post_aws_iam_payload_has_expected_values() -> Result<(), crate::Error> {
let region = Region::UsEast1;
let headers = [("X-Vault-AWS-IAM-Server-ID", "vault.example.com")]
.iter()
.cloned()
.collect();
let payload = post_aws_iam_payload(Some(region.clone()), headers).await?;
assert_eq!(payload.iam_http_request_method, "POST");
assert_eq!(
payload.iam_request_url,
base64::encode(&format!("https://sts.{}.amazonaws.com/", region.name()))
);
assert_eq!(
payload.iam_request_body,
base64::encode("Action=GetCallerIdentity&Version=2011-06-15")
);
assert!(payload.iam_request_headers.contains_key("authorization"));
assert_eq!(
payload
.iam_request_headers
.get(&"X-Vault-AWS-IAM-Server-ID".to_lowercase()),
Some(&vec!["vault.example.com".to_string()])
);
Ok(())
}
#[tokio::test(threaded_scheduler)]
async fn presigned_url_has_expected_values() -> Result<(), crate::Error> {
let region = Region::UsEast1;
let headers = [("X-K8S-AWS-ID", "example")].iter().cloned().collect();
let url = get_presigned_url(Some(region), headers).await?;
let url = url::Url::parse(&url).unwrap();
assert_eq!(
url.host().unwrap().to_string(),
"sts.us-east-1.amazonaws.com"
);
let params: HashMap<_, _> = url.query_pairs().collect();
assert_eq!(params["Action"], "GetCallerIdentity");
assert_eq!(params["Version"], "2011-06-15");
assert_eq!(params["X-Amz-SignedHeaders"], "host;x-k8s-aws-id");
assert_eq!(params["X-Amz-Expires"], "60");
assert_eq!(params["X-Amz-Algorithm"], "AWS4-HMAC-SHA256");
assert!(params.contains_key("X-Amz-Signature"));
assert!(params.contains_key("X-Amz-Credential"));
assert!(params.contains_key("X-Amz-Date"));
Ok(())
}
}