apigateway: Missing authorization on imported/referenced API Gateway #33053
Labels
@aws-cdk/aws-apigateway
Related to Amazon API Gateway
bug
This issue is a bug.
needs-reproduction
This issue needs reproduction.
p2
Describe the bug
When 'importing' or referencing an existing Rest API Gateway using the
fromRestApiAttributes
method, the referenced Gateway is missing at least the default method authorization options.When you define default method options
each route and method under that gateway, when in the same CF Stack will inherit these properties. In this example, if I wanted a public method, I would now have to explicitly make the route and method public.
But when referencing this Gateway via
fromRestApiAttributes
in a different CF Stack, and new route and method I make are public by default.Any method added to
apiRoot
is now public. This is different behaviour from adding a method/root inrootGateway
.In scenarios where people are building directly against an
IRestApi
it's not always obvious if it was referenced/imported or created as part of the same stack. This then requires people to specifically add an authorizer at each child set of routes. This is likely (possibly?) undesired behaviour when a Gateway team may own the central gateway and authorization logic and allow other teams to add their own routes to it.If a team broken an existing API out into multiple stacks for any number of reasons, referencing the parent gateway would now cause a change to the authorization/authentication logic which wouldn't be desirable.
Regression Issue
Last Known Working CDK Version
No response
Expected Behavior
The
IRestApi
returned fromRestApi.fromRestApiAttributes
across stacks has the same behaviour and properties as theIRestApi
created in the same stack. All default options should be inherited by the new child providing consistent behavior regardless of where the gateway was first defined.Current Behavior
The
IRestApi
returned fromRestApi.fromRestApiAttributes
is missing at least the authentication properties from the parent gateway. This results API Methods added to anIRestApi
having different behaviour depending on where theIRestApi
comes fromReproduction Steps
Repo with repro CF Stacks https://github.com/ryancormack/cdk-api-gw-auth-bug
Current behaviour:
Method on root stack:
Method on 'child' stack
Both of these routes get created with the same code
Possible Solution
I think the issue may originate from https://github.com/aws/aws-cdk/blob/main/packages/aws-cdk-lib/aws-apigateway/lib/restapi.ts#L798
The second value passed to the
RootResource
is theResourceOptions
which then get used to populate things like theMethodOptions
As a solution,
fromRestApiAttributes
could be extended to be able to pass these props through, or take a value to "inherit default options" from a parent gatewayAdditional Information/Context
No response
CDK CLI Version
2.173.2
Framework Version
No response
Node.js Version
22
OS
OSx latest
Language
TypeScript
Language Version
No response
Other information
No response
The text was updated successfully, but these errors were encountered: