Skip to content

Commit faa6f34

Browse files
committed
Adds support for Condition element in APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement.
1 parent 1f0b6a5 commit faa6f34

File tree

5 files changed

+161
-2
lines changed

5 files changed

+161
-2
lines changed

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

Lines changed: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -51,6 +51,15 @@ public class IAMPolicyStatement
5151
[System.Text.Json.Serialization.JsonPropertyName("Resource")]
5252
#endif
5353
public HashSet<string> Resource { get; set; }
54+
55+
/// <summary>
56+
/// Gets or sets the conditions for when a policy is in effect.
57+
/// https://docs.aws.amazon.com/IAM/latest/UserGuide/reference_policies_elements_condition.html
58+
/// </summary>
59+
#if NETCOREAPP3_1_OR_GREATER
60+
[System.Text.Json.Serialization.JsonPropertyName("Condition")]
61+
#endif
62+
public IDictionary<string, IDictionary<string, object>> Condition { get; set; }
5463
}
5564
}
5665
}

Libraries/src/Amazon.Lambda.APIGatewayEvents/Amazon.Lambda.APIGatewayEvents.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -6,7 +6,7 @@
66
<TargetFrameworks>netstandard2.0;netcoreapp3.1;net8.0</TargetFrameworks>
77
<Description>Amazon Lambda .NET Core support - API Gateway package.</Description>
88
<AssemblyTitle>Amazon.Lambda.APIGatewayEvents</AssemblyTitle>
9-
<VersionPrefix>2.7.0</VersionPrefix>
9+
<VersionPrefix>2.7.1</VersionPrefix>
1010
<AssemblyName>Amazon.Lambda.APIGatewayEvents</AssemblyName>
1111
<PackageId>Amazon.Lambda.APIGatewayEvents</PackageId>
1212
<PackageTags>AWS;Amazon;Lambda</PackageTags>

Libraries/src/Amazon.Lambda.Serialization.Json/Amazon.Lambda.Serialization.Json.csproj

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@
99
<AssemblyName>Amazon.Lambda.Serialization.Json</AssemblyName>
1010
<PackageId>Amazon.Lambda.Serialization.Json</PackageId>
1111
<PackageTags>AWS;Amazon;Lambda</PackageTags>
12-
<VersionPrefix>2.2.1</VersionPrefix>
12+
<VersionPrefix>2.2.2</VersionPrefix>
1313
</PropertyGroup>
1414

1515
<ItemGroup>

Libraries/src/Amazon.Lambda.Serialization.Json/JsonSerializer.cs

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -52,6 +52,7 @@ public JsonSerializer(Action<JsonSerializerSettings> customizeSerializerSettings
5252
resolver.NamingStrategy = namingStrategy;
5353
};
5454
settings.ContractResolver = resolver;
55+
settings.NullValueHandling = NullValueHandling.Ignore;
5556

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

Libraries/test/EventsTests.Shared/EventTests.cs

Lines changed: 149 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ namespace Amazon.Lambda.Tests
2626
using Amazon.Lambda.SimpleEmailEvents;
2727
using Amazon.Lambda.SNSEvents;
2828
using Amazon.Lambda.SQSEvents;
29+
using Amazon.Runtime.Internal.Transform;
2930
using Newtonsoft.Json;
3031
using Newtonsoft.Json.Linq;
3132
using Newtonsoft.Json.Serialization;
@@ -2007,6 +2008,154 @@ public void APIGatewayAuthorizerResponseTest(Type serializerType)
20072008
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
20082009
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
20092010
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
2011+
Assert.Null(root["policyDocument"]["Statement"][0]["Condition"]);
2012+
}
2013+
2014+
[Theory]
2015+
[InlineData(typeof(JsonSerializer))]
2016+
#if NETCOREAPP3_1_OR_GREATER
2017+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
2018+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
2019+
#endif
2020+
public void APIGatewayAuthorizerWithSimpleIAMConditionResponseTest(Type serializerType)
2021+
{
2022+
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
2023+
var context = new APIGatewayCustomAuthorizerContextOutput();
2024+
context["field1"] = "value1";
2025+
context["field2"] = "value2";
2026+
2027+
var response = new APIGatewayCustomAuthorizerResponse
2028+
{
2029+
PrincipalID = "prin1",
2030+
UsageIdentifierKey = "usageKey",
2031+
Context = context,
2032+
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
2033+
{
2034+
Version = "2012-10-17",
2035+
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
2036+
{
2037+
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
2038+
{
2039+
Action = new HashSet<string>{ "execute-api:Invoke" },
2040+
Effect = "Allow",
2041+
Resource = new HashSet<string>{ "*" },
2042+
Condition = new Dictionary<string, IDictionary<string, object>>()
2043+
{
2044+
{ "StringEquals", new Dictionary<string, object>()
2045+
{
2046+
{ "aws:PrincipalTag/job-category", "iamuser-admin" }
2047+
}
2048+
}
2049+
}
2050+
}
2051+
}
2052+
}
2053+
};
2054+
2055+
string serializedJson;
2056+
using (MemoryStream stream = new MemoryStream())
2057+
{
2058+
serializer.Serialize(response, stream);
2059+
2060+
stream.Position = 0;
2061+
serializedJson = Encoding.UTF8.GetString(stream.ToArray());
2062+
}
2063+
2064+
JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject;
2065+
2066+
Assert.Equal("prin1", root["principalId"]);
2067+
Assert.Equal("usageKey", root["usageIdentifierKey"]);
2068+
Assert.Equal("value1", root["context"]["field1"]);
2069+
Assert.Equal("value2", root["context"]["field2"]);
2070+
2071+
Assert.Equal("2012-10-17", root["policyDocument"]["Version"]);
2072+
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
2073+
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
2074+
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
2075+
Assert.Equal("iamuser-admin", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/job-category"].ToString());
2076+
}
2077+
2078+
[Theory]
2079+
[InlineData(typeof(JsonSerializer))]
2080+
#if NETCOREAPP3_1_OR_GREATER
2081+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.LambdaJsonSerializer))]
2082+
[InlineData(typeof(Amazon.Lambda.Serialization.SystemTextJson.DefaultLambdaJsonSerializer))]
2083+
#endif
2084+
public void APIGatewayAuthorizerWithMultiValueIAMConditionResponseTest(Type serializerType)
2085+
{
2086+
var serializer = Activator.CreateInstance(serializerType) as ILambdaSerializer;
2087+
var context = new APIGatewayCustomAuthorizerContextOutput();
2088+
context["field1"] = "value1";
2089+
context["field2"] = "value2";
2090+
2091+
var response = new APIGatewayCustomAuthorizerResponse
2092+
{
2093+
PrincipalID = "prin1",
2094+
UsageIdentifierKey = "usageKey",
2095+
Context = context,
2096+
PolicyDocument = new APIGatewayCustomAuthorizerPolicy
2097+
{
2098+
Version = "2012-10-17",
2099+
Statement = new List<APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement>
2100+
{
2101+
new APIGatewayCustomAuthorizerPolicy.IAMPolicyStatement
2102+
{
2103+
Action = new HashSet<string>{ "execute-api:Invoke" },
2104+
Effect = "Allow",
2105+
Resource = new HashSet<string>{ "*" },
2106+
Condition = new Dictionary<string, IDictionary<string, object>>()
2107+
{
2108+
{
2109+
"StringEquals",
2110+
new Dictionary<string, object>()
2111+
{
2112+
{ "aws:PrincipalTag/department", new List<string>{ "finance", "hr", "legal" } },
2113+
{ "aws:PrincipalTag/role", new List<string>{ "audit", "security" } }
2114+
}
2115+
},
2116+
{
2117+
"ArnLike",
2118+
new Dictionary<string, object>()
2119+
{
2120+
{ "aws:PrincipalArn", new List<string>{ "arn:aws:iam::XXXXXXXXXXXX:user/User1", "arn:aws:iam::XXXXXXXXXXXX:user/User2" } }
2121+
}
2122+
}
2123+
}
2124+
}
2125+
}
2126+
}
2127+
};
2128+
2129+
string serializedJson;
2130+
using (MemoryStream stream = new MemoryStream())
2131+
{
2132+
serializer.Serialize(response, stream);
2133+
2134+
stream.Position = 0;
2135+
serializedJson = Encoding.UTF8.GetString(stream.ToArray());
2136+
}
2137+
2138+
JObject root = Newtonsoft.Json.JsonConvert.DeserializeObject(serializedJson) as JObject;
2139+
2140+
Assert.Equal("prin1", root["principalId"]);
2141+
Assert.Equal("usageKey", root["usageIdentifierKey"]);
2142+
Assert.Equal("value1", root["context"]["field1"]);
2143+
Assert.Equal("value2", root["context"]["field2"]);
2144+
2145+
Assert.Equal("2012-10-17", root["policyDocument"]["Version"]);
2146+
Assert.Equal("execute-api:Invoke", root["policyDocument"]["Statement"][0]["Action"][0]);
2147+
Assert.Equal("Allow", root["policyDocument"]["Statement"][0]["Effect"]);
2148+
Assert.Equal("*", root["policyDocument"]["Statement"][0]["Resource"][0]);
2149+
Assert.Equal(3, root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"].Values<string>().ToList().Count);
2150+
Assert.Equal("finance", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][0]);
2151+
Assert.Equal("hr", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][1]);
2152+
Assert.Equal("legal", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/department"][2]);
2153+
Assert.Equal(2, root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"].Values<string>().ToList().Count);
2154+
Assert.Equal("audit", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"][0]);
2155+
Assert.Equal("security", root["policyDocument"]["Statement"][0]["Condition"]["StringEquals"]["aws:PrincipalTag/role"][1]);
2156+
Assert.Equal(2, root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"].Values<string>().ToList().Count);
2157+
Assert.Equal("arn:aws:iam::XXXXXXXXXXXX:user/User1", root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"][0]);
2158+
Assert.Equal("arn:aws:iam::XXXXXXXXXXXX:user/User2", root["policyDocument"]["Statement"][0]["Condition"]["ArnLike"]["aws:PrincipalArn"][1]);
20102159
}
20112160

20122161
[Theory]

0 commit comments

Comments
 (0)