@@ -552,7 +552,14 @@ export async function trustTeamMember(
552552 privateKey : Key ,
553553 environmentOptions ?: EnvironmentOptions ,
554554) {
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+ }
556563
557564 if ( newTeamMember . isPrivate ( ) ) {
558565 throw new InvalidEncryptionKey (
@@ -571,23 +578,37 @@ export async function trustTeamMember(
571578
572579 const newTeamMembers = teamMembers . concat ( newTeamMember ) ;
573580
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+
574590 const newEncryptionKeys = await reencryptSymmetricKeys (
575- await loadSymmetricKeys ( true , environmentOptions ) ,
591+ currentKeys ,
576592 newTeamMembers ,
577593 privateKey ,
578594 environmentOptions ,
579595 ) ;
580596
581597 await saveNewMetaFile ( ( meta ) => ( {
582598 ...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+ ) ,
588609 encryptionKeys : addForEnvironment (
589610 newEncryptionKeys ,
590- meta . encryptionKeys ?? [ ] ,
611+ meta . encryptionKeys ?? { } ,
591612 environmentOptions ,
592613 true ,
593614 ) ,
@@ -678,22 +699,27 @@ export async function untrustTeamMember(
678699
679700 await saveNewMetaFile ( ( meta ) => ( {
680701 ...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+ ) ,
686712 encryptionKeys : addForEnvironment (
687713 newEncryptionKeys ,
688- meta . encryptionKeys ?? [ ] ,
714+ meta . encryptionKeys ?? { } ,
689715 environmentOptions ,
690716 true ,
691717 ) ,
692718 } ) ) ;
693719}
694720
695721export function getRevisionNumber ( revision : string ) {
696- const regex = / ^ (?: \w * - ) ? (?< revisionNumber > \d * ) $ / ;
722+ const regex = / ^ (?: \w * - ) ? (?< revisionNumber > \d + ) $ / ;
697723
698724 const match = regex . exec ( revision ) ?. groups ?. revisionNumber ;
699725
@@ -851,12 +877,18 @@ function addForEnvironment<T>(
851877 return orig . concat ( addArray ) ;
852878 } ;
853879
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+
854888 if ( Array . isArray ( values ) ) {
855889 return addOrReplace ( values ) ;
856890 }
857891
858- const environment = currentEnvironment ( environmentOptions ) ;
859-
860892 if ( environment === undefined ) {
861893 if ( 'none' in values ) {
862894 return {
@@ -865,18 +897,10 @@ function addForEnvironment<T>(
865897 } ;
866898 }
867899
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+ } ;
880904 }
881905
882906 if ( environment in values ) {
0 commit comments