Skip to content

Commit

Permalink
removed bucket policy auto-creation
Browse files Browse the repository at this point in the history
  • Loading branch information
lpizzinidev committed Nov 14, 2023
1 parent 496ddfd commit 24c1105
Show file tree
Hide file tree
Showing 5 changed files with 19 additions and 14 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -396,7 +396,7 @@
"UpdateReplacePolicy": "Retain",
"DeletionPolicy": "Retain"
},
"ImportedBucketPolicy71C80354": {
"ImportedBucketPolicyAE50CA2C": {
"Type": "AWS::S3::BucketPolicy",
"Properties": {
"Bucket": {
Expand Down Expand Up @@ -521,7 +521,7 @@
"Type": "application"
},
"DependsOn": [
"ImportedBucketPolicy71C80354",
"ImportedBucketPolicyAE50CA2C",
"VPCPublicSubnet1DefaultRoute91CEF279",
"VPCPublicSubnet1RouteTableAssociation0B0896DC",
"VPCPublicSubnet2DefaultRouteB7481BBA",
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,10 @@ const vpc = new ec2.Vpc(stack, 'VPC', {

const bucket = new s3.Bucket(stack, 'Bucket');
const importedBucket = s3.Bucket.fromBucketName(stack, 'ImportedBucket', bucket.bucketName);
// Imported buckets have `autoCreatePolicy` disabled by default
importedBucket.policy = new s3.BucketPolicy(stack, 'ImportedBucketPolicy', {
bucket: importedBucket,
});

const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', {
vpc,
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -252,21 +252,14 @@ export abstract class BaseLoadBalancer extends Resource {
this.setAttribute('access_logs.s3.bucket', bucket.bucketName.toString());
this.setAttribute('access_logs.s3.prefix', prefix);

const putObjectStatement = new PolicyStatement({
const logsDeliveryServicePrincipal = new ServicePrincipal('delivery.logs.amazonaws.com');
bucket.addToResourcePolicy(new PolicyStatement({
actions: ['s3:PutObject'],
principals: [this.resourcePolicyPrincipal()],
resources: [
bucket.arnForObjects(`${prefix ? prefix + '/' : ''}AWSLogs/${Stack.of(this).account}/*`),
],
});
bucket.addToResourcePolicy(putObjectStatement);
if (!bucket.policy) {
// Imported buckets have `autoCreatePolicy` disabled
bucket.policy = new s3.BucketPolicy(bucket, 'Policy', { bucket });
bucket.addToResourcePolicy(putObjectStatement);
}

const logsDeliveryServicePrincipal = new ServicePrincipal('delivery.logs.amazonaws.com');
}));
bucket.addToResourcePolicy(
new PolicyStatement({
actions: ['s3:PutObject'],
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -408,6 +408,10 @@ describe('tests', () => {
const { stack, lb } = loggingSetup();

const bucket = s3.Bucket.fromBucketName(stack, 'ImportedAccessLoggingBucket', 'imported-bucket');
// Imported buckets have `autoCreatePolicy` disabled by default
bucket.policy = new s3.BucketPolicy(stack, 'ImportedAccessLoggingBucketPolicy', {
bucket,
});

// WHEN
lb.logAccessLogs(bucket);
Expand Down Expand Up @@ -492,7 +496,7 @@ describe('tests', () => {

// verify the ALB depends on the bucket policy
Template.fromStack(stack).hasResource('AWS::ElasticLoadBalancingV2::LoadBalancer', {
DependsOn: ['ImportedAccessLoggingBucketPolicy832A536F'],
DependsOn: ['ImportedAccessLoggingBucketPolicy97AE3371'],
});
});

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -239,6 +239,10 @@ describe('tests', () => {
const stack = new cdk.Stack(app, undefined, { env: { region: 'us-east-1' } });
const vpc = new ec2.Vpc(stack, 'Stack');
const bucket = s3.Bucket.fromBucketName(stack, 'ImportedAccessLoggingBucket', 'imported-bucket');
// Imported buckets have `autoCreatePolicy` disabled by default
bucket.policy = new s3.BucketPolicy(stack, 'ImportedAccessLoggingBucketPolicy', {
bucket,
});
const lb = new elbv2.NetworkLoadBalancer(stack, 'LB', { vpc });

// WHEN
Expand Down Expand Up @@ -325,7 +329,7 @@ describe('tests', () => {

// verify the NLB depends on the bucket policy
Template.fromStack(stack).hasResource('AWS::ElasticLoadBalancingV2::LoadBalancer', {
DependsOn: ['ImportedAccessLoggingBucketPolicy832A536F'],
DependsOn: ['ImportedAccessLoggingBucketPolicy97AE3371'],
});
});

Expand Down

0 comments on commit 24c1105

Please sign in to comment.