-
Notifications
You must be signed in to change notification settings - Fork 494
Add [FunctionUrl] annotation attribute with CORS support and source generator #2324
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
base: dev
Are you sure you want to change the base?
Changes from all commits
File filter
Filter by extension
Conversations
Jump to
Diff view
Diff view
There are no files selected for viewing
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,11 @@ | ||
| { | ||
| "Projects": [ | ||
| { | ||
| "Name": "Amazon.Lambda.Annotations", | ||
| "Type": "Minor", | ||
| "ChangelogMessages": [ | ||
| "Added [FunctionUrl] attribute for configuring Lambda functions with Function URL endpoints, including optional CORS support" | ||
| ] | ||
| } | ||
| ] | ||
| } |
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,48 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System.Linq; | ||
| using Amazon.Lambda.Annotations.APIGateway; | ||
| using Microsoft.CodeAnalysis; | ||
|
|
||
| namespace Amazon.Lambda.Annotations.SourceGenerator.Models.Attributes | ||
| { | ||
| public static class FunctionUrlAttributeBuilder | ||
| { | ||
| public static FunctionUrlAttribute Build(AttributeData att) | ||
| { | ||
| var authType = att.NamedArguments.FirstOrDefault(arg => arg.Key == "AuthType").Value.Value; | ||
|
|
||
| var data = new FunctionUrlAttribute | ||
| { | ||
| AuthType = authType == null ? FunctionUrlAuthType.NONE : (FunctionUrlAuthType)authType | ||
| }; | ||
|
|
||
| var allowOrigins = att.NamedArguments.FirstOrDefault(arg => arg.Key == "AllowOrigins").Value; | ||
| if (!allowOrigins.IsNull) | ||
| data.AllowOrigins = allowOrigins.Values.Select(v => v.Value as string).ToArray(); | ||
|
|
||
| var allowMethods = att.NamedArguments.FirstOrDefault(arg => arg.Key == "AllowMethods").Value; | ||
| if (!allowMethods.IsNull) | ||
| data.AllowMethods = allowMethods.Values.Select(v => v.Value as string).ToArray(); | ||
|
|
||
| var allowHeaders = att.NamedArguments.FirstOrDefault(arg => arg.Key == "AllowHeaders").Value; | ||
| if (!allowHeaders.IsNull) | ||
| data.AllowHeaders = allowHeaders.Values.Select(v => v.Value as string).ToArray(); | ||
|
|
||
| var exposeHeaders = att.NamedArguments.FirstOrDefault(arg => arg.Key == "ExposeHeaders").Value; | ||
| if (!exposeHeaders.IsNull) | ||
| data.ExposeHeaders = exposeHeaders.Values.Select(v => v.Value as string).ToArray(); | ||
|
|
||
| var allowCredentials = att.NamedArguments.FirstOrDefault(arg => arg.Key == "AllowCredentials").Value.Value; | ||
| if (allowCredentials != null) | ||
| data.AllowCredentials = (bool)allowCredentials; | ||
|
|
||
| var maxAge = att.NamedArguments.FirstOrDefault(arg => arg.Key == "MaxAge").Value.Value; | ||
| if (maxAge != null) | ||
| data.MaxAge = (int)maxAge; | ||
|
|
||
| return data; | ||
| } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change |
|---|---|---|
| @@ -0,0 +1,51 @@ | ||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||
| // SPDX-License-Identifier: Apache-2.0 | ||
|
|
||
| using System; | ||
GarrettBeatty marked this conversation as resolved.
Show resolved
Hide resolved
|
||
|
|
||
| namespace Amazon.Lambda.Annotations.APIGateway | ||
| { | ||
| /// <summary> | ||
| /// Configures the Lambda function to be invoked via a Lambda Function URL. | ||
| /// </summary> | ||
| /// <remarks> | ||
| /// Function URLs use the same payload format as HTTP API v2 (APIGatewayHttpApiV2ProxyRequest/Response). | ||
| /// </remarks> | ||
| [AttributeUsage(AttributeTargets.Method)] | ||
| public class FunctionUrlAttribute : Attribute | ||
| { | ||
| /// <inheritdoc cref="FunctionUrlAuthType"/> | ||
| public FunctionUrlAuthType AuthType { get; set; } = FunctionUrlAuthType.NONE; | ||
|
|
||
| /// <summary> | ||
| /// The allowed origins for CORS requests. Example: new[] { "https://example.com" } | ||
| /// </summary> | ||
| public string[] AllowOrigins { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The allowed HTTP methods for CORS requests. Example: new[] { "GET", "POST" } | ||
| /// </summary> | ||
| public string[] AllowMethods { get; set; } | ||
|
Member
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. I think we should be using an enum like we do in the API Gateway events. For the API Gateway we created our own enum to support "Any". Not sure if that is a valid situation here or is the absence of this property essentially "Any". |
||
|
|
||
| /// <summary> | ||
| /// The allowed headers for CORS requests. | ||
| /// </summary> | ||
| public string[] AllowHeaders { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// Whether credentials are included in the CORS request. | ||
| /// </summary> | ||
| public bool AllowCredentials { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The expose headers for CORS responses. | ||
| /// </summary> | ||
| public string[] ExposeHeaders { get; set; } | ||
|
|
||
| /// <summary> | ||
| /// The maximum time in seconds that a browser can cache the CORS preflight response. | ||
| /// A value of 0 means the property is not set. | ||
| /// </summary> | ||
| public int MaxAge { get; set; } | ||
| } | ||
| } | ||
| Original file line number | Diff line number | Diff line change | ||||||||||||||||||||||||
|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|---|
| @@ -0,0 +1,21 @@ | ||||||||||||||||||||||||||
| // Copyright Amazon.com, Inc. or its affiliates. All Rights Reserved. | ||||||||||||||||||||||||||
| // SPDX-License-Identifier: Apache-2.0 | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| namespace Amazon.Lambda.Annotations.APIGateway | ||||||||||||||||||||||||||
|
Contributor
Author
There was a problem hiding this comment. Choose a reason for hiding this commentThe reason will be displayed to describe this comment to others. Learn more. technically function url is completely different than api gateway but uses the same api gateway v2 response type, so i kept it in the same namespace for now
GarrettBeatty marked this conversation as resolved.
Show resolved
Hide resolved
|
||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||
| /// The type of authentication for a Lambda Function URL. | ||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||
| public enum FunctionUrlAuthType | ||||||||||||||||||||||||||
| { | ||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||
| /// No authentication. Anyone with the Function URL can invoke the function. | ||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||
| NONE, | ||||||||||||||||||||||||||
|
|
||||||||||||||||||||||||||
| /// <summary> | ||||||||||||||||||||||||||
| /// IAM authentication. Only authenticated IAM users and roles can invoke the function. | ||||||||||||||||||||||||||
| /// </summary> | ||||||||||||||||||||||||||
| AWS_IAM | ||||||||||||||||||||||||||
|
Comment on lines
+14
to
+19
|
||||||||||||||||||||||||||
| NONE, | |
| /// <summary> | |
| /// IAM authentication. Only authenticated IAM users and roles can invoke the function. | |
| /// </summary> | |
| AWS_IAM | |
| None, | |
| /// <summary> | |
| /// IAM authentication. Only authenticated IAM users and roles can invoke the function. | |
| /// </summary> | |
| AwsIam |
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
i think it should be all caps
Uh oh!
There was an error while loading. Please reload this page.