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

Add support for ipv6 addresses #430

Merged
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
Show all changes
23 commits
Select commit Hold shift + click to select a range
f2e8cd6
Add support for ipv6 addresses
abhattacharyaNS1 Aug 2, 2021
2f460a8
Verify ipv4 and ipv6 addresses with cidr
abhattacharyaNS1 Aug 25, 2021
c2ae4be
use ipv6_prefixes
abhattacharyaNS1 Aug 10, 2021
4a22cb7
pass keyword argument
abhattacharyaNS1 Aug 10, 2021
efe94fa
Fix pep8 and pylint issues
abhattacharyaNS1 Aug 25, 2021
0d5b96d
Verify ipv4 and ipv6 addresses with cidr
abhattacharyaNS1 Aug 25, 2021
9b4ee80
Fix pep8 and pylint issues
abhattacharyaNS1 Aug 25, 2021
788cb36
Fix missing import errors
abhattacharyaNS1 Aug 26, 2021
c7ae886
Fix indentaion and prefixes
abhattacharyaNS1 Aug 26, 2021
8b224a6
Update tests in lookup_aws_service_ip_ranges
abhattacharyaNS1 Sep 10, 2021
c0995cb
Verify ipv4 and ipv6 addresses with cidr
abhattacharyaNS1 Aug 25, 2021
c9dae7b
pass keyword argument
abhattacharyaNS1 Aug 10, 2021
c9057b4
Fix pep8 and pylint issues
abhattacharyaNS1 Aug 25, 2021
d6250d3
Verify ipv4 and ipv6 addresses with cidr
abhattacharyaNS1 Aug 25, 2021
c74cda2
Fix pep8 and pylint issues
abhattacharyaNS1 Aug 25, 2021
70c1306
Fix missing import errors
abhattacharyaNS1 Aug 26, 2021
3201804
Fix indentaion and prefixes
abhattacharyaNS1 Aug 26, 2021
031874f
Removing in file cidr tests
abhattacharyaNS1 Sep 10, 2021
d78792d
Fix syntax of ip_range return
abhattacharyaNS1 Sep 10, 2021
ecf4df2
Fix typo
abhattacharyaNS1 Sep 10, 2021
e810e8e
Apply suggestions from code review
tremble Sep 11, 2021
6e8d0e8
Add changelog fragments
abhattacharyaNS1 Sep 16, 2021
51f4254
Apply suggestions from code review
tremble Sep 16, 2021
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
4 changes: 4 additions & 0 deletions changelogs/fragments/430-add_support_for_ipv6_addresses.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
minor_changes:
- aws_service_ip_ranges - add new option ``ipv6_prefixes``
to get only IPV6 addresses and prefixes for Amazon services
(https://github.com/ansible-collections/amazon.aws/pull/430)
17 changes: 13 additions & 4 deletions plugins/lookup/aws_service_ip_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
description: 'The service to filter ranges by. Options: EC2, S3, CLOUDFRONT, CODEbUILD, ROUTE53, ROUTE53_HEALTHCHECKS'
region:
description: 'The AWS region to narrow the ranges to. Examples: us-east-1, eu-west-2, ap-southeast-1'
ipv6_prefixes:
description: 'When I(ipv6_prefixes=True) the lookup will return ipv6 addresses instead of ipv4 addresses'
version_added: 2.1.0
'''

EXAMPLES = """
Expand All @@ -40,7 +43,6 @@
description: comma-separated list of CIDR ranges
"""


import json

from ansible.errors import AnsibleError
Expand All @@ -55,9 +57,16 @@

class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
if "ipv6_prefixes" in kwargs and kwargs["ipv6_prefixes"]:
prefixes_label = "ipv6_prefixes"
ip_prefix_label = "ipv6_prefix"
else:
prefixes_label = "prefixes"
ip_prefix_label = "ip_prefix"

try:
resp = open_url('https://ip-ranges.amazonaws.com/ip-ranges.json')
amazon_response = json.load(resp)['prefixes']
amazon_response = json.load(resp)[prefixes_label]
except getattr(json.decoder, 'JSONDecodeError', ValueError) as e:
# on Python 3+, json.decoder.JSONDecodeError is raised for bad
# JSON. On 2.x it's a ValueError
Expand All @@ -77,5 +86,5 @@ def run(self, terms, variables, **kwargs):
if 'service' in kwargs:
service = str.upper(kwargs['service'])
amazon_response = (item for item in amazon_response if item['service'] == service)

return [item['ip_prefix'] for item in amazon_response]
iprange = [item[ip_prefix_label] for item in amazon_response]
return iprange
Original file line number Diff line number Diff line change
Expand Up @@ -11,6 +11,7 @@
- name: lookup range with wantlist
set_fact:
want_list: "{{ lookup('amazon.aws.aws_service_ip_ranges', wantlist=True) }}"
want_ipv6_list: "{{ lookup('amazon.aws.aws_service_ip_ranges', wantlist=True, ipv6_prefixes=True) }}"

- name: assert that we're returned a list
assert:
Expand All @@ -20,10 +21,17 @@
- want_list is not string
- want_list | length > 1
- want_list[0] | ansible.netcommon.ipv4
- want_ipv6_list is defined
- want_ipv6_list is iterable
- want_ipv6_list is not string
- want_ipv6_list | length > 1
- want_ipv6_list[0] | ansible.netcommon.ipv6


- name: lookup range with service
set_fact:
s3_ips: "{{ lookup('amazon.aws.aws_service_ip_ranges', service='S3', wantlist=True) }}"
s3_ipv6s: "{{ lookup('amazon.aws.aws_service_ip_ranges', service='S3', wantlist=True, ipv6_prefixes=True) }}"

- name: assert that we're returned a list
assert:
Expand All @@ -33,10 +41,16 @@
- s3_ips is not string
- s3_ips | length > 1
- s3_ips[0] | ansible.netcommon.ipv4
- s3_ipv6s is defined
- s3_ipv6s is iterable
- s3_ipv6s is not string
- s3_ipv6s | length > 1
- s3_ipv6s[0] | ansible.netcommon.ipv6

- name: lookup range with a different service
set_fact:
route53_ips: "{{ lookup('amazon.aws.aws_service_ip_ranges', service='ROUTE53_HEALTHCHECKS', wantlist=True) }}"
route53_ipv6s: "{{ lookup('amazon.aws.aws_service_ip_ranges', service='ROUTE53_HEALTHCHECKS', wantlist=True, ipv6_prefixes=True) }}"

- name: assert that we're returned a list
assert:
Expand All @@ -46,16 +60,27 @@
- route53_ips is not string
- route53_ips | length > 1
- route53_ips[0] | ansible.netcommon.ipv4
- route53_ipv6s is defined
- route53_ipv6s is iterable
- route53_ipv6s is not string
- route53_ipv6s | length > 1
- route53_ipv6s[0] | ansible.netcommon.ipv6


- name: assert that service IPs don't overlap
- name: assert that service IPV4s and IPV6s do not overlap
assert:
that:
- route53_ips | intersect(s3_ips) | length == 0
- route53_ipv6s | intersect(s3_ipv6s) | length == 0

- name: lookup range with region
set_fact:
us_east_1_ips: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='us-east-1', wantlist=True) }}"

- name: lookup IPV6 range with region
set_fact:
us_east_1_ipv6s: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='us-east-1', wantlist=True, ipv6_prefixes=True) }}"

tremble marked this conversation as resolved.
Show resolved Hide resolved
- name: assert that we're returned a list
assert:
that:
Expand All @@ -64,10 +89,16 @@
- us_east_1_ips is not string
- us_east_1_ips | length > 1
- us_east_1_ips[0] | ansible.netcommon.ipv4
- us_east_1_ipv6s is defined
- us_east_1_ipv6s is iterable
- us_east_1_ipv6s is not string
- us_east_1_ipv6s | length > 1
- us_east_1_ipv6s[0] | ansible.netcommon.ipv6

- name: lookup range with a different region
set_fact:
eu_central_1_ips: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='eu-central-1', wantlist=True) }}"
eu_central_1_ipv6s: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='eu-central-1', wantlist=True, ipv6_prefixes=True) }}"

- name: assert that we're returned a list
assert:
Expand All @@ -77,15 +108,22 @@
- eu_central_1_ips is not string
- eu_central_1_ips | length > 1
- eu_central_1_ips[0] | ansible.netcommon.ipv4
- eu_central_1_ipv6s is defined
- eu_central_1_ipv6s is iterable
- eu_central_1_ipv6s is not string
- eu_central_1_ipv6s | length > 1
- eu_central_1_ipv6s[0] | ansible.netcommon.ipv6

- name: assert that regional IPs don't overlap
assert:
that:
- eu_central_1_ips | intersect(us_east_1_ips) | length == 0
- eu_central_1_ipv6s | intersect(us_east_1_ipv6s) | length == 0

tremble marked this conversation as resolved.
Show resolved Hide resolved
- name: lookup range with service and region
set_fact:
s3_us_ips: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='us-east-1', service='S3', wantlist=True) }}"
s3_us_ipv6s: "{{ lookup('amazon.aws.aws_service_ip_ranges', region='us-east-1', service='S3', wantlist=True, ipv6_prefixes=True) }}"

tremble marked this conversation as resolved.
Show resolved Hide resolved
- name: assert that we're returned a list
assert:
Expand All @@ -95,9 +133,16 @@
- s3_us_ips is not string
- s3_us_ips | length > 1
- s3_us_ips[0] | ansible.netcommon.ipv4
- s3_us_ipv6s is defined
- s3_us_ipv6s is iterable
- s3_us_ipv6s is not string
- s3_us_ipv6s | length > 1
- s3_us_ipv6s[0] | ansible.netcommon.ipv6

- name: assert that the regional service IPs are a subset of the regional IPs and service IPs.
assert:
that:
- ( s3_us_ips | intersect(us_east_1_ips) | length ) == ( s3_us_ips | length )
- ( s3_us_ips | intersect(s3_ips) | length ) == ( s3_us_ips | length )
- ( s3_us_ipv6s | intersect(us_east_1_ipv6s) | length ) == ( s3_us_ipv6s | length )
- ( s3_us_ipv6s | intersect(s3_ipv6s) | length ) == ( s3_us_ipv6s | length )