|
| 1 | +import os |
| 2 | +import boto3 |
| 3 | +from neomodel import * |
| 4 | +import logging |
| 5 | + |
| 6 | + |
| 7 | + |
| 8 | +class Permission(StructuredRel): |
| 9 | + FromPort = IntegerProperty() |
| 10 | + ToPort = IntegerProperty() |
| 11 | + IpProtocol = StringProperty() |
| 12 | + |
| 13 | +class CIDR(StructuredNode): |
| 14 | + CidrIp = StringProperty(unique=True) |
| 15 | + grants = RelationshipFrom('SecurityGroup', 'GRANTED_BY', model=Permission) |
| 16 | + |
| 17 | +class SecurityGroup(StructuredNode): |
| 18 | + ARN = StringProperty(unique_index=True) |
| 19 | + OwnerId = StringProperty() |
| 20 | + GroupId = StringProperty() |
| 21 | + GroupName = StringProperty() |
| 22 | + VpcIp = StringProperty() |
| 23 | + CidrIpPermissions = RelationshipTo('CIDR', 'GRANTED_TO', model=Permission) |
| 24 | + SecurityGroupPermissions = RelationshipTo('SecurityGroup', 'GRANTED_TO', model=Permission) |
| 25 | + |
| 26 | + @staticmethod |
| 27 | + def arn(region, owner_id, group_id): |
| 28 | + return 'arn:aws:ec2:{}:{}:security-group/{}'.format(region, owner_id, group_id) |
| 29 | + |
| 30 | + |
| 31 | +def load(): |
| 32 | + log = logging.getLogger() |
| 33 | + groups = {} |
| 34 | + ec2 = boto3.client('ec2') |
| 35 | + paginator = ec2.get_paginator('describe_security_groups') |
| 36 | + pages = paginator.paginate() |
| 37 | + for page in pages: |
| 38 | + for sg in page['SecurityGroups']: |
| 39 | + arn = SecurityGroup.arn(ec2.meta.region_name, sg['OwnerId'], sg['GroupId']) |
| 40 | + groups[arn] = sg |
| 41 | + |
| 42 | + for arn, sg in groups.items(): |
| 43 | + VpcId = sg['VpcId'] if 'VpcId' in sg else None |
| 44 | + properties = {n: sg[n] for n in filter(lambda n: n in sg, ['GroupName', 'OwnerId', 'GroupId', 'VpcId'])} |
| 45 | + properties['ARN'] = arn |
| 46 | + |
| 47 | + security_group = SecurityGroup.create_or_update(properties)[0] |
| 48 | + sys.stderr.write('storing security group {}\n'.format(security_group.GroupId)) |
| 49 | + |
| 50 | + for p in security_group.CidrIpPermissions: |
| 51 | + security_group.CidrIpPermissions.disconnect(p) |
| 52 | + for p in security_group.SecurityGroupPermissions: |
| 53 | + security_group.SecurityGroupPermissions.disconnect(p) |
| 54 | + |
| 55 | + |
| 56 | + for arn, sg in groups.items(): |
| 57 | + security_group = SecurityGroup.get_or_create({'ARN': arn})[0] |
| 58 | + for p in sg['IpPermissions']: |
| 59 | + permission = {n : p[n] for n in filter(lambda n : n in p, ['IpProtocol', 'FromPort', 'ToPort'])} |
| 60 | + if VpcId is not None: |
| 61 | + permission['VpcId'] = VpcId |
| 62 | + |
| 63 | + for c in p['IpRanges']: |
| 64 | + CidrIp = CIDR.get_or_create({'CidrIp': c['CidrIp']})[0] |
| 65 | + security_group.CidrIpPermissions.connect(CidrIp, permission) |
| 66 | + |
| 67 | + for c in p['Ipv6Ranges']: |
| 68 | + CidrIp = CIDR.get_or_create({'CidrIp': c['CidrIpv6']})[0] |
| 69 | + security_group.CidrIpPermissions.connect(CidrIp, permission) |
| 70 | + |
| 71 | + for g in p['UserIdGroupPairs']: |
| 72 | + properties = {} |
| 73 | + properties['ARN'] = SecurityGroup.arn(ec2.meta.region_name, g['UserId'], g['GroupId']) |
| 74 | + properties['OwnerId'] = g['UserId'] |
| 75 | + properties['GroupId'] = g['GroupId'] |
| 76 | + source_sg = SecurityGroup.get_or_create(properties)[0] |
| 77 | + sys.stderr.write('grant IP permission {} from {} to {}\n'.format(permission, security_group.GroupId, source_sg.GroupId)) |
| 78 | + security_group.SecurityGroupPermissions.connect(source_sg, permission) |
| 79 | + |
| 80 | + |
0 commit comments