Skip to content

Commit bd8e1e9

Browse files
committed
feat: override env with whats in the key revision to allow for key reuse
1 parent 2598c16 commit bd8e1e9

File tree

1 file changed

+22
-2
lines changed

1 file changed

+22
-2
lines changed

app-config-encryption/src/index.ts

Lines changed: 22 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -1,4 +1,4 @@
1-
import type { ParsingExtension } from '@app-config/core';
1+
import { AppConfigError, ParsingExtension } from '@app-config/core';
22
import { named } from '@app-config/extension-utils';
33
import { logger } from '@app-config/logging';
44
import { environmentOptionsFromContext } from '@app-config/node';
@@ -22,8 +22,28 @@ export default function encryptedDirective(
2222
);
2323
}
2424

25+
// we override the environment with what's specified in the key revision
26+
// so you can use the same key for multiple environments
27+
28+
const revision = value.split(':')[1];
29+
30+
if (!revision) {
31+
throw new AppConfigError(`Could not find key revision in encrypted value`);
32+
}
33+
34+
const envRegex = /^(?:(?<env>\w*)-)?(?:\d+)$/;
35+
const env = envRegex.exec(revision)?.groups?.env;
2536
const environmentOptions = environmentOptionsFromContext(ctx);
26-
const decrypted = await decryptValue(value, symmetricKey, environmentOptions);
37+
38+
if (env && environmentOptions) {
39+
environmentOptions.override = env;
40+
}
41+
42+
const decrypted = await decryptValue(
43+
value,
44+
symmetricKey,
45+
env ? environmentOptions : undefined,
46+
);
2747

2848
return parse(decrypted, { fromSecrets: true, parsedFromEncryptedValue: true });
2949
};

0 commit comments

Comments
 (0)