Skip to content

Commit 06c7a41

Browse files
committed
final updates for opt-in regions to work
1 parent ee00c65 commit 06c7a41

File tree

5 files changed

+51
-51
lines changed

5 files changed

+51
-51
lines changed

aws_sra_examples/solutions/guardduty/guardduty_org/lambda/src/app.py

Lines changed: 32 additions & 34 deletions
Original file line numberDiff line numberDiff line change
@@ -201,40 +201,6 @@ def process_create_update_event(params: dict, regions: list) -> None:
201201
detectors_exist = False
202202
run_count = 0
203203

204-
# temp move while loop around to configure GD first 061324
205-
# else:
206-
auto_enable_s3_logs = (params.get("AUTO_ENABLE_S3_LOGS", "false")).lower() in "true"
207-
enable_eks_audit_logs = (params.get("ENABLE_EKS_AUDIT_LOGS", "false")).lower() in "true"
208-
auto_enable_malware_protection = (params.get("AUTO_ENABLE_MALWARE_PROTECTION", "false")).lower() in "true"
209-
enable_rds_login_events = (params.get("ENABLE_RDS_LOGIN_EVENTS", "false")).lower() in "true"
210-
enable_eks_addon_management = (params.get("ENABLE_EKS_ADDON_MANAGEMENT", "false")).lower() in "true"
211-
enable_lambda_network_logs = (params.get("ENABLE_LAMBDA_NETWORK_LOGS", "false")).lower() in "true"
212-
enable_runtime_monitoring = (params.get("ENABLE_RUNTIME_MONITORING", "false")).lower() in "true"
213-
enable_ecs_fargate_agent_management = (params.get("ENABLE_ECS_FARGATE_AGENT_MANAGEMENT", "false")).lower() in "true"
214-
enable_ec2_agent_management = (params.get("ENABLE_EC2_AGENT_MANAGEMENT", "false")).lower() in "true"
215-
216-
gd_features = {
217-
"S3_DATA_EVENTS": auto_enable_s3_logs,
218-
"EKS_AUDIT_LOGS": enable_eks_audit_logs,
219-
"EBS_MALWARE_PROTECTION": auto_enable_malware_protection,
220-
"RDS_LOGIN_EVENTS": enable_rds_login_events,
221-
"LAMBDA_NETWORK_LOGS": enable_lambda_network_logs,
222-
"RUNTIME_MONITORING": enable_runtime_monitoring,
223-
"EKS_ADDON_MANAGEMENT": enable_eks_addon_management,
224-
"ECS_FARGATE_AGENT_MANAGEMENT": enable_ecs_fargate_agent_management,
225-
"EC2_AGENT_MANAGEMENT": enable_ec2_agent_management,
226-
}
227-
228-
guardduty.configure_guardduty(
229-
session,
230-
params["DELEGATED_ADMIN_ACCOUNT_ID"],
231-
gd_features,
232-
regions,
233-
params.get("FINDING_PUBLISHING_FREQUENCY", "FIFTEEN_MINUTES"),
234-
params["KMS_KEY_ARN"],
235-
params["PUBLISHING_DESTINATION_BUCKET_ARN"],
236-
)
237-
238204
while not detectors_exist and run_count < MAX_RUN_COUNT:
239205
run_count += 1
240206
detectors_exist = guardduty.check_for_detectors(session, regions)
@@ -244,6 +210,38 @@ def process_create_update_event(params: dict, regions: list) -> None:
244210

245211
if not detectors_exist:
246212
raise ValueError("GuardDuty Detectors did not get created in the allowed time. Check the Org Management delegated admin setup.")
213+
else:
214+
auto_enable_s3_logs = (params.get("AUTO_ENABLE_S3_LOGS", "false")).lower() in "true"
215+
enable_eks_audit_logs = (params.get("ENABLE_EKS_AUDIT_LOGS", "false")).lower() in "true"
216+
auto_enable_malware_protection = (params.get("AUTO_ENABLE_MALWARE_PROTECTION", "false")).lower() in "true"
217+
enable_rds_login_events = (params.get("ENABLE_RDS_LOGIN_EVENTS", "false")).lower() in "true"
218+
enable_eks_addon_management = (params.get("ENABLE_EKS_ADDON_MANAGEMENT", "false")).lower() in "true"
219+
enable_lambda_network_logs = (params.get("ENABLE_LAMBDA_NETWORK_LOGS", "false")).lower() in "true"
220+
enable_runtime_monitoring = (params.get("ENABLE_RUNTIME_MONITORING", "false")).lower() in "true"
221+
enable_ecs_fargate_agent_management = (params.get("ENABLE_ECS_FARGATE_AGENT_MANAGEMENT", "false")).lower() in "true"
222+
enable_ec2_agent_management = (params.get("ENABLE_EC2_AGENT_MANAGEMENT", "false")).lower() in "true"
223+
224+
gd_features = {
225+
"S3_DATA_EVENTS": auto_enable_s3_logs,
226+
"EKS_AUDIT_LOGS": enable_eks_audit_logs,
227+
"EBS_MALWARE_PROTECTION": auto_enable_malware_protection,
228+
"RDS_LOGIN_EVENTS": enable_rds_login_events,
229+
"LAMBDA_NETWORK_LOGS": enable_lambda_network_logs,
230+
"RUNTIME_MONITORING": enable_runtime_monitoring,
231+
"EKS_ADDON_MANAGEMENT": enable_eks_addon_management,
232+
"ECS_FARGATE_AGENT_MANAGEMENT": enable_ecs_fargate_agent_management,
233+
"EC2_AGENT_MANAGEMENT": enable_ec2_agent_management,
234+
}
235+
236+
guardduty.configure_guardduty(
237+
session,
238+
params["DELEGATED_ADMIN_ACCOUNT_ID"],
239+
gd_features,
240+
regions,
241+
params.get("FINDING_PUBLISHING_FREQUENCY", "FIFTEEN_MINUTES"),
242+
params["KMS_KEY_ARN"],
243+
params["PUBLISHING_DESTINATION_BUCKET_ARN"],
244+
)
247245

248246

249247
def process_sns_records(records: list) -> None:

aws_sra_examples/solutions/guardduty/guardduty_org/lambda/src/common.py

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -53,7 +53,7 @@ def assume_role(role: str, role_session_name: str, account: str = None, session:
5353
Returns:
5454
Session object for the specified AWS account
5555
"""
56-
# TODO(liamschn): move this to correct place
56+
# set regional endpoint environment variable to account for potential opt-in regions
5757
os.environ["AWS_STS_REGIONAL_ENDPOINTS"] = "regional"
5858

5959
if not session:

aws_sra_examples/solutions/guardduty/guardduty_org/lambda/src/guardduty.py

Lines changed: 0 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -405,7 +405,6 @@ def set_org_configuration_params(detector_id: str, gd_features: dict) -> dict:
405405
"DetectorId": detector_id,
406406
"AutoEnable": True,
407407
"Features": features_config,
408-
# "AutoEnableOrganizationMembers": "ALL",
409408
}
410409
name = ""
411410
auto_enable_type = ""
@@ -504,9 +503,6 @@ def configure_guardduty( # noqa: CFQ002, CFQ001
504503

505504
# Loop through the regions and enable GuardDuty
506505
for region in region_list:
507-
# if region == "ap-southeast-4":
508-
# LOGGER.info(f"skipping ap-southeast-4")
509-
# else:
510506
LOGGER.info(f"Configuring GuardDuty in {region}")
511507
regional_guardduty: GuardDutyClient = session.client("guardduty", region_name=region, config=BOTO3_CONFIG)
512508
detectors = regional_guardduty.list_detectors()

aws_sra_examples/solutions/guardduty/guardduty_org/templates/sra-guardduty-org-delivery-kms-key.yaml

Lines changed: 8 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -70,6 +70,12 @@ Conditions:
7070

7171
Resources:
7272
rGuardDutyDeliveryKey:
73+
# checkov:skip=CKV_AWS_33:Ensure KMS key policy does not contain wildcard (*) principal
74+
Metadata:
75+
cfn_nag:
76+
rules_to_suppress:
77+
- id: F76
78+
reason: "Opt-in regions may be used and so conditional policy is required"
7379
Type: AWS::KMS::Key
7480
Properties:
7581
Description: SRA GuardDuty Delivery Key
@@ -92,8 +98,8 @@ Resources:
9298
Principal:
9399
Service: guardduty.amazonaws.com
94100

95-
# TODO(liamschn): temp fix for key permissions for opt-in region
96-
- Sid: Allow ap-southeast-4 GuardDuty to encrypt logs
101+
# key permissions for potential opt-in regions (conditional)
102+
- Sid: Allow opt-in region GuardDuty to encrypt logs
97103
Effect: Allow
98104
Action: kms:GenerateDataKey
99105
Resource: '*'

aws_sra_examples/solutions/guardduty/guardduty_org/templates/sra-guardduty-org-delivery-s3-bucket.yaml

Lines changed: 10 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -128,7 +128,7 @@ Resources:
128128
Service:
129129
- guardduty.amazonaws.com
130130

131-
# TODO(liamschn): bucket perm/location check allow for opt-in region (ap-southeast-4)
131+
# Bucket perm/location check allow for potential opt-in regions
132132
- Sid: AWSBucketPermissionsCheck
133133
Effect: Allow
134134
Action:
@@ -152,18 +152,18 @@ Resources:
152152
Service:
153153
- guardduty.amazonaws.com
154154

155-
# TODO(liamschn): bucket delivery allow for opt-in region (ap-southeast-4)
156-
- Sid: AWSBucketDeliveryApSoutheast4
155+
# Bucket delivery allow for potential opt-in regions
156+
- Sid: AWSBucketDeliveryOptinRegions
157157
Effect: Allow
158158
Action: s3:PutObject
159-
# Condition:
160-
# StringEquals:
161-
# s3:x-amz-acl: bucket-owner-full-control
159+
Condition:
162160
Resource: !Sub arn:aws:s3:::${rGuardDutyDeliveryS3Bucket}/*
163161
Principal: '*'
164162
Condition:
165163
StringLike:
166164
aws:PrincipalServiceName: guardduty.*.amazonaws.com
165+
StringEquals:
166+
s3:x-amz-acl: bucket-owner-full-control
167167

168168
- Sid: DenyUnencryptedObjectUploads
169169
Effect: Deny
@@ -176,8 +176,8 @@ Resources:
176176
Service:
177177
- guardduty.amazonaws.com
178178

179-
# TODO(liamschn): unencryption object upload deny for opt-in region (ap-southeast-4)
180-
- Sid: DenyUnencryptedObjectUploadsApSoutheast4
179+
# Unencryption object upload deny for potential opt-in regions
180+
- Sid: DenyUnencryptedObjectUploadsOptinRegions
181181
Effect: Deny
182182
Action: s3:PutObject
183183
Condition:
@@ -200,8 +200,8 @@ Resources:
200200
Service:
201201
- guardduty.amazonaws.com
202202

203-
# TODO(liamschn): incorrect encryption header deny for opt-in region (ap-southeast-4)
204-
- Sid: DenyIncorrectEncryptionHeaderApSoutheast4
203+
# Incorrect encryption header deny for potential opt-in regions
204+
- Sid: DenyIncorrectEncryptionHeaderOptinRegions
205205
Effect: Deny
206206
Action: s3:PutObject
207207
Condition:

0 commit comments

Comments
 (0)