internal string CreateAzureContainerStoredAccessPolicy(IStorageBlobManagement localChannel, string containerName, string policyName, DateTime?startTime, DateTime?expiryTime, string permission) { if (!NameUtil.IsValidStoredAccessPolicyName(policyName)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName)); } //Get existing permissions CloudBlobContainer container = localChannel.GetContainerReference(containerName); BlobContainerPermissions blobContainerPermissions = localChannel.GetContainerPermissions(container); //Add new policy if (blobContainerPermissions.SharedAccessPolicies.Keys.Contains(policyName)) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName)); } SharedAccessBlobPolicy policy = new SharedAccessBlobPolicy(); AccessPolicyHelper.SetupAccessPolicy <SharedAccessBlobPolicy>(policy, startTime, expiryTime, permission); blobContainerPermissions.SharedAccessPolicies.Add(policyName, policy); //Set permissions back to container localChannel.SetContainerPermissions(container, blobContainerPermissions); return(policyName); }
internal string CreateAzureTableStoredAccessPolicy(IStorageTableManagement localChannel, string tableName, string policyName, DateTime?startTime, DateTime?expiryTime, string permission) { if (!NameUtil.IsValidStoredAccessPolicyName(policyName)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName)); } //Get existing permissions CloudTable table = localChannel.GetTableReference(tableName); TablePermissions tablePermissions = localChannel.GetTablePermissions(table, null, TableOperationContext); //Add new policy if (tablePermissions.SharedAccessPolicies.Keys.Contains(policyName)) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName)); } SharedAccessTablePolicy policy = new SharedAccessTablePolicy(); AccessPolicyHelper.SetupAccessPolicy <SharedAccessTablePolicy>(policy, startTime, expiryTime, permission); tablePermissions.SharedAccessPolicies.Add(policyName, policy); //Set permissions back to table localChannel.SetTablePermissions(table, tablePermissions, null, TableOperationContext); return(policyName); }
public override void ExecuteCmdlet() { if (String.IsNullOrEmpty(ShareName) || String.IsNullOrEmpty(Policy)) { return; } NamingUtil.ValidateShareName(this.ShareName, false); if (!NameUtil.IsValidStoredAccessPolicyName(this.Policy)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, this.Policy)); } //Get existing permissions CloudFileShare fileShare = this.Channel.GetShareReference(this.ShareName); FileSharePermissions fileSharePermissions = fileShare.GetPermissions(); //Add new policy if (fileSharePermissions.SharedAccessPolicies.Keys.Contains(this.Policy)) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, this.Policy)); } SharedAccessFilePolicy policy = new SharedAccessFilePolicy(); AccessPolicyHelper.SetupAccessPolicy <SharedAccessFilePolicy>(policy, this.StartTime, this.ExpiryTime, this.Permission); fileSharePermissions.SharedAccessPolicies.Add(this.Policy, policy); //Set permissions back to container fileShare.SetPermissions(fileSharePermissions); WriteObject(Policy); }
internal string CreateAzureQueueStoredAccessPolicy(IStorageQueueManagement localChannel, string queueName, string policyName, DateTime?startTime, DateTime?expiryTime, string permission) { if (!NameUtil.IsValidStoredAccessPolicyName(policyName)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName)); } //Get existing permissions CloudQueue queue = Channel.GetQueueReference(queueName); QueuePermissions queuePermissions = localChannel.GetPermissions(queue); //Add new policy if (queuePermissions.SharedAccessPolicies.Keys.Contains(policyName)) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName)); } SharedAccessQueuePolicy policy = new SharedAccessQueuePolicy(); AccessPolicyHelper.SetupAccessPolicy <SharedAccessQueuePolicy>(policy, startTime, expiryTime, permission); queuePermissions.SharedAccessPolicies.Add(policyName, policy); //Set permissions back to queue localChannel.SetPermissions(queue, queuePermissions); return(policyName); }
public override void ExecuteCmdlet() { if (String.IsNullOrEmpty(ShareName) || String.IsNullOrEmpty(Policy)) { return; } NamingUtil.ValidateShareName(this.ShareName, false); if (!NameUtil.IsValidStoredAccessPolicyName(this.Policy)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, this.Policy)); } //Get existing permissions CloudFileShare fileShare = this.Channel.GetShareReference(this.ShareName); FileSharePermissions fileSharePermissions; try { fileSharePermissions = fileShare.GetPermissionsAsync().Result; } catch (AggregateException e) when(e.InnerException is StorageException) { throw e.InnerException; } //Add new policy if (fileSharePermissions.SharedAccessPolicies.Keys.Contains(this.Policy)) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, this.Policy)); } SharedAccessFilePolicy policy = new SharedAccessFilePolicy(); AccessPolicyHelper.SetupAccessPolicy <SharedAccessFilePolicy>(policy, this.StartTime, this.ExpiryTime, this.Permission); fileSharePermissions.SharedAccessPolicies.Add(this.Policy, policy); //Set permissions back to container try { Task.Run(() => fileShare.SetPermissionsAsync(fileSharePermissions, null, null, OperationContext)).Wait(); } catch (AggregateException e) when(e.InnerException is StorageException) { throw e.InnerException; } WriteObject(Policy); }
internal string CreateAzureContainerStoredAccessPolicy(IStorageBlobManagement localChannel, string containerName, string policyName, DateTime?startTime, DateTime?expiryTime, string permission) { if (!NameUtil.IsValidStoredAccessPolicyName(policyName)) { throw new ArgumentException(String.Format(CultureInfo.CurrentCulture, Resources.InvalidAccessPolicyName, policyName)); } //Get container instance, Get existing permissions CloudBlobContainer container_Track1 = Channel.GetContainerReference(containerName); BlobContainerClient container = AzureStorageContainer.GetTrack2BlobContainerClient(container_Track1, Channel.StorageContext, ClientOptions); BlobContainerAccessPolicy accessPolicy = container.GetAccessPolicy(cancellationToken: CmdletCancellationToken).Value; IEnumerable <BlobSignedIdentifier> signedIdentifiers = accessPolicy.SignedIdentifiers; //Add new policy foreach (BlobSignedIdentifier identifier in signedIdentifiers) { if (identifier.Id == policyName) { throw new ResourceAlreadyExistException(String.Format(CultureInfo.CurrentCulture, Resources.PolicyAlreadyExists, policyName)); } } BlobSignedIdentifier signedIdentifier = new BlobSignedIdentifier(); signedIdentifier.Id = policyName; signedIdentifier.AccessPolicy = new BlobAccessPolicy(); if (StartTime != null) { signedIdentifier.AccessPolicy.PolicyStartsOn = StartTime.Value.ToUniversalTime(); } if (ExpiryTime != null) { signedIdentifier.AccessPolicy.PolicyExpiresOn = ExpiryTime.Value.ToUniversalTime(); } signedIdentifier.AccessPolicy.Permissions = AccessPolicyHelper.OrderBlobPermission(this.Permission); var newsignedIdentifiers = new List <BlobSignedIdentifier>(signedIdentifiers); newsignedIdentifiers.Add(signedIdentifier); //Set permissions back to container container.SetAccessPolicy(accessPolicy.BlobPublicAccess, newsignedIdentifiers, BlobRequestConditions, CmdletCancellationToken); return(policyName); }