Accessing Key Vault from another Subscription over public endpoint

This post has been republished via RSS; it originally appeared at: Core Infrastructure and Security Blog articles.

Introduction

Hello everyone, it has been a while, Andrew Coughlin here and I am a Customer Engineer at Microsoft focusing on Azure IaaS.  I recently received questions from a few of my customers about access a key vault from a different subscription and from a different region in a different subscription.  In this blog I will walk through the process of using a managed identity and access an Azure Key Vault from another subscription.

 

Environment Setup

Let’s first discuss the setup of what we will be discussing in this blog post.  I will have two subscriptions assigned to the same Azure AD Tenant.   Within each Azure subscription I will have a resource group in each.  I will create the Azure Key Vault in one subscription / resource group and then I will create a virtual machine in the other subscription / resource group.  This is just for example purposes; I could utilize other azure services that can use managed identities.   I could also create a service principal for my application to use to get keys or secrets.

In this example we would be using public endpoints, however private endpoints would require some additional work.  Those additional steps would be outlined in another post.

 

Screenshot 2022-06-30 064051.jpg

Prerequisites

Reference Articles

Setup Azure Virtual Machine

  1. Login to the Azure Portal.
  2. Next, I will click on the virtual machine that I will be using in this demonstration.

AndrewCoughlin_18-1656589416421.png

 

  1. Scroll down the identity and ensure that the System assigned identity is On.

AndrewCoughlin_19-1656589416426.png

 

  1. Next, we will move onto configuring the Key Vault.

Setup Azure Key Vault

  1. In the Azure Portal navigate to Key Vaults, click on the Key Vault you want to configure.

AndrewCoughlin_20-1656589416432.png

 

  1. Click on Access control (IAM), Click Add, Click Add role assignment.

AndrewCoughlin_21-1656589416434.png

 

  1. Type key vault and select the role you want to assign based on your requirements.

AndrewCoughlin_22-1656589416440.png

  1. Click Managed Identities.
  2. Click Select members.
  3. Select Subscription, Managed Identity, select the managed identity.
  4. Click Select.
  5. Click Review + assign.

AndrewCoughlin_23-1656589416444.png

 

  1. Click Review + assign.

AndrewCoughlin_24-1656589416445.png

 

NOTE: It may take a few minutes for permissions to fully populate, give it a few minutes (5-10 minutes) before proceeding to the next step.

 

  1. Next, we will connect to the VM and retrieve a secret and/or keys.

Access Azure Key Vault

  1. Navigate to Virtual Machines, click on the Virtual Machine you will connect to.

AndrewCoughlin_25-1656589416447.png

 

  1. Click Connect and click Download RDP File, Login to the virtual machine.

AndrewCoughlin_26-1656589416449.png

 

  1. Open PowerShell on the system you just RDP.

 AndrewCoughlin_27-1656589416450.png

 

NOTE: This requires PowerShell 7.x, if you try to run PowerShell 5.X this process will not work.

AndrewCoughlin_29-1656589416455.png

 

  1. We need to authenticate as the managed identity, for that we will type the two following commands:

 

$Response = Invoke-RestMethod -Uri 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https%3A%2F%2Fvault.azure.net' -Method GET -Headers @{Metadata="true"} $KeyVaultToken = $Response.access_token

 

  1. If you want to confirm there is a value in $KeyVaultToken, you can type $KeyVaultToken and press Enter to see the results.

NOTE: You should not share this token.

 AndrewCoughlin_28-1656589416453.png

 

  1. To get a secret from the key vault we will type the following:

 

Invoke-RestMethod -Uri https://<your-key-vault-URL>/secrets/<secret-name>?api-version=2016-10-01 -Method GET -Headers @{Authorization="Bearer $KeyVaultToken"}

 

NOTE: <your-key-vault-URL, will be the vault URI, secret-name will be the name you give for the secret.

AndrewCoughlin_30-1656589416456.png

 
  1. If required, to get a key from the key vault we will type the following:

 

Invoke-RestMethod -Uri https://<your-key-vault-URL>/keys/<key-name>?api-version=2016-10-01 -Method GET -Headers @{Authorization="Bearer $KeyVaultToken"}

 

NOTE: <your-key-vault-URL, will be the vault URI, key-name will be the name you give for the key.

 

Conclusion

That is, it, in case you were wondering, would this work in a scenario that a virtual machine or service is in region A and the key vault is in region B with both resources in different subscriptions? Yes, this would work as well.  In this blog I have covered how to access a key vault from a different subscription and from a different region in a different subscription.  Thank you for taking the time to read this blog, I hope this helps you and see you next time.

 

Disclaimer

The sample scripts are not supported under any Microsoft standard support program or service. The sample scripts are provided AS IS without warranty of any kind. Microsoft further disclaims all implied warranties including, without limitation, any implied warranties of merchantability or of fitness for a particular purpose. The entire risk arising out of the use or performance of the sample scripts and documentation remains with you. In no event shall Microsoft, its authors, or anyone else involved in the creation, production, or delivery of the scripts be liable for any damages whatsoever (including, without limitation, damages for loss of business profits, business interruption, loss of business information, or other pecuniary loss) arising out of the use of or inability to use the sample scripts or documentation, even if Microsoft has been advised of the possibility of such damages.

Leave a Reply

Your email address will not be published. Required fields are marked *

*

This site uses Akismet to reduce spam. Learn how your comment data is processed.