avatarBill WANG

Free AI web copilot to create summaries, insights and extended knowledge, download it at here

4267

Abstract

as Visual Studio. If multiple identities are in the cache, then the value of the environment variable <code>AZURE_USERNAME</code> is used to select which identity to use. See <a href="https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.sharedtokencachecredential?view=azure-python">SharedTokenCacheCredential</a> for more details.</li><li>The identity currently logged in to the <b>Azure CLI</b>.</li><li>The identity currently logged in to <b>Azure PowerShell</b>.</li><li>The identity currently logged in to the Azure Developer CLI.</li></ol><h1 id="53b6">Biggest difference between AWS ec2 instance profile to Azure virtual machine’s identity.</h1><p id="e216">From my perspective, the most significant distinction is that in AWS, permissions for resources are restricted to a single account. If you require access to other AWS accounts, you must set up an assumed role for each account you want to access. However, this limitation doesn’t exist in Azure. You can assign broader roles to Azure virtual machines, such as within a managed group, granting access to the entirety of the organization’s subscriptions.</p><p id="3e0a">To get better understanding on how managed identity with DefaultAzureCredential works, I show you some samples.</p><h1 id="c52f">Get Access Token with simple curl API call</h1><p id="d522">Ref: <a href="https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-linux-vm-access-arm">https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-linux-vm-access-arm</a></p><div id="44f4"><pre><span class="hljs-variable">$ </span>curl <span class="hljs-string">'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&amp;resource=https://management.azure.com/'</span> -H <span class="hljs-title class_">Metadata</span><span class="hljs-symbol">:true</span>

{<span class="hljs-string">"access_token"</span><span class="hljs-symbol">:<span class="hljs-string">"eyJ0eXAiOi..."</span></span>, <span class="hljs-string">"refresh_token"</span><span class="hljs-symbol">:<span class="hljs-string">""</span></span>, <span class="hljs-string">"expires_in"</span><span class="hljs-symbol">:<span class="hljs-string">"3599"</span></span>, <span class="hljs-string">"expires_on"</span><span class="hljs-symbol">:<span class="hljs-string">"1504130527"</span></span>, <span class="hljs-string">"not_before"</span><span class="hljs-symbol">:<span class="hljs-string">"1504126627"</span></span>, <span class="hljs-string">"resource"</span><span class="hljs-symbol">:<span class="hljs-string">"https://management.azure.com"</span></span>, <span class="hljs-string">"token_type"</span><span class="hljs-symbol">:<span class="hljs-string">"Bearer"</span></span>}

<span class="hljs-variable">$ </span>curl <span class="hljs-symbol">https:</span>/<span class="hljs-regexp">/management.azure.com/subscriptions</span><span class="hljs-regexp">/<SUBSCRIPTION ID>/resource</span>Groups/<<span class="hljs-variable constant_">RESOURCE</span> <span class="hljs-variable constant_">GROUP</span>><span class="hljs-string">?a</span>pi-version=<span class="hljs-number">2016</span>-09-<span class="hljs-number">01</span> -H <span class="hljs-string">"Authorization: Bearer <ACCESS TOKEN>"</span></pre></div><h1 id="2401">Get Access Token with Python SDK</h1><p id="9940">In the context of the Azure Python SDK, implementing <b>DefaultAzureCredential</b> is elegantly simple:</p><div id="5b9c"><pre><span class="hljs-keyword">from</span> azure.identity <span class="hljs-keyword">import</span> DefaultAzureCredential credential = DefaultAzureCredential() scopes = [<span class="hljs-string">"https://management.azure.com/.default"</span>] token = credential.get_token(*scopes) access_token = token.token <span class="hljs-built_in">print</span>(<span class="hljs-string">"Access Token:"</span>, access_token)</pre></div><h1 id="9df7">Use case for reference</h1><p id="ed22">Requirement: Allow the virtual machine to read a KeyVault secret.</p><p id="7a2a">After you create the key vault and set the secret, you need go to its access control (IAM) and assign the Virtual machine as managed identity with role of <code>key

Options

vault secrets user</code></p><h2 id="1652">With Azure CLI</h2><div id="7d3a"><pre><span class="hljs-comment"># login as virtual machine identity. You don't need input any </span> <span class="hljs-comment"># username/password or clientid/clientSecret to login with below command</span> az login --identity az account <span class="hljs-built_in">set</span> -s <subscription_id> az account show

<span class="hljs-comment"># get the secret easilypy</span> az keyvault secret show --vault-name <vault_name> --name <secret_name> --query value -o tsv</pre></div><h2 id="1adf">With Python SDK</h2><div id="c81c"><pre><span class="hljs-keyword">from</span> azure.identity <span class="hljs-keyword">import</span> DefaultAzureCredential <span class="hljs-keyword">from</span> azure.keyvault.secrets <span class="hljs-keyword">import</span> SecretClient

credential = DefaultAzureCredential()

key_vault_url = <span class="hljs-string">"https://your-key-vault-name.vault.azure.net/"</span> secret_client = SecretClient(vault_url=key_vault_url, credential=credential)

secret_name = <span class="hljs-string">"your-secret-name"</span> secret = secret_client.get_secret(secret_name)

secret_value = secret.value <span class="hljs-built_in">print</span>(<span class="hljs-string">"Secret Value:"</span>, secret_value)</pre></div><h1 id="b3ad">In conclusion</h1><p id="7f2e">Azure’s Virtual Machine Identity offers <b>zero credentials solution</b> as AWS EC2 instance profiles. With native support like <b>DefaultAzureCredential</b> and the support of various programming languages, Azure streamlines the process of obtaining secure access tokens automatically.</p><h1 id="257a">Extra topics</h1><h2 id="74bf">Retrieve aws access token and instance metadata</h2><p id="8e49">Ref: <a href="https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html">https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html</a></p><h1 id="ffc0">Reference</h1><div id="a8eb" class="link-block"> <a href="https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-linux-vm-access-arm"> <div> <div> <h2>Quickstart: Use a managed identity to access Azure Resource Manager - Microsoft Entra</h2> <div><h3>A quickstart that walks you through the process of using a Linux VM system-assigned managed identity to access Azure…</h3></div> <div><p>learn.microsoft.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*78XWIH3Rjmy7RKpe)"></div> </div> </div> </a> </div><div id="7540" class="link-block"> <a href="https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python"> <div> <div> <h2>azure.identity.DefaultAzureCredential class</h2> <div><h3>A default credential capable of handling most Azure SDK authentication scenarios. The identity it uses depends on the…</h3></div> <div><p>learn.microsoft.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*2blBDMbHzfMs9hnD)"></div> </div> </div> </a> </div><div id="d478" class="link-block"> <a href="https://github.com/Azure/azure-sdk-for-go/tree/99c0a042b1a228db9cd1f0c54df095f24f13cf28/sdk/azidentity#defaultazurecredential"> <div> <div> <h2>azure-sdk-for-go/sdk/azidentity at 99c0a042b1a228db9cd1f0c54df095f24f13cf28 ·…</h2> <div><h3>This repository is for active development of the Azure SDK for Go. For consumers of the SDK we recommend visiting our…</h3></div> <div><p>github.com</p></div> </div> <div> <div style="background-image: url(https://miro.readmedium.com/v2/resize:fit:320/0*rl7WxtFnuBgvCB1K)"></div> </div> </div> </a> </div></article></body>

Azure Virutal Machine’s Identity — Zero credentials solution

Follow up on my Virtual machine blogs about

Background

In the world of cloud computing, keeping things secure and managing who can access what is a big deal. While I was checking out Azure services, I stumbled upon something interesting — Azure Virtual Machine Identity. It’s a similar feature as AWS EC2 instance profiles if you know more about AWS. It’s all about making sure you can access your Azure resources without giving any secret codes.

What’s AWS ec2 instance profile?

An AWS EC2 instance profile is a container for an AWS Identity and Access Management (IAM) role with permission policies, that you can use to pass role information to an EC2 instance when the instance runs. This allows the EC2 instance to securely access other AWS services and resources without needing to embed long-term credentials directly on the instance.

Why Managed Identity

Azure Service Principal and Managed Identity are both tools for Azure identity management. Whereas Managed Identity is good when you want Azure to handle the login details automatically.

Managed identity scopes

  • Tenant (most top level, similar to AWS Organization Root)
  • Management group and its sub-management groups (similar to AWS Organization OUs)
  • subscription (Similar to AWS Account)
  • resource group
  • resource

Azure’s Solution with its SDKs

Azure’s solution to this requirement is the DefaultAzureCredential. This credential mechanism is already supported by various Azure SDKs and tools, including Azure CLI, Azure Python SDK, Azure Golang SDK and other SDK. Using the DefaultAzureCredential, developers can obtain access tokens without resorting to the exposure of long-term credentials.

Using these identities in turn

Ref: https://learn.microsoft.com/en-us/python/api/azure-identity/azure.identity.defaultazurecredential?view=azure-python

The identity, virtual machine uses, depends on the environment. When an access token is needed, it requests one using these identities in turn, stopping when one provides a token:

  1. A service principal configured by environment variables. See EnvironmentCredential for more details.
  2. WorkloadIdentityCredential if environment variable configuration is set by the Azure workload identity webhook.
  3. An Azure managed identity. See ManagedIdentityCredential for more details.
  4. On Windows only: a user who has signed in with a Microsoft application, such as Visual Studio. If multiple identities are in the cache, then the value of the environment variable AZURE_USERNAME is used to select which identity to use. See SharedTokenCacheCredential for more details.
  5. The identity currently logged in to the Azure CLI.
  6. The identity currently logged in to Azure PowerShell.
  7. The identity currently logged in to the Azure Developer CLI.

Biggest difference between AWS ec2 instance profile to Azure virtual machine’s identity.

From my perspective, the most significant distinction is that in AWS, permissions for resources are restricted to a single account. If you require access to other AWS accounts, you must set up an assumed role for each account you want to access. However, this limitation doesn’t exist in Azure. You can assign broader roles to Azure virtual machines, such as within a managed group, granting access to the entirety of the organization’s subscriptions.

To get better understanding on how managed identity with DefaultAzureCredential works, I show you some samples.

Get Access Token with simple curl API call

Ref: https://learn.microsoft.com/en-us/azure/active-directory/managed-identities-azure-resources/tutorial-linux-vm-access-arm

$ curl 'http://169.254.169.254/metadata/identity/oauth2/token?api-version=2018-02-01&resource=https://management.azure.com/' -H Metadata:true

{"access_token":"eyJ0eXAiOi...",
"refresh_token":"",
"expires_in":"3599",
"expires_on":"1504130527",
"not_before":"1504126627",
"resource":"https://management.azure.com",
"token_type":"Bearer"} 

$ curl https://management.azure.com/subscriptions/<SUBSCRIPTION ID>/resourceGroups/<RESOURCE GROUP>?api-version=2016-09-01 -H "Authorization: Bearer <ACCESS TOKEN>"

Get Access Token with Python SDK

In the context of the Azure Python SDK, implementing DefaultAzureCredential is elegantly simple:

from azure.identity import DefaultAzureCredential
credential = DefaultAzureCredential()
scopes = ["https://management.azure.com/.default"]
token = credential.get_token(*scopes)
access_token = token.token
print("Access Token:", access_token)

Use case for reference

Requirement: Allow the virtual machine to read a KeyVault secret.

After you create the key vault and set the secret, you need go to its access control (IAM) and assign the Virtual machine as managed identity with role of key vault secrets user

With Azure CLI

# login as virtual machine identity. You don't need input any 
# username/password or clientid/clientSecret to login with below command
az login --identity
az account set -s <subscription_id> 
az account show

# get the secret easilypy
az keyvault secret show --vault-name <vault_name> --name <secret_name> --query value -o tsv

With Python SDK

from azure.identity import DefaultAzureCredential
from azure.keyvault.secrets import SecretClient

credential = DefaultAzureCredential()

key_vault_url = "https://your-key-vault-name.vault.azure.net/"
secret_client = SecretClient(vault_url=key_vault_url, credential=credential)

secret_name = "your-secret-name"
secret = secret_client.get_secret(secret_name)

secret_value = secret.value
print("Secret Value:", secret_value)

In conclusion

Azure’s Virtual Machine Identity offers zero credentials solution as AWS EC2 instance profiles. With native support like DefaultAzureCredential and the support of various programming languages, Azure streamlines the process of obtaining secure access tokens automatically.

Extra topics

Retrieve aws access token and instance metadata

Ref: https://docs.aws.amazon.com/AWSEC2/latest/UserGuide/instancedata-data-retrieval.html

Reference

Azure Managed Identities
Azure Cli
Azure
Python
DevOps
Recommended from ReadMedium