@@ -8,11 +8,19 @@ import {
88 CfnBucket ,
99 CfnBucketPolicy
1010} from "aws-cdk-lib/aws-s3"
11- import { Key } from "aws-cdk-lib/aws-kms"
11+ import { CfnKey , Key } from "aws-cdk-lib/aws-kms"
12+ import {
13+ AccountRootPrincipal ,
14+ Effect ,
15+ IPrincipal ,
16+ PolicyDocument ,
17+ PolicyStatement
18+ } from "aws-cdk-lib/aws-iam"
1219
1320export interface S3BucketProps {
1421 readonly bucketName : string
1522 readonly versioned : boolean
23+ readonly deploymentRole : IPrincipal
1624}
1725
1826export class S3Bucket extends Construct {
@@ -40,6 +48,53 @@ export class S3Bucket extends Construct {
4048 objectOwnership : ObjectOwnership . BUCKET_OWNER_ENFORCED
4149 } )
4250
51+ // Adding full deployment roles to this bucket
52+ const deploymentPolicy = new PolicyStatement ( {
53+ effect : Effect . ALLOW ,
54+ principals : [ props . deploymentRole ] ,
55+ actions : [
56+ "s3:Abort*" ,
57+ "s3:GetBucket*" ,
58+ "s3:GetObject*" ,
59+ "s3:List*" ,
60+ "s3:PutObject" ,
61+ "s3:PutObjectLegalHold" ,
62+ "s3:PutObjectRetention" ,
63+ "s3:PutObjectTagging" ,
64+ "s3:PutObjectVersionTagging"
65+ ] ,
66+ resources : [
67+ bucket . bucketArn ,
68+ bucket . arnForObjects ( "*" )
69+ ]
70+ } )
71+
72+ const accountRootPrincipal = new AccountRootPrincipal ( )
73+ const kmsPolicy = new PolicyDocument ( {
74+ statements : [
75+ new PolicyStatement ( {
76+ effect : Effect . ALLOW ,
77+ principals : [ accountRootPrincipal ] ,
78+ actions : [ "kms:*" ] ,
79+ resources : [ "*" ]
80+ } ) ,
81+ new PolicyStatement ( {
82+ effect : Effect . ALLOW ,
83+ principals : [ props . deploymentRole ] ,
84+ actions : [
85+ "kms:Encrypt" ,
86+ "kms:GenerateDataKey*"
87+ ] ,
88+ resources :[ "*" ]
89+ } )
90+ ]
91+ } )
92+
93+ bucket . addToResourcePolicy ( deploymentPolicy )
94+
95+ const contentBucketKmsKey = ( kmsKey . node . defaultChild as CfnKey )
96+ contentBucketKmsKey . keyPolicy = kmsPolicy . toJSON ( )
97+
4398 const cfnBucket = bucket . node . defaultChild as CfnBucket
4499 cfnBucket . cfnOptions . metadata = {
45100 ...cfnBucket . cfnOptions . metadata ,
0 commit comments