-
Notifications
You must be signed in to change notification settings - Fork 4k
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
fix(apigateway): authorizer is not attached to RestApi across projects
When an Authorizer is defined in one node project, imported into another where it is wired up with a RestApi, synthesis throws the error 'Authorizer must be attached to a RestApi'. The root cause of this error is the `instanceof` check. If a user has their project set up in a way that more than one instance of the `@aws-cdk/aws-apigateway` module is present in their `node_modules/` folder, even if they are of the exact same version, type checking is bound to fail. Switching instead to a symbol property based comparison, so that type information survives across installations of the module. fixes #7377
- Loading branch information
Niranjan Jayakar
committed
Apr 24, 2020
1 parent
fda05c3
commit 5d83a33
Showing
4 changed files
with
143 additions
and
8 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,55 @@ | ||
import { Code, Function, Runtime } from '@aws-cdk/aws-lambda'; | ||
import { Stack } from '@aws-cdk/core'; | ||
import { Test } from 'nodeunit'; | ||
import { Authorizer, IdentitySource, RequestAuthorizer, RestApi, TokenAuthorizer } from '../lib'; | ||
|
||
export = { | ||
'isAuthorizer correctly detects an instance of type Authorizer'(test: Test) { | ||
class MyAuthorizer extends Authorizer { | ||
public readonly authorizerId = 'test-authorizer-id'; | ||
public _attachToApi(_: RestApi): void { | ||
// do nothing | ||
} | ||
} | ||
const stack = new Stack(); | ||
const authorizer = new MyAuthorizer(stack, 'authorizer'); | ||
|
||
test.ok(Authorizer.isAuthorizer(authorizer), 'type Authorizer expected but is not'); | ||
test.ok(!Authorizer.isAuthorizer(stack), 'type Authorizer found, when not expected'); | ||
|
||
test.done(); | ||
}, | ||
|
||
'token authorizer is of type Authorizer'(test: Test) { | ||
const stack = new Stack(); | ||
|
||
const handler = new Function(stack, 'token', { | ||
code: Code.fromInline('foo'), | ||
runtime: Runtime.NODEJS_12_X, | ||
handler: 'index.handler', | ||
}); | ||
const authorizer = new TokenAuthorizer(stack, 'authorizer', { handler }); | ||
|
||
test.ok(Authorizer.isAuthorizer(authorizer), 'TokenAuthorizer is not of type Authorizer'); | ||
|
||
test.done(); | ||
}, | ||
|
||
'request authorizer is of type Authorizer'(test: Test) { | ||
const stack = new Stack(); | ||
|
||
const handler = new Function(stack, 'token', { | ||
code: Code.fromInline('foo'), | ||
runtime: Runtime.NODEJS_12_X, | ||
handler: 'index.handler', | ||
}); | ||
const authorizer = new RequestAuthorizer(stack, 'authorizer', { | ||
handler, | ||
identitySources: [ IdentitySource.header('my-header') ], | ||
}); | ||
|
||
test.ok(Authorizer.isAuthorizer(authorizer), 'RequestAuthorizer is not of type Authorizer'); | ||
|
||
test.done(); | ||
}, | ||
}; |