Skip to content

Commit fcb5208

Browse files
Feature/auth (#2314)
1 parent a12a2ed commit fcb5208

83 files changed

Lines changed: 9624 additions & 953 deletions

File tree

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.
Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,11 @@
1+
{
2+
"Projects": [
3+
{
4+
"Name": "Amazon.Lambda.Annotations",
5+
"Type": "Minor",
6+
"ChangelogMessages": [
7+
"Developers can now define Lambda Authorizers and protect API endpoints entirely through C# attributes, eliminating the need for manual CloudFormation configuration."
8+
]
9+
}
10+
]
11+
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Amazon.Lambda.Annotations.SourceGenerator.csproj

Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -98,6 +98,14 @@
9898
<Generator>TextTemplatingFilePreprocessor</Generator>
9999
<LastGenOutput>ExecutableAssembly.cs</LastGenOutput>
100100
</None>
101+
<None Update="Templates\AuthorizerSetupParameters.tt">
102+
<Generator>TextTemplatingFilePreprocessor</Generator>
103+
<LastGenOutput>AuthorizerSetupParameters.cs</LastGenOutput>
104+
</None>
105+
<None Update="Templates\AuthorizerInvoke.tt">
106+
<Generator>TextTemplatingFilePreprocessor</Generator>
107+
<LastGenOutput>AuthorizerInvoke.cs</LastGenOutput>
108+
</None>
101109
</ItemGroup>
102110

103111
<ItemGroup>
@@ -131,6 +139,16 @@
131139
<AutoGen>True</AutoGen>
132140
<DependentUpon>ExecutableAssembly.tt</DependentUpon>
133141
</Compile>
142+
<Compile Update="Templates\AuthorizerSetupParameters.cs">
143+
<DesignTime>True</DesignTime>
144+
<AutoGen>True</AutoGen>
145+
<DependentUpon>AuthorizerSetupParameters.tt</DependentUpon>
146+
</Compile>
147+
<Compile Update="Templates\AuthorizerInvoke.cs">
148+
<DesignTime>True</DesignTime>
149+
<AutoGen>True</AutoGen>
150+
<DependentUpon>AuthorizerInvoke.tt</DependentUpon>
151+
</Compile>
134152
</ItemGroup>
135153

136154
<ItemGroup>
Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,2 +1,18 @@
11
; Unshipped analyzer release
22
; https://github.com/dotnet/roslyn-analyzers/blob/master/src/Microsoft.CodeAnalysis.Analyzers/ReleaseTrackingAnalyzers.Help.md
3+
4+
### New Rules
5+
6+
Rule ID | Category | Severity | Notes
7+
--------|----------|----------|-------
8+
AWSLambda0120 | AWSLambdaCSharpGenerator | Error | Authorizer Name Required
9+
AWSLambda0121 | AWSLambdaCSharpGenerator | Error | HTTP API Authorizer Not Found
10+
AWSLambda0122 | AWSLambdaCSharpGenerator | Error | REST API Authorizer Not Found
11+
AWSLambda0123 | AWSLambdaCSharpGenerator | Error | Authorizer Type Mismatch
12+
AWSLambda0124 | AWSLambdaCSharpGenerator | Error | Authorizer Type Mismatch
13+
AWSLambda0125 | AWSLambdaCSharpGenerator | Error | Duplicate Authorizer Name
14+
AWSLambda0127 | AWSLambdaCSharpGenerator | Error | Invalid Result TTL
15+
AWSLambda0128 | AWSLambdaCSharpGenerator | Error | Authorizer Payload Version Mismatch
16+
AWSLambda0129 | AWSLambdaCSharpGenerator | Error | Missing LambdaFunction Attribute
17+
AWSLambda0130 | AWSLambdaCSharpGenerator | Error | Invalid return type IAuthorizerResult
18+
AWSLambda0131 | AWSLambdaCSharpGenerator | Error | FromBody not supported on Authorizer functions

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Diagnostics/DiagnosticDescriptors.cs

Lines changed: 89 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -153,5 +153,94 @@ public static class DiagnosticDescriptors
153153
category: "AWSLambdaCSharpGenerator",
154154
DiagnosticSeverity.Error,
155155
isEnabledByDefault: true);
156+
157+
// Authorizer diagnostics (ALA0019-ALA0027 per design document)
158+
public static readonly DiagnosticDescriptor AuthorizerMissingName = new DiagnosticDescriptor(
159+
id: "AWSLambda0120",
160+
title: "Authorizer Name Required",
161+
messageFormat: "The Name property is required on [{0}] attribute",
162+
category: "AWSLambdaCSharpGenerator",
163+
DiagnosticSeverity.Error,
164+
isEnabledByDefault: true);
165+
166+
public static readonly DiagnosticDescriptor HttpApiAuthorizerNotFound = new DiagnosticDescriptor(
167+
id: "AWSLambda0121",
168+
title: "HTTP API Authorizer Not Found",
169+
messageFormat: "Authorizer '{0}' referenced in [HttpApi] attribute does not exist. Ensure a Lambda function with [HttpApiAuthorizer] exists with method name '{0}' or ResourceName = \"{0}\".",
170+
category: "AWSLambdaCSharpGenerator",
171+
DiagnosticSeverity.Error,
172+
isEnabledByDefault: true);
173+
174+
public static readonly DiagnosticDescriptor RestApiAuthorizerNotFound = new DiagnosticDescriptor(
175+
id: "AWSLambda0122",
176+
title: "REST API Authorizer Not Found",
177+
messageFormat: "Authorizer '{0}' referenced in [RestApi] attribute does not exist. Ensure a Lambda function with [RestApiAuthorizer] exists with method name '{0}' or ResourceName = \"{0}\".",
178+
category: "AWSLambdaCSharpGenerator",
179+
DiagnosticSeverity.Error,
180+
isEnabledByDefault: true);
181+
182+
public static readonly DiagnosticDescriptor HttpApiAuthorizerTypeMismatch = new DiagnosticDescriptor(
183+
id: "AWSLambda0123",
184+
title: "Authorizer Type Mismatch",
185+
messageFormat: "Cannot use REST API authorizer '{0}' with [HttpApi] attribute. Use an [HttpApiAuthorizer] instead.",
186+
category: "AWSLambdaCSharpGenerator",
187+
DiagnosticSeverity.Error,
188+
isEnabledByDefault: true);
189+
190+
public static readonly DiagnosticDescriptor RestApiAuthorizerTypeMismatch = new DiagnosticDescriptor(
191+
id: "AWSLambda0124",
192+
title: "Authorizer Type Mismatch",
193+
messageFormat: "Cannot use HTTP API authorizer '{0}' with [RestApi] attribute. Use a [RestApiAuthorizer] instead.",
194+
category: "AWSLambdaCSharpGenerator",
195+
DiagnosticSeverity.Error,
196+
isEnabledByDefault: true);
197+
198+
public static readonly DiagnosticDescriptor DuplicateAuthorizerName = new DiagnosticDescriptor(
199+
id: "AWSLambda0125",
200+
title: "Duplicate Authorizer Name",
201+
messageFormat: "Duplicate authorizer name '{0}'. Authorizer names must be unique within the same API type.",
202+
category: "AWSLambdaCSharpGenerator",
203+
DiagnosticSeverity.Error,
204+
isEnabledByDefault: true);
205+
206+
public static readonly DiagnosticDescriptor InvalidAuthorizerResultTtl = new DiagnosticDescriptor(
207+
id: "AWSLambda0127",
208+
title: "Invalid Result TTL",
209+
messageFormat: "Invalid ResultTtlInSeconds '{0}'. Must be between 0 and 3600.",
210+
category: "AWSLambdaCSharpGenerator",
211+
DiagnosticSeverity.Error,
212+
isEnabledByDefault: true);
213+
214+
public static readonly DiagnosticDescriptor AuthorizerPayloadVersionMismatch = new DiagnosticDescriptor(
215+
id: "AWSLambda0128",
216+
title: "Authorizer Payload Version Mismatch",
217+
messageFormat: "The authorizer '{0}' uses AuthorizerPayloadFormatVersion {1} but the endpoint uses HttpApiVersion {2}. This may cause unexpected behavior.",
218+
category: "AWSLambdaCSharpGenerator",
219+
DiagnosticSeverity.Error,
220+
isEnabledByDefault: true);
221+
222+
public static readonly DiagnosticDescriptor MissingLambdaFunctionAttribute = new DiagnosticDescriptor(
223+
id: "AWSLambda0129",
224+
title: "Missing LambdaFunction Attribute",
225+
messageFormat: "Method has [{0}] attribute but is missing the required [LambdaFunction] attribute. Add [LambdaFunction] to this method.",
226+
category: "AWSLambdaCSharpGenerator",
227+
DiagnosticSeverity.Error,
228+
isEnabledByDefault: true);
229+
230+
public static readonly DiagnosticDescriptor AuthorizerResultOnNonAuthorizerFunction = new DiagnosticDescriptor(
231+
id: "AWSLambda0130",
232+
title: $"Invalid return type IAuthorizerResult",
233+
messageFormat: "IAuthorizerResult is not a valid return type for LambdaFunctions without HttpApiAuthorizer or RestApiAuthorizer attributes",
234+
category: "AWSLambdaCSharpGenerator",
235+
DiagnosticSeverity.Error,
236+
isEnabledByDefault: true);
237+
238+
public static readonly DiagnosticDescriptor FromBodyNotSupportedOnAuthorizer = new DiagnosticDescriptor(
239+
id: "AWSLambda0131",
240+
title: "FromBody not supported on Authorizer functions",
241+
messageFormat: "[FromBody] is not supported on authorizer functions. Authorizer functions only support [FromHeader], [FromQuery], and [FromRoute] parameter attributes.",
242+
category: "AWSLambdaCSharpGenerator",
243+
DiagnosticSeverity.Error,
244+
isEnabledByDefault: true);
156245
}
157246
}

Libraries/src/Amazon.Lambda.Annotations.SourceGenerator/Extensions/ParameterListExtension.cs

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -12,7 +12,7 @@ public static bool HasConvertibleParameter(this IList<ParameterModel> parameters
1212
return parameters.Any(p =>
1313
{
1414
// All request types are forwarded to lambda method if specified, there is no parameter conversion required.
15-
if (TypeFullNames.Requests.Contains(p.Type.FullName))
15+
if (TypeFullNames.ApiGatewayRequests.Contains(p.Type.FullName))
1616
{
1717
return false;
1818
}

0 commit comments

Comments
 (0)