Skip to content
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

aws - Added new action and a bugfix for existing action for route53 #9291

Merged
merged 53 commits into from
Mar 4, 2024
Merged
Show file tree
Hide file tree
Changes from 50 commits
Commits
Show all changes
53 commits
Select commit Hold shift + click to select a range
ac6a60f
Changes to ignore config support checks...
kk1532 Apr 5, 2022
38fccf4
Changes to ignore config support checks ...
kk1532 Apr 5, 2022
6f77ff9
Changes to ignore config support checks...
kk1532 Apr 5, 2022
534b6d4
Merge pull request #2 from kk1532/cfgperiodic
kk1532 Apr 5, 2022
984c4e4
mu - changes on LambdaRetry max_attempts
kk1532 Apr 5, 2022
706066f
Merge pull request #3 from kk1532/cfgperiodic
kk1532 Apr 5, 2022
9154b66
Merge branch 'master' into master
kk1532 Apr 7, 2022
bd19ac3
Merge branch 'master' into kk1532/master
ajkerrigan Apr 21, 2022
5af0950
Add validation tests and example policy
ajkerrigan Apr 21, 2022
dca7f73
Merge branch 'master' into master
ajkerrigan Apr 22, 2022
123fcb8
Merge branch 'cloud-custodian:master' into master
kk1532 May 9, 2022
d9ac956
Merge branch 'cloud-custodian:master' into master
kk1532 May 10, 2022
0bdd999
Merge branch 'cloud-custodian:master' into master
kk1532 Jun 21, 2022
2d10cad
Merge branch 'cloud-custodian:master' into master
kk1532 Jul 1, 2022
ba317f2
Merge branch 'cloud-custodian:master' into master
kk1532 Jul 5, 2022
ec003bc
Merge branch 'cloud-custodian:master' into master
kk1532 Jul 20, 2022
ec6afe8
Merge branch 'cloud-custodian:master' into master
kk1532 Aug 1, 2022
d35d6ca
Merge branch 'cloud-custodian:master' into master
kk1532 Aug 8, 2022
79da095
Merge branch 'cloud-custodian:master' into master
kk1532 Aug 26, 2022
ff4a237
Merge branch 'cloud-custodian:master' into master
kk1532 Sep 14, 2022
c7720fb
Merge branch 'cloud-custodian:master' into master
kk1532 Sep 19, 2022
31ad0a7
Merge branch 'cloud-custodian:master' into master
kk1532 Sep 26, 2022
8073a4f
Merge branch 'cloud-custodian:master' into master
kk1532 Sep 28, 2022
90c5c10
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 3, 2022
a68d0a1
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 13, 2022
d689fc3
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 19, 2022
1f73ce5
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 26, 2022
fab4998
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 27, 2022
7bf8039
Merge branch 'cloud-custodian:master' into master
kk1532 Oct 31, 2022
7abfe20
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 2, 2022
3621f2d
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 10, 2022
f312f10
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 16, 2022
eb2a6b2
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 17, 2022
ecb5ecc
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 21, 2022
fb0c630
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 21, 2022
1dcfc08
Merge branch 'cloud-custodian:master' into master
kk1532 Nov 28, 2022
c971442
Merge branch 'cloud-custodian:master' into master
kk1532 Jan 13, 2023
59d7599
Merge remote-tracking branch 'upstream/master'
kk1532 Jan 18, 2023
f605a5b
aws - filter to annotate security configuration and filter with attri…
kk1532 Jan 18, 2023
ad2a776
Merge branch 'master' into master
kk1532 Jan 18, 2023
fb695c9
Merge branch 'cloud-custodian:master' into master
kk1532 Jan 18, 2023
d6463b1
aws - filter to annotate security configuration and filter with attri…
kk1532 Jan 18, 2023
4a61ac2
Merge branch 'cloud-custodian:main' into master
kk1532 Feb 13, 2023
e5784db
aws - Config Id support added for Route53 HostedZoneId
kk1532 Feb 13, 2023
8ecb355
aws - Config Id support added for Route53 HostedZoneId
kk1532 Feb 13, 2023
4a172e4
aws - Config Id support added for Route53 HostedZoneId
kk1532 Feb 13, 2023
8149850
old
kk1532 Feb 13, 2023
622430a
Merge branch 'cloud-custodian:main' into master
kk1532 Feb 11, 2024
0cd1bfd
aws - Added new action and a bugfix for existing action for route53
kk1532 Feb 11, 2024
8ba52cb
aws - Added new action and a bugfix for existing action for route53
kk1532 Feb 12, 2024
9179a6e
aws - Added new action and a bugfix for existing action for route53
kk1532 Feb 12, 2024
b18d91d
aws - Added new action and a bugfix for existing action for route53
kk1532 Feb 12, 2024
6160d02
aws - Added new action and a bugfix for existing action for route53
kk1532 Feb 15, 2024
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Jump to
Jump to file
Failed to load files.
Diff view
Diff view
76 changes: 70 additions & 6 deletions c7n/resources/route53.py
Original file line number Diff line number Diff line change
Expand Up @@ -208,6 +208,65 @@
TagsToDelete=keys)


@ResourceRecordSet.action_registry.register('remove-recordset')
kk1532 marked this conversation as resolved.
Show resolved Hide resolved
class ResourceRecordSetRemove(BaseAction):
"""Action to delete resource records from Route 53 hosted zones.

It is recommended to use a filter to avoid unwanted deletion
of R53 records from all hosted zones.

:example:

.. code-block:: yaml

policies:
- name: route53-remove-filtered-records
resource: aws.rrset
filters:
- type: value
key: AliasTarget.DNSName
value: "email.gc.example.com."
actions:
- type: remove-recordset

"""
schema = type_schema('remove-recordset',)
permissions = ('route53:ChangeResourceRecordSets',)
keys = (
'Name', 'Type', 'TTL', 'SetIdentifier', 'Region', 'AliasTarget', 'ResourceRecords')

def process(self, recordsets):
client = local_session(self.manager.session_factory).client('route53')
try:
for rrset in recordsets:
if rrset['Type'] in ('NS', 'SOA'):
kk1532 marked this conversation as resolved.
Show resolved Hide resolved
continue

Check warning on line 243 in c7n/resources/route53.py

View check run for this annotation

Codecov / codecov/patch

c7n/resources/route53.py#L243

Added line #L243 was not covered by tests

rrsetdata = self.generate_rrset(rrset)
self.manager.retry(
client.change_resource_record_sets,
HostedZoneId=rrset['c7n:parent-id'],
ChangeBatch={
'Changes': [
{
'Action': 'DELETE',
'ResourceRecordSet': rrsetdata,
}
]
},
ignore_err_codes=('InvalidChangeBatch'))
except Exception as e:
self.log.warning(

Check warning on line 259 in c7n/resources/route53.py

View check run for this annotation

Codecov / codecov/patch

c7n/resources/route53.py#L258-L259

Added lines #L258 - L259 were not covered by tests
"ResourceRecordSet delete error: %s", e)

def generate_rrset(self, recordset):
kk1532 marked this conversation as resolved.
Show resolved Hide resolved
rrset_payload = dict()
for key in self.keys:
if key in recordset:
rrset_payload.update({key: recordset[key]})
return rrset_payload


@HostedZone.action_registry.register('delete')
class Delete(BaseAction):
"""Action to delete Route 53 hosted zones.
Expand All @@ -234,6 +293,8 @@

schema = type_schema('delete', force={'type': 'boolean'})
permissions = ('route53:DeleteHostedZone',)
keys = (
'Name', 'Type', 'TTL', 'SetIdentifier', 'Region', 'AliasTarget', 'ResourceRecords')

def process(self, hosted_zones):
client = local_session(self.manager.session_factory).client('route53')
Expand Down Expand Up @@ -267,24 +328,27 @@
# Exempt the two zone associated mandatory records
if rrset['Name'] == hz['Name'] and rrset['Type'] in ('NS', 'SOA'):
continue
rrsetdata = self.generate_rrset(rrset)
Copy link
Collaborator

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

it seems like since we now have a delete record set action, we could remove that implementation from here and just call the delete action on the record set, composition style to avoid the duplication.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@kapilt
Can you check and confirm, are below the expected change?

on @HostedZone.action_registry.register('delete')
class Delete(BaseAction):

 def process(self, hosted_zones):
        client = local_session(self.manager.session_factory).client('route53')
        rrsetdelete = ResourceRecordSetRemove(self.data, self.manager, self.log_dir)
        error = None
        for hz in hosted_zones:
            if self.data.get('force'):
                rrsetdelete.process("", hz)

and remove the method def delete_records(self, client, hz):

add equivalent entry on record set delete action like below

  def process(self, recordsets, hz=None):
        client = local_session(self.manager.session_factory).client('route53')
        if hz is not None:
            paginator = client.get_paginator('list_resource_record_sets')
            paginator.PAGE_ITERATOR_CLS = RetryPageIterator
            recordsets = paginator.paginate(HostedZoneId=hz['Id']).build_full_result()

        try:

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

In this composition style, we can avoid two delete implementation for record deletion.

self.manager.retry(
client.change_resource_record_sets,
HostedZoneId=hz['Id'],
ChangeBatch={
'Changes': [
{
'Action': 'DELETE',
'ResourceRecordSet': {
'Name': rrset['Name'],
'Type': rrset['Type'],
'TTL': rrset['TTL'],
'ResourceRecords': rrset['ResourceRecords']
},
'ResourceRecordSet': rrsetdata,
}
]
},
ignore_err_codes=('InvalidChangeBatch'))

def generate_rrset(self, recordset):
rrset_payload = dict()
for key in self.keys:
if key in recordset:
rrset_payload.update({key: recordset[key]})
return rrset_payload


@HostedZone.action_registry.register('set-query-logging')
class SetQueryLogging(BaseAction):
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ChangeInfo": {
"Id": "/change/C08700492OMT62TVMBS6H",
"Status": "PENDING",
"SubmittedAt": {
"__class__": "datetime",
"year": 2024,
"month": 2,
"day": 11,
"hour": 17,
"minute": 38,
"second": 53,
"microsecond": 103000
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"HostedZones": [
{
"Id": "/hostedzone/Z0703663WE3WTQ3FAPIF",
"Name": "subdomain.example.com.",
"CallerReference": "1560",
"Config": {
"PrivateZone": true
},
"ResourceRecordSetCount": 3
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ResourceRecordSets": [
{
"Name": "subdomain.example.com.",
"Type": "NS",
"TTL": 172800,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk."
},
{
"Value": "ns-0.awsdns-00.com."
},
{
"Value": "ns-1024.awsdns-00.org."
},
{
"Value": "ns-512.awsdns-00.net."
}
]
},
{
"Name": "subdomain.example.com.",
"Type": "SOA",
"TTL": 900,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
}
]
},
{
"Name": "mail.subdomain.example.com.",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [
{
"Value": "mailserver01.subdomain.example.com."
}
]
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,39 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ResourceRecordSets": [
{
"Name": "subdomain.example.com.",
"Type": "NS",
"TTL": 172800,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk."
},
{
"Value": "ns-0.awsdns-00.com."
},
{
"Value": "ns-1024.awsdns-00.org."
},
{
"Value": "ns-512.awsdns-00.net."
}
]
},
{
"Name": "subdomain.example.com.",
"Type": "SOA",
"TTL": 900,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
}
]
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,20 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ChangeInfo": {
"Id": "/change/C0028923170KHKBOZN21G",
"Status": "PENDING",
"SubmittedAt": {
"__class__": "datetime",
"year": 2024,
"month": 2,
"day": 11,
"hour": 14,
"minute": 14,
"second": 7,
"microsecond": 346000
}
}
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,19 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"HostedZones": [
{
"Id": "/hostedzone/Z0703663WE3WTQ3FAPIF",
"Name": "subdomain.example.com.",
"CallerReference": "1560",
"Config": {
"PrivateZone": true
},
"ResourceRecordSetCount": 4
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,58 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ResourceRecordSets": [
{
"Name": "subdomain.example.com.",
"Type": "A",
"AliasTarget": {
"HostedZoneId": "Z1YSA3EXCYUU9Z",
"DNSName": "vpce-12345abcdefgh-mxpozkdy.us-west-2.vpce.amazonaws.com.",
"EvaluateTargetHealth": true
}
},
{
"Name": "subdomain.example.com.",
"Type": "NS",
"TTL": 172800,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk."
},
{
"Value": "ns-0.awsdns-00.com."
},
{
"Value": "ns-1024.awsdns-00.org."
},
{
"Value": "ns-512.awsdns-00.net."
}
]
},
{
"Name": "subdomain.example.com.",
"Type": "SOA",
"TTL": 900,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
}
]
},
{
"Name": "mail.subdomain.example.com.",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [
{
"Value": "mailserver01.subdomain.example.com."
}
]
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,49 @@
{
"status_code": 200,
"data": {
"ResponseMetadata": {},
"ResourceRecordSets": [
{
"Name": "subdomain.example.com.",
"Type": "NS",
"TTL": 172800,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk."
},
{
"Value": "ns-0.awsdns-00.com."
},
{
"Value": "ns-1024.awsdns-00.org."
},
{
"Value": "ns-512.awsdns-00.net."
}
]
},
{
"Name": "subdomain.example.com.",
"Type": "SOA",
"TTL": 900,
"ResourceRecords": [
{
"Value": "ns-1536.awsdns-00.co.uk. awsdns-hostmaster.amazon.com. 1 7200 900 1209600 86400"
}
]
},
{
"Name": "mail.subdomain.example.com.",
"Type": "CNAME",
"TTL": 300,
"ResourceRecords": [
{
"Value": "mailserver01.subdomain.example.com."
}
]
}
],
"IsTruncated": false,
"MaxItems": "100"
}
}