Skip to content

Commit 820575b

Browse files
Jimmy GaussenElad Ben-Israel
authored andcommitted
feat(s3): bucket access control (#3391)
Implements missing Bucket [AccessControl](https://docs.aws.amazon.com/AWSCloudFormation/latest/UserGuide/aws-properties-s3-bucket.html#cfn-s3-bucket-accesscontrol) property The documentation was for the canned ACLs was retrieved from [the developer guide](https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html#canned-acl). Fixes #3383
1 parent dd574cc commit 820575b

File tree

2 files changed

+81
-1
lines changed

2 files changed

+81
-1
lines changed

packages/@aws-cdk/aws-s3/lib/bucket.ts

Lines changed: 60 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -796,6 +796,13 @@ export interface BucketProps {
796796
*/
797797
readonly websiteRedirect?: RedirectTarget;
798798

799+
/**
800+
* Specifies a canned ACL that grants predefined permissions to the bucket.
801+
*
802+
* @default BucketAccessControl.PRIVATE
803+
*/
804+
readonly accessControl?: BucketAccessControl;
805+
799806
/**
800807
* Grants public read access to all objects in the bucket.
801808
* Similar to calling `bucket.grantPublicAccess()`
@@ -933,7 +940,8 @@ export class Bucket extends BucketBase {
933940
websiteConfiguration: this.renderWebsiteConfiguration(props),
934941
publicAccessBlockConfiguration: props.blockPublicAccess,
935942
metricsConfigurations: Lazy.anyValue({ produce: () => this.parseMetricConfiguration() }),
936-
corsConfiguration: Lazy.anyValue({ produce: () => this.parseCorsConfiguration() })
943+
corsConfiguration: Lazy.anyValue({ produce: () => this.parseCorsConfiguration() }),
944+
accessControl: props.accessControl,
937945
});
938946

939947
resource.applyRemovalPolicy(props.removalPolicy);
@@ -1426,6 +1434,57 @@ export interface OnCloudTrailBucketEventOptions extends events.OnEventOptions {
14261434
readonly paths?: string[];
14271435
}
14281436

1437+
/**
1438+
* Default bucket access control types.
1439+
*
1440+
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/acl-overview.html
1441+
*/
1442+
export enum BucketAccessControl {
1443+
/**
1444+
* Owner gets FULL_CONTROL. No one else has access rights.
1445+
*/
1446+
PRIVATE = 'Private',
1447+
1448+
/**
1449+
* Owner gets FULL_CONTROL. The AllUsers group gets READ access.
1450+
*/
1451+
PUBLIC_READ = 'PublicRead',
1452+
1453+
/**
1454+
* Owner gets FULL_CONTROL. The AllUsers group gets READ and WRITE access.
1455+
* Granting this on a bucket is generally not recommended.
1456+
*/
1457+
PUBLIC_READ_WRITE = 'PublicReadWrite',
1458+
1459+
/**
1460+
* Owner gets FULL_CONTROL. The AuthenticatedUsers group gets READ access.
1461+
*/
1462+
AUTHENTICATED_READ = 'AuthenticatedRead',
1463+
1464+
/**
1465+
* The LogDelivery group gets WRITE and READ_ACP permissions on the bucket.
1466+
* @see https://docs.aws.amazon.com/AmazonS3/latest/dev/ServerLogs.html
1467+
*/
1468+
LOG_DELIVERY_WRITE = 'LogDeliveryWrite',
1469+
1470+
/**
1471+
* Object owner gets FULL_CONTROL. Bucket owner gets READ access.
1472+
* If you specify this canned ACL when creating a bucket, Amazon S3 ignores it.
1473+
*/
1474+
BUCKET_OWNER_READ = 'BucketOwnerRead',
1475+
1476+
/**
1477+
* Both the object owner and the bucket owner get FULL_CONTROL over the object.
1478+
* If you specify this canned ACL when creating a bucket, Amazon S3 ignores it.
1479+
*/
1480+
BUCKET_OWNER_FULL_CONTROL = 'BucketOwnerFullControl',
1481+
1482+
/**
1483+
* Owner gets FULL_CONTROL. Amazon EC2 gets READ access to GET an Amazon Machine Image (AMI) bundle from Amazon S3.
1484+
*/
1485+
AWS_EXEC_READ = 'AwsExecRead',
1486+
}
1487+
14291488
function mapOrUndefined<T, U>(list: T[] | undefined, callback: (element: T) => U): U[] | undefined {
14301489
if (!list || list.length === 0) {
14311490
return undefined;

packages/@aws-cdk/aws-s3/test/test.bucket.ts

Lines changed: 21 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -432,6 +432,27 @@ export = {
432432
test.done();
433433
},
434434

435+
'bucket with custom canned access control'(test: Test) {
436+
const stack = new cdk.Stack();
437+
new s3.Bucket(stack, 'MyBucket', {
438+
accessControl: s3.BucketAccessControl.LOG_DELIVERY_WRITE
439+
});
440+
441+
expect(stack).toMatch({
442+
"Resources": {
443+
"MyBucketF68F3FF0": {
444+
"Type": "AWS::S3::Bucket",
445+
"Properties": {
446+
"AccessControl": "LogDeliveryWrite"
447+
},
448+
"DeletionPolicy": "Retain",
449+
"UpdateReplacePolicy": "Retain",
450+
}
451+
}
452+
});
453+
test.done();
454+
},
455+
435456
'permissions': {
436457

437458
'addPermission creates a bucket policy'(test: Test) {

0 commit comments

Comments
 (0)