Skip to content

Commit 6f22446

Browse files
wwsnomergify[bot]
authored andcommitted
fix(s3): access denied when adding an event notification to a s3 bucket (#4219)
* fix(s3): fix potential access denied when adding an event notification to a s3 bucket Sometimes the custom resource finishes creating before the iam role of the lambda function is created, after which the custom resource directly executes the lambda function. This results in an access denied error. fixes #3318 * fix(s3): update expected results with dependencies on iam role and default policy * fix(s3): update expected results with dependencies on iam role and default policy for aws-lambda-event-sources package
1 parent f5daa6e commit 6f22446

File tree

7 files changed

+64
-14
lines changed

7 files changed

+64
-14
lines changed

packages/@aws-cdk/aws-lambda-event-sources/test/integ.s3.expected.json

Lines changed: 8 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -73,9 +73,9 @@
7373
}
7474
},
7575
"B08E7C7AF": {
76-
"DeletionPolicy": "Delete",
76+
"Type": "AWS::S3::Bucket",
7777
"UpdateReplacePolicy": "Delete",
78-
"Type": "AWS::S3::Bucket"
78+
"DeletionPolicy": "Delete"
7979
},
8080
"BNotificationsEB8DA980": {
8181
"Type": "Custom::S3BucketNotifications",
@@ -187,7 +187,11 @@
187187
},
188188
"Runtime": "nodejs8.10",
189189
"Timeout": 300
190-
}
190+
},
191+
"DependsOn": [
192+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
193+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC"
194+
]
191195
}
192196
}
193-
}
197+
}

packages/@aws-cdk/aws-s3-notifications/test/integ.notifications.expected.json

Lines changed: 10 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -1,9 +1,9 @@
11
{
22
"Resources": {
33
"Bucket83908E77": {
4-
"DeletionPolicy": "Delete",
4+
"Type": "AWS::S3::Bucket",
55
"UpdateReplacePolicy": "Delete",
6-
"Type": "AWS::S3::Bucket"
6+
"DeletionPolicy": "Delete"
77
},
88
"BucketNotifications8F2E257D": {
99
"Type": "Custom::S3BucketNotifications",
@@ -222,12 +222,16 @@
222222
},
223223
"Runtime": "nodejs8.10",
224224
"Timeout": 300
225-
}
225+
},
226+
"DependsOn": [
227+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
228+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC"
229+
]
226230
},
227231
"Bucket25524B414": {
228-
"DeletionPolicy": "Delete",
232+
"Type": "AWS::S3::Bucket",
229233
"UpdateReplacePolicy": "Delete",
230-
"Type": "AWS::S3::Bucket"
234+
"DeletionPolicy": "Delete"
231235
},
232236
"Bucket2NotificationsD9BA2A77": {
233237
"Type": "Custom::S3BucketNotifications",
@@ -274,4 +278,4 @@
274278
]
275279
}
276280
}
277-
}
281+
}

packages/@aws-cdk/aws-s3-notifications/test/lambda/integ.bucket-notifications.expected.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -246,7 +246,11 @@
246246
},
247247
"Runtime": "nodejs8.10",
248248
"Timeout": 300
249-
}
249+
},
250+
"DependsOn": [
251+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
252+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC"
253+
]
250254
}
251255
}
252256
}

packages/@aws-cdk/aws-s3-notifications/test/sns/integ.sns-bucket-notifications.expected.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -205,7 +205,11 @@
205205
},
206206
"Runtime": "nodejs8.10",
207207
"Timeout": 300
208-
}
208+
},
209+
"DependsOn": [
210+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
211+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC"
212+
]
209213
}
210214
}
211215
}

packages/@aws-cdk/aws-s3-notifications/test/sqs/integ.bucket-notifications.expected.json

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -192,7 +192,11 @@
192192
},
193193
"Runtime": "nodejs8.10",
194194
"Timeout": 300
195-
}
195+
},
196+
"DependsOn": [
197+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
198+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC"
199+
]
196200
},
197201
"Bucket25524B414": {
198202
"Type": "AWS::S3::Bucket",

packages/@aws-cdk/aws-s3/lib/notifications-resource/notifications-resource-handler.ts

Lines changed: 2 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -84,6 +84,8 @@ export class NotificationsResourceHandler extends cdk.Construct {
8484
}
8585
});
8686

87+
resource.node.addDependency(role);
88+
8789
this.functionArn = resource.getAtt('Arn').toString();
8890
}
8991
}

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

Lines changed: 29 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import { expect, haveResource } from '@aws-cdk/assert';
1+
import { expect, haveResource, haveResourceLike, ResourcePart } from '@aws-cdk/assert';
22
import cdk = require('@aws-cdk/core');
33
import { Test } from 'nodeunit';
44
import s3 = require('../lib');
@@ -64,6 +64,34 @@ export = {
6464
test.done();
6565
},
6666

67+
'the notification lambda handler must depend on the role to prevent executing too early'(test: Test) {
68+
const stack = new cdk.Stack();
69+
70+
const bucket = new s3.Bucket(stack, 'MyBucket');
71+
72+
bucket.addEventNotification(s3.EventType.OBJECT_CREATED, {
73+
bind: () => ({
74+
arn: 'ARN',
75+
type: s3.BucketNotificationDestinationType.TOPIC
76+
})
77+
});
78+
79+
expect(stack).to(haveResourceLike('AWS::Lambda::Function', {
80+
Type: "AWS::Lambda::Function",
81+
Properties: {
82+
Role: {
83+
"Fn::GetAtt": [
84+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC",
85+
"Arn"
86+
]
87+
},
88+
}, DependsOn: [ "BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleDefaultPolicy2CF63D36",
89+
"BucketNotificationsHandler050a0587b7544547bf325f094a3db834RoleB6FB88EC" ]
90+
}, ResourcePart.CompleteDefinition ) );
91+
92+
test.done();
93+
},
94+
6795
'throws with multiple prefix rules in a filter'(test: Test) {
6896
const stack = new cdk.Stack();
6997

0 commit comments

Comments
 (0)