@@ -552,7 +552,14 @@ export async function trustTeamMember(
552
552
privateKey : Key ,
553
553
environmentOptions ?: EnvironmentOptions ,
554
554
) {
555
- const teamMembers = await loadTeamMembers ( environmentOptions ) ;
555
+ let teamMembers : Key [ ] = [ ] ;
556
+
557
+ try {
558
+ teamMembers = await loadTeamMembers ( environmentOptions ) ;
559
+ } catch {
560
+ // if this throws it's just because members for the selected env weren't found
561
+ // if the env wasn't found just add it
562
+ }
556
563
557
564
if ( newTeamMember . isPrivate ( ) ) {
558
565
throw new InvalidEncryptionKey (
@@ -571,23 +578,37 @@ export async function trustTeamMember(
571
578
572
579
const newTeamMembers = teamMembers . concat ( newTeamMember ) ;
573
580
581
+ let currentKeys : EncryptedSymmetricKey [ ] = [ ] ;
582
+
583
+ try {
584
+ currentKeys = await loadSymmetricKeys ( true , environmentOptions ) ;
585
+ } catch {
586
+ // if this throws it's just because keys for the selected env weren't found
587
+ // if the env wasn't found just add it
588
+ }
589
+
574
590
const newEncryptionKeys = await reencryptSymmetricKeys (
575
- await loadSymmetricKeys ( true , environmentOptions ) ,
591
+ currentKeys ,
576
592
newTeamMembers ,
577
593
privateKey ,
578
594
environmentOptions ,
579
595
) ;
580
596
581
597
await saveNewMetaFile ( ( meta ) => ( {
582
598
...meta ,
583
- teamMembers : newTeamMembers . map ( ( key ) => ( {
584
- userId : key . getUserIds ( ) [ 0 ] ,
585
- keyName : key . keyName ?? null ,
586
- publicKey : key . armor ( ) ,
587
- } ) ) ,
599
+ teamMembers : addForEnvironment (
600
+ newTeamMembers . map ( ( key ) => ( {
601
+ userId : key . getUserIds ( ) [ 0 ] ,
602
+ keyName : key . keyName ?? null ,
603
+ publicKey : key . armor ( ) ,
604
+ } ) ) ,
605
+ meta . teamMembers ?? { } ,
606
+ environmentOptions ,
607
+ true ,
608
+ ) ,
588
609
encryptionKeys : addForEnvironment (
589
610
newEncryptionKeys ,
590
- meta . encryptionKeys ?? [ ] ,
611
+ meta . encryptionKeys ?? { } ,
591
612
environmentOptions ,
592
613
true ,
593
614
) ,
@@ -678,22 +699,27 @@ export async function untrustTeamMember(
678
699
679
700
await saveNewMetaFile ( ( meta ) => ( {
680
701
...meta ,
681
- teamMembers : newTeamMembers . map ( ( key ) => ( {
682
- userId : key . getUserIds ( ) [ 0 ] ,
683
- keyName : key . keyName ?? null ,
684
- publicKey : key . armor ( ) ,
685
- } ) ) ,
702
+ teamMembers : addForEnvironment (
703
+ newTeamMembers . map ( ( key ) => ( {
704
+ userId : key . getUserIds ( ) [ 0 ] ,
705
+ keyName : key . keyName ?? null ,
706
+ publicKey : key . armor ( ) ,
707
+ } ) ) ,
708
+ meta . teamMembers ?? { } ,
709
+ environmentOptions ,
710
+ true ,
711
+ ) ,
686
712
encryptionKeys : addForEnvironment (
687
713
newEncryptionKeys ,
688
- meta . encryptionKeys ?? [ ] ,
714
+ meta . encryptionKeys ?? { } ,
689
715
environmentOptions ,
690
716
true ,
691
717
) ,
692
718
} ) ) ;
693
719
}
694
720
695
721
export function getRevisionNumber ( revision : string ) {
696
- const regex = / ^ (?: \w * - ) ? (?< revisionNumber > \d * ) $ / ;
722
+ const regex = / ^ (?: \w * - ) ? (?< revisionNumber > \d + ) $ / ;
697
723
698
724
const match = regex . exec ( revision ) ?. groups ?. revisionNumber ;
699
725
@@ -851,12 +877,18 @@ function addForEnvironment<T>(
851
877
return orig . concat ( addArray ) ;
852
878
} ;
853
879
880
+ const environment = currentEnvironment ( environmentOptions ) ;
881
+
882
+ if ( Array . isArray ( values ) && environment ) {
883
+ throw new AppConfigError (
884
+ 'An environment was specified when adding a key but your meta file is not setup to use per environment keys' ,
885
+ ) ;
886
+ }
887
+
854
888
if ( Array . isArray ( values ) ) {
855
889
return addOrReplace ( values ) ;
856
890
}
857
891
858
- const environment = currentEnvironment ( environmentOptions ) ;
859
-
860
892
if ( environment === undefined ) {
861
893
if ( 'none' in values ) {
862
894
return {
@@ -865,18 +897,10 @@ function addForEnvironment<T>(
865
897
} ;
866
898
}
867
899
868
- if ( 'default' in values ) {
869
- return {
870
- ...values ,
871
- default : addOrReplace ( values . default ) ,
872
- } ;
873
- }
874
-
875
- const environments = Array . from ( Object . keys ( values ) . values ( ) ) . join ( ', ' ) ;
876
-
877
- throw new AppConfigError (
878
- `No current environment selected, found [${ environments } ] when adding environment-specific encryption options to meta file` ,
879
- ) ;
900
+ return {
901
+ ...values ,
902
+ default : addOrReplace ( values . default ) ,
903
+ } ;
880
904
}
881
905
882
906
if ( environment in values ) {
0 commit comments