Skip to content
This repository was archived by the owner on Oct 7, 2025. It is now read-only.

Commit fdfa6cc

Browse files
authored
Merge pull request #2487 from codeenigma/Updatng-permissions-and-adding-more-functions-PR-2.x
Updatng permissions and adding more functions pr 2.x
2 parents 5ec759b + 633aa27 commit fdfa6cc

File tree

3 files changed

+122
-65
lines changed

3 files changed

+122
-65
lines changed

roles/aws/aws_admin_tools/defaults/main.yml

Lines changed: 18 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -14,8 +14,13 @@ aws_admin_tools:
1414
policies: []
1515
- name: "change_asg_scaling"
1616
type: POST
17-
policies:
18-
- arn:aws:iam::aws:policy/AmazonEC2FullAccess
17+
policies: []
18+
inline_policies:
19+
name: "change_asg_scaling"
20+
resource: "*"
21+
action:
22+
- "autoscaling:DescribePolicies"
23+
- "autoscaling:PutScalingPolicy"
1924
- name: "get_list_of_ec2"
2025
type: GET
2126
policies: []
@@ -41,3 +46,14 @@ aws_admin_tools:
4146
resource: "*"
4247
action:
4348
- "wafv2:UpdateIPSet"
49+
- name: "get_acl_list"
50+
type: GET
51+
policies: []
52+
inline_policies:
53+
name: "get_acl_list"
54+
resource: "*"
55+
action:
56+
- "wafv2:ListResourcesForWebACL"
57+
- "wafv2:ListWebACLs"
58+
- "wafv2:GetWebACL"
59+
- "cloudfront:ListDistributionsByWebACLId"
Lines changed: 63 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,63 @@
1+
import json
2+
import boto3
3+
4+
waf_regional = boto3.client("wafv2", region_name="{{ _aws_region }}")
5+
waf_cf = boto3.client("wafv2", region_name="us-east-1")
6+
cf_client = boto3.client('cloudfront', region_name="us-east-1")
7+
8+
def get_rules(waf_client, acl_name, acl_id, scope):
9+
rule_details = waf_client.get_web_acl(Name=acl_name, Scope=scope, Id=acl_id)
10+
return [
11+
{
12+
'Name': rule['Name'],
13+
'Priority': rule['Priority']
14+
}
15+
for rule in rule_details['WebACL']['Rules']
16+
]
17+
18+
def get_cf_associations(cf_client, web_acl_arn):
19+
dist_list = cf_client.list_distributions_by_web_acl_id(WebACLId=web_acl_arn)
20+
return [item['DomainName'] for item in dist_list.get('DistributionList', {}).get('Items', [])]
21+
22+
def get_regional_associations(waf_client, web_acl_arn):
23+
associations = []
24+
for res_type in ['APPLICATION_LOAD_BALANCER', 'API_GATEWAY']:
25+
res_list = waf_client.list_resources_for_web_acl(WebACLArn=web_acl_arn, ResourceType=res_type)
26+
if res_list.get('ResourceArns'):
27+
associations.append({res_type: res_list['ResourceArns']})
28+
return associations
29+
30+
def get_web_acls(waf_client, scope, include_cf_associations=False, cf_client=None):
31+
response = waf_client.list_web_acls(Scope=scope)
32+
web_acls = []
33+
34+
for acl in response['WebACLs']:
35+
rules = get_rules(waf_client, acl['Name'], acl['Id'], scope)
36+
associations = (
37+
get_cf_associations(cf_client, acl['ARN']) if include_cf_associations
38+
else get_regional_associations(waf_client, acl['ARN'])
39+
)
40+
web_acls.append({
41+
'Name': acl['Name'],
42+
'Id': acl['Id'],
43+
'Rules': rules,
44+
'Association': associations
45+
})
46+
return web_acls
47+
48+
def lambda_handler(event, context):
49+
# CloudFront ACLs (Global Scope)
50+
cf_acls = get_web_acls(waf_cf, scope='CLOUDFRONT', include_cf_associations=True, cf_client=cf_client)
51+
52+
# Regional ACLs (EU-West-1)
53+
regional_acls = get_web_acls(waf_regional, scope='REGIONAL')
54+
55+
return {
56+
'statusCode': 200,
57+
'ACLs': {
58+
'CloudFront': cf_acls,
59+
'Regional': {
60+
"{{ _aws_region }}": regional_acls
61+
}
62+
}
63+
}

roles/aws/aws_admin_tools/templates/api_get_list_of_ec2.py.j2

Lines changed: 41 additions & 63 deletions
Original file line numberDiff line numberDiff line change
@@ -5,66 +5,44 @@ import boto3
55
ec2_cli = boto3.client("ec2", region_name="{{ _aws_region }}")
66

77
def lambda_handler(event, context):
8-
9-
print("Gathering instance details.")
10-
ec2_instances=ec2_cli.describe_instances()
11-
12-
instance_exist = False
13-
Ec2_info_list=[]
14-
15-
for reservation in ec2_instances["Reservations"]:
16-
for instance in reservation["Instances"]:
17-
pub_ip = ""
18-
priv_ip = ""
19-
inst_name = ""
20-
eip_list=[]
21-
priv_eip=[]
22-
pub_eip=[]
23-
24-
if "PublicIpAddress" in instance:
25-
pub_ip = instance['PublicIpAddress']
26-
else:
27-
pub_ip = "-"
28-
if "PrivateIpAddress" in instance:
29-
priv_ip = instance['PrivateIpAddress']
30-
else:
31-
priv_ip = "-"
32-
33-
if "Tags" in instance:
34-
for name in instance['Tags']:
35-
if name['Key'] == 'Name':
36-
inst_name = name['Value']
37-
38-
eip_list = ec2_cli.describe_addresses(
39-
Filters=[
40-
{
41-
'Name': 'tag:Name',
42-
'Values': [inst_name]
43-
}
44-
]
45-
)
46-
for eip in eip_list['Addresses']:
47-
if "PublicIp" in eip:
48-
pub_eip.append(eip['PublicIp'])
49-
if "PrivateIpAddress" in eip:
50-
priv_eip.append(eip['PrivateIpAddress'])
51-
else:
52-
inst_name = "-"
53-
54-
new_dict={
55-
'EC2 name': inst_name,
56-
'State': instance['State'],
57-
'Public IP': pub_ip,
58-
'Private IP': priv_ip,
59-
'Instance type': instance['InstanceType'],
60-
'EIP': {
61-
'public': pub_eip,
62-
'private': priv_eip
63-
}
64-
}
65-
Ec2_info_list.append(new_dict)
66-
67-
return {
68-
'statusCode': 200,
69-
'EC2 info': Ec2_info_list
70-
}
8+
print("Gathering instance details.")
9+
10+
# Describe instances and addresses once
11+
instances_response = ec2_cli.describe_instances()
12+
addresses_response = ec2_cli.describe_addresses()
13+
14+
# Preprocess EIPs for quick lookup by tag:Name
15+
eip_map = {}
16+
for eip in addresses_response.get('Addresses', []):
17+
name_tag = next((tag['Value'] for tag in eip.get('Tags', []) if tag['Key'] == 'Name'), None)
18+
if name_tag:
19+
eip_map.setdefault(name_tag, {'Public': [], 'Private': []})
20+
if 'PublicIp' in eip:
21+
eip_map[name_tag]['Public'].append(eip['PublicIp'])
22+
if 'PrivateIpAddress' in eip:
23+
eip_map[name_tag]['Private'].append(eip['PrivateIpAddress'])
24+
25+
ec2_info_list = []
26+
27+
for reservation in instances_response.get("Reservations", []):
28+
for instance in reservation.get("Instances", []):
29+
inst_name = "-"
30+
if "Tags" in instance:
31+
for tag in instance["Tags"]:
32+
if tag["Key"] == "Name":
33+
inst_name = tag["Value"]
34+
break
35+
36+
ec2_info_list.append({
37+
"EC2Name": inst_name,
38+
"State": instance.get("State", {}),
39+
"PublicIP": instance.get("PublicIpAddress", "-"),
40+
"PrivateIP": instance.get("PrivateIpAddress", "-"),
41+
"InstanceType": instance.get("InstanceType", "-"),
42+
"EIP": eip_map.get(inst_name, {"Public": [], "Private": []})
43+
})
44+
45+
return {
46+
"statusCode": 200,
47+
"EC2Info": ec2_info_list
48+
}

0 commit comments

Comments
 (0)