forked from cloud-custodian/cloud-custodian
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathtest_efs.py
629 lines (568 loc) · 21.8 KB
/
test_efs.py
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
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
# Copyright The Cloud Custodian Authors.
# SPDX-License-Identifier: Apache-2.0
from c7n.exceptions import PolicyValidationError
from .common import BaseTest, functional, event_data
from botocore.exceptions import ClientError
import uuid
import time
from operator import itemgetter
from c7n.testing import mock_datetime_now
from dateutil import parser
from pytest_terraform import terraform
import c7n.resources.efs
import c7n.filters.backup
import json
import pytest
class ElasticFileSystem(BaseTest):
@functional
def test_resource_manager(self):
factory = self.replay_flight_data("test_efs_query")
client = factory().client("efs")
token = str(uuid.uuid4())
fs_id = client.create_file_system(CreationToken=token).get("FileSystemId")
self.addCleanup(client.delete_file_system, FileSystemId=fs_id)
tags = [{"Key": "Name", "Value": "Somewhere"}]
client.create_tags(FileSystemId=fs_id, Tags=tags)
if self.recording:
time.sleep(5)
p = self.load_policy(
{
"name": "efs-query",
"resource": "efs",
"filters": [{"FileSystemId": fs_id}, {"tag:Name": "Somewhere"}],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Tags"], tags)
def test_mount_target_loading(self):
factory = self.replay_flight_data("test_efs_subresource")
p = self.load_policy(
{"name": "test-mount-targets", "resource": "efs-mount-target"},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 2)
def test_mount_target_security_group(self):
factory = self.replay_flight_data("test_efs_mount_secgroup")
p = self.load_policy(
{
"name": "test-mount-secgroup",
"resource": "efs-mount-target",
"filters": [
{
"type": "security-group",
"key": "GroupId",
"value": "sg-ccf3a8a4",
},
# Use the same filter twice to excercise cache code
{
"type": "security-group",
"key": "GroupId",
"value": "sg-ccf3a8a4",
},
],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 3)
resources = sorted(resources, key=itemgetter("MountTargetId"))
self.assertEqual(resources[0]["MountTargetId"], "fsmt-a47385dd")
def test_create_efs_mount_target(self):
factory = self.replay_flight_data("test_create_efs_mount_target")
policy = self.load_policy(
{
"name": "create-efs-mount-target",
"resource": "efs-mount-target",
"mode": {"type": "cloudtrail", "events": [{
"source": "elasticfilesystem.amazonaws.com",
"ids": "responseElements.mountTargetId",
"event": "CreateMountTarget"
}]},
},
session_factory=factory,
)
event = {
"detail": event_data("event-cloud-trail-create-efs-mount-target.json"),
"debug": True,
}
resources = policy.push(event, None)
self.assertEqual(len(resources), 1)
def test_modify_efs_mount_target_security_group(self):
factory = self.replay_flight_data("test_modify_efs_mount_target_security_group")
policy = self.load_policy(
{
"name": "modify-efs-mount-target-security-group",
"resource": "efs-mount-target",
"mode": {"type": "cloudtrail", "events": [{
"source": "elasticfilesystem.amazonaws.com",
"ids": "requestParameters.mountTargetId",
"event": "ModifyMountTargetSecurityGroups"
}]},
},
session_factory=factory,
)
event = {
"detail": event_data("event-cloud-trail-update-efs-mount-target-security-group.json"),
"debug": True,
}
resources = policy.push(event, None)
self.assertEqual(len(resources), 1)
def test_delete(self):
factory = self.replay_flight_data("test_efs_delete")
p = self.load_policy(
{
"name": "efs-query",
"resource": "efs",
"filters": [{"Name": "MyDocs"}],
"actions": ["delete"],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Name"], "MyDocs")
client = factory().client("efs")
state = client.describe_file_systems().get("FileSystems", [])
self.assertEqual(state, [])
def test_kms_alias(self):
factory = self.replay_flight_data("test_efs_kms_key_filter")
p = self.load_policy(
{
"name": "efs-kms-alias",
"resource": "efs",
"filters": [
{
"type": "kms-key",
"key": "c7n:AliasName",
"value": "^(alias/aws/)",
"op": "regex"
}
]
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(
resources[0]['KmsKeyId'],
'arn:aws:kms:us-east-1:644160558196:key/8785aeb9-a616-4e2b-bbd3-df3cde76bcc5') # NOQA
def test_enable_lifecycle_policy(self):
factory = self.replay_flight_data("test_enable_lifecycle_policy")
client = factory().client("efs")
res = client.describe_lifecycle_configuration(FileSystemId="fs-fac23c7a")
self.assertEqual(res.get('LifecyclePolicies'), [])
p = self.load_policy(
{
"name": "efs-lifecycle-policy",
"resource": "efs",
"filters": [{"Name": "c7n-test"}],
"actions": [
{
"type": "configure-lifecycle-policy",
"state": "enable",
"rules": [{'TransitionToIA': 'AFTER_7_DAYS'}],
}
]
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Name"], "c7n-test")
self.assertEqual(resources[0]["FileSystemId"], "fs-fac23c7a")
response = client.describe_lifecycle_configuration(FileSystemId="fs-fac23c7a")
self.assertEqual(response.get('LifecyclePolicies'), [{'TransitionToIA': 'AFTER_7_DAYS'}])
def test_disable_lifecycle_policy(self):
factory = self.replay_flight_data("test_disable_lifecycle_policy")
client = factory().client("efs")
res = client.describe_lifecycle_configuration(FileSystemId="fs-fac23c7a")
self.assertEqual(res.get('LifecyclePolicies'), [{'TransitionToIA': 'AFTER_7_DAYS'}])
p = self.load_policy(
{
"name": "efs-lifecycle-policy-disable",
"resource": "efs",
"filters": [{"Name": "c7n-test"}],
"actions": [
{
"type": "configure-lifecycle-policy",
"state": "disable",
}
]
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Name"], "c7n-test")
self.assertEqual(resources[0]["FileSystemId"], "fs-fac23c7a")
response = client.describe_lifecycle_configuration(FileSystemId="fs-fac23c7a")
self.assertEqual(response.get('LifecyclePolicies'), [])
def test_lifecycle_policy_validation_error(self):
self.assertRaises(
PolicyValidationError,
self.load_policy,
{
"name": "efs-lifecycle",
"resource": "efs",
"filters": [{"Name": "c7n-test"}],
"actions": [{"type": "configure-lifecycle-policy", "state": "enable"}],
}
)
def test_filter_lifecycle_policy_present(self):
factory = self.replay_flight_data("test_filter_lifecycle_policy_present")
p = self.load_policy(
{
"name": "efs-lifecycle-policy-enabled",
"resource": "efs",
"filters": [{"type": "lifecycle-policy",
"state": "present"}],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["FileSystemId"], "fs-5f61b0df")
def test_filter_lifecycle_policy_absent(self):
factory = self.replay_flight_data("test_filter_lifecycle_policy_absent")
p = self.load_policy(
{
"name": "efs-lifecycle-policy-disabled",
"resource": "efs",
"filters": [{"type": "lifecycle-policy",
"state": "absent"}],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["FileSystemId"], "fs-a4cc1c24")
def test_filter_lifecycle_policy_value(self):
factory = self.replay_flight_data("test_filter_lifecycle_policy_value")
p = self.load_policy(
{
"name": "efs-lifecycle-policy-enabled",
"resource": "efs",
"filters": [{"type": "lifecycle-policy",
"state": "present", "value": "AFTER_7_DAYS"}],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["FileSystemId"], "fs-5f61b0df")
def test_filter_securetransport_check(self):
factory = self.replay_flight_data("test_efs_filter_check_secure_transport")
p = self.load_policy(
{
"name": "efs-check-securetransport",
"resource": "efs",
"filters": [{"type": "check-secure-transport"}],
},
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Name"], "efs-without-secure-transport")
def test_efs_has_statement(self):
factory = self.replay_flight_data("test_efs_has_statement", region='us-west-1')
region_config = {'region': 'us-west-1'}
p = self.load_policy(
{
"name": "efs-has-statement",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Condition":
{"Bool": {"elasticfilesystem:AccessedViaMountTarget": "true"}},
"Resource": "{fs_arn}"
}
]
}
]
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
self.assertEqual(resources[0]["Name"], "efs-has-statement")
p = self.load_policy(
{
"name": "efs-has-statement",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Deny"
}
]
}
]
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 0)
def test_efs_has_statement_partial(self):
factory = self.replay_flight_data("test_efs_has_statement_partial",
region='us-west-1')
region_config = {'region': 'us-west-1'}
# No PartialMatch key as base case, full match.
p = self.load_policy(
{
"name": "efs-has-statement-partial",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Action": ["elasticfilesystem:ClientRootAccess",
"elasticfilesystem:ClientMount"],
}
]
}
],
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
# Test case insensitive and full-match with PartialMatch key
p = self.load_policy(
{
"name": "efs-has-statement-partial",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Action": ["elasticFilesystem:clientRootAccess",
"elasticfilesystem:clientMount"],
"PartialMatch": ["Action"]
}
]
}
],
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
# Test for presence of just one partial match.
p = self.load_policy(
{
"name": "efs-has-statement-partial",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Action": ["elasticfilesystem:clientRootAccess"],
"PartialMatch": ["Action"]
}
]
}
],
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
# Test for case-insensitive partial match using wildcard
p = self.load_policy(
{
"name": "efs-has-statement-partial",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Action": ["elasticFilesystem:*"],
"PartialMatch": ["Action"]
}
]
}
],
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 1)
# Test for expected fail PartialMatch case
p = self.load_policy(
{
"name": "efs-has-statement-partial",
"resource": "efs",
"filters": [
{
"type": "has-statement",
"statements": [
{
"Effect": "Allow",
"Action": ["elasticfilesystem:DeleteTags"],
"PartialMatch": ["Action"]
}
]
}
],
},
config=region_config,
session_factory=factory,
)
resources = p.run()
self.assertEqual(len(resources), 0)
def test_efs_consecutive_aws_backups_count_filter(self):
session_factory = self.replay_flight_data("test_efs_consecutive_aws_backups_count_filter")
p = self.load_policy(
{
"name": "efs_consecutive_aws_backups_count_filter",
"resource": "efs",
"filters": [
{
"type": "consecutive-aws-backups",
"count": 2,
"period": "days",
"status": "COMPLETED"
}
]
},
session_factory=session_factory,
)
with mock_datetime_now(parser.parse("2022-09-09T00:00:00+00:00"), c7n.filters.backup):
resources = p.run()
self.assertEqual(len(resources), 1)
@terraform('efs_remove_matched', teardown=terraform.TEARDOWN_IGNORE, scope='session')
def test_efs_remove_matched(test, efs_remove_matched):
session_factory = test.replay_flight_data("test_efs_remove_matched")
client = session_factory().client("efs")
filesystem_id = efs_remove_matched['aws_efs_file_system.example.id']
if test.recording:
time.sleep(5)
p = test.load_policy(
{
"name": "efs-rm-matched",
"resource": "aws.efs",
"filters": [
{"FileSystemId": filesystem_id},
{"type": "cross-account", "whitelist": ["185106417252"]},
],
"actions": [{"type": "remove-statements", "statement_ids": "matched"}],
},
session_factory=session_factory,
)
resources = p.run()
test.assertEqual([r["FileSystemId"] for r in resources], [filesystem_id])
data = json.loads(
client.describe_file_system_policy(FileSystemId=filesystem_id).get(
"Policy"
)
)
test.assertEqual(
[s["Sid"] for s in data.get("Statement", ())], ["SpecificAllow"]
)
@terraform('efs_remove_access_denied', teardown=terraform.TEARDOWN_IGNORE)
def test_efs_access_denied(test, efs_remove_access_denied):
session_factory = test.replay_flight_data("test_efs_access_denied")
client = session_factory().client("efs")
filesystem_id = efs_remove_access_denied['aws_efs_file_system.example_test.id']
if test.recording:
time.sleep(10)
p = test.load_policy(
{
"name": "efs-rm-named",
"resource": "aws.efs",
"filters": [{"FileSystemId": filesystem_id},
{"type": "cross-account", "whitelist": ["185106417252"]},]
},
session_factory=session_factory,
)
resources = p.run()
test.assertEqual(len(resources), 0)
with pytest.raises(ClientError) as e:
client.describe_file_system_policy(FileSystemId=filesystem_id)
test.assertTrue('AccessDeniedException' in str(e))
@terraform('efs_remove_matched', teardown=terraform.TEARDOWN_IGNORE, scope='session')
def test_efs_remove_named(test, efs_remove_matched):
session_factory = test.replay_flight_data("test_efs_remove_named")
filesystem_id = efs_remove_matched['aws_efs_file_system.example_client_error.id']
if test.recording:
time.sleep(5)
p = test.load_policy(
{
"name": "efs-rm-named",
"resource": "aws.efs",
"filters": [{"FileSystemId": filesystem_id}],
"actions": [
{"type": "remove-statements", "statement_ids": ["WhatIsIt"]}
],
},
session_factory=session_factory,
)
resources = p.run()
test.assertEqual(len(resources), 1)
@terraform('efs_remove_matched', teardown=terraform.TEARDOWN_IGNORE, scope='session')
def test_efs_client_error(test, efs_remove_matched):
session_factory = test.replay_flight_data("test_efs_client_error")
if test.recording:
time.sleep(5)
p = test.load_policy(
{
"name": "efs-rm-named",
"resource": "aws.efs",
"filters": [{"FileSystemId": "fs-095fec21dd6d065fb"}],
"actions": [
{"type": "remove-statements", "statement_ids": ["WhatIsIt"]}
],
},
session_factory=session_factory,
)
resources = p.run()
test.assertEqual(len(resources), 1)
@terraform('efs_remove_matched', teardown=terraform.TEARDOWN_IGNORE, scope='session')
def test_efs_remove_statement(test, efs_remove_matched):
session_factory = test.replay_flight_data("test_efs_remove_statement")
client = session_factory().client("efs")
filesystem_id = efs_remove_matched['aws_efs_file_system.example_remove_named.id']
if test.recording:
time.sleep(5)
p = test.load_policy(
{
"name": "efs-rm-statement",
"resource": "aws.efs",
"filters": [{"FileSystemId": filesystem_id}],
"actions": [
{"type": "remove-statements", "statement_ids": ["RemoveMe"]}
],
},
session_factory=session_factory,
)
resources = p.run()
test.assertEqual(len(resources), 1)
data = json.loads(
client.describe_file_system_policy(FileSystemId=filesystem_id).get(
"Policy"
)
)
test.assertTrue("RemoveMe" not in [s["Sid"] for s in data.get("Statement", ())])