Exemplo n.º 1
0
        public void NewPolicyDifferentValues()
        {
            List <Utility.RawStoredAccessPolicy> samplePolicies = Utility.SetUpStoredAccessPolicyData <SharedAccessBlobPolicy>();

            string             containerName = Utility.GenNameString("container");
            CloudBlobContainer container     = blobUtil.CreateContainer(containerName);

            Utility.ClearStoredAccessPolicy <CloudBlobContainer>(container);

            try
            {
                foreach (Utility.RawStoredAccessPolicy rawPolicy in samplePolicies)
                {
                    CreateStoredAccessPolicy(rawPolicy.PolicyName, rawPolicy.Permission, rawPolicy.StartTime, rawPolicy.ExpiryTime, container, false);
                }

                Utility.WaitForPolicyBecomeValid <CloudBlobContainer>(container, expectedCount: samplePolicies.Count);

                SharedAccessBlobPolicies expectedPolicies = new SharedAccessBlobPolicies();
                foreach (Utility.RawStoredAccessPolicy rawPolicy in samplePolicies)
                {
                    expectedPolicies.Add(rawPolicy.PolicyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(rawPolicy.StartTime, rawPolicy.ExpiryTime, rawPolicy.Permission));
                }

                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);
            }
            finally
            {
                blobUtil.RemoveContainer(container);
            }
        }
Exemplo n.º 2
0
        internal void CreateStoredAccessPolicyAndValidate(string policyName, string permission, DateTime?startTime, DateTime?expiryTime, string containerName = null, bool ifCleanUpContainer = true, bool ifCleanUpPolicy = true)
        {
            CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            try
            {
                //create the policy
                CreateStoredAccessPolicy(policyName, permission, startTime, expiryTime, container, ifCleanUpPolicy);

                //get the policy and validate
                SharedAccessBlobPolicies expectedPolicies = new SharedAccessBlobPolicies();
                expectedPolicies.Add(policyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(startTime, expiryTime, permission));

                Utility.RawStoredAccessPolicy policy = new Utility.RawStoredAccessPolicy(policyName, startTime, expiryTime, permission);
                Utility.WaitForPolicyBecomeValid <CloudBlobContainer>(container, policy);
                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);
            }
            finally
            {
                if (ifCleanUpPolicy)
                {
                    Utility.ClearStoredAccessPolicy <CloudBlobContainer>(container);
                }
                if (ifCleanUpContainer)
                {
                    blobUtil.RemoveContainer(container);
                }
            }
        }
Exemplo n.º 3
0
        public void SetPolicyNoStartTimeNoExpiryTime()
        {
            CloudBlobContainer container = blobUtil.CreateContainer();

            Utility.ClearStoredAccessPolicy <CloudBlobContainer>(container);
            Utility.RawStoredAccessPolicy samplePolicy = Utility.SetUpStoredAccessPolicyData <SharedAccessBlobPolicy>()[0];
            double effectiveTime = 30;

            try
            {
                CreateStoredAccessPolicy(samplePolicy.PolicyName, samplePolicy.Permission, samplePolicy.StartTime, samplePolicy.ExpiryTime, container);

                //NoStartTime
                Test.Assert(CommandAgent.SetAzureStorageContainerStoredAccessPolicy(container.Name, samplePolicy.PolicyName, null, null, null, true, false),
                            "Set stored access policy with -NoStartTime should succeed");
                Thread.Sleep(TimeSpan.FromSeconds(effectiveTime));
                SharedAccessBlobPolicies expectedPolicies = new SharedAccessBlobPolicies();
                expectedPolicies.Add(samplePolicy.PolicyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, samplePolicy.ExpiryTime, samplePolicy.Permission));
                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);
                SharedAccessBlobPolicy policy = Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, samplePolicy.ExpiryTime, samplePolicy.Permission);
                Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
                comp.Add(Utility.ConstructGetPolicyOutput <SharedAccessBlobPolicy>(policy, samplePolicy.PolicyName));
                CommandAgent.OutputValidation(comp);

                //NoExpiryTime
                Test.Assert(CommandAgent.SetAzureStorageContainerStoredAccessPolicy(container.Name, samplePolicy.PolicyName, null, null, null, false, true),
                            "Set stored access policy with -NoExpiryTime should succeed");
                Thread.Sleep(TimeSpan.FromSeconds(effectiveTime));
                expectedPolicies = new SharedAccessBlobPolicies();
                expectedPolicies.Add(samplePolicy.PolicyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, null, samplePolicy.Permission));
                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);
                policy = Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, null, samplePolicy.Permission);
                comp   = new Collection <Dictionary <string, object> >();
                comp.Add(Utility.ConstructGetPolicyOutput <SharedAccessBlobPolicy>(policy, samplePolicy.PolicyName));
                CommandAgent.OutputValidation(comp);

                //both
                Utility.ClearStoredAccessPolicy <CloudBlobContainer>(container);
                CreateStoredAccessPolicy(samplePolicy.PolicyName, samplePolicy.Permission, samplePolicy.StartTime, samplePolicy.ExpiryTime, container);

                Test.Assert(CommandAgent.SetAzureStorageContainerStoredAccessPolicy(container.Name, samplePolicy.PolicyName, null, null, null, true, true),
                            "Set stored access policy with both -NoStartTime and -NoExpiryTime should succeed");
                Thread.Sleep(TimeSpan.FromSeconds(effectiveTime));
                expectedPolicies = new SharedAccessBlobPolicies();
                expectedPolicies.Add(samplePolicy.PolicyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, null, samplePolicy.Permission));
                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);
                policy = Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(null, null, samplePolicy.Permission);
                comp   = new Collection <Dictionary <string, object> >();
                comp.Add(Utility.ConstructGetPolicyOutput <SharedAccessBlobPolicy>(policy, samplePolicy.PolicyName));
                CommandAgent.OutputValidation(comp);
            }
            finally
            {
                blobUtil.RemoveContainer(container);
            }
        }
Exemplo n.º 4
0
        internal void SetStoredAccessPolicyAndValidate(Utility.RawStoredAccessPolicy policy1, Utility.RawStoredAccessPolicy policy2, string containerName = null, bool ifCleanupPolicy = true, bool ifCleanupContainer = true)
        {
            CloudBlobContainer container = blobUtil.CreateContainer(containerName);

            if (ifCleanupPolicy)
            {
                Utility.ClearStoredAccessPolicy <CloudBlobContainer>(container);
            }

            policy2.PolicyName = policy1.PolicyName;

            try
            {
                CommandAgent.NewAzureStorageContainerStoredAccessPolicy(container.Name, policy1.PolicyName, policy1.Permission, policy1.StartTime, policy1.ExpiryTime);
                Test.Assert(CommandAgent.SetAzureStorageContainerStoredAccessPolicy(container.Name, policy2.PolicyName, policy2.Permission, policy2.StartTime, policy2.ExpiryTime),
                            "Set stored access policy in container should succeed");
                Test.Info("Set stored access policy:{0}", policy2.PolicyName);

                //get the policy and validate
                SharedAccessBlobPolicies expectedPolicies = new SharedAccessBlobPolicies();
                if (policy2.StartTime == null)
                {
                    policy2.StartTime = policy1.StartTime;
                }
                if (policy2.ExpiryTime == null)
                {
                    policy2.ExpiryTime = policy1.ExpiryTime;
                }
                if (policy2.Permission == null)
                {
                    policy2.Permission = policy1.Permission;
                }

                expectedPolicies.Add(policy2.PolicyName, Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(policy2.StartTime, policy2.ExpiryTime, policy2.Permission));
                Utility.WaitForPolicyBecomeValid <CloudBlobContainer>(container, policy2);
                Utility.ValidateStoredAccessPolicies <SharedAccessBlobPolicy>(container.GetPermissions().SharedAccessPolicies, expectedPolicies);

                //validate the output
                SharedAccessBlobPolicy policy = Utility.SetupSharedAccessPolicy <SharedAccessBlobPolicy>(policy2.StartTime, policy2.ExpiryTime, policy2.Permission);
                Collection <Dictionary <string, object> > comp = new Collection <Dictionary <string, object> >();
                comp.Add(Utility.ConstructGetPolicyOutput <SharedAccessBlobPolicy>(policy, policy2.PolicyName));
                CommandAgent.OutputValidation(comp);
            }
            finally
            {
                if (ifCleanupContainer)
                {
                    blobUtil.RemoveContainer(container);
                }
            }
        }
Exemplo n.º 5
0
        static SharedAccessBlobPolicies DeserializeAccessPolicies(XmlReader reader)
        {
            SharedAccessBlobPolicies ret = new SharedAccessBlobPolicies();
            string signedIdentifier      = "SignedIdentifier";
            string accessPolicy          = "AccessPolicy";
            string wrapper = "SignedIdentifiers";

            reader.Read(); //Advance past xml declaration element
            reader.Read();
            while (!reader.EOF)
            {
                if (reader.Name == wrapper && !reader.IsStartElement())
                {
                    break; //Reached the end of the file
                }
                while (reader.Name != signedIdentifier && !reader.EOF)
                {
                    reader.Read(); //Advance to the next policy
                }
                if (reader.EOF)
                {
                    break;
                }
                string         id     = "";
                DateTimeOffset?start  = new DateTimeOffset();
                DateTimeOffset?expiry = new DateTimeOffset();
                //Set permissions to None by default
                SharedAccessBlobPermissions permissionObj = 0;
                reader.Read(); //Go past start tag.
                // Keep reading until we reach the end of the identifier
                while (reader.Name != signedIdentifier)
                {
                    if (reader.Name == "Id")
                    {
                        reader.Read(); //Get to the value.
                        id = reader.Value;
                        reader.Read();
                    }
                    else if (reader.Name == accessPolicy)
                    {
                        reader.Read();
                        while (reader.Name != accessPolicy)
                        {
                            if (reader.Name == "Start")
                            {
                                reader.Read();
                                if (!string.IsNullOrWhiteSpace(reader.Value))
                                {
                                    start = DateTimeOffset.Parse(reader.Value);
                                }
                                else
                                {
                                    start = null;
                                }

                                reader.Read();
                            }
                            else if (reader.Name == "Expiry")
                            {
                                reader.Read();
                                if (!string.IsNullOrWhiteSpace(reader.Value))
                                {
                                    expiry = DateTimeOffset.Parse(reader.Value);
                                }
                                else
                                {
                                    expiry = null;
                                }
                                reader.Read();
                            }
                            else if (reader.Name == "Permission")
                            {
                                reader.Read();
                                string permissions = reader.Value;

                                if (permissions.Contains('r'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Read;
                                }
                                if (permissions.Contains('w'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Write;
                                }
                                if (permissions.Contains('d'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Delete;
                                }
                                if (permissions.Contains('l'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.List;
                                }

                                reader.Read();
                            }
                            reader.Read();
                        }
                    }
                    reader.Read();
                }
                SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime  = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions            = permissionObj
                };

                ret.Add(new KeyValuePair <string, SharedAccessBlobPolicy>(id, policy));
                reader.Read();
            }

            return(ret);
        }
Exemplo n.º 6
0
        static SharedAccessBlobPolicies DeserializeAccessPolicies(XmlReader reader)
        {
            SharedAccessBlobPolicies ret = new SharedAccessBlobPolicies();
            string signedIdentifier = "SignedIdentifier";
            string accessPolicy = "AccessPolicy";
            string wrapper = "SignedIdentifiers";
            reader.Read(); //Advance past xml declaration element
            reader.Read();
            while (!reader.EOF)
            {
                if (reader.Name == wrapper && !reader.IsStartElement())
                {
                    break; //Reached the end of the file
                }
                while (reader.Name != signedIdentifier && !reader.EOF)
                {
                    reader.Read(); //Advance to the next policy
                }
                if (reader.EOF)
                {
                    break;
                }
                string id = "";
                DateTimeOffset? start = null;
                DateTimeOffset? expiry = null;
                //Set permissions to None by default
                SharedAccessBlobPermissions permissionObj = 0;
                reader.Read(); //Go past start tag.
                // Keep reading until we reach the end of the identifier
                while (reader.Name != signedIdentifier)
                {
                    if (reader.Name == "Id")
                    {
                        reader.Read(); //Get to the value.
                        id = reader.Value;
                        reader.Read();
                    }
                    else if (reader.Name == accessPolicy)
                    {
                        reader.Read();
                        while (reader.Name != accessPolicy)
                        {
                            if (reader.Name == "Start")
                            {
                                reader.Read();
                                if (!string.IsNullOrWhiteSpace(reader.Value))
                                {
                                    start = DateTimeOffset.Parse(reader.Value);
                                }
                                else
                                {
                                    start = null;
                                }
                                
                                reader.Read();
                            }
                            else if (reader.Name == "Expiry")
                            {
                                reader.Read();
                                if (!string.IsNullOrWhiteSpace(reader.Value))
                                {
                                    expiry = DateTimeOffset.Parse(reader.Value);
                                }
                                else
                                {
                                    expiry = null;
                                }
                                reader.Read();
                            }
                            else if (reader.Name == "Permission")
                            {
                                reader.Read();
                                string permissions = reader.Value;

                                if (permissions.Contains('r'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Read;
                                }
                                if (permissions.Contains('w'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Write;
                                }
                                if (permissions.Contains('d'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.Delete;
                                }
                                if (permissions.Contains('l'))
                                {
                                    permissionObj |= SharedAccessBlobPermissions.List;
                                }

                                reader.Read();
                            }
                            reader.Read();
                        }
                    }
                    reader.Read();
                }
                SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy()
                {
                    SharedAccessStartTime = start,
                    SharedAccessExpiryTime = expiry,
                    Permissions = permissionObj
                };

                ret.Add(new KeyValuePair<string, SharedAccessBlobPolicy>(id, policy));
                reader.Read();
            }

            return ret;
        }
        private void SavePolicies_Click(object sender, RoutedEventArgs e)
        {
            SharedAccessBlobPolicies policies = new SharedAccessBlobPolicies();

            if (PolicyViews != null)
            {
                foreach (PolicyView policyView in PolicyViews)
                {
                    if (!String.IsNullOrEmpty(policyView.PolicyName))
                    {
                        if (!policyView.Validate())
                        {
                            MessageBox.Show("Policy '" + policyView.PolicyName + "' has one or more fields that are not formatted properly.", "Validation Error", MessageBoxButton.OK);
                            return;
                        }
                        policies.Add(policyView.PolicyName, policyView.Policy);
                    }
                }
            }

            try
            {
                Cursor = Cursors.Wait;
                ViewModel.SetContainerAccessPolicies(containerName, policies);
                MessageBox.Show("Container policies have been saved.", "Save Complete", MessageBoxButton.OK);
            }
            catch (Exception ex)
            {
                MessageBox.Show("Saving policies failed due to an error.\r\n\r\n" + ex.ToString(), "Save Error", MessageBoxButton.OK);
            }
            finally
            {
                Cursor = Cursors.Arrow;
            }
        }
        public SharedAccessBlobPolicies GetContainerAccessPolicies(string containerName)
        {
            SharedAccessBlobPolicies policies = new SharedAccessBlobPolicies();

            CloudBlobClient client = CloudStorageAccount.CreateCloudBlobClient();
            client.RetryPolicy = new LinearRetry(TimeSpan.Zero, 20);
            //client.RetryPolicy = RetryPolicies.Retry(20, TimeSpan.Zero);
            CloudBlobContainer container = client.GetContainerReference(containerName);
            BlobContainerPermissions permissions = container.GetPermissions();

            if (permissions != null)
            {
                foreach (KeyValuePair<string, SharedAccessBlobPolicy> policy in permissions.SharedAccessPolicies)
                {
                    policies.Add(policy.Key, policy.Value);
                }
            }

            return policies;
        }