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

route53: fix CAA record ordering for idempotency #46049

Merged
merged 3 commits into from
Sep 27, 2018
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
14 changes: 12 additions & 2 deletions lib/ansible/modules/cloud/amazon/route53.py
Original file line number Diff line number Diff line change
Expand Up @@ -574,6 +574,13 @@ def main():
else:
wanted_rset.add_value(v)

need_to_sort_records = (type_in == 'CAA')

# Sort records for wanted_rset if necessary (keep original list)
unsorted_records = wanted_rset.resource_records
if need_to_sort_records:
wanted_rset.resource_records = sorted(unsorted_records)

sets = invoke_with_throttling_retries(conn.get_all_rrsets, zone.id, name=record_in,
type=type_in, identifier=identifier_in)
sets_iter = iter(sets)
Expand All @@ -593,13 +600,14 @@ def main():
identifier_in = str(identifier_in)

if rset.type == type_in and decoded_name.lower() == record_in.lower() and rset.identifier == identifier_in:
if need_to_sort_records:
# Sort records
rset.resource_records = sorted(rset.resource_records)
Copy link
Contributor

Choose a reason for hiding this comment

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

I don't understand what this changes, as line 621 just sorts the resource records again whether or not need_to_sort_records is true or not.

Copy link
Contributor Author

Choose a reason for hiding this comment

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

This changes the rset object itself; it's needed because the comparison is done in line 621 between rset and wanted_rset. All the other lines (i.e. putting stuff into record) is done only for state == 'get' if I understand it correctly, and not used anywhere else (in particular, not for comparison).

I kept the sorting in line 621 because that used to be done for all record types (but as I said, it is only used for state == 'get'); I could have only sorted again if it wasn't sorted here already, but I thought that would make it unnecessary complex.

Copy link
Contributor

Choose a reason for hiding this comment

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

Right, I nearly wrote another comment about how I still don't understand but I got there while writing it :)

found_record = True
record['zone'] = zone_in
record['type'] = rset.type
record['record'] = decoded_name
record['ttl'] = rset.ttl
record['value'] = ','.join(sorted(rset.resource_records))
record['values'] = sorted(rset.resource_records)
if hosted_zone_id_in:
record['hosted_zone_id'] = hosted_zone_id_in
record['identifier'] = rset.identifier
Expand Down Expand Up @@ -652,6 +660,8 @@ def main():
command = 'UPSERT'
else:
command = command_in.upper()
# Restore original order of records
wanted_rset.resource_records = unsorted_records
changes.add_change_record(command, wanted_rset)

if not module.check_mode:
Expand Down
2 changes: 2 additions & 0 deletions test/integration/targets/route53/aliases
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
cloud/aws
unsupported
2 changes: 2 additions & 0 deletions test/integration/targets/route53/defaults/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
---
# defaults file for route53 tests
135 changes: 135 additions & 0 deletions test/integration/targets/route53/tasks/main.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,135 @@
---
# tasks file for Route53

- set_fact:
zone_one: '{{ resource_prefix | replace("-", "") }}.one.fakeansible.com.'
zone_two: '{{ resource_prefix | replace("-", "") }}.two.fakeansible.com.'
- debug: msg='Set zones {{ zone_one }} and {{ zone_two }}'

- name: Test basics (new zone, A and AAAA records)
module_defaults:
group/aws:
aws_access_key: "{{ aws_access_key }}"
aws_secret_key: "{{ aws_secret_key }}"
security_token: "{{ security_token }}"
region: "{{ aws_region }}"
route53:
region: null
block:
- route53_zone:
zone: '{{ zone_one }}'
comment: Created in Ansible test {{ resource_prefix }}
register: z1

- debug: msg='TODO write tests'
- debug: var=z1

- name: Create A record using zone fqdn
route53:
state: present
zone: '{{ zone_one }}'
record: 'qdn_test.{{ zone_one }}'
type: A
value: 1.2.3.4
register: qdn
- assert:
that:
- qdn is not failed
- qdn is changed

- name: Create same A record using zone non-qualified domain
route53:
state: present
zone: '{{ zone_one[:-1] }}'
record: 'qdn_test.{{ zone_one[:-1] }}'
type: A
value: 1.2.3.4
register: non_qdn
- assert:
that:
- non_qdn is not failed
- non_qdn is not changed

- name: Create a LetsEncrypt CAA record
route53:
state: present
zone: '{{ zone_one }}'
record: '{{ zone_one }}'
type: CAA
value:
- 0 issue "letsencrypt.org;"
- 0 issuewild "letsencrypt.org;"
overwrite: true
register: caa
- assert:
that:
- caa is not failed
- caa is changed

- name: Re-create the same LetsEncrypt CAA record
route53:
state: present
zone: '{{ zone_one }}'
record: '{{ zone_one }}'
type: CAA
value:
- 0 issue "letsencrypt.org;"
- 0 issuewild "letsencrypt.org;"
overwrite: true
register: caa
- assert:
that:
- caa is not failed
- caa is not changed

- name: Re-create the same LetsEncrypt CAA record in opposite-order
route53:
state: present
zone: '{{ zone_one }}'
record: '{{ zone_one }}'
type: CAA
value:
- 0 issuewild "letsencrypt.org;"
- 0 issue "letsencrypt.org;"
overwrite: true
register: caa
- name: This should not be changed, as CAA records are not order sensitive
assert:
that:
- caa is not failed
- caa is not changed
always:
- route53_facts:
query: record_sets
hosted_zone_id: '{{ z1.zone_id }}'
register: z1_records
- debug: var=z1_records
- name: Loop over A/AAAA/CNAME records and delete them
route53:
state: absent
zone: '{{ zone_one }}'
record: '{{ item.Name }}'
type: '{{ item.Type }}'
value: '{{ item.ResourceRecords | map(attribute="Value") | join(",") }}'
loop: '{{ z1_records.ResourceRecordSets | selectattr("Type", "in", ["A", "AAAA", "CNAME", "CAA"]) | list }}'
- name: Delete test zone one '{{ zone_one }}'
route53_zone:
state: absent
zone: '{{ zone_one }}'
register: delete_one
ignore_errors: yes
retries: 10
until: delete_one is not failed
- name: Delete test zone two '{{ zone_two }}'
route53_zone:
state: absent
zone: '{{ zone_two }}'
register: delete_two
ignore_errors: yes
retries: 10
until: delete_two is not failed
when: false


#TODO(ryansb) build internal-vpc integration tests
#- include_tasks: internal_zone.yml
Empty file.