Please check here for scripts using the latest PowerShell cmdlets.
Access to the Key Vault is secured using AD application token, as we had seen in the ‘Authenticating a Client Application with Azure Key Vault’. Quite often administrators require to manage the AD application created, performing activities like creating new AD applications, changing the certificate used to authenticate with the AD application, remove a certificate or even delete an application. All of these are possible using PowerShell scripts and administrators can even run this as part of their automation scripts. With the latest Azure PowerShell version(0.9.2 or higher), the Key Vault cmdlet’s are included automatically and does not require any additional installations. For managing the Azure AD application we need to install the Azure AD module for PowerShell and import them into the PowerShell command prompt.
Creating AD application
The New-AzureADApplication cmdlet is used to create a new Azure AD application. It also provides an option to specify the certificate details used to authenticate with the AD application at the time of creation itself. This can be done as a separate step if required, which is shown later in the post.
First we need a certificate that is to be used for authenticating against the AD application, for which I use the below commands to generate a test certificate
This certificate is then used to create the AD application using the below script.
1 2 3 4 5 6 7 8 9 10 11
To associate the application created with the Key Vault, we need to create a service principal using New-AzureADServicePrincipal and then associate that with the Vault using the Set-AzureKeyVaultAccessPolicy
1 2 3
Adding a Certificate
The New-MsolServicePrincipalCredential cmdlet is used to add a new credential to a service principal or to an application. The service principal is identified by supplying one of the following: object ID, appPrincipalID, service principal name (SPN).
1 2 3 4 5 6 7 8 9 10
Removing a Certificate
Whenever a credential gets compromised or as part of regular credential refresh, administrators would want to remove an old certificate and replace with a new one. The Remove-MsolServicePrincipalCredential cmdlet is used to remove a credential key from a service principal by specifying the key ID for the credential and the objectID/applicationID/ServicePrincipalName to identify the service principal. To get the key ID of an existing credential, Get-MsolServicePrincipalCredential cmdlet can be used, which returns the list of credentials associated with a service principal. The below script just removes the first credential, you could loop through and remove all.
Delete an application
The Remove-MsolServicePrincipal cmdlet removes a service principal from Microsoft Azure Active Directory, by specifying objectID/applicationID/ServicePrincipalName to identify the service principal.
1 2 3 4 5
Managing the AD application is a very important and necessary process in the life cycle of a Key Vault, as the access to the Vault is controlled using that. Certificates securing the AD applciation should be rolled/updated frequently and application permissions should be reviewed often to make sure that all applications have only the required permissions.