UNPKG

aws-cdk-lib

Version:

Version 2 of the AWS Cloud Development Kit library

2 lines (1 loc) 7.81 kB
"use strict";var __esDecorate=exports&&exports.__esDecorate||function(ctor,descriptorIn,decorators,contextIn,initializers,extraInitializers){function accept(f){if(f!==void 0&&typeof f!="function")throw new TypeError("Function expected");return f}for(var kind=contextIn.kind,key=kind==="getter"?"get":kind==="setter"?"set":"value",target=!descriptorIn&&ctor?contextIn.static?ctor:ctor.prototype:null,descriptor=descriptorIn||(target?Object.getOwnPropertyDescriptor(target,contextIn.name):{}),_,done=!1,i=decorators.length-1;i>=0;i--){var context={};for(var p in contextIn)context[p]=p==="access"?{}:contextIn[p];for(var p in contextIn.access)context.access[p]=contextIn.access[p];context.addInitializer=function(f){if(done)throw new TypeError("Cannot add initializers after decoration has completed");extraInitializers.push(accept(f||null))};var result=(0,decorators[i])(kind==="accessor"?{get:descriptor.get,set:descriptor.set}:descriptor[key],context);if(kind==="accessor"){if(result===void 0)continue;if(result===null||typeof result!="object")throw new TypeError("Object expected");(_=accept(result.get))&&(descriptor.get=_),(_=accept(result.set))&&(descriptor.set=_),(_=accept(result.init))&&initializers.unshift(_)}else(_=accept(result))&&(kind==="field"?initializers.unshift(_):descriptor[key]=_)}target&&Object.defineProperty(target,contextIn.name,descriptor),done=!0},__runInitializers=exports&&exports.__runInitializers||function(thisArg,initializers,value){for(var useValue=arguments.length>2,i=0;i<initializers.length;i++)value=useValue?initializers[i].call(thisArg,value):initializers[i].call(thisArg);return useValue?value:void 0};Object.defineProperty(exports,"__esModule",{value:!0}),exports.BucketNotifications=void 0;var constructs_1=()=>{var tmp=require("constructs");return constructs_1=()=>tmp,tmp},notifications_resource_handler_1=()=>{var tmp=require("./notifications-resource-handler");return notifications_resource_handler_1=()=>tmp,tmp},iam=()=>{var tmp=require("../../../aws-iam");return iam=()=>tmp,tmp},cdk=()=>{var tmp=require("../../../core");return cdk=()=>tmp,tmp},errors_1=()=>{var tmp=require("../../../core/lib/errors");return errors_1=()=>tmp,tmp},helpers_internal_1=()=>{var tmp=require("../../../core/lib/helpers-internal");return helpers_internal_1=()=>tmp,tmp},no_box_stack_traces_1=()=>{var tmp=require("../../../core/lib/no-box-stack-traces");return no_box_stack_traces_1=()=>tmp,tmp},literal_string_1=()=>{var tmp=require("../../../core/lib/private/literal-string");return literal_string_1=()=>tmp,tmp},cxapi=()=>{var tmp=require("../../../cx-api");return cxapi=()=>tmp,tmp},bucket_1=()=>{var tmp=require("../bucket");return bucket_1=()=>tmp,tmp},destination_1=()=>{var tmp=require("../destination");return destination_1=()=>tmp,tmp};let BucketNotifications=(()=>{let _classDecorators=[no_box_stack_traces_1().noBoxStackTraces],_classDescriptor,_classExtraInitializers=[],_classThis,_classSuper=constructs_1().Construct;var BucketNotifications2=class extends _classSuper{static{_classThis=this}static{const _metadata=typeof Symbol=="function"&&Symbol.metadata?Object.create(_classSuper[Symbol.metadata]??null):void 0;__esDecorate(null,_classDescriptor={value:_classThis},_classDecorators,{kind:"class",name:_classThis.name,metadata:_metadata},null,_classExtraInitializers),BucketNotifications2=_classThis=_classDescriptor.value,_metadata&&Object.defineProperty(_classThis,Symbol.metadata,{enumerable:!0,configurable:!0,writable:!0,value:_metadata}),__runInitializers(_classThis,_classExtraInitializers)}eventBridgeEnabled=helpers_internal_1().Box.fromValue(!1);lambdaNotifications=helpers_internal_1().Box.fromArray();queueNotifications=helpers_internal_1().Box.fromArray();topicNotifications=helpers_internal_1().Box.fromArray();resource;bucket;handlerRole;skipDestinationValidation;constructor(scope,id,props){super(scope,id),this.bucket=props.bucket,this.handlerRole=props.handlerRole,this.skipDestinationValidation=props.skipDestinationValidation}addNotification(event,target,...filters){const resource=this.createResourceOnce(),targetProps=target.bind(this,this.bucket),commonConfig={Events:[event],Filter:renderFilters(filters,this)};switch(targetProps.dependencies&&resource.node.addDependency(...targetProps.dependencies),targetProps.type){case destination_1().BucketNotificationDestinationType.LAMBDA:this.lambdaNotifications.push({...commonConfig,LambdaFunctionArn:targetProps.arn});break;case destination_1().BucketNotificationDestinationType.QUEUE:this.queueNotifications.push({...commonConfig,QueueArn:targetProps.arn});break;case destination_1().BucketNotificationDestinationType.TOPIC:this.topicNotifications.push({...commonConfig,TopicArn:targetProps.arn});break;default:throw new(errors_1()).ValidationError((0,literal_string_1().lit)`UnsupportedNotificationTargetType`,"Unsupported notification target type:"+destination_1().BucketNotificationDestinationType[targetProps.type],this)}}enableEventBridgeNotification(){this.createResourceOnce(),this.eventBridgeEnabled.set(!0)}renderNotificationConfiguration(){return helpers_internal_1().Box.combine({eventBridge:this.eventBridgeEnabled,lambda:this.lambdaNotifications,queue:this.queueNotifications,topic:this.topicNotifications},v=>({EventBridgeConfiguration:v.eventBridge?{}:void 0,LambdaFunctionConfigurations:v.lambda.length>0?v.lambda:void 0,QueueConfigurations:v.queue.length>0?v.queue:void 0,TopicConfigurations:v.topic.length>0?v.topic:void 0}))}createResourceOnce(){if(!this.resource){const handler=notifications_resource_handler_1().NotificationsResourceHandler.singleton(this,{role:this.handlerRole});let managed=this.bucket instanceof bucket_1().Bucket;cdk().FeatureFlags.of(this).isEnabled(cxapi().S3_KEEP_NOTIFICATION_IN_IMPORTED_BUCKET)&&(managed=!1);const handlerPolicy=this.addHandlerPermissions(handler,managed);this.resource=new(cdk()).CfnResource(this,"Resource",{type:"Custom::S3BucketNotifications",properties:{ServiceToken:handler.functionArn,BucketName:this.bucket.bucketName,NotificationConfiguration:this.renderNotificationConfiguration(),Managed:managed,SkipDestinationValidation:this.skipDestinationValidation}}),this.resource.node.addDependency(handlerPolicy);const bucket=this.bucket,resource=this.resource;cdk().Aspects.of(this).add({visit(node){node===resource&&bucket.policy&&node.node.addDependency(bucket.policy)}})}return this.resource}addHandlerPermissions(handler,managed){const statements=[new(iam()).PolicyStatement({actions:["s3:PutBucketNotification"],resources:[this.bucket.bucketArn]})];managed||statements.push(new(iam()).PolicyStatement({actions:["s3:GetBucketNotification"],resources:[this.bucket.bucketArn]}));const policy=new(iam()).Policy(this,"HandlerPolicy",{statements});return handler.role.attachInlinePolicy(policy),policy}};return BucketNotifications2=_classThis})();exports.BucketNotifications=BucketNotifications;function renderFilters(filters,scope){if(!filters||filters.length===0)return;const renderedRules=new Array;let hasPrefix=!1,hasSuffix=!1;for(const rule of filters){if(!rule.suffix&&!rule.prefix)throw new(errors_1()).ValidationError((0,literal_string_1().lit)`NotificationKeyFilterMustSpecifyPrefixOrSuffix`,"NotificationKeyFilter must specify `prefix` and/or `suffix`",scope);if(rule.suffix){if(hasSuffix)throw new(errors_1()).ValidationError((0,literal_string_1().lit)`CannotSpecifyMultipleSuffixRules`,"Cannot specify more than one suffix rule in a filter.",scope);renderedRules.push({Name:"suffix",Value:rule.suffix}),hasSuffix=!0}if(rule.prefix){if(hasPrefix)throw new(errors_1()).ValidationError((0,literal_string_1().lit)`CannotSpecifyMultiplePrefixRules`,"Cannot specify more than one prefix rule in a filter.",scope);renderedRules.push({Name:"prefix",Value:rule.prefix}),hasPrefix=!0}}return{Key:{FilterRules:renderedRules}}}