Skip to content

Commit f748f61

Browse files
committed
feat: environment asliases for env-specific encryption
1 parent 9e2025a commit f748f61

File tree

2 files changed

+50
-12
lines changed

2 files changed

+50
-12
lines changed

app-config-encryption/src/encryption.ts

Lines changed: 49 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,7 @@ import {
1616
import { Json } from '@app-config/utils';
1717
import { checkTTY, logger } from '@app-config/logging';
1818
import {
19+
aliasesFor,
1920
currentEnvironment,
2021
EnvironmentOptions,
2122
promptUser,
@@ -289,11 +290,10 @@ export async function saveNewSymmetricKey(
289290
environmentOptions?: EnvironmentOptions,
290291
) {
291292
const encrypted = await encryptSymmetricKey(symmetricKey, teamMembers);
292-
const environment = currentEnvironment(environmentOptions);
293293

294294
await saveNewMetaFile(({ encryptionKeys = [], ...meta }) => ({
295295
...meta,
296-
encryptionKeys: addForEnvironment(encrypted, encryptionKeys, environment),
296+
encryptionKeys: addForEnvironment(encrypted, encryptionKeys, environmentOptions),
297297
}));
298298
}
299299

@@ -309,7 +309,13 @@ export async function loadSymmetricKeys(
309309
value: { encryptionKeys = [] },
310310
} = await loadMeta();
311311

312-
return selectForEnvironment(encryptionKeys, environment);
312+
const selected = selectForEnvironment(encryptionKeys, environmentOptions);
313+
314+
logger.verbose(
315+
`Found ${selected.length} symmetric keys for environment: ${environment ?? 'none'}`,
316+
);
317+
318+
return selected;
313319
}
314320

315321
export async function loadSymmetricKey(
@@ -470,8 +476,14 @@ export async function loadTeamMembers(environmentOptions?: EnvironmentOptions):
470476
value: { teamMembers = [] },
471477
} = await loadMetaConfig();
472478

479+
const currentTeamMembers = selectForEnvironment(teamMembers, environmentOptions);
480+
481+
logger.verbose(
482+
`Found ${currentTeamMembers.length} team members for environment: ${environment ?? 'none'}`,
483+
);
484+
473485
return Promise.all(
474-
selectForEnvironment(teamMembers, environment).map(({ keyName, publicKey }) =>
486+
currentTeamMembers.map(({ keyName, publicKey }) =>
475487
loadKey(publicKey).then((key) => Object.assign(key, { keyName })),
476488
),
477489
);
@@ -492,7 +504,6 @@ export async function trustTeamMember(
492504
privateKey: Key,
493505
environmentOptions?: EnvironmentOptions,
494506
) {
495-
const environment = currentEnvironment(environmentOptions);
496507
const teamMembers = await loadTeamMembers(environmentOptions);
497508

498509
if (newTeamMember.isPrivate()) {
@@ -528,7 +539,7 @@ export async function trustTeamMember(
528539
encryptionKeys: addForEnvironment(
529540
newEncryptionKeys,
530541
meta.encryptionKeys ?? [],
531-
environment,
542+
environmentOptions,
532543
true,
533544
),
534545
}));
@@ -539,7 +550,6 @@ export async function untrustTeamMember(
539550
privateKey: Key,
540551
environmentOptions?: EnvironmentOptions,
541552
) {
542-
const environment = currentEnvironment(environmentOptions);
543553
const teamMembers = await loadTeamMembers(environmentOptions);
544554

545555
const removalCandidates = new Set<Key>();
@@ -613,7 +623,7 @@ export async function untrustTeamMember(
613623
encryptionKeys: addForEnvironment(
614624
newEncryptionKeys,
615625
meta.encryptionKeys ?? [],
616-
environment,
626+
environmentOptions,
617627
true,
618628
),
619629
}));
@@ -689,12 +699,14 @@ async function saveNewMetaFile(mutate: (props: MetaProperties) => MetaProperties
689699

690700
function selectForEnvironment<T>(
691701
values: T[] | Record<string, T[]>,
692-
environment: string | undefined,
702+
environmentOptions: EnvironmentOptions | undefined,
693703
): T[] {
694704
if (Array.isArray(values)) {
695705
return values;
696706
}
697707

708+
const environment = currentEnvironment(environmentOptions);
709+
698710
if (environment === undefined) {
699711
if ('none' in values) {
700712
return values.none;
@@ -713,15 +725,25 @@ function selectForEnvironment<T>(
713725
return values[environment];
714726
}
715727

728+
if (environmentOptions?.aliases) {
729+
for (const alias of aliasesFor(environment, environmentOptions.aliases)) {
730+
if (alias in values) {
731+
return values[alias];
732+
}
733+
}
734+
}
735+
716736
const environments = Array.from(Object.keys(values).values()).join(', ');
717737

718-
throw new AppConfigError(`Current environment was ${environment}, only found [${environments}]`);
738+
throw new AppConfigError(
739+
`Current environment was ${environment}, only found [${environments}] when selecting environment-specific encryption options from meta file`,
740+
);
719741
}
720742

721743
function addForEnvironment<T>(
722744
add: T | T[],
723745
values: T[] | Record<string, T[]>,
724-
environment: string | undefined,
746+
environmentOptions: EnvironmentOptions | undefined,
725747
overwrite = false,
726748
): T[] | Record<string, T[]> {
727749
const addArray = Array.isArray(add) ? add : [add];
@@ -737,6 +759,8 @@ function addForEnvironment<T>(
737759
return values.concat(add);
738760
}
739761

762+
const environment = currentEnvironment(environmentOptions);
763+
740764
if (environment === undefined) {
741765
if ('none' in values) {
742766
return {
@@ -754,7 +778,9 @@ function addForEnvironment<T>(
754778

755779
const environments = Array.from(Object.keys(values).values()).join(', ');
756780

757-
throw new AppConfigError(`No current environment selected, found [${environments}}`);
781+
throw new AppConfigError(
782+
`No current environment selected, found [${environments}] when adding environment-specific encryption options to meta file`,
783+
);
758784
}
759785

760786
if (environment in values) {
@@ -764,6 +790,17 @@ function addForEnvironment<T>(
764790
};
765791
}
766792

793+
if (environmentOptions?.aliases) {
794+
for (const alias of aliasesFor(environment, environmentOptions.aliases)) {
795+
if (alias in values) {
796+
return {
797+
...values,
798+
[alias]: addOrReplace(values[alias]),
799+
};
800+
}
801+
}
802+
}
803+
767804
return {
768805
...values,
769806
[environment]: addArray,

app-config-node/src/index.ts

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,5 +1,6 @@
11
export { FileSource, FlexibleFileSource, resolveFilepath } from './file-source';
22
export {
3+
aliasesFor,
34
asEnvOptions,
45
environmentOptionsFromContext,
56
currentEnvironment,

0 commit comments

Comments
 (0)