Skip to content
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

[app-configuration] Use the x-ms-useragent header when in the browser #6528

Merged
Show file tree
Hide file tree
Changes from all 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
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,7 @@ import {
isTokenCredential,
exponentialRetryPolicy,
systemErrorRetryPolicy,
ServiceClientCredentials,
} from "@azure/core-http";
import { throttlingRetryPolicy } from "./policies/throttlingRetryPolicy";
import { TokenCredential } from "@azure/identity";
Expand Down Expand Up @@ -48,9 +49,9 @@ import {
transformKeyValue,
formatAcceptDateTime
} from "./internal/helpers";
import { tracingPolicy } from "@azure/core-http";
import { tracingPolicy, isNode as isNodeFromCoreHttp } from "@azure/core-http";
import { Spanner } from "./internal/tracingHelpers";
import { GetKeyValuesResponse, AppConfigurationOptions } from "./generated/src/models";
import { GetKeyValuesResponse, AppConfigurationOptions as GeneratedAppConfigurationClientOptions } from "./generated/src/models";
import { syncTokenPolicy, SyncTokens } from './internal/synctokenpolicy';

const apiVersion = "1.0";
Expand Down Expand Up @@ -82,6 +83,11 @@ export interface InternalAppConfigurationClientOptions extends AppConfigurationC
* NOTE: this is an internal option, not for general client usage.
*/
syncTokens?: SyncTokens;
/**
* Whether we want to run as if we're in node or in the browser.
* (currently only affects which name we use for the user agent header)
*/
isNodeOverride?: boolean;
}

/**
Expand Down Expand Up @@ -110,38 +116,37 @@ export class AppConfigurationClient {
tokenCredentialOrOptions?: TokenCredential | AppConfigurationClientOptions,
options?:AppConfigurationClientOptions
) {
let appConfigOptions: InternalAppConfigurationClientOptions = {};
let appConfigCredential: ServiceClientCredentials | TokenCredential;
let appConfigEndpoint: string;

if (isTokenCredential(tokenCredentialOrOptions)) {
const syncTokens =
(options && (options as InternalAppConfigurationClientOptions).syncTokens) ||
new SyncTokens();

this.client = new AppConfiguration(
tokenCredentialOrOptions,
apiVersion,
getAppConfigurationOptions(connectionStringOrEndpoint, syncTokens)
);
appConfigOptions = (options as InternalAppConfigurationClientOptions) || {};
appConfigCredential = tokenCredentialOrOptions;
appConfigEndpoint = connectionStringOrEndpoint;
} else {
const syncTokens =
(tokenCredentialOrOptions &&
(tokenCredentialOrOptions as InternalAppConfigurationClientOptions).syncTokens) ||
new SyncTokens();
appConfigOptions = (tokenCredentialOrOptions as InternalAppConfigurationClientOptions) || {};

const regexMatch = connectionStringOrEndpoint.match(ConnectionStringRegex);
if (regexMatch) {
const appConfigCredential = new AppConfigCredential(regexMatch[2], regexMatch[3]);

this.client = new AppConfiguration(
appConfigCredential,
apiVersion,
getAppConfigurationOptions(regexMatch[1], syncTokens)
);
if (regexMatch) {
appConfigCredential = new AppConfigCredential(regexMatch[2], regexMatch[3]);
appConfigEndpoint = regexMatch[1];
} else {
throw new Error(
`Invalid connection string. Valid connection strings should match the regex '${ConnectionStringRegex.source}'.`
);
}
}

const syncTokens = appConfigOptions.syncTokens || new SyncTokens();

this.client = new AppConfiguration(
appConfigCredential,
apiVersion,
getGeneratedClientOptions(appConfigEndpoint, syncTokens, appConfigOptions)
);

this.spanner = new Spanner<AppConfigurationClient>("Azure.Data.AppConfiguration", "appconfig");
}

Expand Down Expand Up @@ -475,14 +480,20 @@ export class AppConfigurationClient {
}
}

function getAppConfigurationOptions(
/**
* Gets the options for the generated AppConfigurationClient
* @internal
* @ignore
*/
export function getGeneratedClientOptions(
baseUri: string,
syncTokens: SyncTokens,
): AppConfigurationOptions {

internalAppConfigOptions: InternalAppConfigurationClientOptions
): GeneratedAppConfigurationClientOptions {

const retryPolicies = [
exponentialRetryPolicy(),
systemErrorRetryPolicy(),
systemErrorRetryPolicy(),
throttlingRetryPolicy()
];

Expand All @@ -495,8 +506,30 @@ function getAppConfigurationOptions(
tracingPolicy(),
syncTokenPolicy(syncTokens),
...retryPolicies,
...defaults,
]
...defaults,
],
userAgentHeaderName: getUserAgentHeaderName(internalAppConfigOptions)
};
}

/**
* @ignore
* @internal
*/
export function getUserAgentHeaderName(internalAppConfigOptions: InternalAppConfigurationClientOptions) {
richardpark-msft marked this conversation as resolved.
Show resolved Hide resolved
const isNode = internalAppConfigOptions.isNodeOverride == null
? isNodeFromCoreHttp
: internalAppConfigOptions.isNodeOverride;

let userAgentHeaderName: string | undefined;

if (!isNode) {
// we only need to override this when we're in the browser
// where we're (mostly) not allowed to override the User-Agent
// header.
userAgentHeaderName = "x-ms-useragent";
}

return userAgentHeaderName;
}

Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,9 @@ import * as assert from "assert";
import { AppConfigurationClient } from "../../src";
const nock = require("nock");
import { createAppConfigurationClientForTests, assertThrowsRestError, getTokenAuthenticationCredential } from "../testHelpers";
import { getUserAgentHeaderName } from '../../src/appConfigurationClient';

describe("sync tokens", () => {
describe("http request related tests", () => {
describe("unit tests", () => {
describe("parseSyncToken", () => {
it("can parse various sync tokens", () => {
Expand All @@ -30,6 +31,20 @@ describe("sync tokens", () => {
});
});

it("useragentheadername", () => {
assert.ok(getUserAgentHeaderName({
isNodeOverride: true
}) === undefined);

// since we're only running these tests in node this will be the same as the
// case above (undefined, thus using the normal User-Agent header)
assert.ok(getUserAgentHeaderName({}) === undefined);

assert.equal(getUserAgentHeaderName({
isNodeOverride: false
}), "x-ms-useragent");
});

describe("syncTokens", () => {
it("basic", () => {
const syncTokens = new SyncTokens();
Expand Down Expand Up @@ -73,7 +88,7 @@ describe("sync tokens", () => {
// properly extracting and sending the sync token header (which is
// why they appear to not do much of anything meaningful with what
// they send or reply back with).
describe("request/reply tests for header", () => {
describe("request/reply tests for sync token headers", () => {
let client: AppConfigurationClient;
let syncTokens: SyncTokens;
let scope: any;
Expand Down
1 change: 1 addition & 0 deletions sdk/core/core-http/lib/policies/logPolicy.ts
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@ const RedactedString = "REDACTED";
const defaultAllowedHeaderNames = [
"x-ms-client-request-id",
"x-ms-return-client-request-id",
"x-ms-useragent",
"traceparent",

"Accept",
Expand Down