-
Notifications
You must be signed in to change notification settings - Fork 4k
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
chore(s3): use bucket policy instead of ACL for logging access control #20898
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -2,6 +2,7 @@ import { EOL } from 'os'; | |
import * as path from 'path'; | ||
import * as events from '@aws-cdk/aws-events'; | ||
import * as iam from '@aws-cdk/aws-iam'; | ||
import { PolicyStatement, ServicePrincipal } from '@aws-cdk/aws-iam'; | ||
import * as kms from '@aws-cdk/aws-kms'; | ||
import { | ||
Fn, IResource, Lazy, RemovalPolicy, Resource, ResourceProps, Stack, Token, | ||
|
@@ -1658,6 +1659,7 @@ export class Bucket extends BucketBase { | |
private readonly cors: CorsRule[] = []; | ||
private readonly inventories: Inventory[] = []; | ||
private readonly _resource: CfnBucket; | ||
private objectOwnership?: ObjectOwnership; | ||
|
||
constructor(scope: Construct, id: string, props: BucketProps = {}) { | ||
super(scope, id, { | ||
|
@@ -1685,7 +1687,7 @@ export class Bucket extends BucketBase { | |
accessControl: Lazy.string({ produce: () => this.accessControl }), | ||
loggingConfiguration: this.parseServerAccessLogs(props), | ||
inventoryConfigurations: Lazy.any({ produce: () => this.parseInventoryConfiguration() }), | ||
ownershipControls: this.parseOwnershipControls(props), | ||
ownershipControls: Lazy.any({ produce: () => this.parseOwnershipControls() }), | ||
accelerateConfiguration: props.transferAcceleration ? { accelerationStatus: 'Enabled' } : undefined, | ||
intelligentTieringConfigurations: this.parseTieringConfig(props), | ||
}); | ||
|
@@ -1713,14 +1715,15 @@ export class Bucket extends BucketBase { | |
|
||
this.disallowPublicAccess = props.blockPublicAccess && props.blockPublicAccess.blockPublicPolicy; | ||
this.accessControl = props.accessControl; | ||
this.objectOwnership = props.objectOwnership; | ||
|
||
// Enforce AWS Foundational Security Best Practice | ||
if (props.enforceSSL) { | ||
this.enforceSSLStatement(); | ||
} | ||
|
||
if (props.serverAccessLogsBucket instanceof Bucket) { | ||
props.serverAccessLogsBucket.allowLogDelivery(); | ||
props.serverAccessLogsBucket.allowLogDelivery(this, Stack.of(this).account, props.serverAccessLogsPrefix); | ||
} | ||
|
||
for (const inventory of props.inventories ?? []) { | ||
|
@@ -1990,13 +1993,13 @@ export class Bucket extends BucketBase { | |
})); | ||
} | ||
|
||
private parseOwnershipControls({ objectOwnership }: BucketProps): CfnBucket.OwnershipControlsProperty | undefined { | ||
if (!objectOwnership) { | ||
private parseOwnershipControls(): CfnBucket.OwnershipControlsProperty | undefined { | ||
if (!this.objectOwnership) { | ||
return undefined; | ||
} | ||
return { | ||
rules: [{ | ||
objectOwnership, | ||
objectOwnership: this.objectOwnership, | ||
}], | ||
}; | ||
} | ||
|
@@ -2069,17 +2072,28 @@ export class Bucket extends BucketBase { | |
} | ||
|
||
/** | ||
* Allows the LogDelivery group to write, fails if ACL was set differently. | ||
* Allows the 'logging.s3.amazonaws.com' service principal to write log events | ||
* to this bucket. | ||
* | ||
* @see | ||
* https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl | ||
* https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-server-access-logging.html | ||
*/ | ||
private allowLogDelivery() { | ||
if (this.accessControl && this.accessControl !== BucketAccessControl.LOG_DELIVERY_WRITE) { | ||
throw new Error("Cannot enable log delivery to this bucket because the bucket's ACL has been set and can't be changed"); | ||
} | ||
private allowLogDelivery(sourceBucket: IBucket, accountId: string, logFilePrefix?: string) { | ||
const prefix = logFilePrefix ?? ''; | ||
|
||
this.accessControl = BucketAccessControl.LOG_DELIVERY_WRITE; | ||
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. Does this need to be added back then? 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. Yes, I think so, which means that this change adds no value. Closing the PR until CloudFront (and possibly other services) catches up. |
||
this.addToResourcePolicy(new PolicyStatement({ | ||
principals: [new ServicePrincipal('logging.s3.amazonaws.com')], | ||
actions: ['s3:PutObject'], | ||
conditions: { | ||
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. Should this also include the condition:
https://docs.aws.amazon.com/AmazonS3/latest/userguide/enable-server-access-logging.html 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. Added.
I think so. Otherwise, the service principal wouldn't have permission to write the logs. 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 don't think this is being done today. 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. Not sure if I understand... today the 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 added a comment that tries to clarify. |
||
ArnLike: { | ||
'aws:SourceArn': sourceBucket.bucketArn, | ||
}, | ||
StringEquals: { | ||
'aws:SourceAccount': accountId, | ||
}, | ||
}, | ||
resources: [`${this.bucketArn}/${prefix}*`], | ||
})); | ||
} | ||
|
||
private parseInventoryConfiguration(): CfnBucket.InventoryConfigurationProperty[] | undefined { | ||
|
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -1 +1 @@ | ||
{"version":"17.0.0"} | ||
{"version":"20.0.0"} |
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.
This is where we setup the log delivery policy/acl, but it only gets done if the user passes in a bucket to
serverAccessLogsBucket
. If a user providesserverAccessLogsPrefix
but does not provideserverAccessLogsBucket
, then access logs are sent to the source bucket. Does it need to be something likeIt's not being done today so it could be a bug or it could be that you don't need to grant access to allow access logs to be written to the same bucket?