aws-cdk icon indicating copy to clipboard operation
aws-cdk copied to clipboard

[apigateway] add support for lambda token authorizer

Open brianfoody opened this issue 7 years ago • 42 comments

When I specify an authorizerType of CUSTOM for my Lambda integration it doesn't actually populate when deployed to AWS and I have to manually update.

This is the authorizer spec;

this.aggIdApi.addMethod("GET", new LambdaIntegration(getterFn), {
      authorizationType: AuthorizationType.Custom,
      authorizerId: authorizer.authorizerId
});

When deployed it only has an authorizerId screen shot 2018-12-19 at 11 10 34 pm

however when I manually update the authorizerType is populated and everything works perfectly; screen shot 2018-12-19 at 11 11 43 pm

brianfoody avatar Dec 19 '18 15:12 brianfoody

Looks like a bug

eladb avatar Dec 19 '18 17:12 eladb

Happy to do a PR if you can point me in the right direction @eladb

brianfoody avatar Dec 20 '18 05:12 brianfoody

The code the synthesizes the method resource is here. Not 100% sure why this is not propagating.

eladb avatar Dec 20 '18 09:12 eladb

Hi @eladb . Just revisiting this and I wasn't able to see any issue with the code there either. I noticed you removed the gap label. I've done a bit more digging and it looks like the authorizationType is getting populated now but I'm still getting an error. The only difference I can see is that the requestParameters and requestModels is being populated with an empty object in CDK deployment.

screen shot 2019-01-08 at 1 24 44 pm

brianfoody avatar Jan 08 '19 03:01 brianfoody

Looking into it

eladb avatar Jan 08 '19 08:01 eladb

The label was me, I must have misclicked in the UI. Sorry about that.

rix0rrr avatar Jan 08 '19 08:01 rix0rrr

@brianfoody I am trying to understand what the issue is exactly at this point. Any chance you can submit a more complete code snippet so we can try to reproduce?

eladb avatar Jan 08 '19 08:01 eladb

Hi @eladb . Yes sorry it's been a confusing one to hunt down. I turned on CloudWatch logs for my API Gateway method and I can see now that the actual error is that APIGateway doesn't have permission to invoke the authorizer. I'm guessing when I do it manually it adds the permissions as well.

I just need to figure out how to give the API Gateway method permission to call the authorizer now through CDK.

screen shot 2019-01-08 at 6 21 20 pm

brianfoody avatar Jan 08 '19 10:01 brianfoody

Probably ties into https://github.com/awslabs/aws-cdk/issues/1465 somewhat? Or am I wrong?

rix0rrr avatar Jan 08 '19 10:01 rix0rrr

Authorizers are simply not supported by our L2 library yet, so yes- manual permission grants are needed.

eladb avatar Jan 08 '19 10:01 eladb

You should be able to use https://awslabs.github.io/aws-cdk/refs/_aws-cdk_aws-lambda.html#@aws-cdk/aws-lambda.Function.grantInvoke

eladb avatar Jan 08 '19 10:01 eladb

Well 6 hours later I have resolved the issue. The grantInvoke didn't actually work for me. I had to create a custom script to attach the right permissions in the end as it required a weird /authorizers sourceARN string. This was the code that fixed the issue for me in the end and replicated what was happening when I manually attached an authorizer.

screen shot 2019-01-09 at 2 54 56 pm

I guess I should close for now if it's a known issue based on L2 library but it would be good if this could be documented somewhere.

brianfoody avatar Jan 09 '19 07:01 brianfoody

@brianfoody apologies for the unpleasant experience, and thanks for sharing your solution. I am reopening this issue and repurposing it as “support lambda authorizers”.

eladb avatar Jan 15 '19 08:01 eladb

I managed to get this working with the following code (along with cors support): https://github.com/erezrokah/cdk-serverless-example/blob/9627a92fab7186dcecd13346b17fd9f62f9ad36a/services/api-service/lib/apiService.ts#L106

The repository is still work in progress...

Edit: updated the link to a new location

erezrokah avatar Jan 15 '19 19:01 erezrokah

@erezrokah is this working for you without doing something else? Not working for me at the moment..

AlexRex avatar May 15 '19 13:05 AlexRex

@erezrokah is this working for you without doing something else? Not working for me at the moment..

Hey @AlexRex, I did get it to work (a few months ago...).

Have you tried cloning my repository https://github.com/erezrokah/cdk-serverless-example/ ? You'll need to create an .env file based on .env.example with the proper values.

Then use the readme for guidance (you'll need to follow this since I'm using Auth0 management API to automate some stuff)

erezrokah avatar May 15 '19 14:05 erezrokah

I was able to get Authorizers to work by making a custom class that extends CfnAuthorizer. I've made a quick example here https://github.com/IainCole/aws-cdk/blob/ic_add_apig_authorizer/packages/%40aws-cdk/aws-apigateway/lib/authorizer.ts#L17

@eladb would it be worth me submitting a PR along these lines?

IainCole avatar Aug 01 '19 23:08 IainCole

+1. Really needed... thank you.

andreimcristof avatar Oct 01 '19 15:10 andreimcristof

I want try to undestand why it doesn't work on python. So in #4532 you told me that it's incorrect use cfnxxx and non cfnxxx construct. I know, but It seems that all solutions model the cfnxxx.

so I tried to "translate" in python this -> https://github.com/IainCole/aws-cdk/blob/ic_add_apig_authorizer/packages/%40aws-cdk/aws-apigateway/lib/authorizer.ts#L17

class AuthorizerProps(CfnAuthorizerProps):
    pass
class Authorizer (CfnAuthorizer):
    @property
    def authorizer_id(self):
        return self.ref

    def __init__(self, scope: core.Construct, id: str, auth_props: AuthorizerProps, **kwargs) -> None:
        super().__init__(scope, id, 
        rest_api_id=auth_props.rest_api_id,
        type=auth_props.type,
        name=auth_props.name,
        identity_source=auth_props.identity_source,
        identity_validation_expression=auth_props.identity_validation_expression,
        authorizer_uri=auth_props.authorizer_uri, 
        **kwargs)

auth=Authorizer(self,"authorizer",auth_props=AuthorizerProps(
        rest_api_id=apigw_autoscaling.rest_api_id,
        type="TOKEN",
        name="test",
        identity_source="method.request.header.AUTH",
        identity_validation_expression="TESTAUTH",
        authorizer_uri="".join(["arn:aws:apigateway:eu-west-1:lambda:path/2015-03-31/functions/",lamdbda_api_authorizer.function_arn,"/invocations"])
        ))

 auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth.authorizer_id)

but I receive this error: jsii.errors.JSIIError: Expected object reference, got "${Token[TOKEN.89]}"

why?

please don't tell me "translate from this https://docs.aws.amazon.com/cdk/latest/guide/multiple_languages.html"

I've read the doc. How can I use the IAuthorizer? is it possible or a this time do I have to use CfnMethod with CfnAuthorizer?

gidimariastorm avatar Oct 16 '19 21:10 gidimariastorm

I don't really know python but I feel like it should look like this:

 auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth)

Rather than:

 auto_scaling_res.add_method('POST', authorization_type=apigw.AuthorizationType.CUSTOM, authorizer=auth.authorizer_id)

IainCole avatar Oct 16 '19 21:10 IainCole