Skip to content
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
4 changes: 4 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -23,3 +23,7 @@ trademarks or logos is subject to and must follow
[Microsoft's Trademark & Brand Guidelines](https://www.microsoft.com/en-us/legal/intellectualproperty/trademarks/usage/general).
Use of Microsoft trademarks or logos in modified versions of this project must not cause confusion or imply Microsoft sponsorship.
Any use of third-party trademarks or logos are subject to those third-party's policies.

# Data Collection

The software may collect information about you and your use of the software and send it to Microsoft. Microsoft may use this information to provide services and improve our products and services. You may turn off the telemetry by setting the environment variable `AZURE_APP_CONFIGURATION_TRACING_DISABLED` to `TRUE`. There are also some features in the software that may enable you and Microsoft to collect data from users of your applications. If you use these features, you must comply with applicable law, including providing appropriate notices to users of your applications together with a copy of Microsoft’s privacy statement. Our privacy statement is located at https://go.microsoft.com/fwlink/?LinkID=824704. You can learn more about data collection and use in the help documentation and our privacy statement. Your use of the software operates as your consent to these practices.
44 changes: 39 additions & 5 deletions src/AzureAppConfigurationImpl.ts
Original file line number Diff line number Diff line change
@@ -1,14 +1,16 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { AppConfigurationClient, ConfigurationSetting } from "@azure/app-configuration";
import { AppConfigurationClient, ConfigurationSetting, ListConfigurationSettingsOptions } from "@azure/app-configuration";
import { AzureAppConfiguration } from "./AzureAppConfiguration";
import { AzureAppConfigurationOptions } from "./AzureAppConfigurationOptions";
import { IKeyValueAdapter } from "./IKeyValueAdapter";
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
import { KeyFilter } from "./KeyFilter";
import { LabelFilter } from "./LabelFilter";
import { AzureKeyVaultKeyValueAdapter } from "./keyvault/AzureKeyVaultKeyValueAdapter";
import { JsonKeyValueAdapter } from "./JsonKeyValueAdapter";
import { CorrelationContextHeaderName, RequestTracingDisabledEnvironmentVariable } from "./requestTracing/constants";
import { createCorrelationContextHeader } from "./requestTracing/utils";

export class AzureAppConfigurationImpl extends Map<string, unknown> implements AzureAppConfiguration {
private adapters: IKeyValueAdapter[] = [];
Expand All @@ -17,12 +19,23 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
* Since multiple prefixes could start with the same characters, we need to trim the longest prefix first.
*/
private sortedTrimKeyPrefixes: string[] | undefined;
private readonly requestTracingEnabled: boolean;
private correlationContextHeader: string | undefined;

constructor(
private client: AppConfigurationClient,
private options: AzureAppConfigurationOptions | undefined
) {
super();
// Enable request tracing if not opt-out
const requestTracingDisabledEnv = process.env[RequestTracingDisabledEnvironmentVariable];
if (requestTracingDisabledEnv && requestTracingDisabledEnv.toLowerCase() === "true") {
this.requestTracingEnabled = false;
} else {
this.requestTracingEnabled = true;
this.enableRequestTracing();
}

if (options?.trimKeyPrefixes) {
this.sortedTrimKeyPrefixes = [...options.trimKeyPrefixes].sort((a, b) => b.localeCompare(a));
}
Expand All @@ -36,10 +49,17 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
const keyValues: [key: string, value: unknown][] = [];
const selectors = this.options?.selectors ?? [{ keyFilter: KeyFilter.Any, labelFilter: LabelFilter.Null }];
for (const selector of selectors) {
const settings = this.client.listConfigurationSettings({
const listOptions: ListConfigurationSettingsOptions = {
keyFilter: selector.keyFilter,
labelFilter: selector.labelFilter
});
};
if (this.requestTracingEnabled) {
listOptions.requestOptions = {
customHeaders: this.customHeaders()
}
}

const settings = this.client.listConfigurationSettings(listOptions);

for await (const setting of settings) {
if (setting.key) {
Expand All @@ -55,7 +75,7 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
}

private async processAdapters(setting: ConfigurationSetting<string>): Promise<[string, unknown]> {
for(const adapter of this.adapters) {
for (const adapter of this.adapters) {
if (adapter.canProcess(setting)) {
return adapter.processKeyValue(setting);
}
Expand All @@ -73,4 +93,18 @@ export class AzureAppConfigurationImpl extends Map<string, unknown> implements A
}
return key;
}

private enableRequestTracing() {
this.correlationContextHeader = createCorrelationContextHeader(this.options);
}

private customHeaders() {
if (!this.requestTracingEnabled) {
return undefined;
}

const headers = {};
headers[CorrelationContextHeaderName] = this.correlationContextHeader;
return headers;
}
}
2 changes: 1 addition & 1 deletion src/index.ts
Original file line number Diff line number Diff line change
@@ -1,7 +1,7 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export { load } from "./Load";
export { load } from "./load";
export { AzureAppConfiguration } from "./AzureAppConfiguration";
export { KeyFilter } from "./KeyFilter";
export { LabelFilter } from "./LabelFilter";
18 changes: 14 additions & 4 deletions src/Load.ts → src/load.ts
Original file line number Diff line number Diff line change
Expand Up @@ -6,6 +6,7 @@ import { TokenCredential } from "@azure/identity";
import { AzureAppConfiguration } from "./AzureAppConfiguration";
import { AzureAppConfigurationImpl } from "./AzureAppConfigurationImpl";
import { AzureAppConfigurationOptions, MaxRetries, MaxRetryDelayInMs } from "./AzureAppConfigurationOptions";
import * as RequestTracing from "./requestTracing/constants";

export async function load(connectionString: string, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
export async function load(endpoint: URL | string, credential: TokenCredential, options?: AzureAppConfigurationOptions): Promise<AzureAppConfiguration>;
Expand Down Expand Up @@ -38,7 +39,7 @@ export async function load(
const credential = credentialOrOptions as TokenCredential;
options = appConfigOptions;
const clientOptions = getClientOptions(options);
client = new AppConfigurationClient(endpoint.toString(), credential, clientOptions)
client = new AppConfigurationClient(endpoint.toString(), credential, clientOptions);
} else {
throw new Error("A connection string or an endpoint with credential must be specified to create a client.");
}
Expand All @@ -53,15 +54,24 @@ function instanceOfTokenCredential(obj: unknown) {
}

function getClientOptions(options?: AzureAppConfigurationOptions): AppConfigurationClientOptions | undefined {
// TODO: user-agent
// TODO: set correlation context using additional policies
// user-agent
let userAgentPrefix = RequestTracing.UserAgentPrefix; // Default UA for JavaScript Provider
const userAgentOptions = options?.clientOptions?.userAgentOptions;
if (userAgentOptions?.userAgentPrefix) {
userAgentPrefix = `${userAgentOptions.userAgentPrefix} ${userAgentPrefix}`; // Prepend if UA prefix specified by user
}

// retry options
const defaultRetryOptions = {
maxRetries: MaxRetries,
maxRetryDelayInMs: MaxRetryDelayInMs,
}
const retryOptions = Object.assign({}, defaultRetryOptions, options?.clientOptions?.retryOptions);

return Object.assign({}, options?.clientOptions, {
retryOptions
retryOptions,
userAgentOptions: {
userAgentPrefix
}
});
}
45 changes: 45 additions & 0 deletions src/requestTracing/constants.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,45 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { Version } from "../version";

export const RequestTracingDisabledEnvironmentVariable = "AZURE_APP_CONFIGURATION_TRACING_DISABLED";

// User Agent
export const UserAgentPrefix = `javascript-appconfiguration-provider/${Version}`;

// Correlation Context
export const CorrelationContextHeaderName = "Correlation-Context";

// Env
export const NodeJSEnvironmentVariable = "NODE_ENV";
export const NodeJSDevEnvironmentVariableValue = "development";
export const EnvironmentKey = "Env";
export const DevEnvironmentValue = "Dev";

// Host Type
export const HostTypeKey = "Host";
export enum HostType {
AzureFunction = "AzureFunction",
AzureWebApp = "AzureWebApp",
ContainerApp = "ContainerApp",
Kubernetes = "Kubernetes",
ServiceFabric = "ServiceFabric"
}

// Environment variables to identify Host type.
export const AzureFunctionEnvironmentVariable = "FUNCTIONS_EXTENSION_VERSION";
export const AzureWebAppEnvironmentVariable = "WEBSITE_SITE_NAME";
export const ContainerAppEnvironmentVariable = "CONTAINER_APP_NAME";
export const KubernetesEnvironmentVariable = "KUBERNETES_PORT";
export const ServiceFabricEnvironmentVariable = "Fabric_NodeName"; // See: https://docs.microsoft.com/en-us/azure/service-fabric/service-fabric-environment-variables-reference

// Request Type
export const RequestTypeKey = "RequestType";
export enum RequestType {
Startup = "Startup",
Watch = "Watch"
}

// Tag names
export const KeyVaultConfiguredTag = "UsesKeyVault";
78 changes: 78 additions & 0 deletions src/requestTracing/utils.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,78 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

import { AzureAppConfigurationOptions } from "../AzureAppConfigurationOptions";
import {
AzureFunctionEnvironmentVariable,
AzureWebAppEnvironmentVariable,
ContainerAppEnvironmentVariable,
DevEnvironmentValue,
EnvironmentKey,
HostType,
HostTypeKey,
KeyVaultConfiguredTag,
KubernetesEnvironmentVariable,
NodeJSDevEnvironmentVariableValue,
NodeJSEnvironmentVariable,
RequestType,
RequestTypeKey,
ServiceFabricEnvironmentVariable
} from "./constants";

// Utils
export function createCorrelationContextHeader(options: AzureAppConfigurationOptions | undefined): string {
/*
RequestType: 'Startup' during application starting up, 'Watch' after startup completed.
Host: identify with defined envs
Env: identify by env `NODE_ENV` which is a popular but not standard.usually the value can be "development", "production".
UsersKeyVault
*/
const keyValues = new Map<string, string | undefined>();
keyValues.set(RequestTypeKey, RequestType.Startup); // TODO: now always "Startup", until refresh is supported.
keyValues.set(HostTypeKey, getHostType());
keyValues.set(EnvironmentKey, isDevEnvironment() ? DevEnvironmentValue : undefined);

const tags: string[] = [];
if (options?.keyVaultOptions) {
const { credential, secretClients, secretResolver } = options.keyVaultOptions;
if (credential !== undefined || secretClients?.length || secretResolver !== undefined) {
tags.push(KeyVaultConfiguredTag);
}
}

const contextParts: string[] = [];
for (const [k, v] of keyValues) {
if (v !== undefined) {
contextParts.push(`${k}=${v}`);
}
}
for (const tag of tags) {
contextParts.push(tag);
}

return contextParts.join(",");
}

function getHostType(): string | undefined {
let hostType: string | undefined;
if (process.env[AzureFunctionEnvironmentVariable]) {
hostType = HostType.AzureFunction;
} else if (process.env[AzureWebAppEnvironmentVariable]) {
hostType = HostType.AzureWebApp;
} else if (process.env[ContainerAppEnvironmentVariable]) {
hostType = HostType.ContainerApp;
} else if (process.env[KubernetesEnvironmentVariable]) {
hostType = HostType.Kubernetes;
} else if (process.env[ServiceFabricEnvironmentVariable]) {
hostType = HostType.ServiceFabric;
}
return hostType;
}

function isDevEnvironment(): boolean {
const envType = process.env[NodeJSEnvironmentVariable];
if (NodeJSDevEnvironmentVariableValue === envType?.toLowerCase()) {
return true;
}
return false;
}
4 changes: 4 additions & 0 deletions src/version.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,4 @@
// Copyright (c) Microsoft Corporation.
// Licensed under the MIT license.

export const Version = "0.1.0-preview";
Loading