Store access credentials on the Data Science Virtual Machine securely

A common challenge when building cloud applications is how to manage the credentials that need to be in your code for authenticating to cloud services. Keeping these credentials secure is an important task. Ideally, they never appear on developer workstations or get checked into source control.

Managed Service Identity (MSI) makes solving this problem simpler by giving Azure services an automatically managed identity in Azure Active Directory (Azure AD). You can use this identity to authenticate to any service that supports Azure AD authentication, without having any credentials in your code. One common pattern to secure credentials is to use MSI is in combination with the Azure Keyvault, a managed Azure service to store secrets and cryptographic keys securely. You can access Key vault using the managed service identity and retrieve the authorized secrets and cryptographic keys from the Key Vault.

The MSI and Key Vault documentation is a comprehensive resource for in-depth information on these services. The rest of this article is a basic primer showing the basic use of MSI and Key Vault on the Data Science Virtual Machine (DSVM).

Walkthrough of using MSI to securely access Azure resources

1. Create MSI on the DSVM

# Pre-requisites: You have already created a Data Science VM in the usual way

# Create identity principal for the VM
az vm assign-identity -g <Resource Group Name> -n <Name of the VM>
# Get the principal id of the DSVM
az resource list -n <Name of the VM> --query [*].identity.principalId --out tsv

2. Assign Keyvault access permission to VM principal

# Pre-requisites: You have already create an empty Key Vault resource on Azure using Portal or Azure CLI 

# Assign only get and set permission but not the capability to list the keys
az keyvault set-policy --object-id <PrincipalID of the DSVM from previous step> --name <Key Vault Name> -g <Resource Group of Key Vault>  --secret-permissions get set

3. Access a secret in the Keyvault from the DSVM

# Get the access token for VM
x=`curl http://localhost:50342/oauth2/token --data "resource=" -H Metadata:true`
token=`echo $x | python -c "import sys, json; print(json.load(sys.stdin)['access_token'])"`

# Access Keyvault using access token. 
curl https://<Vault Name> -H "Authorization: Bearer $token"

4. Access storage keys from the DSVM

# Pre-requisites: You have granted your VM's MSI access to use storage account access keys based on instruction from this article: that describes this process in more detail.

y=`curl http://localhost:50342/oauth2/token --data "resource=" -H Metadata:true`
ytoken=`echo $y | python -c "import sys, json; print(json.load(sys.stdin)['access_token'])"`
curl<SubscriptionID>/resourceGroups/<ResourceGroup of Storage account>/providers/Microsoft.Storage/storageAccounts/<Storage Account Name>/listKeys?api-version=2016-12-01 --request POST -d "" -H "Authorization: Bearer $ytoken"

#Now you can access the data in the storage account from the retrieved Storage account keys

5. Access Keyvault from Python

from azure.keyvault import KeyVaultClient
from msrestazure.azure_active_directory import MSIAuthentication

"""MSI Authentication example."""

# Get credentials
credentials = MSIAuthentication(

# Create a KeyVault client
key_vault_client = KeyVaultClient(

key_vault_uri = "https://<key Vault Name>"

secret = key_vault_client.get_secret(
key_vault_uri,  # Your KeyVault URL
"SQLPasswd",       # Name of your secret that already exists in the Key Vault
""              # The version of the secret. Empty string for latest
print("My secret value is {}".format(secret.value))

6. Azure CLI access from VM

# With a Managed Service Identity setup on the DSVM, users on the DSVM can execute Azure CLI to perform the authorized functions. Here are commands to access Key Vault from a Azure CLI without having to login to an Azure account. 
# Pre-requisites: MSI is already setup on the DSVM as indicated earlier and specific permission like accessing storage account keys, reading specific secrets and writing new secrets is provided to the MSI . 

# Authenticate to Azure CLI without requiring an Azure Account. 
az login --msi

# Retrieve a secret from Key Vault. 
az keyvault secret show --vault-name <Vault Name> --name SQLPasswd

# Create a new Secret in Key Vault
az keyvault secret set --name MySecret --vault-name <Vault Name> --value "Helloworld"

# List Storage Account Access Keys
az storage account keys list -g <Storage Account Resource Group> -n <Storage Account Name>