Skip to content

Commit 50ba79c

Browse files
authored
[Identity] Caught up with POD Identity fix added on 1.5.1 (#18054)
1 parent 906cecf commit 50ba79c

File tree

3 files changed

+46
-26
lines changed

3 files changed

+46
-26
lines changed

sdk/identity/identity/CHANGELOG.md

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -15,6 +15,7 @@
1515
### Bugs Fixed
1616

1717
- Fixed a bug that caused `AzureCliCredential` to fail when a custom tenant ID was provided.
18+
- Caught up with the bug fixes for Azure POD Identity that were implemented on version 1.5.1.
1819

1920
### Other Changes
2021

sdk/identity/identity/src/credentials/managedIdentityCredential/imdsMsi.ts

Lines changed: 38 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -26,7 +26,7 @@ function expiresInParser(requestBody: any): number {
2626
// Use the expires_on timestamp if it's available
2727
const expires = +requestBody.expires_on * 1000;
2828
logger.info(
29-
`${msiName}: IMDS using expires_on: ${expires} (original value: ${requestBody.expires_on})`
29+
`${msiName}: Using expires_on: ${expires} (original value: ${requestBody.expires_on})`
3030
);
3131
return expires;
3232
} else {
@@ -41,33 +41,51 @@ function expiresInParser(requestBody: any): number {
4141

4242
function prepareRequestOptions(
4343
scopes: string | string[],
44-
clientId?: string
44+
clientId?: string,
45+
options?: {
46+
skipQuery?: boolean;
47+
skipMetadataHeader?: boolean;
48+
}
4549
): PipelineRequestOptions {
4650
const resource = mapScopesToResource(scopes);
4751
if (!resource) {
4852
throw new Error(`${msiName}: Multiple scopes are not supported.`);
4953
}
5054

51-
const queryParameters: any = {
52-
resource,
53-
"api-version": imdsApiVersion
54-
};
55-
56-
if (clientId) {
57-
queryParameters.client_id = clientId;
55+
const { skipQuery, skipMetadataHeader } = options || {};
56+
let query = "";
57+
58+
// Pod Identity will try to process this request even if the Metadata header is missing.
59+
// We can exclude the request query to ensure no IMDS endpoint tries to process the ping request.
60+
if (!skipQuery) {
61+
const queryParameters: any = {
62+
resource,
63+
"api-version": imdsApiVersion
64+
};
65+
if (clientId) {
66+
queryParameters.client_id = clientId;
67+
}
68+
const params = new URLSearchParams(queryParameters);
69+
query = `?${params.toString()}`;
5870
}
5971

60-
const params = new URLSearchParams(queryParameters);
61-
const query = params.toString();
6272
const url = new URL(imdsEndpointPath, process.env.AZURE_POD_IDENTITY_AUTHORITY_HOST ?? imdsHost);
6373

74+
const rawHeaders: Record<string, string> = {
75+
Accept: "application/json",
76+
Metadata: "true"
77+
};
78+
79+
// Remove the Metadata header to invoke a request error from some IMDS endpoints.
80+
if (skipMetadataHeader) {
81+
delete rawHeaders.Metadata;
82+
}
83+
6484
return {
65-
url: `${url}?${query}`,
85+
// In this case, the `?` should be added in the "query" variable `skipQuery` is not set.
86+
url: `${url}${query}`,
6687
method: "GET",
67-
headers: createHttpHeaders({
68-
Accept: "application/json",
69-
Metadata: "true"
70-
})
88+
headers: createHttpHeaders(rawHeaders)
7189
};
7290
}
7391

@@ -100,15 +118,10 @@ export const imdsMsi: MSI = {
100118
return true;
101119
}
102120

103-
const requestOptions = prepareRequestOptions(resource, clientId);
104-
105-
// This will always be populated, but let's make TypeScript happy
106-
if (requestOptions.headers) {
107-
// Remove the Metadata header to invoke a request error from
108-
// IMDS endpoint
109-
requestOptions.headers.delete("Metadata");
110-
}
111-
121+
const requestOptions = prepareRequestOptions(resource, clientId, {
122+
skipMetadataHeader: true,
123+
skipQuery: true
124+
});
112125
requestOptions.tracingOptions = options.tracingOptions;
113126

114127
try {

sdk/identity/identity/test/internal/node/managedIdentityCredential.spec.ts

Lines changed: 7 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,8 @@ import { RestError } from "@azure/core-rest-pipeline";
99
import { ManagedIdentityCredential } from "../../../src";
1010
import {
1111
imdsHost,
12-
imdsApiVersion
12+
imdsApiVersion,
13+
imdsEndpointPath
1314
} from "../../../src/credentials/managedIdentityCredential/constants";
1415
import {
1516
imdsMsi,
@@ -69,6 +70,11 @@ describe("ManagedIdentityCredential", function() {
6970
});
7071

7172
// The first request is the IMDS ping.
73+
// This ping request has to skip a header and the query parameters for it to work on POD identity.
74+
const imdsPingRequest = authDetails.requests[0];
75+
assert.ok(!imdsPingRequest.headers!.metadata);
76+
assert.equal(imdsPingRequest.url, new URL(imdsEndpointPath, imdsHost).toString());
77+
7278
// The second one tries to authenticate against IMDS once we know the endpoint is available.
7379
const authRequest = authDetails.requests[1];
7480

0 commit comments

Comments
 (0)