22
33const _ = require ( 'lodash' ) ;
44const Joi = require ( '@hapi/joi' ) ;
5- const Chance = require ( 'chance ' ) ;
5+ const crypto = require ( 'crypto ' ) ;
66const BbPromise = require ( 'bluebird' ) ;
77const schema = require ( './compileNotifications.schema' ) ;
88
9- const chance = new Chance ( ) ;
10-
119const executionStatuses = [
1210 'ABORTED' , 'FAILED' , 'RUNNING' , 'SUCCEEDED' , 'TIMED_OUT' ,
1311] ;
@@ -25,65 +23,72 @@ const targetPermissions = {
2523 stepFunctions : 'states:StartExecution' ,
2624} ;
2725
28- function randomTargetId ( stateMachineName , status ) {
29- const suffix = chance . string ( {
30- length : 5 ,
31- pool : 'abcdefghijklmnopqrstufwxyzABCDEFGHIJKLMNOPQRSTUFWXYZ1234567890' ,
32- } ) ;
26+ function generateTargetId ( target , index , stateMachineName , status ) {
27+ const suffix = crypto
28+ . createHash ( 'md5' )
29+ . update ( JSON . stringify ( { target, index } ) )
30+ . digest ( 'hex' )
31+ . substr ( 0 , 5 ) ;
3332
3433 return `${ stateMachineName } -${ status } -${ suffix } ` ;
3534}
3635
37- function randomLogicalId ( prefix ) {
38- const suffix = chance . string ( {
39- length : 5 ,
40- pool : 'ABCDEFGHIJKLMNOPQRSTUFWXYZ' ,
41- } ) ;
36+ function generateLogicalId ( prefix , index , resource ) {
37+ const suffix = crypto
38+ . createHash ( 'md5' )
39+ . update ( JSON . stringify ( { index, resource } ) )
40+ . digest ( 'hex' )
41+ . substr ( 0 , 5 ) ;
4242 return `${ prefix } ${ suffix } ` ;
4343}
4444
45- function randomPolicyName ( status , targetType ) {
46- const suffix = chance . string ( {
47- length : 5 ,
48- pool : 'abcdefghijklmnopqrstufwxyzABCDEFGHIJKLMNOPQRSTUFWXYZ' ,
49- } ) ;
45+ function generatePolicyName ( status , targetType , action , resource ) {
46+ const suffix = crypto
47+ . createHash ( 'md5' )
48+ . update ( JSON . stringify ( { action, resource } ) )
49+ . digest ( 'hex' )
50+ . substr ( 0 , 5 ) ;
5051 return `${ status } -${ targetType } -${ suffix } ` ;
5152}
5253
53- function compileTarget ( stateMachineName , status , targetObj , iamRoleLogicalId ) {
54+ function compileTarget ( stateMachineName , status , targetObj , targetIndex , iamRoleLogicalId ) {
5455 // SQS and Kinesis are special cases as they can have additional props
5556 if ( _ . has ( targetObj , 'sqs.arn' ) ) {
56- return {
57+ const target = {
5758 Arn : targetObj . sqs . arn ,
58- Id : randomTargetId ( stateMachineName , status ) ,
5959 SqsParameters : {
6060 MessageGroupId : targetObj . sqs . messageGroupId ,
6161 } ,
6262 } ;
63+ target . Id = generateTargetId ( target , targetIndex , stateMachineName , status ) ;
64+ return target ;
6365 } if ( _ . has ( targetObj , 'kinesis.arn' ) ) {
64- return {
66+ const target = {
6567 Arn : targetObj . kinesis . arn ,
66- Id : randomTargetId ( stateMachineName , status ) ,
6768 KinesisParameters : {
6869 PartitionKeyPath : targetObj . kinesis . partitionKeyPath ,
6970 } ,
7071 } ;
72+ target . Id = generateTargetId ( target , targetIndex , stateMachineName , status ) ;
73+ return target ;
7174 } if ( _ . has ( targetObj , 'stepFunctions' ) ) {
72- return {
75+ const target = {
7376 Arn : targetObj . stepFunctions ,
74- Id : randomTargetId ( stateMachineName , status ) ,
7577 RoleArn : {
7678 'Fn::GetAtt' : [ iamRoleLogicalId , 'Arn' ] ,
7779 } ,
7880 } ;
81+ target . Id = generateTargetId ( target , targetIndex , stateMachineName , status ) ;
82+ return target ;
7983 }
8084
8185 const targetType = supportedTargets . find ( t => _ . has ( targetObj , t ) ) ;
8286 const arn = _ . get ( targetObj , targetType ) ;
83- return {
87+ const target = {
8488 Arn : arn ,
85- Id : randomTargetId ( stateMachineName , status ) ,
8689 } ;
90+ target . Id = generateTargetId ( target , targetIndex , stateMachineName , status ) ;
91+ return target ;
8792}
8893
8994function compileSnsPolicy ( status , snsTarget ) {
@@ -93,7 +98,7 @@ function compileSnsPolicy(status, snsTarget) {
9398 PolicyDocument : {
9499 Version : '2012-10-17' ,
95100 Statement : {
96- Sid : randomPolicyName ( status , 'sns' ) ,
101+ Sid : generatePolicyName ( status , 'sns' , 'sns:Publish' , snsTarget ) ,
97102 Principal : {
98103 Service : 'events.amazonaws.com' ,
99104 } ,
@@ -135,7 +140,7 @@ function compileSqsPolicy(status, sqsTarget) {
135140 PolicyDocument : {
136141 Version : '2012-10-17' ,
137142 Statement : {
138- Sid : randomPolicyName ( status , 'sqs' ) ,
143+ Sid : generatePolicyName ( status , 'sqs' , 'sqs:SendMessage' , sqsTarget ) ,
139144 Principal : {
140145 Service : 'events.amazonaws.com' ,
141146 } ,
@@ -232,18 +237,19 @@ function bootstrapIamRole() {
232237function * compilePermissionResources ( stateMachineLogicalId , iamRoleLogicalId , targets ) {
233238 const { iamRole, addPolicy } = bootstrapIamRole ( ) ;
234239
235- for ( const { status, target } of targets ) {
240+ for ( let index = 0 ; index < targets . length ; index ++ ) {
241+ const { status, target } = targets [ index ] ;
236242 const perm = compilePermissionForTarget ( status , target ) ;
237243 if ( perm . type === 'iam' ) {
238244 const targetType = _ . keys ( target ) [ 0 ] ;
239245 addPolicy (
240- randomPolicyName ( status , targetType ) ,
246+ generatePolicyName ( status , targetType , perm . action , perm . resource ) ,
241247 perm . action ,
242248 perm . resource ,
243249 ) ;
244250 } else if ( perm . type === 'policy' ) {
245251 yield {
246- logicalId : randomLogicalId ( `${ stateMachineLogicalId } ResourcePolicy` ) ,
252+ logicalId : generateLogicalId ( `${ stateMachineLogicalId } ResourcePolicy` , index , perm . resource ) ,
247253 resource : perm . resource ,
248254 } ;
249255 }
@@ -277,8 +283,8 @@ function* compileResources(stateMachineLogicalId, stateMachineName, notification
277283 for ( const status of executionStatuses ) {
278284 const targets = notificationsObj [ status ] ;
279285 if ( ! _ . isEmpty ( targets ) ) {
280- const cfnTargets = targets . map ( t => compileTarget ( stateMachineName ,
281- status , t , iamRoleLogicalId ) ) ;
286+ const cfnTargets = targets . map ( ( t , index ) => compileTarget ( stateMachineName ,
287+ status , t , index , iamRoleLogicalId ) ) ;
282288
283289 const eventRuleLogicalId = `${ stateMachineLogicalId } Notifications${ status . replace ( '_' , '' ) } EventRule` ;
284290 const eventRule = {
0 commit comments