Azure Key Vaults: Assign Access Policies to a single object using PowerShell
In this blog I will share one way to assign a key vault access policies to a single application, security group or user using PowerShell.
My blogs have relatively simple, and sometimes complex, examples and I’m hoping that you will be able to tailor them to your need or use them in your own scripts.
To assign access policies to multiple applications, security groups or users, review Azure Key Vaults: Assign Access Policies to multiple objects using PowerShell.
Prerequisites
- Install Azure PowerShell if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
- Install Azure Active Directory PowerShell module if you haven’t already. You can use Cloud Shell if you prefer to stay within Azure Portal.
- A key vault already created. You can use Quickstart: Create a key vault using the Azure portal to create one.
Introduction
The goal of this blog is to show one way to accomplish a task. It is not to show how to write a perfect script, the perfect solution to a challenge or the perfect process to accomplish a task.
Because key vault supports up to 1024 access policy entries, it is recommended to assign access policies to groups of users, where possible, rather than individual users.
Using PowerShell, I will show you how to assign a key vault access policies to:
- A single application, security group or user using PowerShell.
PowerShell Cmdlets
- Connect-AzAccount Connect to Azure with an authenticated account for use with cmdlets from the Az PowerShell modules.
- Set-AzContext Sets the tenant, subscription, and environment for cmdlets to use in the current session.
- Get-AzADGroup Filters active directory groups.
- Set-AzKeyVaultAccessPolicy Grants or modifies existing permissions for a user, application, or security group to perform operations with a key vault. The cmdlet will be used with the following parameters: -VaultName, -UserPrincipalName, -ObjectId, -ServicePrincipalName, -PermissionsToKeys, and -PermissionsToSecrets.
By default, Set-AzKeyVaultAccessPolicy does not generate any output. Use -PassThru to returns an object representing the item with which you are working.
Sign in to Azure
- Sign in to Azure. If you have multiple subscriptions or tenants, make sure to sign in to the correct subscription.
- You can use Set-AzContext to set the tenant, subscription, and environment for cmdlets to use in the current session.
1Connect-AzAccount -Subscription "aa1111a1-1111-1a1a-11a1-1111a1a1a1a1"
Assign access policies to a user using -UserPrincipalName
- The first example specifies the user principal name (UPN) name ‘user1@contosodev.com’, and the command grants the user permissions to set, delete and get secrets.
- The second example specifies the user principal name (UPN) name ‘user1@contosodev.com’, and the command grants the user permissions to (a) set, delete and get secrets and (b) create, import, delete and list keys.
- The final command specifies the user principal name (UPN) name ‘user1@contosodev.com’, and the command removes all permissions to key operations.
12345678910111213$VaultName = 'SaadDev03Vault'$UserPN = 'user1@saadkhamis.com'$PermToSecrets = @("set", "delete", "get")# Grants permissions for $UserPN to perform operations $PermToSecrets for a key vault named $VaultNameSet-AzKeyVaultAccessPolicy -VaultName $VaultName -UserPrincipalName $UserPN -PermissionsToSecrets $PermToSecrets# Grants permissions for $UserPN to perform operations $PermToKeys and $PermToSecrets for a key vault named $VaultName$PermToKeys = @("create", "import", "delete", "list")Set-AzKeyVaultAccessPolicy -VaultName $VaultName -UserPrincipalName $UserPN -PermissionsToSecrets $PermToSecrets -PermissionsToKeys $PermToKeys# Removes all permissions to key operations for $UserPN for a key vault named $VaultNameSet-AzKeyVaultAccessPolicy -VaultName $VaultName -UserPrincipalName $UserPN -PermissionsToKeys @()
Assign access policies to a security group using -ObjectId
In order to assign access policies to a security group, the security group object Id is needed. The Get-AzADGroup cmdlet is used to get the security group object Id. You can:
- Save the result of Get-AzADGroup in a variable then use the variable or
- Get security group object Id inline in the same command line. The advantage of the first method is the ability to check validate the group name. I will show you both method.
123456789101112131415$VaultName = 'SaadDev03Vault'$GroupName = 'Group1'$PermToSecrets = @("set", "delete", "get")# 1. Save the result of Get-AzADGroup in a variable then use the variable$GroupId = (Get-AzADGroup | Where {$_.DisplayName -eq $GroupName}).Idif ($GroupId -eq $null) { # Security group does not exist, write an error messageWrite-Host "[$GroupName] does not exist" -ForegroundColor Red}else { # Security group exists, grants the security group permissionsSet-AzKeyVaultAccessPolicy -VaultName $VaultName -ObjectId $GroupId -PermissionsToSecrets $PermToSecrets}# OR# 2. Get security group object Id inline in the same command lineSet-AzKeyVaultAccessPolicy -VaultName $VaultName -ObjectId (Get-AzADGroup | Where {$_.DisplayName -eq $GroupName}).Id -PermissionsToSecrets $PermToSecrets
Assign access policies to an application using -ServicePrincipalName
The application must be registered in your Azure Active Directory.
- This example specifies the service principal name ‘http://payroll.saadkhamis.com’, and the command grants the application permissions to set and get secrets.
123456$VaultName = 'SaadDev03Vault'$AppServPN = 'http://payroll.saadkhamis.com'$PermToSecrets = @("set", "get")# Grants permissions for $AppServPN to perform operations $PermToSecrets for a key vault named $VaultNameSet-AzKeyVaultAccessPolicy -VaultName $VaultName -ServicePrincipalName $AppServPN -PermissionsToSecrets $PermToSecrets
Conclusion
In this blog we explored how to assign a key vault access policies to a single application, security group or user using PowerShell.
Did you find this blog easy to follow and helpful to you? Let me know in the comments below.
Disclaimer
Purpose of the code contained in blog is solely for learning and demo purposes. Author will not be held responsible for any failure or damages caused due to any other usage.
There's no comments