1
1
import * as fs from 'fs'
2
2
import { URL } from 'url'
3
3
import Bottleneck from 'bottleneck'
4
- import dotenv , { DotenvParseOptions , DotenvParseOutput } from 'dotenv'
4
+ import dotenv , { DotenvParseOptions } from 'dotenv'
5
5
import { ManagedIdentityCredential , ClientSecretCredential } from '@azure/identity'
6
6
import { SecretClient } from '@azure/keyvault-secrets'
7
7
import { AppConfigurationClient , ConfigurationSetting } from '@azure/app-configuration'
@@ -60,8 +60,8 @@ export default class DotenvAzure {
60
60
async config ( options : DotenvAzureConfigOptions = { } ) : Promise < DotenvAzureConfigOutput > {
61
61
const { safe = false } = options
62
62
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 )
65
65
const joinedVars = { ...azureVars , ...dotenvResult . parsed }
66
66
67
67
populateProcessEnv ( azureVars )
@@ -96,10 +96,15 @@ export default class DotenvAzure {
96
96
* @param dotenvVars - dotenv parse() output containing azure credentials variables
97
97
* @returns an object with keys and values
98
98
*/
99
- async loadFromAzure ( dotenvVars ?: DotenvParseOutput ) : Promise < VariablesObject > {
99
+ async loadFromAzure ( dotenvVars ?: Record < string , string | undefined > ) : Promise < VariablesObject > {
100
+ // const vars = {...dotenvVars, ...process.env}
100
101
const credentials = this . getAzureCredentials ( dotenvVars )
101
102
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
+ )
103
108
const keyVaultSecrets = await this . getSecretsFromKeyVault ( credentials , keyVaultReferences )
104
109
return { ...appConfigVars , ...keyVaultSecrets }
105
110
}
@@ -114,11 +119,14 @@ export default class DotenvAzure {
114
119
}
115
120
}
116
121
117
- protected async getAppConfigurations ( client : AppConfigurationClient ) : Promise < AppConfigurations > {
122
+ protected async getAppConfigurations (
123
+ client : AppConfigurationClient ,
124
+ labels = ''
125
+ ) : Promise < AppConfigurations > {
118
126
const appConfigVars : VariablesObject = { }
119
127
const keyVaultReferences : KeyVaultReferences = { }
120
128
121
- for await ( const config of client . listConfigurationSettings ( ) ) {
129
+ for await ( const config of client . listConfigurationSettings ( { labelFilter : labels } ) ) {
122
130
if ( this . isKeyVaultReference ( config ) ) {
123
131
keyVaultReferences [ config . key ] = this . getKeyVaultReferenceInfo ( config )
124
132
} else {
@@ -194,10 +202,10 @@ export default class DotenvAzure {
194
202
)
195
203
}
196
204
197
- private getAzureCredentials ( dotenvVars : DotenvParseOutput = { } ) : AzureCredentials {
205
+ private getAzureCredentials ( dotenvVars : Record < string , string | undefined > = { } ) : AzureCredentials {
198
206
const vars = { ...dotenvVars , ...process . env }
199
207
const connectionString = this . connectionString || vars . AZURE_APP_CONFIG_CONNECTION_STRING
200
-
208
+
201
209
if ( ! connectionString ) {
202
210
throw new MissingAppConfigCredentialsError ( )
203
211
}
0 commit comments