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

updating the ipaddress.ip_network logic to allow host bits set #103

Open
wants to merge 1 commit into
base: dev
Choose a base branch
from
Open
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
16 changes: 10 additions & 6 deletions hammer/library/aws/security_groups.py
Original file line number Diff line number Diff line change
Expand Up @@ -65,7 +65,7 @@ def find_source_s3(account,
if objects is None:
logging.error(f"Failed to find '{group_id}' rules backup in {account}")
return
backup_objects = [ obj["Key"] for obj in objects if obj.get("Key", "").startswith(f"{prefix}{group_id}_") ]
backup_objects = [obj["Key"] for obj in objects if obj.get("Key", "").startswith(f"{prefix}{group_id}_")]
# return most recent backup
recent_backup = max(backup_objects)
source = json.loads(S3Operations.get_object(s3_client, bucket, recent_backup))
Expand Down Expand Up @@ -99,8 +99,8 @@ def restore_s3(cls,
from_port = ingress.get("FromPort", None)
to_port = ingress.get("ToPort", None)
ip_protocol = ingress["IpProtocol"]
cidrs = [ ipv6_range["CidrIpv6"] for ipv6_range in ingress.get("Ipv6Ranges", []) ]
cidrs += [ ip_range["CidrIp"] for ip_range in ingress.get("IpRanges", []) ]
cidrs = [ipv6_range["CidrIpv6"] for ipv6_range in ingress.get("Ipv6Ranges", [])]
cidrs += [ip_range["CidrIp"] for ip_range in ingress.get("IpRanges", [])]
for cidr in cidrs:
cls.add_inbound_rule(ec2_client, group_id, ip_protocol, from_port, to_port, cidr)

Expand All @@ -117,12 +117,12 @@ def ip_permissions(ip_protocol, from_port, to_port, cidr):

:return: dict with `IpPermissions` element
"""
perms = { 'IpProtocol': ip_protocol }
perms = {'IpProtocol': ip_protocol}
if from_port is not None and \
to_port is not None:
perms['FromPort'] = from_port
perms['ToPort'] = to_port
ipv = ipaddress.ip_network(cidr).version
ipv = ipaddress.ip_network(cidr, False).version
if ipv == 4:
perms['IpRanges'] = [{'CidrIp': cidr}]
else:
Expand Down Expand Up @@ -238,6 +238,7 @@ class IPRange(object):
Basic class for security group CIDR range.
Encapsulates CIDR and boolean marker if CIDR restricted or not.
"""

def __init__(self, cidr):
self.cidr = cidr
# by default assume that CIDR is restricted,
Expand All @@ -261,6 +262,7 @@ class SecurityGroupPermission(object):
Basic class for security group `IpPermissions`.
Encapsulates `IpProtocol`/`FromPort`/`ToPort` and list of `IpRanges`.
"""

def __init__(self, group, ingress):
"""
:param group: `SecurityGroup` instance which contains this `IpPermissions` (to be able to perform operations against it)
Expand Down Expand Up @@ -348,6 +350,7 @@ class SecurityGroup(object):
Basic class for security group.
Encapsulates `GroupName`/`GroupId`/`Tags` and list of `IpPermissions`.
"""

def __init__(self, account, source):
"""
:param account: `Account` instance where security group is present
Expand Down Expand Up @@ -383,7 +386,7 @@ def restriction_status(self, cidr):
status = RestrictionStatus.Restricted
if cidr.endswith("/0"):
status = RestrictionStatus.OpenCompletely
elif ipaddress.ip_network(cidr).is_global:
elif ipaddress.ip_network(cidr, False).is_global:
status = RestrictionStatus.OpenPartly
logging.debug(f"Checked '{cidr}' - '{status.value}'")
return status
Expand Down Expand Up @@ -490,6 +493,7 @@ class SecurityGroupsChecker(object):
Basic class for checking security group in account/region.
Encapsulates check settings and discovered security groups.
"""

def __init__(self,
account,
restricted_ports):
Expand Down