Azure / Azure/azure-powershell
New-AzKeyVault fails when built-in 'Key vaults should have soft delete enabled' Azure Policy is applied with 'Deny' effect
- Dominant language
- C#
- Stars
- 4.8k
- Forks
- 4.3k
- Avg merge
- 2d 17h
- Merged PRs (30d)
- 51
Description
## Description
The `EnableSoftDelete` parameter in `New-AzKeyVault` was [removed as of Azure PowerShell 5.0](https://docs.microsoft.com/en-us/powershell/azure/release-notes-azureps?view=azps-6.4.0#500---october-2020) (Oct 2020) with the intention that soft delete would be enabled by default for all new Key Vaults.
Thus, it should be safe to apply an Azure Policy that rejects any ARM operations that would provision a new Key Vault without soft delete enabled. However, this is not the case and users are prevented from creating new Key Vaults, even when the operation would result in soft delete enabled due to the default behavior of the Key Vault resource provider.
This prevents our organization from properly enforcing that Key Vaults are only created with soft delete enabled.
## Steps to reproduce
1. Assign the built-in Azure Policy 'Key vaults should have soft delete enabled' (Definition ID: `/providers/Microsoft.Authorization/policyDefinitions/1e66c121-a66a-4b1f-9b83-0fd99bf0fc2d`) to a subscription with the `effect` parameter set to `Deny`. This should prevent new Key Vaults that don't have soft-delete enabled from being created.

2. Attempt to create a new Key Vault with Az.KeyVault version 3.5:
```powershell
PS > New-AzKeyVault -Name "" -ResourceGroupName "" -Location "West US 3"
New-AzKeyVault : Resource '' was disallowed by policy. Policy identifiers:
'[{"policyAssignment":{"name":"Key vaults should have soft delete enabled","id":"/providers/Microsoft.Management/manage
mentGroups//providers/Microsoft.Authorization/policyAssignments/"},"policyDefinition":{"name":"Key vaults should have soft delete enabled","id":"/providers/Microsoft.Authorization/policyDefinitions/"}}]'.
At line:1 char:1
+ New-AzKeyVault -Name "" -ResourceGroupName " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzKeyVault], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.NewAzureKeyVault
```
## Notes
Note also that this version of PowerShell does not support explicitly setting the soft delete property:
```powershell
PS C:\Users\pshelton> New-AzKeyVault -Name "" -ResourceGroupName "" -Location "West US 3" -EnableSoftDelete $true
New-AzKeyVault : A parameter cannot be found that matches parameter name 'EnableSoftDelete'.
```
`New-AzureRmKeyVault` is properly blocked when the `EnableSoftDelete` flag is not supplied:
```powershell
PS > New-AzureRmKeyVault -Name "" -ResourceGroupName "" -Location "West US 3"
New-AzureRmKeyVault : Resource '' was disallowed by policy. Policy identifiers:
'[{"policyAssignment":{"name":"Key vaults should have soft delete enabled","id":"/providers/Microsoft.Management/manage
mentGroups//providers/Microsoft.Authorization/policyAssignments/"},"policyDefinition":{"name":"Key vaults should have soft delete enabled","id":"/providers/Microsoft.Authorization/policyDefinitions/"}}]'.
At line:1 char:1
+ New-AzureRmKeyVault -Name "" -ResourceGroupName " ...
+ ~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~~
+ CategoryInfo : CloseError: (:) [New-AzureRmKeyVault], CloudException
+ FullyQualifiedErrorId : Microsoft.Azure.Commands.KeyVault.NewAzureKeyVault
````
but the request is properly allowed when the flag is supplied as expected:
```powershell
PS C:\Users\pshelton> New-AzureRmKeyVault -Name "" -ResourceGroupName "" -Location "West US 3" -EnableSoftDelete
VaultUri : https://.vault.azure.net
TenantId :
TenantName :
Sku : Standard
EnabledForDeployment : False
EnabledForTemplateDeployment : False
EnabledForDiskEncryption : False
EnableSoftDelete : True
```
## Environment data
```
Name Value
---- -----
PSVersion 5.1.19041.1237
PSEdition Desktop
PSCompatibleVersions {1.0, 2.0, 3.0, 4.0...}
BuildVersion 10.0.19041.1237
CLRVersion 4.0.30319.42000
WSManStackVersion 3.0
PSRemotingProtocolVersion 2.3
SerializationVersion 1.1.0.1
```
## Module versions
```powershell
PS C:\Users\pshelton> Get-Module -ListAvailable
Directory: C:\Program Files\WindowsPowerShell\Modules
ModuleType Version Name ExportedCommands
---------- ------- ---- ----------------
Script 2.5.3 Az.Accounts {Disable-AzDataCollection, Disable-AzContextAutosave, Enab...
Script 1.1.1 Az.Advisor {Get-AzAdvisorRecommendation, Enable-AzAdvisorRecommendati...
Script 2.4.0 Az.Aks {Get-AzAksCluster, New-AzAksCluster, Remove-AzAksCluster, ...
Script 1.1.4 Az.AnalysisServices {Resume-AzAnalysisServicesServer, Suspend-AzAnalysisServic...
Script 2.3.0 Az.ApiManagement {Add-AzApiManagementApiToGateway, Add-AzApiManagementApiTo...
Script 1.0.0 Az.AppConfiguration {Get-AzAppConfigurationStore, Get-AzAppConfigurationStoreK...
Script 1.2.0 Az.ApplicationInsights {Get-AzApplicationInsights, New-AzApplicationInsights, Rem...
Script 1.7.1 Az.Automation {Get-AzAutomationHybridWorkerGroup, Remove-AzAutomationHyb...
Script 3.1.0 Az.Batch {Remove-AzBatchAccount, Get-AzBatchAccount, Get-AzBatchAcc...
Script 2.0.0 Az.Billing {Get-AzBillingInvoice, Get-AzBillingPeriod, Get-AzEnrollme...
Script 1.8.0 Az.Cdn {Get-AzCdnProfile, Get-AzCdnProfileSsoUrl, New-AzCdnProfil...
Script 1.9.0 Az.CognitiveServices {Get-AzCognitiveServicesAccount, Get-AzCognitiveServicesAc...
Script 4.17.0 Az.Compute {Remove-AzAvailabilitySet, Get-AzAvailabilitySet, New-AzAv...
Script 2.1.0 Az.ContainerInstance {Add-AzContainerInstanceOutput, Get-AzContainerGroup, Get-...
Script 2.2.3 Az.ContainerRegistry {New-AzContainerRegistry, Get-AzContainerRegistry, Update-...
Script 1.3.1 Az.CosmosDB {Get-AzCosmosDBSqlContainer, Get-AzCosmosDBSqlContainerThr...
Script 1.1.0 Az.DataBoxEdge {Get-AzDataBoxEdgeJob, Get-AzDataBoxEdgeDevice, Invoke-AzD...
Script 1.1.0 Az.Databricks {Get-AzDatabricksVNetPeering, Get-AzDatabricksWorkspace, N...
Script 1.14.0 Az.DataFactory {Set-AzDataFactoryV2, Update-AzDataFactoryV2, Get-AzDataFa...
Script 1.0.2 Az.DataLakeAnalytics {Get-AzDataLakeAnalyticsDataSource, New-AzDataLakeAnalytic...
Script 1.3.0 Az.DataLakeStore {Get-AzDataLakeStoreTrustedIdProvider, Remove-AzDataLakeSt...
Script 1.0.0 Az.DataShare {New-AzDataShareAccount, Get-AzDataShareAccount, Remove-Az...
Script 1.1.0 Az.DeploymentManager {Get-AzDeploymentManagerArtifactSource, New-AzDeploymentMa...
Script 3.0.0 Az.DesktopVirtualization {Disconnect-AzWvdUserSession, Expand-AzWvdMsixImage, Get-A...
Script 1.0.2 Az.DevTestLabs {Get-AzDtlAllowedVMSizesPolicy, Get-AzDtlAutoShutdownPolic...
Script 1.1.2 Az.Dns {Get-AzDnsRecordSet, New-AzDnsRecordConfig, Remove-AzDnsRe...
Script 1.3.0 Az.EventGrid {New-AzEventGridTopic, Get-AzEventGridTopic, Set-AzEventGr...
Script 1.8.0 Az.EventHub {New-AzEventHubNamespace, Get-AzEventHubNamespace, Set-AzE...
Script 1.8.0 Az.FrontDoor {New-AzFrontDoor, Get-AzFrontDoor, Set-AzFrontDoor, Remove...
Script 3.1.0 Az.Functions {Get-AzFunctionApp, Get-AzFunctionAppAvailableLocation, Ge...
Script 4.3.0 Az.HDInsight {Get-AzHDInsightJob, New-AzHDInsightSqoopJobDefinition, Wa...
Script 1.3.1 Az.HealthcareApis {New-AzHealthcareApisService, Remove-AzHealthcareApisServi...
Script 2.7.3 Az.IotHub {Add-AzIotHubKey, Get-AzIotHubEventHubConsumerGroup, Get-A...
Script 3.5.0 Az.KeyVault {Add-AzKeyVaultCertificate, Update-AzKeyVaultCertificate, ...
Script 2.0.0 Az.Kusto {Add-AzKustoClusterLanguageExtension, Add-AzKustoDatabaseP...
Script 1.5.0 Az.LogicApp {Get-AzIntegrationAccountAgreement, Get-AzIntegrationAccou...
Script 1.1.3 Az.MachineLearning {Move-AzMlCommitmentAssociation, Get-AzMlCommitmentAssocia...
Script 1.1.1 Az.Maintenance {Get-AzApplyUpdate, Get-AzConfigurationAssignment, Get-AzM...
Script 2.0.0 Az.ManagedServices {Get-AzManagedServicesAssignment, New-AzManagedServicesAss...
Script 1.0.2 Az.MarketplaceOrdering {Get-AzMarketplaceTerms, Set-AzMarketplaceTerms}
Script 1.1.1 Az.Media {Sync-AzMediaServiceStorageKey, Set-AzMediaServiceKey, Get...
Script 1.1.1 Az.Migrate {Get-AzMigrateDiscoveredServer, Get-AzMigrateJob, Get-AzMi...
Script 2.7.0 Az.Monitor {Get-AzMetricDefinition, Get-AzMetric, Remove-AzLogProfile...
Script 4.11.0 Az.Network {Add-AzApplicationGatewayAuthenticationCertificate, Get-Az...
Script 1.1.1 Az.NotificationHubs {Get-AzNotificationHub, Get-AzNotificationHubAuthorization...
Script 2.3.0 Az.OperationalInsights {New-AzOperationalInsightsAzureActivityLogDataSource, New-...
Script 1.4.1 Az.PolicyInsights {Get-AzPolicyEvent, Get-AzPolicyState, Get-AzPolicyStateSu...
Script 1.1.2 Az.PowerBIEmbedded {Remove-AzPowerBIWorkspaceCollection, Get-AzPowerBIWorkspa...
Script 1.0.3 Az.PrivateDns {Get-AzPrivateDnsZone, Remove-AzPrivateDnsZone, Set-AzPriv...
Script 4.6.0 Az.RecoveryServices {Get-AzRecoveryServicesBackupProperty, Get-AzRecoveryServi...
Script 1.5.0 Az.RedisCache {Remove-AzRedisCachePatchSchedule, New-AzRedisCacheSchedul...
Script 1.0.0 Az.RedisEnterpriseCache {Export-AzRedisEnterpriseCache, Get-AzRedisEnterpriseCache...
Script 1.0.3 Az.Relay {New-AzRelayNamespace, Get-AzRelayNamespace, Set-AzRelayNa...
Script 1.0.0 Az.ResourceMover {Add-AzResourceMoverMoveResource, Get-AzResourceMoverMoveC...
Script 4.3.1 Az.Resources {Get-AzProviderOperation, Remove-AzRoleAssignment, Get-AzR...
Script 1.0.0 Az.Security {Get-AzSecurityAlert, Set-AzSecurityAlert, Get-AzSecurityA...
Script 1.1.0 Az.SecurityInsights {Get-AzSentinelAlertRuleAction, New-AzSentinelAlertRuleAct...
Script 1.5.0 Az.ServiceBus {New-AzServiceBusNamespace, Get-AzServiceBusNamespace, Set...
Script 3.0.1 Az.ServiceFabric {Add-AzServiceFabricClientCertificate, Add-AzServiceFabric...
Script 1.3.0 Az.SignalR {New-AzSignalR, Get-AzSignalR, Get-AzSignalRKey, New-AzSig...
Script 3.5.0 Az.Sql {Get-AzSqlDatabaseTransparentDataEncryption, Get-AzSqlData...
Script 1.1.0 Az.SqlVirtualMachine {New-AzSqlVM, Get-AzSqlVM, Update-AzSqlVM, Remove-AzSqlVM...}
Script 3.11.0 Az.Storage {Get-AzStorageAccount, Get-AzStorageAccountKey, New-AzStor...
Script 1.6.0 Az.StorageSync {Invoke-AzStorageSyncCompatibilityCheck, New-AzStorageSync...
Script 2.0.0 Az.StreamAnalytics {Get-AzStreamAnalyticsCluster, Get-AzStreamAnalyticsCluste...
Script 1.0.0 Az.Support {Get-AzSupportService, Get-AzSupportProblemClassification,...
Script 1.0.4 Az.TrafficManager {Add-AzTrafficManagerCustomHeaderToEndpoint, Remove-AzTraf...
Script 2.8.2 Az.Websites {Get-AzAppServicePlan, Set-AzAppServicePlan, New-AzAppServ...
Script 5.1.2 Azure {Get-AzureAutomationCertificate, Get-AzureAutomationConnec...
Manifest 0.5.0 Azure.AnalysisServices {Add-AzureAnalysisServicesAccount, Restart-AzureAnalysisSe...
Script 4.2.1 Azure.Storage {Get-AzureStorageTable, New-AzureStorageTableSASToken, New...
Binary 2.0.2.140 AzureAD {Add-AzureADApplicationOwner, Get-AzureADApplication, Get-...
Script 5.7.0 AzureRM
Script 0.6.6 AzureRM.AnalysisServices {Resume-AzureRmAnalysisServicesServer, Suspend-AzureRmAnal...
Script 5.1.2 AzureRM.ApiManagement {Add-AzureRmApiManagementRegion, Get-AzureRmApiManagementS...
Script 0.1.3 AzureRM.ApplicationInsights {Get-AzureRmApplicationInsights, New-AzureRmApplicationIns...
Script 4.3.2 AzureRM.Automation {Get-AzureRMAutomationHybridWorkerGroup, Get-AzureRmAutoma...
Script 4.0.4 AzureRM.Backup {Backup-AzureRmBackupItem, Enable-AzureRmBackupContainerRe...
Script 4.0.6 AzureRM.Batch {Remove-AzureRmBatchAccount, Get-AzureRmBatchAccount, Get-...
Script 0.14.1 AzureRM.Billing {Get-AzureRmBillingInvoice, Get-AzureRmBillingPeriod, Get-...
Script 4.2.2 AzureRM.Cdn {Get-AzureRmCdnProfile, Get-AzureRmCdnProfileSsoUrl, New-A...
Script 0.9.4 AzureRM.CognitiveServices {Get-AzureRmCognitiveServicesAccount, Get-AzureRmCognitive...
Script 4.6.0 AzureRM.Compute {Remove-AzureRmAvailabilitySet, Get-AzureRmAvailabilitySet...
Script 0.3.1 AzureRM.Consumption Get-AzureRmConsumptionUsageDetail
Script 0.2.5 AzureRM.ContainerInstance {New-AzureRmContainerGroup, Get-AzureRmContainerGroup, Rem...
Script 1.0.4 AzureRM.ContainerRegistry {New-AzureRmContainerRegistry, Get-AzureRmContainerRegistr...
Script 4.2.2 AzureRM.DataFactories {Remove-AzureRmDataFactory, Get-AzureRmDataFactoryRun, Get...
Script 0.5.3 AzureRM.DataFactoryV2 {Set-AzureRmDataFactoryV2, Update-AzureRmDataFactoryV2, Ge...
Script 4.2.3 AzureRM.DataLakeAnalytics {Get-AzureRmDataLakeAnalyticsDataSource, New-AzureRmDataLa...
Script 5.2.0 AzureRM.DataLakeStore {Get-AzureRmDataLakeStoreTrustedIdProvider, Remove-AzureRm...
Script 4.0.4 AzureRM.DevTestLabs {Get-AzureRmDtlAllowedVMSizesPolicy, Get-AzureRmDtlAutoShu...
Script 4.1.2 AzureRM.Dns {Get-AzureRmDnsRecordSet, New-AzureRmDnsRecordConfig, Remo...
Script 0.3.2 AzureRM.EventGrid {New-AzureRmEventGridTopic, Get-AzureRmEventGridTopic, Set...
Script 0.6.3 AzureRM.EventHub {New-AzureRmEventHubNamespace, Get-AzureRmEventHubNamespac...
Script 4.1.2 AzureRM.HDInsight {Get-AzureRmHDInsightJob, New-AzureRmHDInsightSqoopJobDefi...
Script 4.0.4 AzureRM.Insights {Get-AzureRmMetricDefinition, Get-AzureRmMetric, Remove-Az...
Script 3.1.2 AzureRM.IotHub {Add-AzureRmIotHubKey, Get-AzureRmIotHubEventHubConsumerGr...
Script 4.3.0 AzureRM.KeyVault {Add-AzureKeyVaultCertificate, Set-AzureKeyVaultCertificat...
Script 4.0.3 AzureRM.LogicApp {Get-AzureRmIntegrationAccountAgreement, Get-AzureRmIntegr...
Script 0.17.2 AzureRM.MachineLearning {Move-AzureRmMlCommitmentAssociation, Get-AzureRmMlCommitm...
Script 0.4.2 AzureRM.MachineLearningCompute {Get-AzureRmMlOpCluster, Get-AzureRmMlOpClusterKey, Test-A...
Script 0.2.1 AzureRM.MarketplaceOrdering {Get-AzureRmMarketplaceTerms, Set-AzureRmMarketplaceTerms}
Script 0.9.2 AzureRM.Media {Sync-AzureRmMediaServiceStorageKeys, Set-AzureRmMediaServ...
Script 5.4.2 AzureRM.Network {Add-AzureRmApplicationGatewayAuthenticationCertificate, G...
Script 4.1.1 AzureRM.NotificationHubs {Get-AzureRmNotificationHub, Get-AzureRmNotificationHubAut...
Script 4.3.2 AzureRM.OperationalInsights {New-AzureRmOperationalInsightsAzureActivityLogDataSource,...
Script 4.1.4 AzureRM.PowerBIEmbedded {Remove-AzureRmPowerBIWorkspaceCollection, Get-AzureRmPowe...
Script 4.6.0 AzureRM.Profile {Disable-AzureRmDataCollection, Disable-AzureRmContextAuto...
Script 4.1.2 AzureRM.RecoveryServices {Get-AzureRmRecoveryServicesBackupProperty, Get-AzureRmRec...
Script 4.1.2 AzureRM.RecoveryServices.Backup {Backup-AzureRmRecoveryServicesBackupItem, Get-AzureRmReco...
Script 0.2.4 AzureRM.RecoveryServices.SiteRec... {Edit-AzureRmRecoveryServicesAsrRecoveryPlan, Get-AzureRmR...
Script 4.1.2 AzureRM.RedisCache {Remove-AzureRmRedisCachePatchSchedule, New-AzureRmRedisCa...
Script 0.3.3 AzureRM.Relay {New-AzureRmRelayNamespace, Get-AzureRmRelayNamespace, Set...
Script 5.5.2 AzureRM.Resources {Get-AzureRmProviderOperation, Remove-AzureRmRoleAssignmen...
Script 0.16.3 AzureRM.Scheduler {Disable-AzureRmSchedulerJobCollection, Enable-AzureRmSche...
Script 4.1.2 AzureRM.ServerManagement {Invoke-AzureRmServerManagementPowerShellCommand, Get-Azur...
Script 0.6.4 AzureRM.ServiceBus {New-AzureRmServiceBusNamespace, Get-AzureRmServiceBusName...
Script 0.3.4 AzureRM.ServiceFabric {Add-AzureRmServiceFabricApplicationCertificate, Add-Azure...
Script 5.0.6 AzureRM.SiteRecovery {Get-AzureRmSiteRecoveryFabric, New-AzureRmSiteRecoveryFab...
Script 4.4.0 AzureRM.Sql {Get-AzureRmSqlDatabaseTransparentDataEncryption, Get-Azur...
Script 4.2.3 AzureRM.Storage {Get-AzureRmStorageAccount, Get-AzureRmStorageAccountKey, ...
Script 4.0.4 AzureRM.StreamAnalytics {Get-AzureRmStreamAnalyticsFunction, Get-AzureRmStreamAnal...
Script 4.0.1 AzureRM.Tags {Remove-AzureRmTag, Get-AzureRmTag, New-AzureRmTag}
Script 4.0.3 AzureRM.TrafficManager {Disable-AzureRmTrafficManagerEndpoint, Enable-AzureRmTraf...
Script 4.0.2 AzureRM.UsageAggregates Get-UsageAggregates
Script 4.2.2 AzureRM.Websites {Get-AzureRmAppServicePlan, Set-AzureRmAppServicePlan, New...
Script 1.0.1 Microsoft.PowerShell.Operation.V... {Get-OperationValidation, Invoke-OperationValidation}
Binary 1.0.0.1 PackageManagement {Find-Package, Get-Package, Get-PackageProvider, Get-Packa...
Script 3.4.0 Pester {Describe, Context, It, Should...}
Script 1.0.0.1 PowerShellGet {Install-Module, Find-Module, Save-Module, Update-Module...}
Script 2.0.0 PSReadline {Get-PSReadLineKeyHandler, Set-PSReadLineKeyHandler, Remov...
```
Contributor guide
Assessment
This issue has not been assessed yet.