Skip to content

Commit

Permalink
feat: support for labels
Browse files Browse the repository at this point in the history
- Adding support for labels
- Mixing in process.env for Azure loadFromAzure
  • Loading branch information
Kyle Hayes committed Nov 18, 2020
1 parent a042b4c commit 7fc8c29
Show file tree
Hide file tree
Showing 3 changed files with 19 additions and 11 deletions.
2 changes: 1 addition & 1 deletion package-lock.json

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -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",
Expand Down
26 changes: 17 additions & 9 deletions src/dotenv-azure.ts
Original file line number Diff line number Diff line change
@@ -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'
Expand Down Expand Up @@ -60,8 +60,8 @@ export default class DotenvAzure {
async config(options: DotenvAzureConfigOptions = {}): Promise<DotenvAzureConfigOutput> {
const { safe = false } = options
const dotenvResult = dotenv.config(options)

const azureVars = await this.loadFromAzure(dotenvResult.parsed)
const vars:Record<string, string | undefined> = {...(dotenvResult.parsed || {}), ...process.env}
const azureVars = await this.loadFromAzure(vars)
const joinedVars = { ...azureVars, ...dotenvResult.parsed }

populateProcessEnv(azureVars)
Expand Down Expand Up @@ -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<VariablesObject> {
async loadFromAzure(dotenvVars?: Record<string, string | undefined>): Promise<VariablesObject> {
// 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 }
}
Expand All @@ -114,11 +119,14 @@ export default class DotenvAzure {
}
}

protected async getAppConfigurations(client: AppConfigurationClient): Promise<AppConfigurations> {
protected async getAppConfigurations(
client: AppConfigurationClient,
labels = ''
): Promise<AppConfigurations> {
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 {
Expand Down Expand Up @@ -194,10 +202,10 @@ export default class DotenvAzure {
)
}

private getAzureCredentials(dotenvVars: DotenvParseOutput = {}): AzureCredentials {
private getAzureCredentials(dotenvVars: Record<string, string | undefined> = {}): AzureCredentials {
const vars = { ...dotenvVars, ...process.env }
const connectionString = this.connectionString || vars.AZURE_APP_CONFIG_CONNECTION_STRING

if (!connectionString) {
throw new MissingAppConfigCredentialsError()
}
Expand Down

0 comments on commit 7fc8c29

Please sign in to comment.