-
Notifications
You must be signed in to change notification settings - Fork 4.1k
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
fix(elbv2): missing permission to write NLB access logs to S3 bucket #8114
Merged
mergify
merged 8 commits into
aws:master
from
DaWyz:DaWyz/nlb-access-logs-permissions-issue
Jun 9, 2020
Merged
Changes from 5 commits
Commits
Show all changes
8 commits
Select commit
Hold shift + click to select a range
30214d7
fix(elbv2): fix access log bucket permissions
DaWyz fb90490
Merge branch 'master' into DaWyz/nlb-access-logs-permissions-issue
DaWyz 7a10a52
improve iam rule arn generation for s3 bucket
DaWyz 04ae03a
Merge branch 'master' into DaWyz/nlb-access-logs-permissions-issue
DaWyz 8af9fc3
Merge branch 'master' into DaWyz/nlb-access-logs-permissions-issue
DaWyz df334cd
improve code
DaWyz 8cbd19b
Merge branch 'master' into DaWyz/nlb-access-logs-permissions-issue
DaWyz 57ae2f5
Merge branch 'master' into DaWyz/nlb-access-logs-permissions-issue
mergify[bot] File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1,6 +1,8 @@ | ||
import * as cloudwatch from '@aws-cdk/aws-cloudwatch'; | ||
import * as ec2 from '@aws-cdk/aws-ec2'; | ||
import { Construct, Resource } from '@aws-cdk/core'; | ||
import { PolicyStatement, ServicePrincipal } from '@aws-cdk/aws-iam'; | ||
import { IBucket } from '@aws-cdk/aws-s3'; | ||
import { Construct, Resource, Stack } from '@aws-cdk/core'; | ||
import { BaseLoadBalancer, BaseLoadBalancerProps, ILoadBalancerV2 } from '../shared/base-load-balancer'; | ||
import { BaseNetworkListenerProps, NetworkListener } from './network-listener'; | ||
|
||
|
@@ -101,6 +103,39 @@ export class NetworkLoadBalancer extends BaseLoadBalancer implements INetworkLoa | |
}); | ||
} | ||
|
||
/** | ||
* Enable access logging for this load balancer. | ||
* | ||
* A region must be specified on the stack containing the load balancer; you cannot enable logging on | ||
* environment-agnostic stacks. See https://docs.aws.amazon.com/cdk/latest/guide/environments.html | ||
* | ||
* This is extending the BaseLoadBalancer.logAccessLogs method to match the bucket permissions described | ||
* at https://docs.aws.amazon.com/elasticloadbalancing/latest/network/load-balancer-access-logs.html#access-logging-bucket-requirements | ||
*/ | ||
public logAccessLogs(bucket: IBucket, prefix?: string) { | ||
super.logAccessLogs(bucket, prefix); | ||
|
||
bucket.addToResourcePolicy( | ||
new PolicyStatement({ | ||
actions: ['s3:PutObject'], | ||
principals: [new ServicePrincipal('delivery.logs.amazonaws.com')], | ||
resources: [ | ||
bucket.arnForObjects(`${prefix ? prefix + '/' : ''}AWSLogs/${Stack.of(this).account}/*`), | ||
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think you should be able to do There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. It does work. Updated the code. |
||
], | ||
conditions: { | ||
StringEquals: { 's3:x-amz-acl': 'bucket-owner-full-control' }, | ||
}, | ||
}), | ||
); | ||
bucket.addToResourcePolicy( | ||
new PolicyStatement({ | ||
actions: ['s3:GetBucketAcl'], | ||
principals: [new ServicePrincipal('delivery.logs.amazonaws.com')], | ||
resources: [bucket.bucketArn], | ||
}), | ||
); | ||
} | ||
|
||
/** | ||
* Return the given named metric for this Network Load Balancer | ||
* | ||
|
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
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Assign the principal to a variable and reuse in both places
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Updated the code.