Skip to content

Commit a8ea8c6

Browse files
committed
chore: tests for env secret key
1 parent d08f16f commit a8ea8c6

File tree

2 files changed

+78
-1
lines changed

2 files changed

+78
-1
lines changed

app-config-encryption/src/encryption.test.ts

Lines changed: 72 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -4,6 +4,7 @@ import { SecretsRequireTTYError } from '@app-config/core';
44
import { loadMetaConfig } from '@app-config/meta';
55
import { withTempFiles, mockedStdin } from '@app-config/test-utils';
66

7+
import { defaultEnvOptions } from '@app-config/node';
78
import {
89
initializeKeys,
910
initializeKeysManually,
@@ -102,6 +103,77 @@ describe('User Keys', () => {
102103
});
103104
});
104105

106+
const createKeys = async () => {
107+
const { privateKeyArmored, publicKeyArmored } = await initializeKeysManually({
108+
name: 'Tester',
109+
110+
});
111+
112+
return {
113+
privateKey: await loadPrivateKey(privateKeyArmored),
114+
publicKey: await loadPublicKey(publicKeyArmored),
115+
privateKeyArmored,
116+
publicKeyArmored,
117+
};
118+
};
119+
120+
describe('User keys from environment', () => {
121+
it('loads user keys from environment', async () => {
122+
const keys = await createKeys();
123+
124+
process.env.APP_CONFIG_SECRETS_PUBLIC_KEY = keys.publicKeyArmored;
125+
process.env.APP_CONFIG_SECRETS_KEY = keys.privateKeyArmored;
126+
127+
const privateKey = await loadPrivateKey();
128+
const publicKey = await loadPublicKey();
129+
130+
expect(privateKey.getFingerprint()).toEqual(keys.privateKey.getFingerprint());
131+
expect(publicKey.getFingerprint()).toEqual(keys.publicKey.getFingerprint());
132+
});
133+
134+
it('loads environment user keys from environment', async () => {
135+
const keys = await createKeys();
136+
137+
process.env.APP_CONFIG_SECRETS_PUBLIC_KEY_PRODUCTION = keys.publicKeyArmored;
138+
process.env.APP_CONFIG_SECRETS_KEY_PRODUCTION = keys.privateKeyArmored;
139+
process.env.APP_CONFIG_ENV = 'prod';
140+
141+
const privateKey = await loadPrivateKey(undefined, defaultEnvOptions);
142+
const publicKey = await loadPublicKey(undefined, defaultEnvOptions);
143+
144+
expect(privateKey.getFingerprint()).toEqual(keys.privateKey.getFingerprint());
145+
expect(publicKey.getFingerprint()).toEqual(keys.publicKey.getFingerprint());
146+
});
147+
148+
it('loads aliased environment user keys from environment', async () => {
149+
const keys = await createKeys();
150+
151+
process.env.APP_CONFIG_SECRETS_PUBLIC_KEY_PROD = keys.publicKeyArmored;
152+
process.env.APP_CONFIG_SECRETS_KEY_PROD = keys.privateKeyArmored;
153+
process.env.APP_CONFIG_ENV = 'prod';
154+
155+
const privateKey = await loadPrivateKey(undefined, defaultEnvOptions);
156+
const publicKey = await loadPublicKey(undefined, defaultEnvOptions);
157+
158+
expect(privateKey.getFingerprint()).toEqual(keys.privateKey.getFingerprint());
159+
expect(publicKey.getFingerprint()).toEqual(keys.publicKey.getFingerprint());
160+
});
161+
162+
it('falls back to key with no environment', async () => {
163+
const keys = await createKeys();
164+
165+
process.env.APP_CONFIG_SECRETS_PUBLIC_KEY = keys.publicKeyArmored;
166+
process.env.APP_CONFIG_SECRETS_KEY = keys.privateKeyArmored;
167+
process.env.APP_CONFIG_ENV = 'prod';
168+
169+
const privateKey = await loadPrivateKey(undefined, defaultEnvOptions);
170+
const publicKey = await loadPublicKey(undefined, defaultEnvOptions);
171+
172+
expect(privateKey.getFingerprint()).toEqual(keys.privateKey.getFingerprint());
173+
expect(publicKey.getFingerprint()).toEqual(keys.publicKey.getFingerprint());
174+
});
175+
});
176+
105177
const createKey = async () => {
106178
const { privateKeyArmored } = await initializeKeysManually({
107179
name: 'Tester',

app-config-encryption/src/encryption.ts

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -156,7 +156,7 @@ export async function loadPrivateKey(
156156
if (override) {
157157
overrideKey = override;
158158
} else {
159-
overrideKey = getKeyFromEnv('public', environmentOptions);
159+
overrideKey = getKeyFromEnv('private', environmentOptions);
160160
}
161161

162162
if (overrideKey) {
@@ -244,6 +244,11 @@ function getKeyFromEnv(keyType: 'private' | 'public', envOptions?: EnvironmentOp
244244
}
245245
}
246246

247+
// if we didn't find a key with an environment, fallback on one without if it exists
248+
if (!key) {
249+
key = process.env[envVarPrefix];
250+
}
251+
247252
return key;
248253
}
249254

0 commit comments

Comments
 (0)