Beispiel #1
0
        // Generate a new statement
        internal static Statement GenerateStatement(List <string> actions, string resourcePrefix, string effect = "Allow", string aws = "*", bool withConditions = false, string withStringSet = "hello", string condition = "StringEquals")
        {
            Statement stmt = new Statement();

            stmt.resources = new Resources(resourcePrefix);
            stmt.actions   = actions;
            stmt.effect    = effect;
            stmt.principal = new Principal(aws);
            if (withConditions)
            {
                stmt.conditions = new ConditionMap();
                ConditionKeyMap ckmap = new ConditionKeyMap();
                if (withStringSet != null)
                {
                    ckmap.Add("s3:prefix", new HashSet <string>()
                    {
                        withStringSet
                    });
                }
                if (condition != null && ckmap != null)
                {
                    stmt.conditions.Add(condition, ckmap);
                }
            }

            return(stmt);
        }
Beispiel #2
0
        public void TestConditionMapAdd()
        {
            ConditionMap cmap = new ConditionMap();

            ConditionKeyMap ckmap1 = new ConditionKeyMap("s3:prefix", "hello");
            ConditionKeyMap ckmap2 = new ConditionKeyMap("s3:prefix", new HashSet <string> {
                "hello", "world"
            });

            var testCases = new List <KeyValuePair <Tuple <string, ConditionKeyMap>, string> >()
            {
                // Add new key and value
                new KeyValuePair <Tuple <string, ConditionKeyMap>, string> (Tuple.Create <string, ConditionKeyMap>("StringEquals", ckmap1), @"{""StringEquals"":{""s3:prefix"":[""hello""]}}"),
                //Add existing key and value
                new KeyValuePair <Tuple <string, ConditionKeyMap>, string> (Tuple.Create <string, ConditionKeyMap>("StringEquals", ckmap1), @"{""StringEquals"":{""s3:prefix"":[""hello""]}}"),
                //Add existing key and new value
                new KeyValuePair <Tuple <string, ConditionKeyMap>, string> (Tuple.Create <string, ConditionKeyMap>("StringEquals", ckmap2), @"{""StringEquals"":{""s3:prefix"":[""hello"",""world""]}}"),
            };
            int index = 0;

            foreach (KeyValuePair <Tuple <string, ConditionKeyMap>, string> pair in testCases)
            {
                Tuple <string, ConditionKeyMap> tuple = pair.Key;
                string expectedJSON = pair.Value;

                index += 1;
                cmap.Put(tuple.Item1, tuple.Item2);
                string cmapJSON = JsonConvert.SerializeObject(cmap, Formatting.None,
                                                              new JsonSerializerSettings
                {
                    NullValueHandling = NullValueHandling.Ignore
                });
                Assert.AreEqual(expectedJSON, cmapJSON);
            }
        }
Beispiel #3
0
        public void TestConditionKeyMapRemove()
        {
            var testCases = new List <KeyValuePair <Tuple <string, HashSet <string> >, string> >()
            {
                // Add new k-v pair
                new KeyValuePair <Tuple <string, HashSet <string> >, string>(new Tuple <string, HashSet <string> >("s3:myprefix", new HashSet <string>()
                {
                    "hello"
                }), @"{""s3:prefix"":[""hello"",""world""]}"),
                // Add existing k-v pair
                new KeyValuePair <Tuple <string, HashSet <string> >, string>(new Tuple <string, HashSet <string> >("s3:prefix", new HashSet <string>()
                {
                    "hello"
                }), @"{""s3:prefix"":[""world""]}"),
                // Add existing key and not value
                new KeyValuePair <Tuple <string, HashSet <string> >, string>(new Tuple <string, HashSet <string> >("s3:prefix", new HashSet <string>()
                {
                    "world"
                }), @"{}"),
            };
            ConditionKeyMap cmap = new ConditionKeyMap();

            cmap.Add("s3:prefix", new HashSet <string>()
            {
                "hello", "world"
            });

            int index = 0;

            foreach (KeyValuePair <Tuple <string, HashSet <string> >, string> pair in testCases)
            {
                try
                {
                    index += 1;
                    var              testcase              = pair.Key;
                    string           prefix                = testcase.Item1;
                    HashSet <string> stringSet             = testcase.Item2;
                    string           expectedConditionKMap = pair.Value;
                    cmap.remove(prefix, stringSet);
                    string cmpstring = JsonConvert.SerializeObject(cmap, Formatting.None,
                                                                   new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                    Assert.AreEqual(cmpstring, expectedConditionKMap);
                }
                catch (ArgumentException)
                {
                    Assert.AreNotEqual(index, 1);
                }
            }
        }
Beispiel #4
0
        public override object ReadJson(JsonReader reader, Type objectType, object existingValue, JsonSerializer serializer)
        {
            object          retVal   = new Object();
            bool            isParsed = false;
            ISet <string>   parseSet = new HashSet <string>();
            string          key      = null;
            ConditionKeyMap instance = null;

            if (reader.TokenType == JsonToken.StartObject)
            {
                instance = new ConditionKeyMap();
            }
            do
            {
                {
                    if (reader.TokenType == JsonToken.PropertyName)
                    {
                        if (key == null)
                        {
                            key = reader.Value.ToString();
                        }
                    }
                    else if (reader.TokenType == JsonToken.String)
                    {
                        parseSet.Add(reader.Value.ToString());
                        instance.Put(key, parseSet);
                        isParsed = true;
                    }
                    else if (reader.TokenType == JsonToken.StartArray)
                    {
                        JArray array = JArray.Load(reader);
                        var    rs    = array.ToObject <ISet <string> >();
                        parseSet = new HashSet <string>();
                        foreach (var el in rs)
                        {
                            parseSet.Add(el);
                        }
                        instance.Put(key, parseSet);
                        isParsed = true;
                    }
                }
            }while (reader.Read() && !isParsed);
            return(instance);
        }
Beispiel #5
0
        // Tests if condition key map merges existing values
        public void TestConditionKeyMapPut()
        {
            ConditionKeyMap cmap1 = new ConditionKeyMap();

            cmap1.Add("s3:prefix", new HashSet <string>()
            {
                "hello"
            });

            ConditionKeyMap cmap2 = new ConditionKeyMap();

            cmap2.Add("s3:prefix", new HashSet <string>()
            {
                "world"
            });

            ConditionKeyMap cmap3 = new ConditionKeyMap();

            cmap3.Add("s3:myprefix", new HashSet <string>()
            {
                "world"
            });

            ConditionKeyMap cmap4 = new ConditionKeyMap();

            cmap4.Add("s3:prefix", new HashSet <string>()
            {
                "hello"
            });
            var testCases = new List <KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string> >()
            {
                // Both args are empty
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(new ConditionKeyMap(), new ConditionKeyMap()), @"{}"),
                // First arg empty
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(new ConditionKeyMap(), cmap1), @"{""s3:prefix"":[""hello""]}"),
                //Second arg empty
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(cmap1, new ConditionKeyMap()), @"{""s3:prefix"":[""hello""]}"),
                //Both args have same value
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(cmap1, cmap4), @"{""s3:prefix"":[""hello""]}"),
                //Value of second arg will be merged
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(cmap1, cmap2), @"{""s3:prefix"":[""hello"",""world""]}"),
                //second arg will be added
                new KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string>(Tuple.Create(cmap1, cmap3), @"{""s3:prefix"":[""hello"",""world""],""s3:myprefix"":[""world""]}"),
            };


            int index = 0;

            foreach (KeyValuePair <Tuple <ConditionKeyMap, ConditionKeyMap>, string> pair in testCases)
            {
                try
                {
                    index += 1;
                    var             testcase = pair.Key;
                    ConditionKeyMap first    = testcase.Item1;
                    ConditionKeyMap second   = testcase.Item2;
                    string          expectedConditionKMapJSON = pair.Value;
                    foreach (KeyValuePair <string, ISet <string> > kvpair in second)
                    {
                        first.Put(kvpair.Key, kvpair.Value);
                    }
                    string cmpstring = JsonConvert.SerializeObject(first, Formatting.None,
                                                                   new JsonSerializerSettings
                    {
                        NullValueHandling = NullValueHandling.Ignore
                    });

                    Assert.AreEqual(cmpstring, expectedConditionKMapJSON);
                }
                catch (ArgumentException)
                {
                    Assert.Fail();
                }
            }
        }