@@ -147,12 +147,20 @@ export async function loadKey(contents: string | Buffer): Promise<Key> {
147
147
}
148
148
149
149
export async function loadPrivateKey (
150
- override : string | Buffer | undefined = process . env . APP_CONFIG_SECRETS_KEY ,
150
+ override : string | Buffer | undefined = undefined ,
151
+ environmentOptions ?: EnvironmentOptions ,
151
152
) : Promise < Key > {
152
153
let key : Key ;
154
+ let overrideKey ;
153
155
154
156
if ( override ) {
155
- key = await loadKey ( override ) ;
157
+ overrideKey = override ;
158
+ } else {
159
+ overrideKey = getKeyFromEnv ( 'public' , environmentOptions ) ;
160
+ }
161
+
162
+ if ( overrideKey ) {
163
+ key = await loadKey ( overrideKey ) ;
156
164
} else {
157
165
if ( process . env . CI ) {
158
166
logger . info ( 'Warning! Trying to load encryption keys from home folder in a CI environment' ) ;
@@ -183,12 +191,20 @@ export async function loadPrivateKey(
183
191
}
184
192
185
193
export async function loadPublicKey (
186
- override : string | Buffer | undefined = process . env . APP_CONFIG_SECRETS_PUBLIC_KEY ,
194
+ override : string | Buffer | undefined = undefined ,
195
+ environmentOptions ?: EnvironmentOptions ,
187
196
) : Promise < Key > {
188
197
let key : Key ;
198
+ let overrideKey ;
189
199
190
200
if ( override ) {
191
- key = await loadKey ( override ) ;
201
+ overrideKey = override ;
202
+ } else {
203
+ overrideKey = getKeyFromEnv ( 'public' , environmentOptions ) ;
204
+ }
205
+
206
+ if ( overrideKey ) {
207
+ key = await loadKey ( overrideKey ) ;
192
208
} else {
193
209
if ( process . env . CI ) {
194
210
logger . warn ( 'Warning! Trying to load encryption keys from home folder in a CI environment' ) ;
@@ -203,17 +219,47 @@ export async function loadPublicKey(
203
219
return key ;
204
220
}
205
221
222
+ function getKeyFromEnv ( keyType : 'private' | 'public' , envOptions ?: EnvironmentOptions ) {
223
+ const env = currentEnvironment ( envOptions ) ;
224
+
225
+ const envVarPrefix =
226
+ keyType === 'private' ? 'APP_CONFIG_SECRETS_KEY' : 'APP_CONFIG_SECRETS_PUBLIC_KEY' ;
227
+
228
+ if ( ! envOptions || ! env ) {
229
+ return process . env [ envVarPrefix ] ;
230
+ }
231
+
232
+ let key = process . env [ `${ envVarPrefix } _${ env . toUpperCase ( ) } ` ] ;
233
+
234
+ // try an alias if we didn't find the key first try
235
+ if ( ! key ) {
236
+ const aliases = aliasesFor ( env , envOptions . aliases ) ;
237
+
238
+ for ( const alias of aliases ) {
239
+ key = process . env [ `${ envVarPrefix } _${ alias . toUpperCase ( ) } ` ] ;
240
+
241
+ if ( key ) {
242
+ break ;
243
+ }
244
+ }
245
+ }
246
+
247
+ return key ;
248
+ }
249
+
206
250
let loadedPrivateKey : Promise < Key > | undefined ;
207
251
208
- export async function loadPrivateKeyLazy ( ) : Promise < Key > {
252
+ export async function loadPrivateKeyLazy ( environmentOptions ?: EnvironmentOptions ) : Promise < Key > {
209
253
if ( ! loadedPrivateKey ) {
210
254
logger . verbose ( 'Loading local private key' ) ;
211
255
212
256
if ( checkTTY ( ) ) {
213
257
// help the end user, if they haven't initialized their local keys yet
214
- loadedPrivateKey = initializeLocalKeys ( ) . then ( ( ) => loadPrivateKey ( ) ) ;
258
+ loadedPrivateKey = initializeLocalKeys ( ) . then ( ( ) =>
259
+ loadPrivateKey ( undefined , environmentOptions ) ,
260
+ ) ;
215
261
} else {
216
- loadedPrivateKey = loadPrivateKey ( ) ;
262
+ loadedPrivateKey = loadPrivateKey ( undefined , environmentOptions ) ;
217
263
}
218
264
}
219
265
@@ -222,15 +268,17 @@ export async function loadPrivateKeyLazy(): Promise<Key> {
222
268
223
269
let loadedPublicKey : Promise < Key > | undefined ;
224
270
225
- export async function loadPublicKeyLazy ( ) : Promise < Key > {
271
+ export async function loadPublicKeyLazy ( environmentOptions ?: EnvironmentOptions ) : Promise < Key > {
226
272
if ( ! loadedPublicKey ) {
227
273
logger . verbose ( 'Loading local public key' ) ;
228
274
229
275
if ( checkTTY ( ) ) {
230
276
// help the end user, if they haven't initialized their local keys yet
231
- loadedPublicKey = initializeLocalKeys ( ) . then ( ( ) => loadPublicKey ( ) ) ;
277
+ loadedPublicKey = initializeLocalKeys ( ) . then ( ( ) =>
278
+ loadPublicKey ( undefined , environmentOptions ) ,
279
+ ) ;
232
280
} else {
233
- loadedPublicKey = loadPublicKey ( ) ;
281
+ loadedPublicKey = loadPublicKey ( undefined , environmentOptions ) ;
234
282
}
235
283
}
236
284
@@ -393,7 +441,10 @@ export async function encryptValue(
393
441
if ( symmetricKeyOverride ) {
394
442
symmetricKey = symmetricKeyOverride ;
395
443
} else {
396
- symmetricKey = await loadLatestSymmetricKeyLazy ( await loadPrivateKeyLazy ( ) , environmentOptions ) ;
444
+ symmetricKey = await loadLatestSymmetricKeyLazy (
445
+ await loadPrivateKeyLazy ( environmentOptions ) ,
446
+ environmentOptions ,
447
+ ) ;
397
448
}
398
449
399
450
// all encrypted data is JSON encoded
@@ -447,7 +498,7 @@ export async function decryptValue(
447
498
448
499
symmetricKey = await loadSymmetricKeyLazy (
449
500
revisionNumber ,
450
- await loadPrivateKeyLazy ( ) ,
501
+ await loadPrivateKeyLazy ( environmentOptions ) ,
451
502
environmentOptions ,
452
503
) ;
453
504
}
0 commit comments