From 7fc8c29dffece1ad755d2d0bd46dc190aa255099 Mon Sep 17 00:00:00 2001 From: Kyle Hayes Date: Wed, 4 Nov 2020 07:18:57 -0800 Subject: [PATCH] feat: support for labels - Adding support for labels - Mixing in process.env for Azure loadFromAzure --- package-lock.json | 2 +- package.json | 2 +- src/dotenv-azure.ts | 26 +++++++++++++++++--------- 3 files changed, 19 insertions(+), 11 deletions(-) diff --git a/package-lock.json b/package-lock.json index e1ed44cb..3f0fb96f 100644 --- a/package-lock.json +++ b/package-lock.json @@ -1,6 +1,6 @@ { "name": "dotenv-azure", - "version": "2.0.0", + "version": "2.0.5", "lockfileVersion": 1, "requires": true, "dependencies": { diff --git a/package.json b/package.json index 394ad0c8..bf97fcaf 100644 --- a/package.json +++ b/package.json @@ -1,6 +1,6 @@ { "name": "dotenv-azure", - "version": "2.0.0", + "version": "2.0.5", "description": "Load environment variables from Azure's services App Configuration, Key Vault or a .env file", "keywords": [ "azure", diff --git a/src/dotenv-azure.ts b/src/dotenv-azure.ts index 1a5ce761..7e5aae66 100644 --- a/src/dotenv-azure.ts +++ b/src/dotenv-azure.ts @@ -1,7 +1,7 @@ import * as fs from 'fs' import { URL } from 'url' import Bottleneck from 'bottleneck' -import dotenv, { DotenvParseOptions, DotenvParseOutput } from 'dotenv' +import dotenv, { DotenvParseOptions } from 'dotenv' import { ManagedIdentityCredential, ClientSecretCredential } from '@azure/identity' import { SecretClient } from '@azure/keyvault-secrets' import { AppConfigurationClient, ConfigurationSetting } from '@azure/app-configuration' @@ -60,8 +60,8 @@ export default class DotenvAzure { async config(options: DotenvAzureConfigOptions = {}): Promise { const { safe = false } = options const dotenvResult = dotenv.config(options) - - const azureVars = await this.loadFromAzure(dotenvResult.parsed) + const vars:Record = {...(dotenvResult.parsed || {}), ...process.env} + const azureVars = await this.loadFromAzure(vars) const joinedVars = { ...azureVars, ...dotenvResult.parsed } populateProcessEnv(azureVars) @@ -96,10 +96,15 @@ export default class DotenvAzure { * @param dotenvVars - dotenv parse() output containing azure credentials variables * @returns an object with keys and values */ - async loadFromAzure(dotenvVars?: DotenvParseOutput): Promise { + async loadFromAzure(dotenvVars?: Record): Promise { + // const vars = {...dotenvVars, ...process.env} const credentials = this.getAzureCredentials(dotenvVars) const appConfigClient = new AppConfigurationClient(credentials.connectionString) - const { appConfigVars, keyVaultReferences } = await this.getAppConfigurations(appConfigClient) + const labels = dotenvVars?.AZURE_APP_CONFIG_LABELS || '' + const { appConfigVars, keyVaultReferences } = await this.getAppConfigurations( + appConfigClient, + labels + ) const keyVaultSecrets = await this.getSecretsFromKeyVault(credentials, keyVaultReferences) return { ...appConfigVars, ...keyVaultSecrets } } @@ -114,11 +119,14 @@ export default class DotenvAzure { } } - protected async getAppConfigurations(client: AppConfigurationClient): Promise { + protected async getAppConfigurations( + client: AppConfigurationClient, + labels = '' + ): Promise { const appConfigVars: VariablesObject = {} const keyVaultReferences: KeyVaultReferences = {} - for await (const config of client.listConfigurationSettings()) { + for await (const config of client.listConfigurationSettings({ labelFilter: labels })) { if (this.isKeyVaultReference(config)) { keyVaultReferences[config.key] = this.getKeyVaultReferenceInfo(config) } else { @@ -194,10 +202,10 @@ export default class DotenvAzure { ) } - private getAzureCredentials(dotenvVars: DotenvParseOutput = {}): AzureCredentials { + private getAzureCredentials(dotenvVars: Record = {}): AzureCredentials { const vars = { ...dotenvVars, ...process.env } const connectionString = this.connectionString || vars.AZURE_APP_CONFIG_CONNECTION_STRING - + if (!connectionString) { throw new MissingAppConfigCredentialsError() }