Skip to content

Commit e14abce

Browse files
author
EC2 Default User
committed
reverting cb lookup lambda code
1 parent 4f1b62c commit e14abce

File tree

1 file changed

+36
-69
lines changed
  • src/lambda/gc04_check_alerts_flag_misuse

1 file changed

+36
-69
lines changed

src/lambda/gc04_check_alerts_flag_misuse/app.py

+36-69
Original file line numberDiff line numberDiff line change
@@ -5,15 +5,7 @@
55
import json
66
import logging
77

8-
from utils import (
9-
is_scheduled_notification,
10-
check_required_parameters,
11-
flatten_dict,
12-
check_guardrail_requirement_by_cloud_usage_profile,
13-
get_cloud_profile_from_tags,
14-
GuardrailType,
15-
GuardrailRequirementType,
16-
)
8+
from utils import is_scheduled_notification, check_required_parameters, flatten_dict, check_guardrail_requirement_by_cloud_usage_profile, get_cloud_profile_from_tags, GuardrailType, GuardrailRequirementType
179
from boto_util.organizations import get_account_tags
1810
from boto_util.client import get_client
1911
from boto_util.config import build_evaluation, submit_evaluations
@@ -33,7 +25,7 @@ def subscription_is_confirmed(sns_client, subscription_arn):
3325
attributes = response.get("Attributes")
3426
logger.info("Subscription attributes: %s", attributes)
3527

36-
if attributes is None:
28+
if attributes == None:
3729
return False
3830

3931
return attributes.get("PendingConfirmation") == "false"
@@ -52,7 +44,7 @@ def subscription_is_confirmed(sns_client, subscription_arn):
5244

5345
def rule_matches_against_cb_role_identity(rule_event_pattern, cb_role_arn):
5446
logger.info("rule_event_pattern: %s", rule_event_pattern)
55-
if rule_event_pattern is None:
47+
if rule_event_pattern == None:
5648
return False
5749

5850
event_pattern_dict = json.loads(rule_event_pattern)
@@ -64,31 +56,23 @@ def rule_matches_against_cb_role_identity(rule_event_pattern, cb_role_arn):
6456
) and "Role" in ep_detail.get("userIdentity.sessionContext.sessionIssuer.type", [])
6557

6658

67-
def get_role_arn(iam_client, cb_role_pattern: str) -> str | None:
68-
"""
69-
aws iam list-roles --query "Roles[?contains(RoleName, 'CloudBrokering')].[RoleName, Arn]"
70-
"""
59+
def get_role_arn(iam_client, cb_role: str) -> str | None:
7160
try:
72-
paginator = iam_client.get_paginator("list_roles")
73-
matched_roles = []
74-
75-
for page in paginator.paginate():
76-
for role in page["Roles"]:
77-
if cb_role_pattern in role["RoleName"]:
78-
matched_roles.append(role)
79-
80-
if not matched_roles:
81-
return None
82-
83-
# Return the ARN of the first matched role
84-
return matched_roles[0]["Arn"]
61+
response = iam_client.get_role(RoleName=cb_role)
62+
return response.get("Role").get("Arn")
8563
except botocore.exceptions.ClientError as ex:
86-
ex.response["Error"]["Message"] = "Error listing or matching roles."
87-
ex.response["Error"]["Code"] = "InternalError"
64+
if "NoSuchEntity" in ex.response["Error"]["Code"]:
65+
return None
66+
elif "ServiceFailure" in ex.response["Error"]["Code"]:
67+
ex.response["Error"]["Message"] = "get_role operation failed due to a Service Failure error."
68+
else:
69+
ex.response["Error"]["Message"] = "InternalError"
70+
ex.response["Error"]["Code"] = "InternalError"
8871
raise ex
8972

9073

9174
def check_cb_role(cloud_trail_client, cb_role, event, aws_account_id):
75+
"""Checks cloudtrail events to see if the CloudBroker role has been changed. Build evaluation based on discovery."""
9276

9377
role_change_events = [
9478
"DeleteRolePolicy",
@@ -133,8 +117,8 @@ def check_rule_sns_target_is_setup(sns_client, event_bridge_client, rule, event)
133117

134118
for target in targets:
135119
logger.info("Checking rule target: %s", target)
136-
target_arn: str = target.get("Arn")
137120
# is target an SNS input transformer?
121+
target_arn: str = target.get("Arn")
138122
if target_arn.startswith("arn:aws:sns:"):
139123
# yes, get a list of topic subscriptions
140124
subscriptions = list_all_sns_subscriptions_by_topic(sns_client, target_arn)
@@ -184,6 +168,7 @@ def lambda_handler(event, context):
184168
execution_role_name = rule_parameters.get("ExecutionRoleName")
185169
audit_account_id = rule_parameters.get("AuditAccountID", "")
186170
aws_account_id = event["accountId"]
171+
is_not_audit_account = aws_account_id != audit_account_id
187172

188173
evaluations = []
189174

@@ -192,60 +177,44 @@ def lambda_handler(event, context):
192177
aws_sns_client = get_client("sns", aws_account_id, execution_role_name)
193178
aws_cloud_trail_client = get_client("cloudtrail", aws_account_id, execution_role_name)
194179
aws_iam_client = get_client("iam", aws_account_id, execution_role_name)
195-
180+
196181
# Check cloud profile
197182
tags = get_account_tags(get_client("organizations", assume_role=False), aws_account_id)
198183
cloud_profile = get_cloud_profile_from_tags(tags)
199184
gr_requirement_type = check_guardrail_requirement_by_cloud_usage_profile(GuardrailType.Guardrail4, cloud_profile)
200-
185+
201186
# If the guardrail is recommended
202187
if gr_requirement_type == GuardrailRequirementType.Recommended:
203-
return submit_evaluations(
204-
aws_config_client,
188+
return submit_evaluations(aws_config_client, event, [build_evaluation(
189+
aws_account_id,
190+
"COMPLIANT",
205191
event,
206-
[
207-
build_evaluation(
208-
aws_account_id,
209-
"COMPLIANT",
210-
event,
211-
gr_requirement_type=gr_requirement_type,
212-
)
213-
],
214-
)
192+
gr_requirement_type=gr_requirement_type
193+
)])
215194
# If the guardrail is not required
216195
elif gr_requirement_type == GuardrailRequirementType.Not_Required:
217-
return submit_evaluations(
218-
aws_config_client,
196+
return submit_evaluations(aws_config_client, event, [build_evaluation(
197+
aws_account_id,
198+
"NOT_APPLICABLE",
219199
event,
220-
[
221-
build_evaluation(
222-
aws_account_id,
223-
"NOT_APPLICABLE",
224-
event,
225-
gr_requirement_type=gr_requirement_type,
226-
)
227-
],
228-
)
229-
200+
gr_requirement_type=gr_requirement_type
201+
)])
202+
203+
rules_are_compliant = False
230204
rules = list_all_event_bridge_rules(aws_event_bridge_client)
231-
cb_role_pattern = rule_parameters["IAM_Role_Name"]
205+
cb_role = rule_parameters["IAM_Role_Name"]
232206

233-
# Now we lookup the CloudBroker role by partial match
234-
cb_role_arn = get_role_arn(aws_iam_client, cb_role_pattern)
207+
cb_role_arn = get_role_arn(aws_iam_client, cb_role)
235208

236209
if not cb_role_arn:
237210
compliance_type = "NON_COMPLIANT"
238-
annotation = f"Cloud Broker Role containing '{cb_role_pattern}' in the name was not found."
211+
annotation = f"Cloud Broker Role with name '{cb_role}' not found."
239212
evaluation = build_evaluation(aws_account_id, compliance_type, event, annotation=annotation)
240213
logger.info(f"{compliance_type}: {annotation}")
241214
submit_evaluations(aws_config_client, event, [evaluation])
242215
return
243216

244-
cb_rules = [
245-
rule
246-
for rule in rules
247-
if rule_matches_against_cb_role_identity(rule.get("EventPattern"), cb_role_arn)
248-
]
217+
cb_rules = [rule for rule in rules if rule_matches_against_cb_role_identity(rule.get("EventPattern"), cb_role_arn)]
249218

250219
if len(cb_rules) == 0:
251220
evaluations.append(
@@ -257,7 +226,6 @@ def lambda_handler(event, context):
257226
)
258227
)
259228
else:
260-
rules_are_compliant = False
261229
for rule in cb_rules:
262230
logger.info(f"Checking rule: {rule}")
263231
rule_evaluation = check_rule_sns_target_is_setup(aws_sns_client, aws_event_bridge_client, rule, event)
@@ -266,8 +234,7 @@ def lambda_handler(event, context):
266234
evaluations.append(rule_evaluation)
267235

268236
if rules_are_compliant:
269-
extracted_role_name = cb_role_arn.split("/")[-1] if "/" in cb_role_arn else cb_role_arn
270-
evaluations.append(check_cb_role(aws_cloud_trail_client, extracted_role_name, event, aws_account_id))
237+
evaluations.append(check_cb_role(aws_cloud_trail_client, cb_role, event, aws_account_id))
271238
else:
272239
evaluations.append(
273240
build_evaluation(
@@ -278,4 +245,4 @@ def lambda_handler(event, context):
278245
)
279246
)
280247

281-
submit_evaluations(aws_config_client, event, evaluations)
248+
submit_evaluations(aws_config_client, event, evaluations)

0 commit comments

Comments
 (0)