Skip to content

Commit 665c062

Browse files
committed
Add support for security group references
1 parent f713186 commit 665c062

File tree

4 files changed

+62
-0
lines changed

4 files changed

+62
-0
lines changed

introspector/aws/ec2.py

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -22,6 +22,17 @@ def _synthesize_defaults(proxy: ServiceProxy,
2222
yield 'Defaults', defaults
2323

2424

25+
def _add_security_group_references(proxy: ServiceProxy, response: Dict):
26+
security_groups = response.get('SecurityGroups', [])
27+
for security_group in security_groups:
28+
group_id = security_group['GroupId']
29+
result = proxy.list('describe_security_group_references', GroupId=group_id)
30+
if result is not None:
31+
# everything is mutable, sigh...
32+
security_group['references'] = result[1].get('SecurityGroupReferenceSet',
33+
[])
34+
35+
2536
def _add_user_data(proxy: ServiceProxy, response: Dict):
2637
reservations = response.get('Reservations', [])
2738
for reservation in reservations:
@@ -86,6 +97,8 @@ def _import_ec2_region(
8697
_add_launch_permissions(proxy, result[1])
8798
elif resource == 'describe_images':
8899
_add_image_attributes(proxy, result[1])
100+
elif resource == 'describe_security_groups':
101+
_add_security_group_references(proxy, result[1])
89102
yield result[0], result[1]
90103
_log.info(f'done with {resource}')
91104
if resource_gate(spec, 'Defaults'):

introspector/aws/transforms/ec2/SecurityGroups.yml

Lines changed: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -29,3 +29,10 @@ resources:
2929
value: vpc
3030
id:
3131
path: ''
32+
- relation: referenced-by
33+
path: references
34+
uri:
35+
resource_name:
36+
value: vpc-peering-connection
37+
id:
38+
path: VpcPeeringConnectionId

introspector/queries/0028-aws_ec2_securitygroup.sql

Lines changed: 24 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -101,3 +101,27 @@ SET
101101
_account_id = EXCLUDED._account_id
102102
;
103103

104+
105+
106+
INSERT INTO aws_ec2_securitygroup_vpcpeeringconnection
107+
SELECT
108+
aws_ec2_securitygroup.id AS securitygroup_id,
109+
aws_ec2_vpcpeeringconnection.id AS vpcpeeringconnection_id,
110+
aws_ec2_securitygroup.provider_account_id AS provider_account_id
111+
FROM
112+
resource AS aws_ec2_securitygroup
113+
INNER JOIN resource_relation AS RR
114+
ON RR.resource_id = aws_ec2_securitygroup.id
115+
AND RR.relation = 'referenced-by'
116+
INNER JOIN resource AS aws_ec2_vpcpeeringconnection
117+
ON aws_ec2_vpcpeeringconnection.id = RR.target_id
118+
AND aws_ec2_vpcpeeringconnection.provider_type = 'VpcPeeringConnection'
119+
AND aws_ec2_vpcpeeringconnection.service = 'ec2'
120+
AND aws_ec2_vpcpeeringconnection.provider_account_id = :provider_account_id
121+
WHERE
122+
aws_ec2_securitygroup.provider_account_id = :provider_account_id
123+
AND aws_ec2_securitygroup.provider_type = 'SecurityGroup'
124+
AND aws_ec2_securitygroup.service = 'ec2'
125+
ON CONFLICT (securitygroup_id, vpcpeeringconnection_id)
126+
DO NOTHING
127+
;
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
-- migrate:up
2+
3+
CREATE TABLE IF NOT EXISTS aws_ec2_securitygroup_vpcpeeringconnection (
4+
securitygroup_id INTEGER NOT NULL REFERENCES aws_ec2_securitygroup (_id) ON DELETE CASCADE,
5+
vpcpeeringconnection_id INTEGER NOT NULL REFERENCES aws_ec2_vpcpeeringconnection (_id) ON DELETE CASCADE,
6+
provider_account_id INTEGER NOT NULL REFERENCES provider_account (id) ON DELETE CASCADE,PRIMARY KEY (securitygroup_id, vpcpeeringconnection_id)
7+
);
8+
9+
ALTER TABLE aws_ec2_securitygroup_vpcpeeringconnection ENABLE ROW LEVEL SECURITY;
10+
CREATE POLICY read_aws_ec2_securitygroup_vpcpeeringconnection ON aws_ec2_securitygroup_vpcpeeringconnection
11+
USING (
12+
current_user = 'introspector_ro'
13+
OR
14+
provider_account_id = current_setting('introspector.provider_account_id', true)::int
15+
);
16+
17+
-- migrate:down
18+
DROP TABLE aws_ec2_securitygroup_vpcpeeringconnection;

0 commit comments

Comments
 (0)