@orion-js/env 3.7.4 → 3.9.0
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/lib/cli/add/index.js +3 -0
- package/lib/cli/init/index.js +2 -1
- package/lib/environment/getDts.js +5 -1
- package/lib/environment/getVariables.d.ts +4 -1
- package/lib/environment/getVariables.js +38 -3
- package/lib/environment/index.js +0 -3
- package/lib/environment/index.test.js +88 -1
- package/package.json +2 -2
package/lib/cli/add/index.js
CHANGED
|
@@ -9,6 +9,8 @@ const getParams_1 = require("./getParams");
|
|
|
9
9
|
const yaml_1 = __importDefault(require("yaml"));
|
|
10
10
|
const files_1 = require("../../files");
|
|
11
11
|
const sortObjectByKeys = (object) => {
|
|
12
|
+
if (!object)
|
|
13
|
+
return {};
|
|
12
14
|
const sorted = {};
|
|
13
15
|
Object.keys(object)
|
|
14
16
|
.sort()
|
|
@@ -29,6 +31,7 @@ async function envAdd({ path }) {
|
|
|
29
31
|
// sort keys alphabetically
|
|
30
32
|
config.cleanKeys = sortObjectByKeys(config.cleanKeys);
|
|
31
33
|
config.encryptedKeys = sortObjectByKeys(config.encryptedKeys);
|
|
34
|
+
config.readFromSecret = sortObjectByKeys(config.readFromSecret);
|
|
32
35
|
const text = yaml_1.default.stringify(config);
|
|
33
36
|
(0, files_1.writeFile)(path, text);
|
|
34
37
|
}
|
package/lib/cli/init/index.js
CHANGED
|
@@ -15,7 +15,8 @@ async function envInit({ path }) {
|
|
|
15
15
|
version: '1.0',
|
|
16
16
|
publicKey: keypair.encryptKey,
|
|
17
17
|
cleanKeys: {},
|
|
18
|
-
encryptedKeys: {}
|
|
18
|
+
encryptedKeys: {},
|
|
19
|
+
readFromSecret: {}
|
|
19
20
|
};
|
|
20
21
|
const text = yaml_1.default.stringify(envFile);
|
|
21
22
|
(0, files_1.writeFile)(path, text);
|
|
@@ -4,7 +4,11 @@ exports.writeDtsFileFromConfigFile = exports.writeDtsFile = exports.getDts = voi
|
|
|
4
4
|
const getConfig_1 = require("../cli/add/getConfig");
|
|
5
5
|
const files_1 = require("../files");
|
|
6
6
|
function getDts(config) {
|
|
7
|
-
const keys = [
|
|
7
|
+
const keys = [
|
|
8
|
+
...Object.keys(config.cleanKeys),
|
|
9
|
+
...Object.keys(config.encryptedKeys),
|
|
10
|
+
...Object.values(config.readFromSecret).flat()
|
|
11
|
+
];
|
|
8
12
|
return `declare module '@orion-js/env' {
|
|
9
13
|
export const env: {
|
|
10
14
|
${keys.map(key => ` ${key}: string`).join('\n')}
|
|
@@ -7,8 +7,11 @@ export interface Config {
|
|
|
7
7
|
encryptedKeys: {
|
|
8
8
|
[key: string]: string;
|
|
9
9
|
};
|
|
10
|
+
readFromSecret?: {
|
|
11
|
+
[key: string]: string[];
|
|
12
|
+
};
|
|
10
13
|
}
|
|
11
14
|
export interface Variables {
|
|
12
15
|
[key: string]: string;
|
|
13
16
|
}
|
|
14
|
-
export declare function getVariables(config: Config, secretKey
|
|
17
|
+
export declare function getVariables(config: Config, secretKey?: string): Variables;
|
|
@@ -2,9 +2,44 @@
|
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
exports.getVariables = void 0;
|
|
4
4
|
const crypto_1 = require("../crypto");
|
|
5
|
-
function
|
|
6
|
-
const { cleanKeys, encryptedKeys } = config;
|
|
5
|
+
function readSecrets(readFromSecret) {
|
|
7
6
|
const variables = {};
|
|
7
|
+
let secretKey = null;
|
|
8
|
+
if (!readFromSecret)
|
|
9
|
+
return { variables, secretKey };
|
|
10
|
+
for (const secretName in readFromSecret) {
|
|
11
|
+
const keys = readFromSecret[secretName];
|
|
12
|
+
if (!process.env[secretName]) {
|
|
13
|
+
console.warn(`@orion/env could not find the secret "${secretName}" in the environment. Related variables will be undefined.`);
|
|
14
|
+
continue;
|
|
15
|
+
}
|
|
16
|
+
try {
|
|
17
|
+
const values = JSON.parse(process.env[secretName]);
|
|
18
|
+
if (values.ORION_ENV_SECRET_KEY) {
|
|
19
|
+
secretKey = values.ORION_ENV_SECRET_KEY;
|
|
20
|
+
}
|
|
21
|
+
for (const key of keys) {
|
|
22
|
+
if (values[key]) {
|
|
23
|
+
variables[key] = values[key];
|
|
24
|
+
}
|
|
25
|
+
else {
|
|
26
|
+
console.warn(`@orion/env could not find the variable "${key}" in the secret "${secretName}". Related variables will be undefined.`);
|
|
27
|
+
}
|
|
28
|
+
}
|
|
29
|
+
}
|
|
30
|
+
catch (error) {
|
|
31
|
+
console.warn(`'@orion/env found a the secret "${secretName}" variable in the environment but it is not a valid JSON. Related variables will be undefined.'`);
|
|
32
|
+
}
|
|
33
|
+
}
|
|
34
|
+
return { variables, secretKey: secretKey };
|
|
35
|
+
}
|
|
36
|
+
function getVariables(config, secretKey) {
|
|
37
|
+
const { cleanKeys, encryptedKeys, readFromSecret } = config;
|
|
38
|
+
const { variables, secretKey: foundSecretKey } = readSecrets(readFromSecret);
|
|
39
|
+
let decryptKey = foundSecretKey || secretKey;
|
|
40
|
+
if (!decryptKey) {
|
|
41
|
+
throw new Error('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
|
|
42
|
+
}
|
|
8
43
|
for (const key in cleanKeys) {
|
|
9
44
|
const value = cleanKeys[key];
|
|
10
45
|
variables[key] = value;
|
|
@@ -12,7 +47,7 @@ function getVariables(config, secretKey) {
|
|
|
12
47
|
for (const key in encryptedKeys) {
|
|
13
48
|
const encrypted = encryptedKeys[key];
|
|
14
49
|
try {
|
|
15
|
-
variables[key] = (0, crypto_1.decrypt)(
|
|
50
|
+
variables[key] = (0, crypto_1.decrypt)(decryptKey, encrypted);
|
|
16
51
|
}
|
|
17
52
|
catch (error) {
|
|
18
53
|
throw new Error(`Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not the right key for "${key}"`);
|
package/lib/environment/index.js
CHANGED
|
@@ -28,9 +28,6 @@ if (g.__orion_env_final__) {
|
|
|
28
28
|
variables = g.__orion_env_final__;
|
|
29
29
|
}
|
|
30
30
|
else if (envFilePath) {
|
|
31
|
-
if (!secretKey) {
|
|
32
|
-
throw new Error('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
|
|
33
|
-
}
|
|
34
31
|
variables = (0, exports.readEnv)();
|
|
35
32
|
}
|
|
36
33
|
g.__orion_env_final__ = variables;
|
|
@@ -1,6 +1,7 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
3
|
const crypto_1 = require("../crypto");
|
|
4
|
+
const getDts_1 = require("./getDts");
|
|
4
5
|
const getVariables_1 = require("./getVariables");
|
|
5
6
|
describe('Environment', () => {
|
|
6
7
|
beforeEach(() => {
|
|
@@ -19,13 +20,22 @@ describe('Environment', () => {
|
|
|
19
20
|
},
|
|
20
21
|
encryptedKeys: {
|
|
21
22
|
secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
|
|
23
|
+
},
|
|
24
|
+
readFromSecret: {
|
|
25
|
+
SECRET_ENV: ['secret2'],
|
|
26
|
+
SECRET2_ENV: ['secret3', 'secret4']
|
|
22
27
|
}
|
|
23
28
|
};
|
|
29
|
+
process.env.SECRET_ENV = JSON.stringify({ secret2: 'this_is_secret' });
|
|
30
|
+
process.env.SECRET2_ENV = JSON.stringify({ secret3: '3', secret4: '4' });
|
|
24
31
|
process.env.ORION_ENV_SECRET_KEY = secretKey;
|
|
25
32
|
const env = (0, getVariables_1.getVariables)(data, secretKey);
|
|
26
33
|
expect(env).toEqual({
|
|
27
34
|
a_key: 'a_value',
|
|
28
|
-
secret1: secretValue
|
|
35
|
+
secret1: secretValue,
|
|
36
|
+
secret2: 'this_is_secret',
|
|
37
|
+
secret3: '3',
|
|
38
|
+
secret4: '4'
|
|
29
39
|
});
|
|
30
40
|
});
|
|
31
41
|
it('should thow an error when the secret key is not the one used to encrypt', () => {
|
|
@@ -46,4 +56,81 @@ describe('Environment', () => {
|
|
|
46
56
|
}
|
|
47
57
|
expect.assertions(1);
|
|
48
58
|
});
|
|
59
|
+
it('should read the decyrpt key from the secret', () => {
|
|
60
|
+
const secretKey = 'QShwQT1+d5wk/F6FVpT5VmZFXm50aFRt9/LaDbwSEGo=';
|
|
61
|
+
const secretValue = 'this_is_secret';
|
|
62
|
+
const data = {
|
|
63
|
+
version: '1.0',
|
|
64
|
+
publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
|
|
65
|
+
cleanKeys: {
|
|
66
|
+
a_key: 'a_value'
|
|
67
|
+
},
|
|
68
|
+
encryptedKeys: {
|
|
69
|
+
secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
|
|
70
|
+
},
|
|
71
|
+
readFromSecret: {
|
|
72
|
+
SECRET_ENV: ['secret2']
|
|
73
|
+
}
|
|
74
|
+
};
|
|
75
|
+
process.env.SECRET_ENV = JSON.stringify({
|
|
76
|
+
secret2: 'this_is_secret',
|
|
77
|
+
ORION_ENV_SECRET_KEY: secretKey
|
|
78
|
+
});
|
|
79
|
+
const env = (0, getVariables_1.getVariables)(data, secretKey);
|
|
80
|
+
expect(env).toEqual({
|
|
81
|
+
a_key: 'a_value',
|
|
82
|
+
secret1: secretValue,
|
|
83
|
+
secret2: 'this_is_secret'
|
|
84
|
+
});
|
|
85
|
+
});
|
|
86
|
+
it('should log an error when the secret is not a valid JSON, and related secrets undefined', () => {
|
|
87
|
+
console.warn = jest.fn();
|
|
88
|
+
const secretKey = 'QShwQT1+d5wk/F6FVpT5VmZFXm50aFRt9/LaDbwSEGo=';
|
|
89
|
+
const secretValue = 'this_is_secret';
|
|
90
|
+
const data = {
|
|
91
|
+
version: '1.0',
|
|
92
|
+
publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
|
|
93
|
+
cleanKeys: {
|
|
94
|
+
a_key: 'a_value'
|
|
95
|
+
},
|
|
96
|
+
encryptedKeys: {
|
|
97
|
+
secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
|
|
98
|
+
},
|
|
99
|
+
readFromSecret: {
|
|
100
|
+
SECRET_ENV: ['secret2']
|
|
101
|
+
}
|
|
102
|
+
};
|
|
103
|
+
process.env.SECRET_ENV = 'not a json';
|
|
104
|
+
const env = (0, getVariables_1.getVariables)(data, secretKey);
|
|
105
|
+
expect(env).toEqual({
|
|
106
|
+
a_key: 'a_value',
|
|
107
|
+
secret1: secretValue,
|
|
108
|
+
secret2: undefined
|
|
109
|
+
});
|
|
110
|
+
expect(console.warn.mock.calls[0][0].includes('it is not a valid JSON')).toBe(true);
|
|
111
|
+
});
|
|
112
|
+
it('Dts should return the right types', () => {
|
|
113
|
+
const dts = (0, getDts_1.getDts)({
|
|
114
|
+
version: '1.0',
|
|
115
|
+
publicKey: 'public',
|
|
116
|
+
cleanKeys: {
|
|
117
|
+
a_key: 'a_value'
|
|
118
|
+
},
|
|
119
|
+
encryptedKeys: {
|
|
120
|
+
secret: 'encrypted'
|
|
121
|
+
},
|
|
122
|
+
readFromSecret: {
|
|
123
|
+
SECRET_ENV: ['secret2', 'secret3']
|
|
124
|
+
}
|
|
125
|
+
});
|
|
126
|
+
expect(dts).toEqual(`declare module '@orion-js/env' {
|
|
127
|
+
export const env: {
|
|
128
|
+
a_key: string
|
|
129
|
+
secret: string
|
|
130
|
+
secret2: string
|
|
131
|
+
secret3: string
|
|
132
|
+
}
|
|
133
|
+
}
|
|
134
|
+
`);
|
|
135
|
+
});
|
|
49
136
|
});
|
package/package.json
CHANGED
|
@@ -1,6 +1,6 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/env",
|
|
3
|
-
"version": "3.
|
|
3
|
+
"version": "3.9.0",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
@@ -32,5 +32,5 @@
|
|
|
32
32
|
"publishConfig": {
|
|
33
33
|
"access": "public"
|
|
34
34
|
},
|
|
35
|
-
"gitHead": "
|
|
35
|
+
"gitHead": "23e0eda5348153dcc07b307a97b641d52eb41f39"
|
|
36
36
|
}
|