-
Notifications
You must be signed in to change notification settings - Fork 1.5k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Add object lock configuration to augment table #9532
Open
dmytro-afanasiev
wants to merge
6
commits into
cloud-custodian:main
Choose a base branch
from
dmytro-afanasiev:aws.s3.filters.lock-configuration
base: main
Could not load branches
Branch not found: {{ refName }}
Loading
Could not load tags
Nothing to show
Loading
Are you sure you want to change the base?
Some commits from the old base branch may be removed from the timeline,
and old review comments may become outdated.
Open
Changes from all commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
fe2b36f
Add object lock configuration to augment table
dmytro-afanasiev 88a7650
Fix tests
dmytro-afanasiev 732fd5c
Merge remote-tracking branch 'origin/main' into aws.s3.filters.lock-c…
dmytro-afanasiev b4dbe47
Add lock-configration filter
dmytro-afanasiev a7914d5
Fix lint
dmytro-afanasiev d6e5791
Add more tests
dmytro-afanasiev File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -839,6 +839,65 @@ | |
} | ||
|
||
|
||
@S3.filter_registry.register('lock-configuration') | ||
class S3LockConfigurationFilter(ValueFilter): | ||
""" | ||
Filter S3 buckets based on their object lock configurations | ||
|
||
:example: | ||
|
||
Get all buckets where lock configuration mode is COMPLIANCE | ||
|
||
.. code-block:: yaml | ||
|
||
policies: | ||
- name: lock-configuration-compliance | ||
resource: aws.s3 | ||
filters: | ||
- type: lock-configuration | ||
key: Rule.DefaultRetention.Mode | ||
value: COMPLIANCE | ||
|
||
""" | ||
schema = type_schema('lock-configuration', rinherit=ValueFilter.schema) | ||
permissions = ('s3:GetBucketObjectLockConfiguration',) | ||
annotate = True | ||
annotation_key = 'ObjectLockConfiguration' | ||
|
||
def _process_resource(self, client, resource): | ||
try: | ||
config = client.get_object_lock_configuration( | ||
Bucket=resource['Name'] | ||
)['ObjectLockConfiguration'] | ||
except ClientError as e: | ||
if e.response['Error']['Code'] == 'ObjectLockConfigurationNotFoundError': | ||
config = None | ||
else: | ||
raise | ||
resource[self.annotation_key] = config | ||
|
||
def process(self, resources, event=None): | ||
client = local_session(self.manager.session_factory).client('s3') | ||
with self.executor_factory(max_workers=3) as w: | ||
futures = [] | ||
for res in resources: | ||
if self.annotation_key in res: | ||
continue | ||
futures.append(w.submit(self._process_resource, client, res)) | ||
for f in as_completed(futures): | ||
exc = f.exception() | ||
if exc: | ||
self.log.error( | ||
"Exception getting bucket lock configuration \n %s" % ( | ||
exc)) | ||
return super().process(resources, event) | ||
|
||
def __call__(self, r): | ||
if self.annotate: | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. afaics given the class value for annotate = true, there's no need for the conditional here. |
||
return super().__call__(r.setdefault(self.annotation_key, None)) | ||
return super().__call__(r.pop(self.annotation_key, None)) | ||
|
||
|
||
ENCRYPTION_STATEMENT_GLOB = { | ||
'Effect': 'Deny', | ||
'Principal': '*', | ||
|
16 changes: 16 additions & 0 deletions
16
...ata/placebo/test_s3_bucket_object_lock_configuration/s3.GetObjectLockConfiguration_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"status_code": 200, | ||
"data": { | ||
"ResponseMetadata": {}, | ||
"ObjectLockConfiguration": { | ||
"ObjectLockEnabled": "Enabled", | ||
"Rule": { | ||
"DefaultRetention": { | ||
"Mode": "GOVERNANCE", | ||
"Days": 7, | ||
"Years": 0 | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...ata/placebo/test_s3_bucket_object_lock_configuration/s3.GetObjectLockConfiguration_2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"status_code": 404, | ||
"data": { | ||
"Error": { | ||
"Code": "ObjectLockConfigurationNotFoundError", | ||
"Message": "Object Lock configuration does not exist for this bucket", | ||
"BucketName": "c7n-test-s3-bucket-bucket-key-disabled" | ||
}, | ||
"ResponseMetadata": {} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/data/placebo/test_s3_bucket_object_lock_configuration/s3.ListBuckets_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"status_code": 200, | ||
"data": { | ||
"ResponseMetadata": {}, | ||
"Buckets": [ | ||
{ | ||
"Name": "c7n-test-s3-bucket", | ||
"CreationDate": { | ||
"__class__": "datetime", | ||
"year": 2023, | ||
"month": 8, | ||
"day": 22, | ||
"hour": 13, | ||
"minute": 27, | ||
"second": 29, | ||
"microsecond": 0 | ||
} | ||
}, | ||
{ | ||
"Name": "c7n-test-s3-bucket-bucket-key-disabled", | ||
"CreationDate": { | ||
"__class__": "datetime", | ||
"year": 2023, | ||
"month": 8, | ||
"day": 22, | ||
"hour": 13, | ||
"minute": 29, | ||
"second": 11, | ||
"microsecond": 0 | ||
} | ||
} | ||
], | ||
"Owner": { | ||
"DisplayName": "mandeep.bal", | ||
"ID": "e7c8bb65a5fc49cf906715eae09de9e4bb7861a96361ba79b833aa45f6833b15" | ||
} | ||
} | ||
} |
16 changes: 16 additions & 0 deletions
16
...o/test_s3_bucket_object_lock_configuration_exception/s3.GetObjectLockConfiguration_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,16 @@ | ||
{ | ||
"status_code": 200, | ||
"data": { | ||
"ResponseMetadata": {}, | ||
"ObjectLockConfiguration": { | ||
"ObjectLockEnabled": "Enabled", | ||
"Rule": { | ||
"DefaultRetention": { | ||
"Mode": "GOVERNANCE", | ||
"Days": 7, | ||
"Years": 0 | ||
} | ||
} | ||
} | ||
} | ||
} |
11 changes: 11 additions & 0 deletions
11
...o/test_s3_bucket_object_lock_configuration_exception/s3.GetObjectLockConfiguration_2.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,11 @@ | ||
{ | ||
"status_code": 404, | ||
"data": { | ||
"Error": { | ||
"Code": "NoSuchBucket", | ||
"Message": "The specified bucket does not exist", | ||
"BucketName": "c7n-test-s3-bucket-bucket-key-disabled" | ||
}, | ||
"ResponseMetadata": {} | ||
} | ||
} |
38 changes: 38 additions & 0 deletions
38
tests/data/placebo/test_s3_bucket_object_lock_configuration_exception/s3.ListBuckets_1.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,38 @@ | ||
{ | ||
"status_code": 200, | ||
"data": { | ||
"ResponseMetadata": {}, | ||
"Buckets": [ | ||
{ | ||
"Name": "c7n-test-s3-bucket", | ||
"CreationDate": { | ||
"__class__": "datetime", | ||
"year": 2023, | ||
"month": 8, | ||
"day": 22, | ||
"hour": 13, | ||
"minute": 27, | ||
"second": 29, | ||
"microsecond": 0 | ||
} | ||
}, | ||
{ | ||
"Name": "c7n-test-s3-bucket-bucket-key-disabled", | ||
"CreationDate": { | ||
"__class__": "datetime", | ||
"year": 2023, | ||
"month": 8, | ||
"day": 22, | ||
"hour": 13, | ||
"minute": 29, | ||
"second": 11, | ||
"microsecond": 0 | ||
} | ||
} | ||
], | ||
"Owner": { | ||
"DisplayName": "mandeep.bal", | ||
"ID": "e7c8bb65a5fc49cf906715eae09de9e4bb7861a96361ba79b833aa45f6833b15" | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
generally want a c7n: prefix on annotation keys to distinguish from native attributes.