Skip to content

Commit d966740

Browse files
authored
Add NotResource field for custom authorizer IAM policy (#2165)
1 parent 48c0560 commit d966740

File tree

4 files changed

+90
-4
lines changed

4 files changed

+90
-4
lines changed
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.APIGatewayEvents",
5+
"Type": "Patch",
6+
"ChangelogMessages": [
7+
"Added NotResource field to APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement"
8+
]
9+
}
10+
]
11+
}

Libraries/src/Amazon.Lambda.APIGatewayEvents/APIGatewayCustomAuthorizerPolicy.cs

Lines changed: 9 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
namespace Amazon.Lambda.APIGatewayEvents
1+
namespace Amazon.Lambda.APIGatewayEvents
22
{
33
using System.Collections.Generic;
44

@@ -52,6 +52,14 @@ public class IAMPolicyStatement
5252
#endif
5353
public HashSet<string> Resource { get; set; }
5454

55+
/// <summary>
56+
/// Gets or sets the resources the statement does not apply to.
57+
/// </summary>
58+
#if NETCOREAPP3_1_OR_GREATER
59+
[System.Text.Json.Serialization.JsonPropertyName("NotResource")]
60+
#endif
61+
public HashSet<string> NotResource { get; set; }
62+
5563
/// <summary>
5664
/// Gets or sets the conditions for when a policy is in effect.
5765
/// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html

Libraries/test/Amazon.Lambda.AspNetCoreServer.Test/TestCallingWebAPI.cs

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
using System;
1+
using System;
22
using System.Collections.Generic;
33
using System.IO;
44
using System.IO.Compression;
@@ -221,10 +221,11 @@ public void TestCustomAuthorizerSerialization()
221221

222222
var json = JsonSerializer.Serialize(response, new JsonSerializerOptions
223223
{
224-
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
224+
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping,
225+
DefaultIgnoreCondition = System.Text.Json.Serialization.JsonIgnoreCondition.WhenWritingNull
225226
});
226227
Assert.NotNull(json);
227-
var expected = "{\"principalId\":\"com.amazon.someuser\",\"policyDocument\":{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"execute-api:Invoke\"],\"Resource\":[\"arn:aws:execute-api:us-west-2:1234567890:apit123d45/Prod/GET/*\"],\"Condition\":null}]},\"context\":{\"stringKey\":\"Hey I'm a string\",\"boolKey\":true,\"numKey\":9},\"usageIdentifierKey\":null}";
228+
var expected = "{\"principalId\":\"com.amazon.someuser\",\"policyDocument\":{\"Version\":\"2012-10-17\",\"Statement\":[{\"Effect\":\"Allow\",\"Action\":[\"execute-api:Invoke\"],\"Resource\":[\"arn:aws:execute-api:us-west-2:1234567890:apit123d45/Prod/GET/*\"]}]},\"context\":{\"stringKey\":\"Hey I'm a string\",\"boolKey\":true,\"numKey\":9}}";
228229
Assert.Equal(expected, json);
229230
}
230231

Libraries/test/EventsTests.Shared/EventTests.cs

Lines changed: 66 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2165,6 +2165,72 @@ public void APIGatewayAuthorizerWithMultiValueIAMConditionResponseTest(Type seri
21652165
Assert.Equal("arn:aws:iam::XXXXXXXXXXXX:user/User2", root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"][1]);
21662166
}
21672167

2168+
[Theory]
2169+
[InlineData(typeof(JsonSerializer))]
2170+
#if NETCOREAPP3_1_OR_GREATER
2171+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
2172+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
2173+
#endif
2174+
public void APIGatewayAuthorizerResponseNotResourceTest(Type serializerType)
2175+
{
2176+
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
2177+
var context = new APIGatewayCustomAuthorizerContextOutput();
2178+
context["field1"] = "value1";
2179+
context["field2"] = "value2";
2180+
2181+
var response = new APIGatewayCustomAuthorizerResponse
2182+
{
2183+
PrincipalID = "prin1",
2184+
UsageIdentifierKey = "usageKey",
2185+
Context = context,
2186+
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
2187+
{
2188+
Version = "2012-10-17",
2189+
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
2190+
{
2191+
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
2192+
{
2193+
Action = new HashSet<string>{ "execute-api:Invoke" },
2194+
Effect = "Deny",
2195+
NotResource = new HashSet<string>
2196+
{
2197+
"arn:aws:execute-api:us-east-1:1234567890:abcdef1234/Prod/GET/resource1",
2198+
"arn:aws:execute-api:us-east-1:1234567890:abcdef1234/Prod/GET/resource2"
2199+
}
2200+
}
2201+
}
2202+
}
2203+
};
2204+
2205+
string serializedJson;
2206+
using (MemoryStream stream = new MemoryStream())
2207+
{
2208+
serializer.Serialize(response, stream);
2209+
2210+
stream.Position = 0;
2211+
serializedJson = Encoding.UTF8.GetString(stream.ToArray());
2212+
}
2213+
2214+
JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject;
2215+
2216+
Assert.Equal("prin1", root["principalId"]);
2217+
Assert.Equal("usageKey", root["usageIdentifierKey"]);
2218+
Assert.Equal("value1", root["context"]["field1"]);
2219+
Assert.Equal("value2", root["context"]["field2"]);
2220+
2221+
Assert.Equal("2012-10-17", root["policyDocument"]["Version"]);
2222+
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
2223+
Assert.Equal("Deny", root["policyDocument"]["Statement"][0]["Effect"]);
2224+
2225+
var allowedResources = root["policyDocument"]["Statement"][0]["NotResource"];
2226+
Assert.Equal(2, allowedResources.Count());
2227+
Assert.Contains("arn:aws:execute-api:us-east-1:1234567890:abcdef1234/Prod/GET/resource1", allowedResources);
2228+
Assert.Contains("arn:aws:execute-api:us-east-1:1234567890:abcdef1234/Prod/GET/resource2", allowedResources);
2229+
2230+
Assert.Null(root["policyDocument"]["Statement"][0]["Condition"]);
2231+
Assert.Null(root["policyDocument"]["Statement"][0]["Resource"]);
2232+
}
2233+
21682234
[Theory]
21692235
[InlineData(typeof(JsonSerializer))]
21702236
#if NETCOREAPP3_1_OR_GREATER

0 commit comments

Comments
 (0)