@@ -16,6 +16,7 @@ import {
16
16
import { Json } from '@app-config/utils' ;
17
17
import { checkTTY , logger } from '@app-config/logging' ;
18
18
import {
19
+ aliasesFor ,
19
20
currentEnvironment ,
20
21
EnvironmentOptions ,
21
22
promptUser ,
@@ -289,11 +290,10 @@ export async function saveNewSymmetricKey(
289
290
environmentOptions ?: EnvironmentOptions ,
290
291
) {
291
292
const encrypted = await encryptSymmetricKey ( symmetricKey , teamMembers ) ;
292
- const environment = currentEnvironment ( environmentOptions ) ;
293
293
294
294
await saveNewMetaFile ( ( { encryptionKeys = [ ] , ...meta } ) => ( {
295
295
...meta ,
296
- encryptionKeys : addForEnvironment ( encrypted , encryptionKeys , environment ) ,
296
+ encryptionKeys : addForEnvironment ( encrypted , encryptionKeys , environmentOptions ) ,
297
297
} ) ) ;
298
298
}
299
299
@@ -309,7 +309,13 @@ export async function loadSymmetricKeys(
309
309
value : { encryptionKeys = [ ] } ,
310
310
} = await loadMeta ( ) ;
311
311
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 ;
313
319
}
314
320
315
321
export async function loadSymmetricKey (
@@ -470,8 +476,14 @@ export async function loadTeamMembers(environmentOptions?: EnvironmentOptions):
470
476
value : { teamMembers = [ ] } ,
471
477
} = await loadMetaConfig ( ) ;
472
478
479
+ const currentTeamMembers = selectForEnvironment ( teamMembers , environmentOptions ) ;
480
+
481
+ logger . verbose (
482
+ `Found ${ currentTeamMembers . length } team members for environment: ${ environment ?? 'none' } ` ,
483
+ ) ;
484
+
473
485
return Promise . all (
474
- selectForEnvironment ( teamMembers , environment ) . map ( ( { keyName, publicKey } ) =>
486
+ currentTeamMembers . map ( ( { keyName, publicKey } ) =>
475
487
loadKey ( publicKey ) . then ( ( key ) => Object . assign ( key , { keyName } ) ) ,
476
488
) ,
477
489
) ;
@@ -492,7 +504,6 @@ export async function trustTeamMember(
492
504
privateKey : Key ,
493
505
environmentOptions ?: EnvironmentOptions ,
494
506
) {
495
- const environment = currentEnvironment ( environmentOptions ) ;
496
507
const teamMembers = await loadTeamMembers ( environmentOptions ) ;
497
508
498
509
if ( newTeamMember . isPrivate ( ) ) {
@@ -528,7 +539,7 @@ export async function trustTeamMember(
528
539
encryptionKeys : addForEnvironment (
529
540
newEncryptionKeys ,
530
541
meta . encryptionKeys ?? [ ] ,
531
- environment ,
542
+ environmentOptions ,
532
543
true ,
533
544
) ,
534
545
} ) ) ;
@@ -539,7 +550,6 @@ export async function untrustTeamMember(
539
550
privateKey : Key ,
540
551
environmentOptions ?: EnvironmentOptions ,
541
552
) {
542
- const environment = currentEnvironment ( environmentOptions ) ;
543
553
const teamMembers = await loadTeamMembers ( environmentOptions ) ;
544
554
545
555
const removalCandidates = new Set < Key > ( ) ;
@@ -613,7 +623,7 @@ export async function untrustTeamMember(
613
623
encryptionKeys : addForEnvironment (
614
624
newEncryptionKeys ,
615
625
meta . encryptionKeys ?? [ ] ,
616
- environment ,
626
+ environmentOptions ,
617
627
true ,
618
628
) ,
619
629
} ) ) ;
@@ -689,12 +699,14 @@ async function saveNewMetaFile(mutate: (props: MetaProperties) => MetaProperties
689
699
690
700
function selectForEnvironment < T > (
691
701
values : T [ ] | Record < string , T [ ] > ,
692
- environment : string | undefined ,
702
+ environmentOptions : EnvironmentOptions | undefined ,
693
703
) : T [ ] {
694
704
if ( Array . isArray ( values ) ) {
695
705
return values ;
696
706
}
697
707
708
+ const environment = currentEnvironment ( environmentOptions ) ;
709
+
698
710
if ( environment === undefined ) {
699
711
if ( 'none' in values ) {
700
712
return values . none ;
@@ -713,15 +725,25 @@ function selectForEnvironment<T>(
713
725
return values [ environment ] ;
714
726
}
715
727
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
+
716
736
const environments = Array . from ( Object . keys ( values ) . values ( ) ) . join ( ', ' ) ;
717
737
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
+ ) ;
719
741
}
720
742
721
743
function addForEnvironment < T > (
722
744
add : T | T [ ] ,
723
745
values : T [ ] | Record < string , T [ ] > ,
724
- environment : string | undefined ,
746
+ environmentOptions : EnvironmentOptions | undefined ,
725
747
overwrite = false ,
726
748
) : T [ ] | Record < string , T [ ] > {
727
749
const addArray = Array . isArray ( add ) ? add : [ add ] ;
@@ -737,6 +759,8 @@ function addForEnvironment<T>(
737
759
return values . concat ( add ) ;
738
760
}
739
761
762
+ const environment = currentEnvironment ( environmentOptions ) ;
763
+
740
764
if ( environment === undefined ) {
741
765
if ( 'none' in values ) {
742
766
return {
@@ -754,7 +778,9 @@ function addForEnvironment<T>(
754
778
755
779
const environments = Array . from ( Object . keys ( values ) . values ( ) ) . join ( ', ' ) ;
756
780
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
+ ) ;
758
784
}
759
785
760
786
if ( environment in values ) {
@@ -764,6 +790,17 @@ function addForEnvironment<T>(
764
790
} ;
765
791
}
766
792
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
+
767
804
return {
768
805
...values ,
769
806
[ environment ] : addArray ,
0 commit comments