private bool ShallBeRemoved(PSKeyVaultAccessPolicy ap, string objectId, Guid? applicationId) { // If both object id and application id are specified, remove the compound identity policy only. // If only object id is specified, remove all policies refer to the object id including the compound identity policies. var sameObjectId = string.Equals(ap.ObjectId, objectId, StringComparison.OrdinalIgnoreCase); return applicationId.HasValue ? (ap.ApplicationId == applicationId && sameObjectId) : sameObjectId; }
private bool MatchVaultAccessPolicyIdentity(PSKeyVaultAccessPolicy ap, string objectId, Guid?applicationId) { return(ap.ApplicationId == applicationId && string.Equals(ap.ObjectId, objectId, StringComparison.OrdinalIgnoreCase)); }
public override void ExecuteCmdlet() { if (InputObject != null) { VaultName = InputObject.VaultName; ResourceGroupName = InputObject.ResourceGroupName; } else if (ResourceId != null) { var resourceIdentifier = new ResourceIdentifier(ResourceId); VaultName = resourceIdentifier.ResourceName; ResourceGroupName = resourceIdentifier.ResourceGroupName; } if (ShouldProcess(VaultName, Properties.Resources.SetVaultAccessPolicy)) { if (ParameterSetName == ForVault && !EnabledForDeployment.IsPresent && !EnabledForTemplateDeployment.IsPresent && !EnabledForDiskEncryption.IsPresent) { throw new ArgumentException(Resources.VaultPermissionFlagMissing); } ResourceGroupName = string.IsNullOrWhiteSpace(ResourceGroupName) ? GetResourceGroupName(VaultName) : ResourceGroupName; if (ResourceGroupName == null) { throw new ArgumentException(string.Format(Resources.VaultDoesNotExist, VaultName)); } PSKeyVault vault = null; // Get the vault to be updated if (!string.IsNullOrWhiteSpace(ResourceGroupName)) { vault = KeyVaultManagementClient.GetVault( VaultName, ResourceGroupName); } if (vault == null) { throw new ArgumentException(string.Format(Resources.VaultNotFound, VaultName, ResourceGroupName)); } if (!string.IsNullOrWhiteSpace(this.ObjectId) && !this.IsValidObjectIdSyntax(this.ObjectId)) { throw new ArgumentException(Resources.InvalidObjectIdSyntax); } // Update vault policies PSKeyVaultAccessPolicy[] updatedListOfAccessPolicies = vault.AccessPolicies; if (!string.IsNullOrEmpty(UserPrincipalName) || !string.IsNullOrEmpty(ServicePrincipalName) || !string.IsNullOrWhiteSpace(this.ObjectId) || !string.IsNullOrWhiteSpace(this.EmailAddress)) { var objId = this.ObjectId; if (!this.BypassObjectIdValidation.IsPresent && ActiveDirectoryClient != null) { objId = GetObjectId(this.ObjectId, this.UserPrincipalName, this.EmailAddress, this.ServicePrincipalName); } else if (ActiveDirectoryClient == null && objId == null) { throw new Exception(Resources.ActiveDirectoryClientNull); } if (ApplicationId.HasValue && ApplicationId.Value == Guid.Empty) { throw new ArgumentException(Resources.InvalidApplicationId); } //All permission arrays cannot be null if (PermissionsToKeys == null && PermissionsToSecrets == null && PermissionsToCertificates == null && PermissionsToStorage == null) { throw new ArgumentException(Resources.PermissionsNotSpecified); } else { //Is there an existing policy for this policy identity? var existingPolicy = vault.AccessPolicies.FirstOrDefault(ap => MatchVaultAccessPolicyIdentity(ap, objId, ApplicationId)); //New policy will have permission arrays that are either from cmdlet input //or if that's null, then from the old policy for this object ID if one existed var keys = PermissionsToKeys ?? (existingPolicy != null && existingPolicy.PermissionsToKeys != null ? existingPolicy.PermissionsToKeys.ToArray() : null); var secrets = PermissionsToSecrets ?? (existingPolicy != null && existingPolicy.PermissionsToSecrets != null ? existingPolicy.PermissionsToSecrets.ToArray() : null); var certificates = PermissionsToCertificates ?? (existingPolicy != null && existingPolicy.PermissionsToCertificates != null ? existingPolicy.PermissionsToCertificates.ToArray() : null); var managedStorage = PermissionsToStorage ?? (existingPolicy != null && existingPolicy.PermissionsToStorage != null ? existingPolicy.PermissionsToStorage.ToArray() : null); //Remove old policies for this policy identity and add a new one with the right permissions, iff there were some non-empty permissions updatedListOfAccessPolicies = vault.AccessPolicies.Where(ap => !MatchVaultAccessPolicyIdentity(ap, objId, this.ApplicationId)).ToArray(); if ((keys != null && keys.Length > 0) || (secrets != null && secrets.Length > 0) || (certificates != null && certificates.Length > 0) || (managedStorage != null && managedStorage.Length > 0)) { var policy = new PSKeyVaultAccessPolicy(vault.TenantId, objId, this.ApplicationId, keys, secrets, certificates, managedStorage); updatedListOfAccessPolicies = updatedListOfAccessPolicies.Concat(new[] { policy }).ToArray(); } } } // Update the vault var updatedVault = KeyVaultManagementClient.UpdateVault( vault, updatedListOfAccessPolicies, EnabledForDeployment.IsPresent ? true : vault.EnabledForDeployment, EnabledForTemplateDeployment.IsPresent ? true : vault.EnabledForTemplateDeployment, EnabledForDiskEncryption.IsPresent ? true : vault.EnabledForDiskEncryption, vault.EnableSoftDelete, vault.EnablePurgeProtection, vault.NetworkAcls, ActiveDirectoryClient); if (PassThru.IsPresent) { WriteObject(updatedVault); } } }