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-apigatewayv2-authorizers): Expose Authorizer ID to make it reusable from other projects #31605

Open
2 tasks
JonWallsten opened this issue Oct 1, 2024 · 7 comments · May be fixed by #31622
Open
2 tasks
Labels
@aws-cdk/aws-apigatewayv2-authorizers Related to aws-apigatewayv2-authorizers package effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2

Comments

@JonWallsten
Copy link

JonWallsten commented Oct 1, 2024

Describe the feature

I need to be able to retrieve the Authorizer ID (the generated ID, not the one passed to the Construct) from the L2 construct.
Looking through the code I guess the issue is that the authorizer is not created until it's attached to a route. So I cannot personally create a PR for this since I don't know how references are resolved when the templates are generated.

Use Case

I want to be able to reuse the authorizer in other projects in the same account.

Proposed Solution

Expose the id the same way other constructs are.

Other Information

No response

Acknowledgements

  • I may be able to implement this feature request
  • This feature might incur a breaking change

CDK version used

2.159.0

Environment details (OS name and version, etc.)

Windows 11 23H2

@JonWallsten JonWallsten added feature-request A feature should be added or improved. needs-triage This issue or PR still needs to be triaged. labels Oct 1, 2024
@github-actions github-actions bot added the @aws-cdk/aws-apigatewayv2-authorizers Related to aws-apigatewayv2-authorizers package label Oct 1, 2024
@JonWallsten JonWallsten changed the title (aws-apigatewayv2-authorizers): Expose authorizer id to make it reusable from other projects (aws-apigatewayv2-authorizers): Expose Authorizer ID to make it reusable from other projects Oct 1, 2024
@pahud
Copy link
Contributor

pahud commented Oct 1, 2024

Makes sense to me.

I think we can consider exposing the authorizer from here:

this.authorizer = new HttpAuthorizer(options.scope, this.id, {

which is currently a private attribute

We welcome PRs and please help us prioritize with 👍

@pahud pahud added p2 effort/medium Medium work item – several days of effort and removed needs-triage This issue or PR still needs to be triaged. labels Oct 1, 2024
@pahud
Copy link
Contributor

pahud commented Oct 1, 2024

related to: V1533536242

@JonWallsten
Copy link
Author

JonWallsten commented Oct 1, 2024

@pahud I tried to access that variable, but it is undefined when you setup the autherizer. It's not until you attach it to the first HttpRoute that you actually get the token. But I guess that's "as designed", it might not be clear to everyone though. My workaround for now is to access the private HttpRoute.authBindResult for the first HttpRoute I assigned it to. But I guess I could also access it through the Authorizer AFTER the first binding. But it still feels a bit weird to have to do it after it's first usage and not after the construction.

@JonWallsten
Copy link
Author

JonWallsten commented Oct 1, 2024

I can try to create a pr but I will need some guidance! I'll tag you in the PR when it's done.
Would you prefer to expose the entire authorizer or add a getter for exposing only the authorizerId on its own ?
The authorizerType also has to be exposed since it's needed to "lookup" the authorizer layer on.

@pahud
Copy link
Contributor

pahud commented Oct 1, 2024

@JonWallsten

At this moment, I guess you'll need the trick like this

export class HttplambdaStack extends cdk.Stack {
  constructor(scope: Construct, id: string, props?: cdk.StackProps) {
    super(scope, id, props);

    // Define the Lambda function for the authorizer
    const authorizerFunction = new lambda.Function(this, 'AuthorizerFunction', {
      runtime: lambda.Runtime.NODEJS_18_X,
      handler: 'authorizer.handler',
      code: lambda.Code.fromInline('dummy'),
    });

    // Define the Lambda function for the API endpoint
    const apiFunction = new lambda.Function(this, 'ApiFunction', {
      runtime: lambda.Runtime.NODEJS_LATEST,
      handler: 'api.handler',
      code: lambda.Code.fromInline('dummy'),
    });

    // Define the HTTP API
    const httpApi = new apigatewayv2.HttpApi(this, 'HttpApi', {
      apiName: 'MyHttpApi',
    });

    // Define the Lambda authorizer
    const httpLambdaAuthorizer = new authorizers.HttpLambdaAuthorizer('LambdaAuthorizer', authorizerFunction, {
      responseTypes: [authorizers.HttpLambdaResponseType.SIMPLE],
    });
    
    // Add a route with the Lambda authorizer
    httpApi.addRoutes({
      path: '/my-endpoint',
      methods: [apigatewayv2.HttpMethod.GET],
      integration: new integrations.HttpLambdaIntegration('ApiIntegration', apiFunction),
      authorizer: httpLambdaAuthorizer,
    });

    const cfnauthorizer = httpApi.node.tryFindChild('LambdaAuthorizer')?.node.defaultChild as apigatewayv2.CfnAuthorizer

    new cdk.CfnOutput(this, 'LambdaAuthorizerId', {
      value: cfnauthorizer.ref,
      description: 'The ID of the authorizer',
    });
  }
}

Let me know if it works for you.

@JonWallsten
Copy link
Author

JonWallsten commented Oct 2, 2024

@pahud :
You should also be able to to this:

const authorizer = /* your authorizer code here*/;
const httpRoute = new HttpRoute(this, `HttpRoute`, {
    httpApi: props.httpApi,
    integration: props.integration,
    authorizer: authorizer,
    routeKey: HttpRouteKey.with(props.route, props.method)
});

const id = httpRoute['authBindResult'].authorizerId;
// or
const id = authorizer['authorizer'].authorizerId;

@pahud
Copy link
Contributor

pahud commented Oct 2, 2024

@JonWallsten Agree. That's why I mentioned we need to make this public before we are allowed to do that and before that what I was offering could be a temporary workaround.

That being said, there may be other concerns I haven't considered yet.

Feel free to submit a PR whenever you're ready, and we can move forward from there.

Thank you.

@JonWallsten JonWallsten linked a pull request Oct 2, 2024 that will close this issue
1 task
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
@aws-cdk/aws-apigatewayv2-authorizers Related to aws-apigatewayv2-authorizers package effort/medium Medium work item – several days of effort feature-request A feature should be added or improved. p2
Projects
None yet
Development

Successfully merging a pull request may close this issue.

2 participants