Skip to content
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.

Commit 16602a9

Browse files
author
ievgeniia ieromenko
committedMar 13, 2025·
optionally enable s3 server access logging
1 parent 4c533b5 commit 16602a9

File tree

2 files changed

+78
-70
lines changed

2 files changed

+78
-70
lines changed
 

‎aws_sra_examples/solutions/genai/sra_guardduty_malware_protection_for_s3/README.md

+1
Original file line numberDiff line numberDiff line change
@@ -18,6 +18,7 @@ A key use case for this solution is in the preparation of knowledge bases for Re
1818
- Creates or uses existing S3 bucket for malware protection
1919
- Creates a new KMS key for encrypting the S3 bucket (when creating a new bucket)
2020
- Creates a KMS key alias for easy management
21+
- Provides an option to enable S3 server access logging during bucket creation
2122
- Configures GuardDuty Malware Protection Plan
2223
- Sets up EventBridge rules for scan result notifications
2324
- Implements SNS notifications for alerts

‎aws_sra_examples/solutions/genai/sra_guardduty_malware_protection_for_s3/templates/sra-guardduty-malware-protection-for-s3-main.yaml

+77-70
Original file line numberDiff line numberDiff line change
@@ -25,6 +25,7 @@ Metadata:
2525
Parameters:
2626
- pCreateNewBucket
2727
- pS3MalwareProtectedBucketNamePrefix
28+
- pS3AccessLogsBucket
2829
- pKmsKeyAlias
2930
- Label:
3031
default: EventBridge Properties
@@ -35,81 +36,83 @@ Metadata:
3536
Parameters:
3637
- pSRAAlarmEmail
3738
ParameterLabels:
38-
pExistingBucketName:
39-
default: Existing S3 Bucket Name
39+
pEventRuleRoleName:
40+
default: Event Rule Role Name
4041
pExistingBucketKmsKey:
4142
default: Existing S3 KMS Key ARN
42-
pSRAAlarmEmail:
43-
default: (Optional) SRA Alarm Email
44-
pSRASolutionName:
45-
default: SRA Solution Name
43+
pExistingBucketName:
44+
default: Existing S3 Bucket Name
4645
pKmsKeyAlias:
4746
default: KMS Key Alias
47+
pS3AccessLogsBucket:
48+
default: S3 Access Logs Bucket Name
4849
pS3MalwareProtectedBucketNamePrefix:
4950
default: S3 Malware Protected Bucket Name Prefix
50-
pEventRuleRoleName:
51-
default: Event Rule Role Name
51+
pSRAAlarmEmail:
52+
default: (Optional) SRA Alarm Email
53+
pSRASolutionName:
54+
default: SRA Solution Name
5255
pUseExistingBucket:
5356
default: Use Existing S3 Bucket
5457

5558
Parameters:
5659
pCreateNewBucket:
5760
AllowedValues: ['true', 'false']
5861
Default: 'true'
59-
Description: Create a new S3 bucket for malware protection
62+
Description: Create a new S3 bucket
6063
Type: String
61-
pUseExistingBucket:
62-
AllowedValues: ['true', 'false']
63-
Default: 'false'
64-
Description: Use an existing S3 bucket for malware protection
64+
pEventRuleRoleName:
65+
AllowedPattern: ^[\w+=,.@-]{1,64}$
66+
ConstraintDescription: Max 64 alphanumeric characters. Also special characters supported [+, =, ., @, -].
67+
Default: sra-guardduty-malware-protection-for-s3-events
68+
Description: Event rule role name
69+
Type: String
70+
pExistingBucketKmsKey:
71+
AllowedPattern: ^$|^arn:(aws[a-zA-Z-]*){1}:kms:[a-z0-9-]+:\d{12}:key\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$
72+
ConstraintDescription: "Key ARN example: arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
73+
Default: ''
74+
Description: (Optional) Existing S3 KMS key ARN for existing S3 bucket
6575
Type: String
6676
pExistingBucketName:
6777
AllowedPattern: ^$|^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$
68-
ConstraintDescription: Bucket name can include numbers, lowercase letters,
69-
uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).
78+
ConstraintDescription: Bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).
7079
Default: ''
71-
Description: (Optional) Existing S3 bucket name for malware protection
80+
Description: (Optional) Existing S3 bucket name for malware protection.
7281
Type: String
73-
pExistingBucketKmsKey:
74-
AllowedPattern: ^$|^arn:(aws[a-zA-Z-]*){1}:kms:[a-z0-9-]+:\d{12}:key\/[a-f0-9]{8}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{4}-[a-f0-9]{12}$
75-
ConstraintDescription: "Key ARN example: arn:aws:kms:us-east-2:111122223333:key/1234abcd-12ab-34cd-56ef-1234567890ab"
82+
pKmsKeyAlias:
83+
AllowedPattern: ^[a-zA-Z0-9/_-]+$
84+
ConstraintDescription: The alias must be string of 1-256 characters. It can contain only alphanumeric characters, forward slashes (/), underscores (_), and dashes (-).
85+
Default: sra-guardduty-malware-protection-for-s3-key
86+
Description: KMS Key Alias
87+
Type: String
88+
pS3AccessLogsBucket:
89+
AllowedPattern: ^$|^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$
90+
ConstraintDescription: Bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).
7691
Default: ''
77-
Description: (Optional) Existing S3 KMS key ARN for malware protection
92+
Description: (Optional) S3 bucket name for the S3 Server Access Logs
93+
Type: String
94+
pS3MalwareProtectedBucketNamePrefix:
95+
AllowedPattern: ^$|^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$
96+
ConstraintDescription: S3 bucket name can include numbers, lowercase letters, uppercase letters, and hyphens (-). It cannot start or end with a hyphen (-).
97+
Default: sra-protected-bucket
98+
Description: S3 Malware Protected Bucket Name Prefix
7899
Type: String
79100
pSRAAlarmEmail:
80101
AllowedPattern: ^$|^([a-zA-Z0-9_.+-]+@[a-zA-Z0-9-]+\.[a-zA-Z0-9-.]+)$
81102
ConstraintDescription: Must be a valid email address.
82103
Default: ''
83-
Description: (Optional) Email address for receiving SRA alarms
104+
Description: Email address for receiving SRA alarms
84105
Type: String
85106
pSRASolutionName:
86107
AllowedValues:
87108
- sra-guardduty-malware-protection-for-s3
88109
Default: sra-guardduty-malware-protection-for-s3
89110
Description: The SRA solution name. The default value is the folder name of the solution
90111
Type: String
91-
pKmsKeyAlias:
92-
AllowedPattern: ^[a-zA-Z0-9/_-]+$
93-
ConstraintDescription: The alias must be string of 1-256 characters. It can
94-
contain only alphanumeric characters, forward slashes (/), underscores
95-
(_), and dashes (-).
96-
Default: sra-guardduty-malware-protection-for-s3-key
97-
Description: Organization CloudTrail KMS Key Alias
98-
Type: String
99-
pS3MalwareProtectedBucketNamePrefix:
100-
AllowedPattern: ^$|^[0-9a-zA-Z]+([0-9a-zA-Z-]*[0-9a-zA-Z])*$
101-
ConstraintDescription: S3 bucket name can include numbers, lowercase letters,
102-
uppercase letters, and hyphens (-). It cannot start or end with a hyphen
103-
(-).
104-
Default: sra-protected-bucket
105-
Description: S3 Malware Protected Bucket Name Prefix
106-
Type: String
107-
pEventRuleRoleName:
108-
AllowedPattern: ^[\w+=,.@-]{1,64}$
109-
ConstraintDescription: Max 64 alphanumeric characters. Also special characters
110-
supported [+, =, ., @, -].
111-
Default: sra-guardduty-malware-protection-for-s3-events
112-
Description: Event rule role name for putting events on the home region event bus
112+
pUseExistingBucket:
113+
AllowedValues: ['true', 'false']
114+
Default: 'false'
115+
Description: Use an existing S3 bucket for malware protection
113116
Type: String
114117

115118
Rules:
@@ -140,12 +143,13 @@ Conditions:
140143
- 'true'
141144
cExistingBucket: !Not [!Equals [!Ref pExistingBucketName, '']]
142145
cExistingKmsKey: !Not [!Equals [!Ref pExistingBucketKmsKey, '']]
146+
cEnableAccessLogging: !Not [!Equals [!Ref pS3AccessLogsBucket, '']]
143147

144148
Resources:
145149
rKMSKeyForBucket:
146150
Type: AWS::KMS::Key
147151
Condition: cCreateNewBucket
148-
DeletionPolicy: Delete # todo: retain
152+
DeletionPolicy: Delete
149153
UpdateReplacePolicy: Retain
150154
Properties:
151155
EnableKeyRotation: true
@@ -169,7 +173,7 @@ Resources:
169173
AliasName: !Sub alias/${pKmsKeyAlias}-${AWS::AccountId}-${AWS::Region}
170174
TargetKeyId: !Ref rKMSKeyForBucket
171175

172-
rS3MalwareProtectedBucket:
176+
rGuardDutyMalwareProtectedBucket:
173177
Type: AWS::S3::Bucket
174178
Condition: cCreateNewBucket
175179
DeletionPolicy: Retain
@@ -185,10 +189,12 @@ Resources:
185189
comment: S3 access logging is not enabled.
186190
Properties:
187191
BucketName: !Sub ${pS3MalwareProtectedBucketNamePrefix}-${AWS::AccountId}-${AWS::Region}
188-
# LoggingConfiguration: !If
189-
# # - cEnableAccessLogging
190-
# - DestinationBucketName: !Ref pAccessLogsBucket
191-
# - LogFilePrefix: !Sub ${pSRASolutionName}-logs/
192+
LoggingConfiguration:
193+
!If
194+
- cEnableAccessLogging
195+
- DestinationBucketName: !Ref pS3AccessLogsBucket
196+
LogFilePrefix: !Sub ${pSRASolutionName}-logs/
197+
- !Ref AWS::NoValue
192198
VersioningConfiguration:
193199
Status: Enabled
194200
BucketEncryption:
@@ -236,15 +242,15 @@ Resources:
236242
Statement:
237243
- Effect: Allow
238244
Action: sns:Publish
239-
Resource: !GetAtt rS3MalwareProtectionPlanAlarmTopic.TopicArn
245+
Resource: !GetAtt rGuardDutyMalwareProtectionForS3AlarmTopic.TopicArn
240246
- Effect: Allow
241247
Action: sqs:SendMessage
242-
Resource: !GetAtt rGuardDutyS3ProtectionRuleDLQ.Arn
248+
Resource: !GetAtt rGuardDutyMalwareProtectionForS3RuleDLQ.Arn
243249
Tags:
244250
- Key: sra-solution
245251
Value: !Ref pSRASolutionName
246252

247-
rGuardDutyS3ProtectionRuleDLQ:
253+
rGuardDutyMalwareProtectionForS3RuleDLQ:
248254
Type: AWS::SQS::Queue
249255
Properties:
250256
KmsMasterKeyId: alias/aws/sqs
@@ -256,25 +262,25 @@ Resources:
256262
DeletionPolicy: Delete
257263
UpdateReplacePolicy: Delete
258264

259-
rGuardDutyS3ProtectionRuleDLQPolicy:
265+
rGuardDutyMalwareProtectionForS3RuleDLQPolicy:
260266
Type: AWS::SQS::QueuePolicy
261267
Properties:
262268
Queues:
263-
- !Ref rGuardDutyS3ProtectionRuleDLQ
269+
- !Ref rGuardDutyMalwareProtectionForS3RuleDLQ
264270
PolicyDocument:
265271
Statement:
266272
- Action: SQS:SendMessage
267273
Condition:
268274
ArnEquals:
269275
aws:SourceArn:
270-
- !GetAtt rGuardDutyS3MalwareProtectionEventRule.Arn
276+
- !GetAtt rGuardDutyMalwareProtectionForS3EventRule.Arn
271277
Effect: Allow
272278
Principal:
273279
Service: events.amazonaws.com
274280
Resource:
275-
- !GetAtt rGuardDutyS3MalwareProtectionEventRule.Arn
281+
- !GetAtt rGuardDutyMalwareProtectionForS3EventRule.Arn
276282

277-
rGuardDutyS3MalwareProtectionEventRule:
283+
rGuardDutyMalwareProtectionForS3EventRule:
278284
Type: AWS::Events::Rule
279285
Properties:
280286
Description: GuardDuty Copy S3 Object Rule for source bucket
@@ -303,16 +309,16 @@ Resources:
303309
- ACCESS_DENIED
304310
State: ENABLED
305311
Targets:
306-
- Arn: !GetAtt rS3MalwareProtectionPlanAlarmTopic.TopicArn
307-
Id: !GetAtt rS3MalwareProtectionPlanAlarmTopic.DisplayName
312+
- Arn: !GetAtt rGuardDutyMalwareProtectionForS3AlarmTopic.TopicArn
313+
Id: !GetAtt rGuardDutyMalwareProtectionForS3AlarmTopic.DisplayName
308314
DeadLetterConfig:
309-
Arn: !GetAtt rGuardDutyS3ProtectionRuleDLQ.Arn
315+
Arn: !GetAtt rGuardDutyMalwareProtectionForS3RuleDLQ.Arn
310316
RetryPolicy:
311317
MaximumEventAgeInSeconds: 86400 # 24 hours
312318
MaximumRetryAttempts: 185
313319
RoleArn: !Sub arn:${AWS::Partition}:iam::${AWS::AccountId}:role/${pEventRuleRoleName}-${AWS::Region}
314320

315-
rIAMS3MalwareBucketPolicy:
321+
rGuardDutyMalwareProtectionForS3IamPolicy:
316322
Type: AWS::IAM::ManagedPolicy
317323
DeletionPolicy: Delete
318324
Properties:
@@ -410,9 +416,10 @@ Resources:
410416
kms:ViaService: s3.*.amazonaws.com
411417
- !Ref AWS::NoValue
412418

413-
rIAMS3MalwareBucketRole:
419+
rGuardDutyMalwareProtectionForS3IamRole:
414420
Type: AWS::IAM::Role
415421
Properties:
422+
RoleName: !Sub ${pSRASolutionName}-${AWS::Region}
416423
AssumeRolePolicyDocument:
417424
Statement:
418425
- Action: sts:AssumeRole
@@ -421,12 +428,12 @@ Resources:
421428
Service: malware-protection-plan.guardduty.amazonaws.com
422429
Version: '2012-10-17'
423430
ManagedPolicyArns:
424-
- !Sub ${rIAMS3MalwareBucketPolicy.PolicyArn}
431+
- !Sub ${rGuardDutyMalwareProtectionForS3IamPolicy.PolicyArn}
425432
Tags:
426433
- Key: sra-solution
427434
Value: !Ref pSRASolutionName
428435

429-
rGuardDutyS3MalwareProtectionPlan:
436+
rGuardDutyMalwareProtectionForS3:
430437
Type: AWS::GuardDuty::MalwareProtectionPlan
431438
Properties:
432439
Actions:
@@ -438,12 +445,12 @@ Resources:
438445
- cExistingBucket
439446
- !Ref pExistingBucketName
440447
- !Sub ${pS3MalwareProtectedBucketNamePrefix}-${AWS::AccountId}-${AWS::Region}
441-
Role: !GetAtt rIAMS3MalwareBucketRole.Arn
448+
Role: !GetAtt rGuardDutyMalwareProtectionForS3IamRole.Arn
442449
Tags:
443450
- Key: sra-solution
444451
Value: !Ref pSRASolutionName
445452

446-
rS3MalwareProtectionPlanAlarmTopic:
453+
rGuardDutyMalwareProtectionForS3AlarmTopic:
447454
Type: AWS::SNS::Topic
448455
Properties:
449456
DisplayName: !Sub ${pSRASolutionName}-alarm
@@ -459,17 +466,17 @@ Resources:
459466
Outputs:
460467
GuardDutyS3MalwareProtectionPlanArn:
461468
Description: Amazon Resource Name (ARN) associated with this Malware Protection plan
462-
Value: !GetAtt rGuardDutyS3MalwareProtectionPlan.Arn
469+
Value: !GetAtt rGuardDutyMalwareProtectionForS3.Arn
463470

464471
GuardDutyS3MalwareProtectionPlanId:
465472
Description: A unique identifier associated with Malware Protection plan
466-
Value: !GetAtt rGuardDutyS3MalwareProtectionPlan.MalwareProtectionPlanId
473+
Value: !GetAtt rGuardDutyMalwareProtectionForS3.MalwareProtectionPlanId
467474

468475
S3MalwareProtectionRole:
469476
Description: IAM role created for S3 Malware Protection
470-
Value: !Ref rIAMS3MalwareBucketRole
477+
Value: !Ref rGuardDutyMalwareProtectionForS3IamRole
471478

472479
SourceBucketArn:
473480
Condition: cCreateNewBucket
474481
Description: The bucket arn which has been created and enabled for S3 Malware protection
475-
Value: !GetAtt rS3MalwareProtectedBucket.Arn
482+
Value: !GetAtt rGuardDutyMalwareProtectedBucket.Arn

0 commit comments

Comments
 (0)
Please sign in to comment.