diff --git a/commands/prepare.py b/commands/prepare.py index 74c4da110..e1f20068e 100644 --- a/commands/prepare.py +++ b/commands/prepare.py @@ -28,7 +28,7 @@ import argparse import pyjq from netaddr import IPNetwork, IPAddress -from shared.common import get_account, query_aws, get_regions +from shared.common import get_account, query_aws, get_regions, is_external_cidr from shared.nodes import Account, Region, Vpc, Az, Subnet, Ec2, Elb, Rds, Cidr, Connection __description__ = "Generate network connection information file" @@ -92,17 +92,6 @@ def get_sgs(vpc): return pyjq.all('.SecurityGroups[] | select(.VpcId == "{}")'.format(vpc.local_id), sgs) -def is_external_cidr(cidr): - ipnetwork = IPNetwork(cidr) - if ( - ipnetwork in IPNetwork("10.0.0.0/8") or - ipnetwork in IPNetwork("172.16.0.0/12") or - ipnetwork in IPNetwork("192.168.0.0/16") - ): - return False - return True - - def get_external_cidrs(account, config): external_cidrs = [] unique_cidrs = {} diff --git a/shared/common.py b/shared/common.py index 6d3720164..d11d6b31c 100644 --- a/shared/common.py +++ b/shared/common.py @@ -5,6 +5,7 @@ import datetime import pyjq import sys +from netaddr import IPNetwork class Severity: DEBUG = 0 @@ -61,6 +62,8 @@ def log_issue(severity, msg, location=None, reasons=[]): 'Reasons': reasons } print(json.dumps(json_issue, sort_keys=True), file=sys.stderr) + + def datetime_handler(x): if isinstance(x, datetime.datetime): return x.isoformat() @@ -73,6 +76,17 @@ def make_list(v): return v +def is_external_cidr(cidr): + ipnetwork = IPNetwork(cidr) + if ( + ipnetwork in IPNetwork("10.0.0.0/8") or + ipnetwork in IPNetwork("172.16.0.0/12") or + ipnetwork in IPNetwork("192.168.0.0/16") + ): + return False + return True + + def query_aws(account, query, region=None): if not region: file_name = 'account-data/{}/{}.json'.format(account.name, query)