Skip to content

Commit

Permalink
feat: add origin access control type bucket policy
Browse files Browse the repository at this point in the history
  • Loading branch information
yicr committed Apr 5, 2024
1 parent d7ff17b commit 94982d8
Show file tree
Hide file tree
Showing 2 changed files with 60 additions and 13 deletions.
73 changes: 60 additions & 13 deletions src/index.ts
Original file line number Diff line number Diff line change
@@ -1,31 +1,78 @@
import { SecureBucket, SecureBucketEncryption } from '@gammarer/aws-secure-bucket';
import * as iam from 'aws-cdk-lib/aws-iam';
import * as s3 from 'aws-cdk-lib/aws-s3';
import { Construct } from 'constructs';

export interface SecureCloudFrontOriginBucketProps {
export enum SecureCloudFrontOriginType {
/**
* OriginAccessIdentity
*/
ORIGIN_ACCESS_IDENTITY,

/**
* OriginAccessControl
*/
ORIGIN_ACCESS_CONTROL,
}

interface BaseSecureCloudFrontOriginBucketProps {
readonly bucketName?: string;
}

export interface SecureCloudFrontOriginAccessControlBucketProps extends BaseSecureCloudFrontOriginBucketProps {
readonly cloudFrontOriginType: SecureCloudFrontOriginType.ORIGIN_ACCESS_CONTROL;
readonly cloudFrontArn: string;
}

export interface SecureCloudFrontOriginAccessIdentityBucketProps extends BaseSecureCloudFrontOriginBucketProps {
readonly cloudFrontOriginType: SecureCloudFrontOriginType.ORIGIN_ACCESS_IDENTITY;
readonly cloudFrontOriginAccessIdentityS3CanonicalUserId: string;
}

export class SecureCloudFrontOriginBucket extends SecureBucket {

constructor(scope: Construct, id: string, props: SecureCloudFrontOriginBucketProps) {
constructor(scope: Construct, id: string, props: SecureCloudFrontOriginAccessControlBucketProps | SecureCloudFrontOriginAccessIdentityBucketProps) {
super(scope, id, {
bucketName: props.bucketName,
encryption: SecureBucketEncryption.S3_MANAGED, // Notice)Only S3 Managed
versioned: false,
});

// 👇Allow CloudFront access
this.addToResourcePolicy(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject'],
principals: [
new iam.CanonicalUserPrincipal(
props.cloudFrontOriginAccessIdentityS3CanonicalUserId,
),
],
resources: [`${this.bucketArn}/*`],
}));
// 👇 add BucketPolicy
const bucketPolicy = new s3.BucketPolicy(scope, 'BucketPolicy', {
bucket: this,
});

switch (props.cloudFrontOriginType) {
case SecureCloudFrontOriginType.ORIGIN_ACCESS_CONTROL:
// 👇 add bucket policy statement for cloud front origin access identity
bucketPolicy.document.addStatements(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject'],
principals: [
new iam.ServicePrincipal('cloudfront.amazonaws.com'),
],
resources: [`${this.bucketArn}/*`],
conditions: {
StringEquals: {
'AWS:SourceArn': props.cloudFrontArn,
},
},
}));
break;
case SecureCloudFrontOriginType.ORIGIN_ACCESS_IDENTITY:
// 👇 add bucket policy statement for cloud front origin access identity
bucketPolicy.document.addStatements(new iam.PolicyStatement({
effect: iam.Effect.ALLOW,
actions: ['s3:GetObject'],
principals: [
new iam.CanonicalUserPrincipal(
props.cloudFrontOriginAccessIdentityS3CanonicalUserId,
),
],
resources: [`${this.bucketArn}/*`],
}));
break;
}
}
}
File renamed without changes.

0 comments on commit 94982d8

Please sign in to comment.