Skip to content

Commit 370b905

Browse files
made2591rix0rrr
authored andcommitted
fix(elbv2): restrict ALB access logs bucket permissions to minimum (#2929)
1 parent 58a80ab commit 370b905

File tree

2 files changed

+18
-12
lines changed

2 files changed

+18
-12
lines changed

packages/@aws-cdk/aws-elasticloadbalancingv2/lib/alb/application-load-balancer.ts

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,7 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
5252
* Import an existing Application Load Balancer
5353
*/
5454
public static fromApplicationLoadBalancerAttributes(
55-
scope: Construct, id: string, attrs: ApplicationLoadBalancerAttributes): IApplicationLoadBalancer {
55+
scope: Construct, id: string, attrs: ApplicationLoadBalancerAttributes): IApplicationLoadBalancer {
5656

5757
return new ImportedApplicationLoadBalancer(scope, id, attrs);
5858
}
@@ -97,7 +97,7 @@ export class ApplicationLoadBalancer extends BaseLoadBalancer implements IApplic
9797
}
9898

9999
prefix = prefix || '';
100-
bucket.grantPut(new iam.AccountPrincipal(account), prefix + '*');
100+
bucket.grantPut(new iam.AccountPrincipal(account), `${(prefix ? prefix + "/" : "")}AWSLogs/${Stack.of(this).account}/*`);
101101

102102
// make sure the bucket's policy is created before the ALB (see https://github.com/awslabs/aws-cdk/issues/1633)
103103
this.node.addDependency(bucket);
@@ -519,7 +519,7 @@ export interface ApplicationLoadBalancerAttributes {
519519
}
520520

521521
// https://docs.aws.amazon.com/elasticloadbalancing/latest/application/load-balancer-access-logs.html#access-logging-bucket-permissions
522-
const ELBV2_ACCOUNTS: {[region: string]: string } = {
522+
const ELBV2_ACCOUNTS: { [region: string]: string } = {
523523
'us-east-1': '127311923021',
524524
'us-east-2': '033677994240',
525525
'us-west-1': '027434742980',

packages/@aws-cdk/aws-elasticloadbalancingv2/test/alb/test.load-balancer.ts

Lines changed: 15 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -110,7 +110,7 @@ export = {
110110

111111
'Access logging'(test: Test) {
112112
// GIVEN
113-
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1' }});
113+
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1' } });
114114
const vpc = new ec2.Vpc(stack, 'Stack');
115115
const bucket = new s3.Bucket(stack, 'AccessLoggingBucket');
116116
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
@@ -140,26 +140,29 @@ export = {
140140
Version: '2012-10-17',
141141
Statement: [
142142
{
143-
Action: [ "s3:PutObject*", "s3:Abort*" ],
143+
Action: ["s3:PutObject*", "s3:Abort*"],
144144
Effect: 'Allow',
145-
Principal: { AWS: { "Fn::Join": [ "", [ "arn:", { Ref: "AWS::Partition" }, ":iam::127311923021:root" ] ] } },
146-
Resource: { "Fn::Join": [ "", [ { "Fn::GetAtt": [ "AccessLoggingBucketA6D88F29", "Arn" ] }, "/*" ] ] }
145+
Principal: { AWS: { "Fn::Join": ["", ["arn:", { Ref: "AWS::Partition" }, ":iam::127311923021:root"]] } },
146+
Resource: {
147+
"Fn::Join": ["", [{ "Fn::GetAtt": ["AccessLoggingBucketA6D88F29", "Arn"] }, "/AWSLogs/",
148+
{ Ref: "AWS::AccountId" }, "/*"]]
149+
}
147150
}
148151
]
149152
}
150153
}));
151154

152155
// verify the ALB depends on the bucket *and* the bucket policy
153156
expect(stack).to(haveResource('AWS::ElasticLoadBalancingV2::LoadBalancer', {
154-
DependsOn: [ 'AccessLoggingBucketPolicy700D7CC6', 'AccessLoggingBucketA6D88F29' ]
157+
DependsOn: ['AccessLoggingBucketPolicy700D7CC6', 'AccessLoggingBucketA6D88F29']
155158
}, ResourcePart.CompleteDefinition));
156159

157160
test.done();
158161
},
159162

160163
'access logging with prefix'(test: Test) {
161164
// GIVEN
162-
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1' }});
165+
const stack = new cdk.Stack(undefined, undefined, { env: { region: 'us-east-1' } });
163166
const vpc = new ec2.Vpc(stack, 'Stack');
164167
const bucket = new s3.Bucket(stack, 'AccessLoggingBucket');
165168
const lb = new elbv2.ApplicationLoadBalancer(stack, 'LB', { vpc });
@@ -192,10 +195,13 @@ export = {
192195
Version: '2012-10-17',
193196
Statement: [
194197
{
195-
Action: [ "s3:PutObject*", "s3:Abort*" ],
198+
Action: ["s3:PutObject*", "s3:Abort*"],
196199
Effect: 'Allow',
197-
Principal: { AWS: { "Fn::Join": [ "", [ "arn:", { Ref: "AWS::Partition" }, ":iam::127311923021:root" ] ] } },
198-
Resource: { "Fn::Join": [ "", [ { "Fn::GetAtt": [ "AccessLoggingBucketA6D88F29", "Arn" ] }, "/prefix-of-access-logs*" ] ] }
200+
Principal: { AWS: { "Fn::Join": ["", ["arn:", { Ref: "AWS::Partition" }, ":iam::127311923021:root"]] } },
201+
Resource: {
202+
"Fn::Join": ["", [{ "Fn::GetAtt": ["AccessLoggingBucketA6D88F29", "Arn"] }, "/prefix-of-access-logs/AWSLogs/",
203+
{ Ref: "AWS::AccountId" }, "/*"]]
204+
}
199205
}
200206
]
201207
}

0 commit comments

Comments
 (0)