forked from jmgilman/vaultrs
-
Notifications
You must be signed in to change notification settings - Fork 0
/
aws.rs
486 lines (400 loc) · 16.6 KB
/
aws.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
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
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
#[macro_use]
extern crate tracing;
mod common;
use common::{LocalStackServer, VaultServer, VaultServerHelper};
use test_log::test;
use vaultrs::client::Client;
use vaultrs::error::ClientError;
#[test]
fn test() {
let test = common::new_aws_test();
test.run(|instance| async move {
let server: VaultServer = instance.server();
let localstack: LocalStackServer = instance.server();
let client = server.client();
let endpoint = setup(&server, &client).await.unwrap();
crate::config::client::test_set(&localstack, &client, &endpoint).await;
crate::config::client::test_read(&client, &endpoint).await;
crate::config::client::test_delete(&client, &endpoint).await;
crate::config::identity::test_set(&client, &endpoint).await;
crate::config::identity::test_read(&client, &endpoint).await;
crate::config::certificate::test_create(&client, &endpoint).await;
crate::config::certificate::test_read(&client, &endpoint).await;
crate::config::certificate::test_list(&client, &endpoint).await;
crate::config::certificate::test_delete(&client, &endpoint).await;
crate::config::sts::test_create(&client, &endpoint).await;
crate::config::sts::test_read(&client, &endpoint).await;
crate::config::sts::test_list(&client, &endpoint).await;
crate::config::sts::test_delete(&client, &endpoint).await;
crate::config::tidy::identity_access_list::test_set(&client, &endpoint).await;
crate::config::tidy::identity_access_list::test_read(&client, &endpoint).await;
crate::config::tidy::identity_access_list::test_delete(&client, &endpoint).await;
crate::config::tidy::role_tag_deny_list::test_set(&client, &endpoint).await;
crate::config::tidy::role_tag_deny_list::test_read(&client, &endpoint).await;
crate::config::tidy::role_tag_deny_list::test_delete(&client, &endpoint).await;
crate::role::test_create_iam(&client, &endpoint).await;
crate::role::test_create_ec2(&client, &endpoint).await;
crate::role::test_read(&client, &endpoint).await;
crate::role::test_list(&client, &endpoint).await;
let role_tag = crate::role::test_create_tag(&client, &endpoint).await;
crate::role_tag_deny_list::test_create(&client, &endpoint, &role_tag).await;
crate::role_tag_deny_list::test_read(&client, &endpoint, &role_tag).await;
crate::role_tag_deny_list::test_list(&client, &endpoint, &role_tag).await;
crate::role_tag_deny_list::test_tidy(&client, &endpoint).await;
crate::role_tag_deny_list::test_delete(&client, &endpoint, &role_tag).await;
// role is needed for role_tag_deny_list operations
crate::role::test_delete(&client, &endpoint).await;
crate::identity_access_list::test_list(&client, &endpoint).await;
crate::identity_access_list::test_tidy(&client, &endpoint).await;
});
}
#[derive(Debug)]
pub struct AwsAuthEndpoint {
pub path: String,
pub role_name: String,
}
async fn setup(server: &VaultServer, client: &impl Client) -> Result<AwsAuthEndpoint, ClientError> {
debug!("setting up AWS auth engine");
let path = "aws_test";
let role_name = "test";
// Mount the AppRole auth engine
server.mount_auth(client, path, "aws").await?;
// configure aws client
Ok(AwsAuthEndpoint {
path: path.to_string(),
role_name: role_name.to_string(),
})
}
mod config {
pub mod client {
use dockertest_server::servers::cloud::localstack::LocalStackServer;
use vaultrs::{api::auth::aws::requests::ConfigureClientRequest, auth::aws};
use crate::{AwsAuthEndpoint, Client};
pub async fn test_set(
localstack: &LocalStackServer,
client: &impl Client,
endpoint: &AwsAuthEndpoint,
) {
let res = aws::config::client::set(
client,
&endpoint.path,
Some(
&mut ConfigureClientRequest::builder()
.access_key("test")
.secret_key("test")
.sts_region("local")
.endpoint(localstack.internal_url())
.sts_endpoint(localstack.internal_url())
.iam_endpoint(localstack.internal_url()),
),
)
.await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::client::read(client, &endpoint.path).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().access_key, Some("test".to_string()));
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::client::delete(client, &endpoint.path).await;
assert!(res.is_ok());
}
}
pub mod identity {
use vaultrs::{api::auth::aws::requests::ConfigureIdentityRequest, auth::aws};
use crate::{AwsAuthEndpoint, Client};
pub async fn test_set(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::identity::set(
client,
&endpoint.path,
Some(
&mut ConfigureIdentityRequest::builder()
.iam_alias("unique_id")
.ec2_alias("instance_id"),
),
)
.await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::identity::read(client, &endpoint.path).await;
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res.iam_alias, Some("unique_id".to_string()));
assert_eq!(res.ec2_alias, Some("instance_id".to_string()));
}
}
pub mod certificate {
use vaultrs::auth::aws;
use crate::{AwsAuthEndpoint, Client};
const CERT_NAME: &str = "test_cert";
const CERT: &str = include_str!("files/aws.crt");
pub async fn test_create(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::certificate::create(
client,
&endpoint.path,
CERT_NAME,
&base64::encode(CERT),
None,
)
.await;
assert!(res.is_ok());
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::certificate::read(client, &endpoint.path, CERT_NAME).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().aws_public_cert, CERT)
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::certificate::delete(client, &endpoint.path, CERT_NAME).await;
assert!(res.is_ok())
}
pub async fn test_list(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::certificate::list(client, &endpoint.path).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().keys, vec![CERT_NAME])
}
}
pub mod sts {
use vaultrs::auth::aws;
use crate::{AwsAuthEndpoint, Client};
const SATELLITE_ACCOUNT_ID: &str = "000000000001";
const ROLE_NAME: &str = "SomeRole";
pub async fn test_create(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res =
aws::config::sts::create(client, &endpoint.path, SATELLITE_ACCOUNT_ID, ROLE_NAME)
.await;
assert!(res.is_ok())
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::sts::read(client, &endpoint.path, SATELLITE_ACCOUNT_ID).await;
assert!(res.is_ok());
assert!(res.unwrap().sts_role.ends_with(ROLE_NAME));
}
pub async fn test_list(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::sts::list(client, &endpoint.path).await;
assert!(res.is_ok());
assert_eq!(res.unwrap().keys, [SATELLITE_ACCOUNT_ID]);
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::sts::delete(client, &endpoint.path, SATELLITE_ACCOUNT_ID).await;
assert!(res.is_ok())
}
}
pub mod tidy {
pub mod identity_access_list {
use vaultrs::{
api::auth::aws::requests::ConfigureIdentityAccessListTidyOperationRequest,
auth::aws,
};
use crate::{AwsAuthEndpoint, Client};
pub async fn test_set(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::tidy::identity_access_list::set(
client,
&endpoint.path,
Some(
&mut ConfigureIdentityAccessListTidyOperationRequest::builder()
.safety_buffer("24h")
.disable_periodic_tidy(true),
),
)
.await;
assert!(res.is_ok())
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res =
aws::config::tidy::identity_access_list::read(client, &endpoint.path).await;
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res.safety_buffer, 86400);
assert_eq!(res.disable_periodic_tidy, true);
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res =
aws::config::tidy::identity_access_list::read(client, &endpoint.path).await;
assert!(res.is_ok());
}
}
pub mod role_tag_deny_list {
use vaultrs::{
api::auth::aws::requests::ConfigureRoleTagDenyListTidyOperationRequest, auth::aws,
};
use crate::{AwsAuthEndpoint, Client};
pub async fn test_set(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::tidy::role_tag_deny_list::set(
client,
&endpoint.path,
Some(
&mut ConfigureRoleTagDenyListTidyOperationRequest::builder()
.safety_buffer("24h"),
),
)
.await;
assert!(res.is_ok())
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::tidy::role_tag_deny_list::read(client, &endpoint.path).await;
assert!(res.is_ok());
let res = res.unwrap();
assert_eq!(res.safety_buffer, 86400);
assert_eq!(res.disable_periodic_tidy, false);
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::config::tidy::role_tag_deny_list::read(client, &endpoint.path).await;
assert!(res.is_ok());
}
}
}
}
mod role {
use vaultrs::{
api::auth::aws::{
requests::{CreateRoleRequest, CreateRoleTagRequest},
responses::CreateRoleTagResponse,
},
auth::aws,
};
use crate::{AwsAuthEndpoint, Client};
const ROLE_NAME_IAM: &str = "test_role_iam";
const ROLE_NAME_EC2: &str = "test_role_ec2";
pub async fn test_create_iam(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role::create(
client,
&endpoint.path,
ROLE_NAME_IAM,
Some(
&mut CreateRoleRequest::builder()
.auth_type("iam")
.bound_iam_principal_arn(["000000000001".to_string()])
.resolve_aws_unique_ids(false),
),
)
.await;
assert!(res.is_ok())
}
pub async fn test_create_ec2(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role::create(
client,
&endpoint.path,
ROLE_NAME_EC2,
Some(
&mut CreateRoleRequest::builder()
.auth_type("ec2")
.role_tag("Testing")
.bound_ec2_instance_id(["i-1234567890abcdef0".to_string()]),
),
)
.await;
assert!(res.is_ok())
}
pub async fn test_read(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role::read(client, &endpoint.path, ROLE_NAME_IAM).await;
assert!(res.is_ok());
assert_eq!(
res.unwrap().bound_iam_principal_arn.unwrap_or_default(),
["000000000001"]
);
}
pub async fn test_list(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role::list(client, &endpoint.path).await;
assert!(res.is_ok());
let res = res.unwrap();
assert!(res.keys.contains(&ROLE_NAME_IAM.to_string()));
assert!(res.keys.contains(&ROLE_NAME_EC2.to_string()));
}
pub async fn test_delete(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role::delete(client, &endpoint.path, ROLE_NAME_IAM).await;
assert!(res.is_ok());
let res = aws::role::delete(client, &endpoint.path, ROLE_NAME_EC2).await;
assert!(res.is_ok());
}
pub async fn test_create_tag(
client: &impl Client,
endpoint: &AwsAuthEndpoint,
) -> CreateRoleTagResponse {
// role_tag is only used with ec2 auth method
let res = aws::role::create_tag(
client,
&endpoint.path,
ROLE_NAME_EC2,
Some(&mut CreateRoleTagRequest::builder().max_ttl("48h")),
)
.await;
assert!(res.is_ok());
res.unwrap()
}
}
mod identity_access_list {
use vaultrs::{api::auth::aws::requests::TidyIdentityAccessListEntriesRequest, auth::aws};
use crate::{AwsAuthEndpoint, Client, ClientError};
pub async fn test_list(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::identity_access_list::list(client, &endpoint.path).await;
assert!(match res {
// vault returns 404 instead of empty list
// https://github.com/hashicorp/vault/issues/1365
Err(ClientError::APIError { code, errors: _ }) => code == 404,
_ => false,
})
}
pub async fn test_tidy(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::identity_access_list::tidy(
client,
&endpoint.path,
Some(&mut TidyIdentityAccessListEntriesRequest::builder().safety_buffer("12h")),
)
.await;
assert!(res.is_ok());
}
}
mod role_tag_deny_list {
use vaultrs::{
api::auth::aws::{requests::TidyDenyListTagsRequest, responses::CreateRoleTagResponse},
auth::aws,
};
use crate::{AwsAuthEndpoint, Client};
pub async fn test_create(
client: &impl Client,
endpoint: &AwsAuthEndpoint,
role_tag: &CreateRoleTagResponse,
) {
let res =
aws::role_tag_deny_list::create(client, &endpoint.path, &role_tag.tag_value).await;
assert!(res.is_ok());
}
pub async fn test_read(
client: &impl Client,
endpoint: &AwsAuthEndpoint,
role_tag: &CreateRoleTagResponse,
) {
let res = aws::role_tag_deny_list::read(client, &endpoint.path, &role_tag.tag_value).await;
assert!(res.is_ok());
}
pub async fn test_list(
client: &impl Client,
endpoint: &AwsAuthEndpoint,
role_tag: &CreateRoleTagResponse,
) {
let res = aws::role_tag_deny_list::list(client, &endpoint.path).await;
assert!(res.is_ok());
assert!(res.unwrap().keys.contains(&role_tag.tag_value));
}
pub async fn test_delete(
client: &impl Client,
endpoint: &AwsAuthEndpoint,
role_tag: &CreateRoleTagResponse,
) {
let res =
aws::role_tag_deny_list::delete(client, &endpoint.path, &role_tag.tag_value).await;
assert!(res.is_ok());
}
pub async fn test_tidy(client: &impl Client, endpoint: &AwsAuthEndpoint) {
let res = aws::role_tag_deny_list::tidy(
client,
&endpoint.path,
Some(&mut TidyDenyListTagsRequest::builder().safety_buffer("8h")),
)
.await;
assert!(res.is_ok())
}
}