Skip to content

[2.x] Fix Crashing with Old Node.JS Versions When Manually Instrumenting #1362

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
Merged
Show file tree
Hide file tree
Changes from 3 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
5 changes: 2 additions & 3 deletions Bootstrap/Default.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,4 @@
import * as azureCoreAuth from "@azure/core-auth";

import type { TokenCredential } from "@azure/core-auth";
import * as types from "../applicationinsights";
import * as Helpers from "./Helpers";
import Constants = require("../Declarations/Constants");
Expand Down Expand Up @@ -40,7 +39,7 @@ export function setStatusLogger(statusLogger: StatusLogger) {
* Try to setup and start this app insights instance if attach is enabled.
* @param aadTokenCredential Optional AAD credential
*/
export function setupAndStart(aadTokenCredential?: azureCoreAuth.TokenCredential, isAzureFunction?: boolean): typeof types | null {
export function setupAndStart(aadTokenCredential?: TokenCredential, isAzureFunction?: boolean): typeof types | null {
// If app already contains SDK, skip agent attach
if (!forceStart && Helpers.sdkAlreadyExists(_logger)) {
_statusLogger.logStatus({
Expand Down
4 changes: 2 additions & 2 deletions Declarations/Interfaces.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
import http = require("http");
import https = require("https");
import * as azureCoreAuth from "@azure/core-auth";
import type { TokenCredential } from "@azure/core-auth";
import { DistributedTracingModes } from "../applicationinsights";
import { IDisabledExtendedMetrics } from "../AutoCollection/NativePerformance";

Expand Down Expand Up @@ -224,5 +224,5 @@ export interface IConfig extends IBaseConfig {
/** An https.Agent to use for SDK HTTPS traffic (Optional, Default undefined) */
httpsAgent: https.Agent;
/** AAD TokenCredential to use to authenticate the app */
aadTokenCredential?: azureCoreAuth.TokenCredential;
aadTokenCredential?: TokenCredential;
}
34 changes: 22 additions & 12 deletions Library/AuthorizationHandler.ts
Original file line number Diff line number Diff line change
@@ -1,32 +1,42 @@
import http = require("http");
import https = require("https");
import azureCoreAuth = require("@azure/core-auth");
import azureCore = require("@azure/core-rest-pipeline");
import { TokenCredential } from "@azure/core-auth";
import { PipelineRequest, PipelineResponse, PipelinePolicy } from "@azure/core-rest-pipeline";
import Logging = require("./Logging");

const applicationInsightsResource = "https://monitor.azure.com//.default";

let azureCore: any;
try {
azureCore = require("@azure/core-rest-pipeline");
} catch (e) {
Logging.warn("Cannot load @azure/core-auth package. This package is required for AAD token authentication. It's likely that your node.js version is not supported by the JS Azure SDK.");
}

function emptySendRequest(_request: azureCore.PipelineRequest): Promise<azureCore.PipelineResponse> {
function emptySendRequest(_request: PipelineRequest): Promise<PipelineResponse> {
return null;
}

class AuthorizationHandler {

private _azureTokenPolicy: azureCore.PipelinePolicy;
private _azureTokenPolicy: PipelinePolicy;

constructor(credential: azureCoreAuth.TokenCredential, aadAudience?: string) {
let scopes: string[] = aadAudience ? [aadAudience] : [applicationInsightsResource];
this._azureTokenPolicy = azureCore.bearerTokenAuthenticationPolicy({ credential, scopes });
constructor(credential: TokenCredential, aadAudience?: string) {
if (azureCore) {
let scopes: string[] = aadAudience ? [aadAudience] : [applicationInsightsResource];
this._azureTokenPolicy = azureCore.bearerTokenAuthenticationPolicy({ credential, scopes });
}
}

/**
* Applies the Bearer token to the request through the Authorization header.
*/
public async addAuthorizationHeader(requestOptions: http.RequestOptions | https.RequestOptions): Promise<void> {
let authHeaderName = "authorization";
let webResource = azureCore.createPipelineRequest({ url: "https://" });
await this._azureTokenPolicy.sendRequest(webResource, emptySendRequest);
requestOptions.headers[authHeaderName] = webResource.headers.get(authHeaderName);
if (azureCore) {
let authHeaderName = "authorization";
let webResource = azureCore.createPipelineRequest({ url: "https://" });
await this._azureTokenPolicy.sendRequest(webResource, emptySendRequest);
requestOptions.headers[authHeaderName] = webResource.headers.get(authHeaderName);
}
}
}

Expand Down
5 changes: 2 additions & 3 deletions Library/Config.ts
Original file line number Diff line number Diff line change
@@ -1,5 +1,3 @@
import azureCoreAuth = require("@azure/core-auth");

import CorrelationIdManager = require("./CorrelationIdManager");
import ConnectionStringParser = require("./ConnectionStringParser");
import Logging = require("./Logging");
Expand All @@ -11,6 +9,7 @@ import { JsonConfig } from "./JsonConfig";
import { IConfig, IWebInstrumentationConfig } from "../Declarations/Interfaces";
import { DistributedTracingModes } from "../applicationinsights";
import { IDisabledExtendedMetrics } from "../AutoCollection/NativePerformance";
import type { TokenCredential } from "@azure/core-auth";

class Config implements IConfig {

Expand All @@ -33,7 +32,7 @@ class Config implements IConfig {
public httpAgent: http.Agent;
public httpsAgent: https.Agent;
public ignoreLegacyHeaders: boolean;
public aadTokenCredential?: azureCoreAuth.TokenCredential;
public aadTokenCredential?: TokenCredential;
public aadAudience?: string;
public enableAutoCollectConsole: boolean;
public enableLoggerErrorToTrace: boolean;
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -32,7 +32,8 @@ You can manually track more aspects of your app and system using the API describ



## Supported Node.JS versions
## Supported Node.JS versions
> *Note:* Versions of Node.JS that are not supported by the [JS Azure SDK](https://github.com/Azure/azure-sdk-for-js/blob/main/SUPPORT.md) will not support AAD authentication.
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This is a little bit confusing, we need azure/identity package to be included to use the feature, as customer need to pass an instance of a Credential object, so this extra note could cause more confusion than helping here.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I'll reword to make it clearer what the exception to our support for older versions really is.

Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I moved the note to the aadCredential description. Hopefully that makes it clearer that old versions of Node don't impact using any other scenario with the SDK.


| Platform Version | Supported |
|------------------|-------------------------------------------------|
Expand Down
Loading