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
/credentialendpoint./credentialand 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:
sequenceDiagram participant Application participant MSAL participant IMDS participant ESTS Application ->> MSAL: 1. Request token using Managed Identity MSAL ->> IMDS: 2. Probe for `/credential` endpoint availability IMDS -->> MSAL: 3. Response (200 OK / 404 Not Found) alt `/credential` endpoint available MSAL ->> IMDS: 4. Request Short-Lived Credential (SLC) via `/credential` IMDS -->> MSAL: 5. Return SLC MSAL ->> ESTS: 6. Exchange SLC for Access Token via MTLS ESTS -->> MSAL: 7. Return Access Token MSAL ->> Application: 8. Return Access Token else `/credential` endpoint not available MSAL ->> IMDS: 4a. Fallback to legacy `/token` endpoint IMDS -->> MSAL: 5a. Return Access Token MSAL ->> Application: 6a. Return Access Token endShort-Lived Credential Retrieval from
/credentialEndpoint/credentialendpoint./credentialendpoint 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
/credentialendpoint. 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
/credentialendpoint.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
/credentialendpoint with the certificate details.Metadata: trueheader.X-ms-Client-Request-idheader with a GUID.jwkformat. RFCregional_token_urltenant_idclient_idcredential(short-lived credential).5. Request Access Token from ESTS
grant_type=client_credentialsscope=https://management.azure.com/.defaultclient_idfrom the MSI response.client_assertioncontaining the short-lived credential.client_assertion_type=urn:ietf:params:oauth:client-assertion-type:jwt-bearer.regional_token_urlwith 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.