-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat(cloudtrail): accept existing S3 bucket (#3680)
Make CloudTrail accept existing S3 bucket to write to. Fixes #3651.
- Loading branch information
Showing
4 changed files
with
207 additions
and
17 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
110 changes: 110 additions & 0 deletions
110
packages/@aws-cdk/aws-cloudtrail/test/integ.cloudtrail-supplied-bucket.lit.expected.json
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,110 @@ | ||
{ | ||
"Resources": { | ||
"Bucket83908E77": { | ||
"Type": "AWS::S3::Bucket", | ||
"UpdateReplacePolicy": "Delete", | ||
"DeletionPolicy": "Delete" | ||
}, | ||
"S3486F821D": { | ||
"Type": "AWS::S3::Bucket", | ||
"UpdateReplacePolicy": "Retain", | ||
"DeletionPolicy": "Retain" | ||
}, | ||
"S3Policy2E4AA1D6": { | ||
"Type": "AWS::S3::BucketPolicy", | ||
"Properties": { | ||
"Bucket": { | ||
"Ref": "S3486F821D" | ||
}, | ||
"PolicyDocument": { | ||
"Statement": [ | ||
{ | ||
"Action": "s3:GetBucketAcl", | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "cloudtrail.amazonaws.com" | ||
}, | ||
"Resource": { | ||
"Fn::GetAtt": [ | ||
"S3486F821D", | ||
"Arn" | ||
] | ||
} | ||
}, | ||
{ | ||
"Action": "s3:PutObject", | ||
"Condition": { | ||
"StringEquals": { | ||
"s3:x-amz-acl": "bucket-owner-full-control" | ||
} | ||
}, | ||
"Effect": "Allow", | ||
"Principal": { | ||
"Service": "cloudtrail.amazonaws.com" | ||
}, | ||
"Resource": { | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"S3486F821D", | ||
"Arn" | ||
] | ||
}, | ||
"/AWSLogs/", | ||
{ | ||
"Ref": "AWS::AccountId" | ||
}, | ||
"/*" | ||
] | ||
] | ||
} | ||
} | ||
], | ||
"Version": "2012-10-17" | ||
} | ||
} | ||
}, | ||
"Trail022F0CF2": { | ||
"Type": "AWS::CloudTrail::Trail", | ||
"Properties": { | ||
"IsLogging": true, | ||
"S3BucketName": { | ||
"Ref": "S3486F821D" | ||
}, | ||
"EnableLogFileValidation": true, | ||
"EventSelectors": [ | ||
{ | ||
"DataResources": [ | ||
{ | ||
"Type": "AWS::S3::Object", | ||
"Values": [ | ||
{ | ||
"Fn::Join": [ | ||
"", | ||
[ | ||
{ | ||
"Fn::GetAtt": [ | ||
"Bucket83908E77", | ||
"Arn" | ||
] | ||
}, | ||
"/" | ||
] | ||
] | ||
} | ||
] | ||
} | ||
] | ||
} | ||
], | ||
"IncludeGlobalServiceEvents": true, | ||
"IsMultiRegionTrail": true | ||
}, | ||
"DependsOn": [ | ||
"S3Policy2E4AA1D6" | ||
] | ||
} | ||
} | ||
} |
37 changes: 37 additions & 0 deletions
37
packages/@aws-cdk/aws-cloudtrail/test/integ.cloudtrail-supplied-bucket.lit.ts
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,37 @@ | ||
import iam = require('@aws-cdk/aws-iam'); | ||
import s3 = require('@aws-cdk/aws-s3'); | ||
import cdk = require('@aws-cdk/core'); | ||
import { Stack } from '@aws-cdk/core'; | ||
|
||
import cloudtrail = require('../lib'); | ||
|
||
const app = new cdk.App(); | ||
const stack = new cdk.Stack(app, 'integ-cloudtrail'); | ||
|
||
const bucket = new s3.Bucket(stack, 'Bucket', { removalPolicy: cdk.RemovalPolicy.DESTROY }); | ||
|
||
// using exctecy the same code as inside the cloudtrail class to produce the supplied bucket and policy | ||
const cloudTrailPrincipal = new iam.ServicePrincipal("cloudtrail.amazonaws.com"); | ||
|
||
const Trailbucket = new s3.Bucket(stack, 'S3', {encryption: s3.BucketEncryption.UNENCRYPTED}); | ||
|
||
Trailbucket.addToResourcePolicy(new iam.PolicyStatement({ | ||
resources: [Trailbucket.bucketArn], | ||
actions: ['s3:GetBucketAcl'], | ||
principals: [cloudTrailPrincipal], | ||
})); | ||
|
||
Trailbucket.addToResourcePolicy(new iam.PolicyStatement({ | ||
resources: [Trailbucket.arnForObjects(`AWSLogs/${Stack.of(stack).account}/*`)], | ||
actions: ["s3:PutObject"], | ||
principals: [cloudTrailPrincipal], | ||
conditions: { | ||
StringEquals: {'s3:x-amz-acl': "bucket-owner-full-control"} | ||
} | ||
})); | ||
|
||
const trail = new cloudtrail.Trail(stack, 'Trail', {bucket: Trailbucket}); | ||
|
||
trail.addS3EventSelector([bucket.arnForObjects('')]); | ||
|
||
app.synth(); |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters