Skip to content

Commit c22424c

Browse files
committed
chore: readd key file env var support
1 parent b1a285e commit c22424c

File tree

1 file changed

+39
-8
lines changed

1 file changed

+39
-8
lines changed

app-config-encryption/src/encryption.ts

Lines changed: 39 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export async function loadPrivateKey(
156156
if (override) {
157157
overrideKey = override;
158158
} else {
159-
overrideKey = getKeyFromEnv('private', environmentOptions);
159+
overrideKey = await getKeyFromEnv('private', environmentOptions);
160160
}
161161

162162
if (overrideKey) {
@@ -200,7 +200,7 @@ export async function loadPublicKey(
200200
if (override) {
201201
overrideKey = override;
202202
} else {
203-
overrideKey = getKeyFromEnv('public', environmentOptions);
203+
overrideKey = await getKeyFromEnv('public', environmentOptions);
204204
}
205205

206206
if (overrideKey) {
@@ -219,7 +219,7 @@ export async function loadPublicKey(
219219
return key;
220220
}
221221

222-
function getKeyFromEnv(keyType: 'private' | 'public', envOptions?: EnvironmentOptions) {
222+
async function getKeyFromEnv(keyType: 'private' | 'public', envOptions?: EnvironmentOptions) {
223223
const env = currentEnvironment(envOptions);
224224

225225
const envVarPrefix =
@@ -231,24 +231,55 @@ function getKeyFromEnv(keyType: 'private' | 'public', envOptions?: EnvironmentOp
231231

232232
let key = process.env[`${envVarPrefix}_${env.toUpperCase()}`];
233233

234-
// try an alias if we didn't find the key first try
235-
if (!key) {
234+
const tryAliases = (envVarName: (alias: string) => string) => {
236235
const aliases = aliasesFor(env, envOptions.aliases);
237236

238237
for (const alias of aliases) {
239-
key = process.env[`${envVarPrefix}_${alias.toUpperCase()}`];
238+
const val = process.env[envVarName(alias.toUpperCase())];
240239

241-
if (key) {
242-
break;
240+
if (val) {
241+
return val;
243242
}
244243
}
244+
};
245+
246+
// try an alias if we didn't find the key first try
247+
if (!key) {
248+
key = tryAliases((alias) => `${envVarPrefix}_${alias}`);
249+
}
250+
251+
// see if a file was specified for the environment
252+
if (!key) {
253+
const file = process.env[`${envVarPrefix}_${env.toUpperCase()}_FILE`];
254+
255+
if (file) {
256+
key = (await fs.readFile(file)).toString();
257+
}
258+
}
259+
260+
// try an env alias if we don't have the key from a file
261+
if (!key) {
262+
const file = tryAliases((alias) => `${envVarPrefix}_${alias}_FILE`);
263+
264+
if (file) {
265+
key = (await fs.readFile(file)).toString();
266+
}
245267
}
246268

247269
// if we didn't find a key with an environment, fallback on one without if it exists
248270
if (!key) {
249271
key = process.env[envVarPrefix];
250272
}
251273

274+
// if a key still wasn't found try read from a file specified
275+
if (!key) {
276+
const file = process.env[`${envVarPrefix}_FILE`];
277+
278+
if (file) {
279+
key = (await fs.readFile(file)).toString();
280+
}
281+
}
282+
252283
return key;
253284
}
254285

0 commit comments

Comments
 (0)