Skip to content

websocket event authorizer now supports name and arn fields together#8335

Merged
medikoo merged 5 commits into
serverless:masterfrom
rzaldana:master
Oct 12, 2020
Merged

websocket event authorizer now supports name and arn fields together#8335
medikoo merged 5 commits into
serverless:masterfrom
rzaldana:master

Conversation

@rzaldana

@rzaldana rzaldana commented Oct 2, 2020

Copy link
Copy Markdown
Contributor

Closes: #8328

@codecov-commenter

codecov-commenter commented Oct 2, 2020

Copy link
Copy Markdown

Codecov Report

Merging #8335 into master will increase coverage by 0.00%.
The diff coverage is 100.00%.

Impacted file tree graph

@@           Coverage Diff           @@
##           master    #8335   +/-   ##
=======================================
  Coverage   88.01%   88.02%           
=======================================
  Files         249      249           
  Lines        9306     9308    +2     
=======================================
+ Hits         8191     8193    +2     
  Misses       1115     1115           
Impacted Files Coverage Δ
...ins/aws/package/compile/events/websockets/index.js 100.00% <ø> (ø)
.../package/compile/events/websockets/lib/validate.js 97.36% <100.00%> (+0.14%) ⬆️

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc34140...423269d. Read the comment docs.

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zaldanaraul thank you! That looks very good. I have just remark concerning tests. We now stay away from old way of writing tests (as they're very problematic for eventual refactors), see my comment

});

it('should add authorizer config when authorizer is specified as an object with both arn and name', () => {
awsCompileWebsocketsEvents.serverless.service.functions = {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

hi @medikoo I just did this. Please take a look to see if its appropriate

@rzaldana
rzaldana requested a review from medikoo October 9, 2020 00:29

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zaldanaraul ! That looks great. I've spotted just one issue in configured test

const nameField = 'authName';
const arnName = 'auth';

runServerless({

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use async/await (note also that here promise as being not returned is orphaned)

@rzaldana rzaldana Oct 9, 2020

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@medikoo just changed to use async/await. This explains some issues I was having earlier. Thanks for pointing out!

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zaldanaraul as I look they return the promise (note no brackets wrapping arrow function body).

There's no async/await as they were written before we could use it before breaking things. While it be nice to have everything migrated to async/await, it's not easily doable, do we do it on demand

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

you're right. Just noticed the missing brackets. My bad!

@codecov-io

codecov-io commented Oct 9, 2020

Copy link
Copy Markdown

Codecov Report

Merging #8335 into master will increase coverage by 0.02%.
The diff coverage is 85.71%.

Impacted file tree graph

@@            Coverage Diff             @@
##           master    #8335      +/-   ##
==========================================
+ Coverage   88.01%   88.04%   +0.02%     
==========================================
  Files         249      249              
  Lines        9306     9287      -19     
==========================================
- Hits         8191     8177      -14     
+ Misses       1115     1110       -5     
Impacted Files Coverage Δ
...ins/aws/package/compile/events/websockets/index.js 100.00% <ø> (ø)
.../package/compile/events/websockets/lib/validate.js 97.56% <83.33%> (+0.33%) ⬆️
...ckage/compile/events/websockets/lib/permissions.js 100.00% <100.00%> (+5.88%) ⬆️
...ws/package/compile/events/cloudWatchEvent/index.js 97.67% <0.00%> (-2.33%) ⬇️
...ns/aws/package/compile/events/eventBridge/index.js 98.30% <0.00%> (-1.70%) ⬇️
.../package/compile/events/apiGateway/lib/validate.js 96.23% <0.00%> (-0.88%) ⬇️
lib/plugins/aws/deployFunction/index.js 89.56% <0.00%> (-0.71%) ⬇️
lib/classes/Variables.js 99.72% <0.00%> (-0.01%) ⬇️
lib/plugins/aws/lib/monitorStack.js 100.00% <0.00%> (ø)
lib/plugins/aws/package/compile/events/s3/index.js 100.00% <0.00%> (ø)
... and 17 more

Continue to review full report at Codecov.

Legend - Click here to learn more
Δ = absolute <relative> (impact), ø = not affected, ? = missing data
Powered by Codecov. Last update fc34140...027d24f. Read the comment docs.

@rzaldana
rzaldana requested a review from medikoo October 9, 2020 14:52

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Great thanks for update!

Sorry for late notice, but I've just realized it needs a bit of additional work, to be a complete fix for #8328. Please see my comments

@@ -72,7 +72,7 @@ class AwsCompileWebsockets {
arn: { $ref: '#/definitions/awsArnString' },

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To fully fix #8328 we need to change here awsArnString to awsArn

route: '$connect',
authorizer: {
name: nameField,
arn: `arn:aws:${arnName}`,

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's use object form of ARN for this test

Comment thread lib/plugins/aws/package/compile/events/websockets/lib/validate.js
websocketObj.authorizer.name = getAuthorizerNameFromArn(
event.websocket.authorizer.arn
);
if (event.websocket.authorizer.name) {

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

To not be breaking, we actually need to consturct this resolution as:

if (_.isObject(event.websocket.authorizer.arn)) {
  if (!event.websocket.authorizer.name) throw new ServerlessError(....)
  websocketObj.authorizer.name = event.websocket.authorizer.name;
} else {
  websocketObj.authorizer.name = getAuthorizerNameFromArn(...)
}

@rzaldana
rzaldana marked this pull request as draft October 11, 2020 14:25
@rzaldana
rzaldana marked this pull request as ready for review October 11, 2020 16:00
@rzaldana

rzaldana commented Oct 11, 2020

Copy link
Copy Markdown
Contributor Author

Hi @medikoo , Just implemented suggested changes. Also had to change a line in permissions.js . See below:

Old:

if (event.authorizer.permission.includes(':')) {

New:

if (_.isObject(event.authorizer.permission) || event.authorizer.permission.includes(':')) {

Please let me know if it needs further changes!

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@zaldanaraul that looks great! I have just one last very minor remark and we're good to go

if (_.isObject(event.websocket.authorizer.arn)) {
if (!event.websocket.authorizer.name) {
throw new ServerlessError(
'Websocket Authorizer: Non-string arn needs to be accompanied with "name"'

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's put arn in quotes

Copy link
Copy Markdown
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

done!

@medikoo medikoo left a comment

Copy link
Copy Markdown
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Thank you @zaldanaraul !

@medikoo
medikoo merged commit 9303d8e into serverless:master Oct 12, 2020
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment

Projects

None yet

Development

Successfully merging this pull request may close these issues.

Fn::ImportValue for authorizer arn in websockets doesn't work like HTTP

4 participants