Skip to content

Commit 7fc8c29

Browse files
author
Kyle Hayes
committed
feat: support for labels
- Adding support for labels - Mixing in process.env for Azure loadFromAzure
1 parent a042b4c commit 7fc8c29

File tree

3 files changed

+19
-11
lines changed

3 files changed

+19
-11
lines changed

package-lock.json

+1-1
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

package.json

+1-1
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,6 @@
11
{
22
"name": "dotenv-azure",
3-
"version": "2.0.0",
3+
"version": "2.0.5",
44
"description": "Load environment variables from Azure's services App Configuration, Key Vault or a .env file",
55
"keywords": [
66
"azure",

src/dotenv-azure.ts

+17-9
Original file line numberDiff line numberDiff line change
@@ -1,7 +1,7 @@
11
import * as fs from 'fs'
22
import { URL } from 'url'
33
import Bottleneck from 'bottleneck'
4-
import dotenv, { DotenvParseOptions, DotenvParseOutput } from 'dotenv'
4+
import dotenv, { DotenvParseOptions } from 'dotenv'
55
import { ManagedIdentityCredential, ClientSecretCredential } from '@azure/identity'
66
import { SecretClient } from '@azure/keyvault-secrets'
77
import { AppConfigurationClient, ConfigurationSetting } from '@azure/app-configuration'
@@ -60,8 +60,8 @@ export default class DotenvAzure {
6060
async config(options: DotenvAzureConfigOptions = {}): Promise<DotenvAzureConfigOutput> {
6161
const { safe = false } = options
6262
const dotenvResult = dotenv.config(options)
63-
64-
const azureVars = await this.loadFromAzure(dotenvResult.parsed)
63+
const vars:Record<string, string | undefined> = {...(dotenvResult.parsed || {}), ...process.env}
64+
const azureVars = await this.loadFromAzure(vars)
6565
const joinedVars = { ...azureVars, ...dotenvResult.parsed }
6666

6767
populateProcessEnv(azureVars)
@@ -96,10 +96,15 @@ export default class DotenvAzure {
9696
* @param dotenvVars - dotenv parse() output containing azure credentials variables
9797
* @returns an object with keys and values
9898
*/
99-
async loadFromAzure(dotenvVars?: DotenvParseOutput): Promise<VariablesObject> {
99+
async loadFromAzure(dotenvVars?: Record<string, string | undefined>): Promise<VariablesObject> {
100+
// const vars = {...dotenvVars, ...process.env}
100101
const credentials = this.getAzureCredentials(dotenvVars)
101102
const appConfigClient = new AppConfigurationClient(credentials.connectionString)
102-
const { appConfigVars, keyVaultReferences } = await this.getAppConfigurations(appConfigClient)
103+
const labels = dotenvVars?.AZURE_APP_CONFIG_LABELS || ''
104+
const { appConfigVars, keyVaultReferences } = await this.getAppConfigurations(
105+
appConfigClient,
106+
labels
107+
)
103108
const keyVaultSecrets = await this.getSecretsFromKeyVault(credentials, keyVaultReferences)
104109
return { ...appConfigVars, ...keyVaultSecrets }
105110
}
@@ -114,11 +119,14 @@ export default class DotenvAzure {
114119
}
115120
}
116121

117-
protected async getAppConfigurations(client: AppConfigurationClient): Promise<AppConfigurations> {
122+
protected async getAppConfigurations(
123+
client: AppConfigurationClient,
124+
labels = ''
125+
): Promise<AppConfigurations> {
118126
const appConfigVars: VariablesObject = {}
119127
const keyVaultReferences: KeyVaultReferences = {}
120128

121-
for await (const config of client.listConfigurationSettings()) {
129+
for await (const config of client.listConfigurationSettings({ labelFilter: labels })) {
122130
if (this.isKeyVaultReference(config)) {
123131
keyVaultReferences[config.key] = this.getKeyVaultReferenceInfo(config)
124132
} else {
@@ -194,10 +202,10 @@ export default class DotenvAzure {
194202
)
195203
}
196204

197-
private getAzureCredentials(dotenvVars: DotenvParseOutput = {}): AzureCredentials {
205+
private getAzureCredentials(dotenvVars: Record<string, string | undefined> = {}): AzureCredentials {
198206
const vars = { ...dotenvVars, ...process.env }
199207
const connectionString = this.connectionString || vars.AZURE_APP_CONFIG_CONNECTION_STRING
200-
208+
201209
if (!connectionString) {
202210
throw new MissingAppConfigCredentialsError()
203211
}

0 commit comments

Comments
 (0)