UNPKG

@azure/identity

Version:

Provides credential implementations for Azure SDK libraries that can authenticate with Microsoft Entra ID

102 lines (101 loc) 4.05 kB
var __defProp = Object.defineProperty; var __getOwnPropDesc = Object.getOwnPropertyDescriptor; var __getOwnPropNames = Object.getOwnPropertyNames; var __hasOwnProp = Object.prototype.hasOwnProperty; var __export = (target, all) => { for (var name in all) __defProp(target, name, { get: all[name], enumerable: true }); }; var __copyProps = (to, from, except, desc) => { if (from && typeof from === "object" || typeof from === "function") { for (let key of __getOwnPropNames(from)) if (!__hasOwnProp.call(to, key) && key !== except) __defProp(to, key, { get: () => from[key], enumerable: !(desc = __getOwnPropDesc(from, key)) || desc.enumerable }); } return to; }; var __toCommonJS = (mod) => __copyProps(__defProp({}, "__esModule", { value: true }), mod); var imdsMsi_exports = {}; __export(imdsMsi_exports, { imdsMsi: () => imdsMsi }); module.exports = __toCommonJS(imdsMsi_exports); var import_core_rest_pipeline = require("@azure/core-rest-pipeline"); var import_core_util = require("@azure/core-util"); var import_logging = require("../../util/logging.js"); var import_utils = require("./utils.js"); var import_tracing = require("../../util/tracing.js"); const msiName = "ManagedIdentityCredential - IMDS"; const logger = (0, import_logging.credentialLogger)(msiName); const imdsHost = "http://169.254.169.254"; const imdsEndpointPath = "/metadata/identity/oauth2/token"; function prepareInvalidRequestOptions(scopes) { const resource = (0, import_utils.mapScopesToResource)(scopes); if (!resource) { throw new Error(`${msiName}: Multiple scopes are not supported.`); } const url = new URL(imdsEndpointPath, process.env.AZURE_POD_IDENTITY_AUTHORITY_HOST ?? imdsHost); const rawHeaders = { Accept: "application/json" // intentionally leave out the Metadata header to invoke an error from IMDS endpoint. }; return { // intentionally not including any query url: `${url}`, method: "GET", headers: (0, import_core_rest_pipeline.createHttpHeaders)(rawHeaders) }; } const imdsMsi = { name: "imdsMsi", async isAvailable(options) { const { scopes, identityClient, getTokenOptions } = options; const resource = (0, import_utils.mapScopesToResource)(scopes); if (!resource) { logger.info(`${msiName}: Unavailable. Multiple scopes are not supported.`); return false; } if (process.env.AZURE_POD_IDENTITY_AUTHORITY_HOST) { return true; } if (!identityClient) { throw new Error("Missing IdentityClient"); } const requestOptions = prepareInvalidRequestOptions(resource); return import_tracing.tracingClient.withSpan( "ManagedIdentityCredential-pingImdsEndpoint", getTokenOptions ?? {}, async (updatedOptions) => { requestOptions.tracingOptions = updatedOptions.tracingOptions; const request = (0, import_core_rest_pipeline.createPipelineRequest)(requestOptions); request.timeout = updatedOptions.requestOptions?.timeout || 1e3; request.allowInsecureConnection = true; let response; try { logger.info(`${msiName}: Pinging the Azure IMDS endpoint`); response = await identityClient.sendRequest(request); } catch (err) { if ((0, import_core_util.isError)(err)) { logger.verbose(`${msiName}: Caught error ${err.name}: ${err.message}`); } logger.info(`${msiName}: The Azure IMDS endpoint is unavailable`); return false; } if (response.status === 403) { if (response.bodyAsText?.includes("unreachable")) { logger.info(`${msiName}: The Azure IMDS endpoint is unavailable`); logger.info(`${msiName}: ${response.bodyAsText}`); return false; } } logger.info(`${msiName}: The Azure IMDS endpoint is available`); return true; } ); } }; // Annotate the CommonJS export names for ESM import in node: 0 && (module.exports = { imdsMsi }); //# sourceMappingURL=imdsMsi.js.map