Skip to content

Commit 3cf016d

Browse files
committed
fix: select all symmetric keys when no environment is selected
1 parent ac2d077 commit 3cf016d

File tree

2 files changed

+27
-28
lines changed

2 files changed

+27
-28
lines changed

app-config-encryption/src/encryption.ts

Lines changed: 23 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -362,13 +362,17 @@ export async function loadSymmetricKeys(
362362
value: { encryptionKeys = [] },
363363
} = await loadMeta();
364364

365-
const selected = selectForEnvironment(encryptionKeys, environmentOptions);
365+
if (environmentOptions) {
366+
const selected = selectForEnvironment(encryptionKeys, environmentOptions);
366367

367-
logger.verbose(
368-
`Found ${selected.length} symmetric keys for environment: ${environment ?? 'none'}`,
369-
);
368+
logger.verbose(
369+
`Found ${selected.length} symmetric keys for environment: ${environment ?? 'none'}`,
370+
);
370371

371-
return selected;
372+
return selected;
373+
}
374+
375+
return selectAll(encryptionKeys);
372376
}
373377

374378
export async function loadSymmetricKey(
@@ -819,6 +823,20 @@ async function saveNewMetaFile(mutate: (props: MetaProperties) => MetaProperties
819823
await fs.writeFile(writeFilePath, stringify(writeMeta, writeFileType));
820824
}
821825

826+
function selectAll<T>(values: T[] | Record<string, T[]>): T[] {
827+
if (Array.isArray(values)) {
828+
return values;
829+
}
830+
831+
const allValues: T[] = [];
832+
833+
for (const key of Object.keys(values)) {
834+
allValues.push(...values[key]);
835+
}
836+
837+
return allValues;
838+
}
839+
822840
function selectForEnvironment<T>(
823841
values: T[] | Record<string, T[]>,
824842
environmentOptions: EnvironmentOptions | undefined,

app-config-encryption/src/index.ts

Lines changed: 4 additions & 23 deletions
Original file line numberDiff line numberDiff line change
@@ -13,7 +13,7 @@ export default function encryptedDirective(
1313
symmetricKey?: DecryptedSymmetricKey,
1414
shouldShowDeprecationNotice?: true,
1515
): ParsingExtension {
16-
return named('encryption', (value, _, __, ctx) => {
16+
return named('encryption', (value) => {
1717
if (typeof value === 'string' && value.startsWith('enc:')) {
1818
return async (parse) => {
1919
if (shouldShowDeprecationNotice) {
@@ -22,28 +22,9 @@ 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;
36-
const environmentOptions = environmentOptionsFromContext(ctx);
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-
);
25+
// we don't need to pass the environment here - we use the key revision
26+
// to determine which symmetric key to use
27+
const decrypted = await decryptValue(value, symmetricKey);
4728

4829
return parse(decrypted, { fromSecrets: true, parsedFromEncryptedValue: true });
4930
};

0 commit comments

Comments
 (0)