Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

(aws-apigateway): incorrect cross-region/partition LambdaAuthorizer references #18443

Closed
aisamu opened this issue Jan 14, 2022 · 2 comments · Fixed by #18444
Closed

(aws-apigateway): incorrect cross-region/partition LambdaAuthorizer references #18443

aisamu opened this issue Jan 14, 2022 · 2 comments · Fixed by #18444
Assignees
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. p2

Comments

@aisamu
Copy link

aisamu commented Jan 14, 2022

What is the problem?

The existing ARN reference mechanism used by the TokenAuthorizer only allows pointing to lambdas on the same account.

Both the partition and the region are being incorrectly inferred from the stack where the reference itself was defined instead from what it refers to.

More concretely:

const stack = new cdk.Stack(app, "TestStack", {
  env: { region: "REGION-A" },
});

const fn = lambda.Function.fromFunctionArn(
  stack,
  "AnExternalFunction",
  "arn:aws:lambda:REGION-B:someaccount:function:name"
);

cdk.Stack.of(fn).region refers to REGION-A, not the desired/expected REGION-B.

Reproduction Steps

Given the following Stack

Stack

import * as cdk from "@aws-cdk/core";
import * as lambda from "@aws-cdk/aws-lambda";
import * as apigateway from "@aws-cdk/aws-apigateway";

const app = new cdk.App();

const stack = new cdk.Stack(app, "Example", {
  env: { region: "us-east-2" },
});

const authorizerFunctionArn = cdk.Arn.format(
  {
    service: "lambda",
    resource: "function",
    resourceName: "example-authorizer",
    arnFormat: cdk.ArnFormat.COLON_RESOURCE_NAME,
    region: "us-east-1",
  },
  stack
);

const authorizerFunction = lambda.Function.fromFunctionArn(
  stack,
  "AuthorizerFunction",
  authorizerFunctionArn
);

const api = new apigateway.RestApi(stack, "API", {
  cloudWatchRole: false,
  endpointTypes: [apigateway.EndpointType.REGIONAL],
});

const authorizer = new apigateway.TokenAuthorizer(stack, "Authorizer", {
  handler: authorizerFunction,
});

api.root.addResource("test").addMethod(
  "GET",
  new apigateway.MockIntegration({
    integrationResponses: [
      {
        statusCode: "200",
        responseTemplates: {
          "application/json": '{ "message": "It works!"}',
        },
      },
    ],
    passthroughBehavior: apigateway.PassthroughBehavior.NEVER,
    requestTemplates: {
      "application/json": '{ "statusCode": 200 }',
    },
  }),
  { authorizer, methodResponses: [{ statusCode: "200" }] }
);

What did you expect to happen?

The authorizerUri should be set us-east-1

After manually editing the Authorizer on the console:

image (1)

$> aws apigateway get-authorizers --rest-api-id xxxxxxxxxx --region "us-east-2" | jq

{
  "items": [
    {
      "id": "xxxxxxxxxx",
      "name": "ExampleAuthorizer9D85B54E",
      "type": "TOKEN",
      "authType": "custom",
      "authorizerUri": "arn:aws:apigateway:us-east-1:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:zzzzzzzzzz:function:example-function/invocations",
      "identitySource": "method.request.header.Authorization",
      "authorizerResultTtlInSeconds": 300
    }
  ]
}

What actually happened?

The authorizerUri was set to the incorrect region (e.g. us-east-2)

$> aws apigateway get-authorizers --rest-api-id xxxxxxxxxx --region "us-east-2" | jq

{
  "items": [
    {
      "id": "xxxxxxxxxx",
      "name": "ExampleAuthorizer9D85B54E",
      "type": "TOKEN",
      "authType": "custom",
      "authorizerUri": "arn:aws:apigateway:us-east-2:lambda:path/2015-03-31/functions/arn:aws:lambda:us-east-1:zzzzzzzzzz:function:example-authorizer/invocations",
      "identitySource": "method.request.header.Authorization"
    }
  ]
}

CDK CLI Version

1.137.0

Framework Version

No response

Node.js Version

v12.22.9

OS

mass Big Sur 11.6.1

Language

Typescript

Language Version

TypeScript (4.5.4)

Other information

Co-authored-by: Sam Hewitt sam@stedi.com
Co-authored-by: Cameron Sumpter cameron@stedi.com

@aisamu aisamu added bug This issue is a bug. needs-triage This issue or PR still needs to be triaged. labels Jan 14, 2022
@github-actions github-actions bot added the @aws-cdk/aws-apigateway Related to Amazon API Gateway label Jan 14, 2022
@ryparker ryparker added p2 and removed needs-triage This issue or PR still needs to be triaged. labels Jan 14, 2022
@shooit
Copy link
Contributor

shooit commented Jan 17, 2022

@ryparker @otaviomacedo how could we help expedite review of #18444 that is associated with this issue? This bug is blocking our team's multi-region deployments due to the authorizer being in a single region.

@mergify mergify bot closed this as completed in #18444 Jan 21, 2022
mergify bot pushed a commit that referenced this issue Jan 21, 2022
Fixes: #18443

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
@github-actions
Copy link

⚠️COMMENT VISIBILITY WARNING⚠️

Comments on closed issues are hard for our team to see.
If you need more assistance, please either tag a team member or open a new issue that references this one.
If you wish to keep having a conversation with other community members under this issue feel free to do so.

LukvonStrom pushed a commit to LukvonStrom/aws-cdk that referenced this issue Jan 26, 2022
Fixes: aws#18443

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
TikiTDO pushed a commit to TikiTDO/aws-cdk that referenced this issue Feb 21, 2022
Fixes: aws#18443

*By submitting this pull request, I confirm that my contribution is made under the terms of the Apache-2.0 license*
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigateway Related to Amazon API Gateway bug This issue is a bug. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

4 participants