Skip to content

Commit 1840428

Browse files
rahulalapati43Rahul AlapatiCopilotCopilot
authored
[Key Vault] az keyvault create: Fix keyvault create RequestDisallowedByPolicy error by explicitly setting enableSoftDelete in the request body (#33265)
Co-authored-by: Rahul Alapati <rahulalapati@microsoft.com> Co-authored-by: Copilot <223556219+Copilot@users.noreply.github.com> Co-authored-by: Copilot <175728472+Copilot@users.noreply.github.com>
1 parent 871742a commit 1840428

2 files changed

Lines changed: 51 additions & 0 deletions

File tree

src/azure-cli/azure/cli/command_modules/keyvault/custom.py

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -649,6 +649,9 @@ def create_vault(cmd, client, # pylint: disable=too-many-locals, too-many-state
649649
enabled_for_disk_encryption=enabled_for_disk_encryption,
650650
enabled_for_template_deployment=enabled_for_template_deployment,
651651
enable_rbac_authorization=enable_rbac_authorization,
652+
# Intentionally include this field in the request body to satisfy
653+
# Azure Policy checks that require soft delete to be explicitly set.
654+
enable_soft_delete=True,
652655
enable_purge_protection=enable_purge_protection,
653656
soft_delete_retention_in_days=int(retention_days),
654657
public_network_access=public_network_access)

src/azure-cli/azure/cli/command_modules/keyvault/tests/latest/test_keyvault_commands.py

Lines changed: 48 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -106,6 +106,54 @@ def test_parse_asn1_date(self):
106106
self.assertEqual(_asn1_to_iso8601("20170424163720Z"), expected)
107107

108108

109+
class CreateVaultSoftDeleteTest(unittest.TestCase):
110+
"""Verify that create_vault explicitly sets enable_soft_delete=True in the request body
111+
so that Azure Policy checks requiring the property to be present are satisfied."""
112+
113+
@mock.patch('azure.cli.command_modules.keyvault.custom._create_network_rule_set', return_value=None)
114+
@mock.patch('azure.cli.core._profile.Profile')
115+
@mock.patch('azure.cli.core.util.sdk_no_wait')
116+
def test_create_vault_sets_enable_soft_delete_true(self, mock_sdk_no_wait, mock_profile, _mock_network):
117+
from azure.cli.command_modules.keyvault.custom import create_vault
118+
119+
mock_profile.return_value.get_subscription.return_value = {
120+
'tenantId': '00000000-0000-0000-0000-000000000000'
121+
}
122+
123+
# Build a minimal cmd mock that returns simple model classes
124+
cmd = mock.MagicMock()
125+
cmd.cli_ctx.data = {}
126+
127+
# get_models returns a simple class that records its kwargs
128+
def fake_get_models(name, **kwargs):
129+
return type(name, (), {'__init__': lambda self, **kw: self.__dict__.update(kw)})
130+
131+
cmd.get_models.side_effect = fake_get_models
132+
133+
# Client whose get() raises so vault-already-exists check is skipped
134+
client = mock.MagicMock()
135+
client.get.side_effect = HttpResponseError()
136+
137+
create_vault(
138+
cmd, client,
139+
resource_group_name='rg',
140+
vault_name='testvault',
141+
location='eastus',
142+
retention_days='90',
143+
no_self_perms=True,
144+
)
145+
146+
# sdk_no_wait is called with the VaultCreateOrUpdateParameters as 'parameters'
147+
mock_sdk_no_wait.assert_called_once()
148+
call_kwargs = mock_sdk_no_wait.call_args
149+
parameters = call_kwargs.kwargs.get('parameters') or call_kwargs[1].get('parameters')
150+
vault_properties = parameters.properties
151+
152+
self.assertIs(vault_properties.enable_soft_delete, True,
153+
"create_vault must explicitly set enable_soft_delete=True in the request body "
154+
"to satisfy Azure Policy checks")
155+
156+
109157
class KeyVaultPrivateLinkResourceScenarioTest(ScenarioTest):
110158
@ResourceGroupPreparer(name_prefix='cli_test_keyvault_plr')
111159
@KeyVaultPreparer(name_prefix='cli-test-kv-plr-', location='eastus2')

0 commit comments

Comments
 (0)