@@ -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