Skip to content

Commit

Permalink
Add support for ipv6 addresses
Browse files Browse the repository at this point in the history
Remove formating changes
  • Loading branch information
abhattacharyaNS1 committed Aug 9, 2021
1 parent 37cd338 commit 9e5c9a0
Showing 1 changed file with 11 additions and 2 deletions.
13 changes: 11 additions & 2 deletions plugins/lookup/aws_service_ip_ranges.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
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_prefix:
description: 'Return only ipv6 addresses. Option: ipv6_prefix=True'
'''

EXAMPLES = """
Expand Down Expand Up @@ -55,9 +57,16 @@

class LookupModule(LookupBase):
def run(self, terms, variables, **kwargs):
if "ipv6_prefix" in kwargs["ipv6_prefix"] is False:
prefixes_label = "prefixes"
ip_prefix_label = "ip_prefix"
else:
prefixes_label = "ipv6_prefixes"
ip_prefix_label = "ipv6_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 @@ -78,4 +87,4 @@ def run(self, terms, variables, **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]
return [item[ip_prefix_label] for item in amazon_response]

0 comments on commit 9e5c9a0

Please sign in to comment.