The error Set-AzKeyVaultSecret : Operation returned an invalid status code 'Forbidden'
indicates that the caller (your user or service principal) does not have the necessary permissions to set a secret in the Azure Key Vault.
Steps to Resolve the Issue
Step 1: Assign Key Vault Access Policy
- Assign Access Policy using Azure Portal:
- Navigate to your Azure Key Vault in the Azure Portal.
- In the Key Vault settings, select Access policies.
- Click on + Add Access Policy.
- In the Configure from template (optional) dropdown, select Secret Management.
- Under Select principal, search for your user or the service principal (the
oid
mentioned in your error message). - Click Add and then Save.
- Assign Access Policy using Azure CLI:
- You can also assign the access policy using the Azure CLI. Replace
<YourKeyVaultName>
,<YourPrincipalId>
, and<YourSubscriptionId>
with appropriate values.az keyvault set-policy --name <YourKeyVaultName> --spn <YourPrincipalId> --secret-permissions set
- For a user, use
--upn
instead of--spn
.
- You can also assign the access policy using the Azure CLI. Replace
Step 2: Assign Role-Based Access Control (RBAC) Role
- Assign RBAC Role using Azure Portal:
- Navigate to the Azure Key Vault in the Azure Portal.
- Select Access control (IAM).
- Click on Add role assignment.
- Select the role Key Vault Secrets Officer or Key Vault Contributor.
- Assign this role to your user or service principal.
- Assign RBAC Role using Azure CLI:
- You can also assign the RBAC role using the Azure CLI. Replace
<YourSubscriptionId>
,<YourResourceGroupName>
,<YourKeyVaultName>
, and<YourPrincipalId>
with appropriate values.az role assignment create --assignee <YourPrincipalId> --role "Key Vault Secrets Officer" --scope /subscriptions/<YourSubscriptionId>/resourceGroups/<YourResourceGroupName>/providers/Microsoft.KeyVault/vaults/<YourKeyVaultName>
- You can also assign the RBAC role using the Azure CLI. Replace
Example
If your Key Vault name is nico-dhv-123
and you need to assign access to the service principal with oid
ee10a891-417a-4a8d-862b-b8fc0b458ec3
, you would use the following commands:
Azure CLI - Assign Access Policy
az keyvault set-policy --name nico-dhv-123 --spn ee10a891-417a-4a8d-862b-b8fc0b458ec3 --secret-permissions set
Azure CLI - Assign RBAC Role
az role assignment create --assignee ee10a891-417a-4a8d-862b-b8fc0b458ec3 --role "Key Vault Secrets Officer" --scope /subscriptions/7083c8f7-299c-4dd7-9297-7c6c76acb8a0/resourceGroups/learn-7151c2f1-5c4b-4f94-9d02-619789e1fbc6/providers/Microsoft.KeyVault/vaults/nico-dhv-123
Step 3: Wait for Propagation
After making these changes, it may take a few minutes for the permissions to propagate. Wait for a short period and then retry the command:
Set-AzKeyVaultSecret -VaultName $keyVaultName -Name 'sqlServerAdministratorLogin' -SecretValue (ConvertTo-SecureString "your_secret_value" -AsPlainText -Force)
Final Considerations
- Check Propagation Time: Permissions changes can sometimes take a few minutes to propagate.
- Verify Principal: Ensure you are assigning permissions to the correct user or service principal.
- Check Existing Policies: Ensure there are no conflicting policies that might deny access.
By following these steps, you should be able to resolve the authorization issue and successfully set a secret in your Azure Key Vault.