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

Adds support for Condition element in APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement. #1779

Merged
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,15 @@ public class IAMPolicyStatement
[System.Text.Json.Serialization.JsonPropertyName("Resource")]
#endif
public HashSet<string> Resource { get; set; }

/// <summary>
/// Gets or sets the conditions for when a policy is in effect.
/// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
/// </summary>
#if NETCOREAPP3_1_OR_GREATER
[System.Text.Json.Serialization.JsonPropertyName("Condition")]
#endif
public IDictionary<string, IDictionary<string, object>> Condition { get; set; }
}
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -6,7 +6,7 @@
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
<Description>Amazon Lambda .NET Core support - API Gateway package.</Description>
<AssemblyTitle>Amazon.Lambda.APIGatewayEvents</AssemblyTitle>
<VersionPrefix>2.7.0</VersionPrefix>
<VersionPrefix>2.7.1</VersionPrefix>
<AssemblyName>Amazon.Lambda.APIGatewayEvents</AssemblyName>
<PackageId>Amazon.Lambda.APIGatewayEvents</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -9,7 +9,7 @@
<AssemblyName>Amazon.Lambda.Serialization.Json</AssemblyName>
<PackageId>Amazon.Lambda.Serialization.Json</PackageId>
<PackageTags>AWS;Amazon;Lambda</PackageTags>
<VersionPrefix>2.2.1</VersionPrefix>
<VersionPrefix>2.2.2</VersionPrefix>
</PropertyGroup>

<ItemGroup>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -52,6 +52,7 @@ public JsonSerializer(Action<JsonSerializerSettings> customizeSerializerSettings
resolver.NamingStrategy = namingStrategy;
};
settings.ContractResolver = resolver;
settings.NullValueHandling = NullValueHandling.Ignore;

serializer = Newtonsoft.Json.JsonSerializer.Create(settings);

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -215,7 +215,7 @@ public void TestCustomAuthorizerSerialization()
Encoder = JavaScriptEncoder.UnsafeRelaxedJsonEscaping
});
Assert.NotNull(json);
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},\"usageIdentifierKey\":null}";
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}";
Assert.Equal(expected, json);
}

Expand Down
149 changes: 149 additions & 0 deletions Libraries/test/EventsTests.Shared/EventTests.cs
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,7 @@ namespace Amazon.Lambda.Tests
using Amazon.Lambda.SimpleEmailEvents;
using Amazon.Lambda.SNSEvents;
using Amazon.Lambda.SQSEvents;
using Amazon.Runtime.Internal.Transform;
using Newtonsoft.Json;
using Newtonsoft.Json.Linq;
using Newtonsoft.Json.Serialization;
Expand Down Expand Up @@ -2007,6 +2008,154 @@ public void APIGatewayAuthorizerResponseTest(Type serializerType)
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
Assert.Null(root["policyDocument"]["Statement"][0]["Condition"]);
}

[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
#endif
public void APIGatewayAuthorizerWithSimpleIAMConditionResponseTest(Type serializerType)
{
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
var context = new APIGatewayCustomAuthorizerContextOutput();
context["field1"] = "value1";
context["field2"] = "value2";

var response = new APIGatewayCustomAuthorizerResponse
{
PrincipalID = "prin1",
UsageIdentifierKey = "usageKey",
Context = context,
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
{
Version = "2012-10-17",
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
{
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
{
Action = new HashSet<string>{ "execute-api:Invoke" },
Effect = "Allow",
Resource = new HashSet<string>{ "*" },
Condition = new Dictionary<string, IDictionary<string, object>>()
{
{ "StringEquals", new Dictionary<string, object>()
{
{ "aws:PrincipalTag/job-category", "iamuser-admin" }
}
}
}
}
}
}
};

string serializedJson;
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(response, stream);

stream.Position = 0;
serializedJson = Encoding.UTF8.GetString(stream.ToArray());
}

JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject;

Assert.Equal("prin1", root["principalId"]);
Assert.Equal("usageKey", root["usageIdentifierKey"]);
Assert.Equal("value1", root["context"]["field1"]);
Assert.Equal("value2", root["context"]["field2"]);

Assert.Equal("2012-10-17", root["policyDocument"]["Version"]);
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
Assert.Equal("iamuser-admin", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/job-category"].ToString());
}

[Theory]
[InlineData(typeof(JsonSerializer))]
#if NETCOREAPP3_1_OR_GREATER
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
#endif
public void APIGatewayAuthorizerWithMultiValueIAMConditionResponseTest(Type serializerType)
{
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
var context = new APIGatewayCustomAuthorizerContextOutput();
context["field1"] = "value1";
context["field2"] = "value2";

var response = new APIGatewayCustomAuthorizerResponse
{
PrincipalID = "prin1",
UsageIdentifierKey = "usageKey",
Context = context,
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
{
Version = "2012-10-17",
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
{
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
{
Action = new HashSet<string>{ "execute-api:Invoke" },
Effect = "Allow",
Resource = new HashSet<string>{ "*" },
Condition = new Dictionary<string, IDictionary<string, object>>()
{
{
"StringEquals",
new Dictionary<string, object>()
{
{ "aws:PrincipalTag/department", new List<string>{ "finance", "hr", "legal" } },
{ "aws:PrincipalTag/role", new List<string>{ "audit", "security" } }
}
},
{
"ArnLike",
new Dictionary<string, object>()
{
{ "aws:PrincipalArn", new List<string>{ "arn:aws:iam::XXXXXXXXXXXX:user/User1", "arn:aws:iam::XXXXXXXXXXXX:user/User2" } }
}
}
}
}
}
}
};

string serializedJson;
using (MemoryStream stream = new MemoryStream())
{
serializer.Serialize(response, stream);

stream.Position = 0;
serializedJson = Encoding.UTF8.GetString(stream.ToArray());
}

JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject;

Assert.Equal("prin1", root["principalId"]);
Assert.Equal("usageKey", root["usageIdentifierKey"]);
Assert.Equal("value1", root["context"]["field1"]);
Assert.Equal("value2", root["context"]["field2"]);

Assert.Equal("2012-10-17", root["policyDocument"]["Version"]);
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
Assert.Equal(3, root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"].Values<string>().ToList().Count);
Assert.Equal("finance", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][0]);
Assert.Equal("hr", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][1]);
Assert.Equal("legal", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][2]);
Assert.Equal(2, root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"].Values<string>().ToList().Count);
Assert.Equal("audit", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"][0]);
Assert.Equal("security", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"][1]);
Assert.Equal(2, root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"].Values<string>().ToList().Count);
Assert.Equal("arn:aws:iam::XXXXXXXXXXXX:user/User1", root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"][0]);
Assert.Equal("arn:aws:iam::XXXXXXXXXXXX:user/User2", root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"][1]);
}

[Theory]
Expand Down
Loading