Do not MERGE: MSI v2 Sample: working example for MSIv2 #558
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Goals
The primary objective is to enable seamless token acquisition in MSI V2 for VM/VMSS, utilizing the
/credential
endpoint./credential
and the ESTS regional token endpoint.Token Acquisition Process
In MSI V1, IMDS or any other Managed Identity Resource Provider (MIRP) directly returns an access token. However, in MSI V2, the process involves two steps:
Short-Lived Credential Retrieval from
/credential
Endpoint/credential
endpoint./credential
endpoint to retrieve a short-lived credential (SLC).Access Token Acquisition via ESTS
Retry Logic
MSAL uses the default Managed Identity retry policy for MSI V2 credential/token requests, whether calling the ESTS endpoint or the new
/credential
endpoint. i.e. MSAL performs 3 retries with a 1 second pause between each retry. Retries are performed on certain error codes only.Steps for MSI V2 Authentication
This section outlines the necessary steps to acquire an access token using the MSI V2
/credential
endpoint.1. Check for an Existing (Platform) Certificate (Windows only)
devicecert.mtlsauth.local
) in(Cert:\LocalMachine\My)
.(Cert:\CurrentUser\My)
.2. Generate a New Certificate (if platform certificate is not found)
Certificate Creation Requirements
Certificate Rotation Strategy
3. Extract Certificate Data
x5c
).4. Request MSI Credential
/credential
endpoint with the certificate details.Metadata: true
header.X-ms-Client-Request-id
header with a GUID.jwk
format. RFCregional_token_url
tenant_id
client_id
credential
(short-lived credential).5. Request Access Token from ESTS
grant_type=client_credentials
scope=https://management.azure.com/.default
client_id
from the MSI response.client_assertion
containing the short-lived credential.client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer
.regional_token_url
with the certificate for mutual TLS (mTLS) authentication.6. Retrieve and Use Access Token
access_token
.End-to-End Script
This is just replication of what is in the .ps script.