@orion-js/env 3.1.9 → 3.1.20
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/encryptValue.d.ts +2 -0
- package/lib/cli/add/encryptValue.js +8 -0
- package/lib/cli/add/getConfig.d.ts +2 -0
- package/lib/cli/add/getConfig.js +16 -0
- package/lib/cli/add/getParams.d.ts +5 -0
- package/lib/cli/add/getParams.js +26 -0
- package/lib/cli/add/index.d.ts +3 -0
- package/lib/cli/add/index.js +35 -0
- package/lib/cli/index.d.ts +2 -0
- package/lib/cli/index.js +35 -0
- package/lib/cli/init/index.d.ts +3 -0
- package/lib/cli/init/index.js +28 -0
- package/lib/crypto/index.d.ts +13 -0
- package/lib/crypto/index.js +37 -0
- package/lib/crypto/index.test.d.ts +1 -0
- package/lib/crypto/index.test.js +30 -0
- package/lib/crypto/tweetnacl.d.ts +3 -0
- package/lib/crypto/tweetnacl.js +31 -0
- package/lib/environment/getVariables.d.ts +14 -0
- package/lib/environment/getVariables.js +23 -0
- package/lib/environment/index.d.ts +5 -1
- package/lib/environment/index.js +19 -21
- package/lib/environment/index.test.d.ts +1 -1
- package/lib/environment/index.test.js +10 -25
- package/lib/files/index.d.ts +3 -0
- package/lib/files/index.js +27 -0
- package/lib/index.d.ts +1 -0
- package/lib/index.js +1 -0
- package/lib/internalGetEnv.d.ts +1 -0
- package/lib/internalGetEnv.js +14 -0
- package/package.json +10 -4
|
@@ -0,0 +1,8 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.encryptValue = void 0;
|
|
4
|
+
const crypto_1 = require("../../crypto");
|
|
5
|
+
const encryptValue = (key, value, config) => {
|
|
6
|
+
config.encryptedKeys[key] = (0, crypto_1.encrypt)(config.publicKey, value);
|
|
7
|
+
};
|
|
8
|
+
exports.encryptValue = encryptValue;
|
|
@@ -0,0 +1,16 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getConfig = void 0;
|
|
7
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
8
|
+
const files_1 = require("../../files");
|
|
9
|
+
const getConfig = (envPath) => {
|
|
10
|
+
const configFile = (0, files_1.readFile)(envPath);
|
|
11
|
+
if (!configFile) {
|
|
12
|
+
throw new Error('No config file found at path ' + envPath);
|
|
13
|
+
}
|
|
14
|
+
return yaml_1.default.parse(configFile);
|
|
15
|
+
};
|
|
16
|
+
exports.getConfig = getConfig;
|
|
@@ -0,0 +1,26 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.getParams = void 0;
|
|
7
|
+
const prompts_1 = __importDefault(require("prompts"));
|
|
8
|
+
const getParams = async (config) => {
|
|
9
|
+
const response = await (0, prompts_1.default)([
|
|
10
|
+
{
|
|
11
|
+
type: 'text',
|
|
12
|
+
name: 'key',
|
|
13
|
+
message: 'Key'
|
|
14
|
+
},
|
|
15
|
+
{
|
|
16
|
+
type: 'text',
|
|
17
|
+
name: 'value',
|
|
18
|
+
message: 'Value'
|
|
19
|
+
}
|
|
20
|
+
]);
|
|
21
|
+
return {
|
|
22
|
+
key: response.key,
|
|
23
|
+
value: response.value
|
|
24
|
+
};
|
|
25
|
+
};
|
|
26
|
+
exports.getParams = getParams;
|
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const encryptValue_1 = require("./encryptValue");
|
|
7
|
+
const getConfig_1 = require("./getConfig");
|
|
8
|
+
const getParams_1 = require("./getParams");
|
|
9
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
10
|
+
const files_1 = require("../../files");
|
|
11
|
+
const sortObjectByKeys = (object) => {
|
|
12
|
+
const sorted = {};
|
|
13
|
+
Object.keys(object)
|
|
14
|
+
.sort()
|
|
15
|
+
.forEach(key => {
|
|
16
|
+
sorted[key] = object[key];
|
|
17
|
+
});
|
|
18
|
+
return sorted;
|
|
19
|
+
};
|
|
20
|
+
async function envAdd({ envPath }) {
|
|
21
|
+
if (!envPath) {
|
|
22
|
+
envPath = '.env.local.yml';
|
|
23
|
+
}
|
|
24
|
+
const config = (0, getConfig_1.getConfig)(envPath);
|
|
25
|
+
const { key, value } = await (0, getParams_1.getParams)(config);
|
|
26
|
+
if (!value)
|
|
27
|
+
return;
|
|
28
|
+
(0, encryptValue_1.encryptValue)(key, value, config);
|
|
29
|
+
// sort keys alphabetically
|
|
30
|
+
config.cleanKeys = sortObjectByKeys(config.cleanKeys);
|
|
31
|
+
config.encryptedKeys = sortObjectByKeys(config.encryptedKeys);
|
|
32
|
+
const text = yaml_1.default.stringify(config);
|
|
33
|
+
(0, files_1.writeFile)(envPath, text);
|
|
34
|
+
}
|
|
35
|
+
exports.default = envAdd;
|
package/lib/cli/index.js
ADDED
|
@@ -0,0 +1,35 @@
|
|
|
1
|
+
#!/usr/bin/env node
|
|
2
|
+
"use strict";
|
|
3
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
4
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
5
|
+
};
|
|
6
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
7
|
+
const commander_1 = require("commander");
|
|
8
|
+
const safe_1 = __importDefault(require("colors/safe"));
|
|
9
|
+
const init_1 = __importDefault(require("./init"));
|
|
10
|
+
const add_1 = __importDefault(require("./add"));
|
|
11
|
+
const program = new commander_1.Command();
|
|
12
|
+
const run = function (action) {
|
|
13
|
+
return async function (...args) {
|
|
14
|
+
try {
|
|
15
|
+
await action(...args);
|
|
16
|
+
}
|
|
17
|
+
catch (e) {
|
|
18
|
+
console.error(safe_1.default.red('Error: ' + e.message));
|
|
19
|
+
}
|
|
20
|
+
};
|
|
21
|
+
};
|
|
22
|
+
program
|
|
23
|
+
.command('init')
|
|
24
|
+
.description('Creates a new encrypted env file')
|
|
25
|
+
.option('--path <path>', 'Specify the env file name')
|
|
26
|
+
.action(run(init_1.default));
|
|
27
|
+
program
|
|
28
|
+
.command('add')
|
|
29
|
+
.description('Adds a new environment to the encrypted env file')
|
|
30
|
+
.option('--path <path>', 'Specify the env file name')
|
|
31
|
+
.action(run(add_1.default));
|
|
32
|
+
program.parse(process.argv);
|
|
33
|
+
if (!process.argv.slice(2).length) {
|
|
34
|
+
program.outputHelp();
|
|
35
|
+
}
|
|
@@ -0,0 +1,28 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
const yaml_1 = __importDefault(require("yaml"));
|
|
7
|
+
const crypto_1 = require("../../crypto");
|
|
8
|
+
const files_1 = require("../../files");
|
|
9
|
+
async function envInit({ envPath }) {
|
|
10
|
+
if (!envPath) {
|
|
11
|
+
envPath = '.env.local.yml';
|
|
12
|
+
}
|
|
13
|
+
const keypair = (0, crypto_1.generateKeys)();
|
|
14
|
+
const envFile = {
|
|
15
|
+
version: '1.0',
|
|
16
|
+
publicKey: keypair.encryptKey,
|
|
17
|
+
cleanKeys: {},
|
|
18
|
+
encryptedKeys: {}
|
|
19
|
+
};
|
|
20
|
+
const text = yaml_1.default.stringify(envFile);
|
|
21
|
+
(0, files_1.writeFile)(envPath, text);
|
|
22
|
+
console.log('');
|
|
23
|
+
console.log(`Environment file created. You need to use the following key to decrypt the environment variables:`);
|
|
24
|
+
console.log('');
|
|
25
|
+
console.log(keypair.decryptKey);
|
|
26
|
+
console.log('');
|
|
27
|
+
}
|
|
28
|
+
exports.default = envInit;
|
|
@@ -0,0 +1,13 @@
|
|
|
1
|
+
export declare function generateKeys(): {
|
|
2
|
+
encryptKey: string;
|
|
3
|
+
decryptKey: string;
|
|
4
|
+
};
|
|
5
|
+
/**
|
|
6
|
+
* Creates a temporal keypair just to encrypt one message.
|
|
7
|
+
* Saves the public key in the result so that the message can be decrypted.
|
|
8
|
+
*/
|
|
9
|
+
export declare function encrypt(encryptKey: string, message: string): string;
|
|
10
|
+
/**
|
|
11
|
+
* Ecrypts a message using the decrypt key
|
|
12
|
+
*/
|
|
13
|
+
export declare function decrypt(decryptKey: string, encrypted: string): string;
|
|
@@ -0,0 +1,37 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decrypt = exports.encrypt = exports.generateKeys = void 0;
|
|
4
|
+
const tweetnacl_1 = require("./tweetnacl");
|
|
5
|
+
const tweetnacl_util_1 = require("tweetnacl-util");
|
|
6
|
+
function generateKeys() {
|
|
7
|
+
const { publicKey, secretKey } = (0, tweetnacl_1.generateKeyPair)();
|
|
8
|
+
const encryptKeyHex = (0, tweetnacl_util_1.encodeBase64)(publicKey);
|
|
9
|
+
const decryptKeyHex = (0, tweetnacl_util_1.encodeBase64)(secretKey);
|
|
10
|
+
return {
|
|
11
|
+
encryptKey: encryptKeyHex,
|
|
12
|
+
decryptKey: decryptKeyHex
|
|
13
|
+
};
|
|
14
|
+
}
|
|
15
|
+
exports.generateKeys = generateKeys;
|
|
16
|
+
/**
|
|
17
|
+
* Creates a temporal keypair just to encrypt one message.
|
|
18
|
+
* Saves the public key in the result so that the message can be decrypted.
|
|
19
|
+
*/
|
|
20
|
+
function encrypt(encryptKey, message) {
|
|
21
|
+
const encryptPublicKey = (0, tweetnacl_util_1.decodeBase64)(encryptKey);
|
|
22
|
+
const tempPair = (0, tweetnacl_1.generateKeyPair)();
|
|
23
|
+
const encrypted = (0, tweetnacl_1.encrypt)(tempPair.secretKey, encryptPublicKey, message);
|
|
24
|
+
const hexTempPublic = (0, tweetnacl_util_1.encodeBase64)(tempPair.publicKey);
|
|
25
|
+
return `${hexTempPublic}:${encrypted}`;
|
|
26
|
+
}
|
|
27
|
+
exports.encrypt = encrypt;
|
|
28
|
+
/**
|
|
29
|
+
* Ecrypts a message using the decrypt key
|
|
30
|
+
*/
|
|
31
|
+
function decrypt(decryptKey, encrypted) {
|
|
32
|
+
const decryptSecretKey = (0, tweetnacl_util_1.decodeBase64)(decryptKey);
|
|
33
|
+
const [messagePubKeyHex, encryptedMessage] = encrypted.split(':');
|
|
34
|
+
const messagePubKey = (0, tweetnacl_util_1.decodeBase64)(messagePubKeyHex);
|
|
35
|
+
return (0, tweetnacl_1.decrypt)(decryptSecretKey, messagePubKey, encryptedMessage);
|
|
36
|
+
}
|
|
37
|
+
exports.decrypt = decrypt;
|
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export {};
|
|
@@ -0,0 +1,30 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
const _1 = require(".");
|
|
4
|
+
describe('Asymetric encryption lib', () => {
|
|
5
|
+
it('should generate public and private keys', async () => {
|
|
6
|
+
const { decryptKey, encryptKey } = (0, _1.generateKeys)();
|
|
7
|
+
expect(decryptKey.length).toBeGreaterThan(0);
|
|
8
|
+
expect(encryptKey.length).toBeGreaterThan(0);
|
|
9
|
+
expect(decryptKey).not.toEqual(encryptKey);
|
|
10
|
+
});
|
|
11
|
+
it('should encrypt a message using the encrypt key', async () => {
|
|
12
|
+
const { encryptKey } = (0, _1.generateKeys)();
|
|
13
|
+
const encrypted = (0, _1.encrypt)(encryptKey, 'hello');
|
|
14
|
+
expect(encrypted).toBeTruthy();
|
|
15
|
+
expect(encrypted.length).toBeGreaterThan(0);
|
|
16
|
+
});
|
|
17
|
+
it('should decrypt a message using de decrypt key', async () => {
|
|
18
|
+
const message = 'hello';
|
|
19
|
+
const { encryptKey, decryptKey } = (0, _1.generateKeys)();
|
|
20
|
+
const encrypted = (0, _1.encrypt)(encryptKey, message);
|
|
21
|
+
const decrypted = (0, _1.decrypt)(decryptKey, encrypted);
|
|
22
|
+
expect(decrypted).toEqual(message);
|
|
23
|
+
});
|
|
24
|
+
it('should not produce two equal encryptions for the same message', async () => {
|
|
25
|
+
const { encryptKey } = (0, _1.generateKeys)();
|
|
26
|
+
const encrypted = (0, _1.encrypt)(encryptKey, 'hello');
|
|
27
|
+
const encrypted2 = (0, _1.encrypt)(encryptKey, 'hello');
|
|
28
|
+
expect(encrypted).not.toEqual(encrypted2);
|
|
29
|
+
});
|
|
30
|
+
});
|
|
@@ -0,0 +1,3 @@
|
|
|
1
|
+
export declare const generateKeyPair: () => import("tweetnacl").BoxKeyPair;
|
|
2
|
+
export declare const encrypt: (bSecretKey: Uint8Array, aPublicKey: Uint8Array, message: string) => string;
|
|
3
|
+
export declare const decrypt: (aSecretKey: Uint8Array, bPublicKey: Uint8Array, messageWithNonce: string) => string;
|
|
@@ -0,0 +1,31 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.decrypt = exports.encrypt = exports.generateKeyPair = void 0;
|
|
4
|
+
const tweetnacl_1 = require("tweetnacl");
|
|
5
|
+
const tweetnacl_util_1 = require("tweetnacl-util");
|
|
6
|
+
const newNonce = () => (0, tweetnacl_1.randomBytes)(tweetnacl_1.box.nonceLength);
|
|
7
|
+
const generateKeyPair = () => tweetnacl_1.box.keyPair();
|
|
8
|
+
exports.generateKeyPair = generateKeyPair;
|
|
9
|
+
const encrypt = (bSecretKey, aPublicKey, message) => {
|
|
10
|
+
const nonce = newNonce();
|
|
11
|
+
const messageUint8 = (0, tweetnacl_util_1.decodeUTF8)(message);
|
|
12
|
+
const encrypted = (0, tweetnacl_1.box)(messageUint8, nonce, aPublicKey, bSecretKey);
|
|
13
|
+
const fullMessage = new Uint8Array(nonce.length + encrypted.length);
|
|
14
|
+
fullMessage.set(nonce);
|
|
15
|
+
fullMessage.set(encrypted, nonce.length);
|
|
16
|
+
const base64FullMessage = (0, tweetnacl_util_1.encodeBase64)(fullMessage);
|
|
17
|
+
return base64FullMessage;
|
|
18
|
+
};
|
|
19
|
+
exports.encrypt = encrypt;
|
|
20
|
+
const decrypt = (aSecretKey, bPublicKey, messageWithNonce) => {
|
|
21
|
+
const messageWithNonceAsUint8Array = (0, tweetnacl_util_1.decodeBase64)(messageWithNonce);
|
|
22
|
+
const nonce = messageWithNonceAsUint8Array.slice(0, tweetnacl_1.box.nonceLength);
|
|
23
|
+
const message = messageWithNonceAsUint8Array.slice(tweetnacl_1.box.nonceLength, messageWithNonce.length);
|
|
24
|
+
const decrypted = tweetnacl_1.box.open(message, nonce, bPublicKey, aSecretKey);
|
|
25
|
+
if (!decrypted) {
|
|
26
|
+
throw new Error('Could not decrypt message');
|
|
27
|
+
}
|
|
28
|
+
const base64DecryptedMessage = (0, tweetnacl_util_1.encodeUTF8)(decrypted);
|
|
29
|
+
return base64DecryptedMessage;
|
|
30
|
+
};
|
|
31
|
+
exports.decrypt = decrypt;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
export interface Config {
|
|
2
|
+
version: string;
|
|
3
|
+
publicKey: string;
|
|
4
|
+
cleanKeys: {
|
|
5
|
+
[key: string]: string;
|
|
6
|
+
};
|
|
7
|
+
encryptedKeys: {
|
|
8
|
+
[key: string]: string;
|
|
9
|
+
};
|
|
10
|
+
}
|
|
11
|
+
export interface Variables {
|
|
12
|
+
[key: string]: string;
|
|
13
|
+
}
|
|
14
|
+
export declare function getVariables(config: Config, secretKey: string): Variables;
|
|
@@ -0,0 +1,23 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.getVariables = void 0;
|
|
4
|
+
const crypto_1 = require("../crypto");
|
|
5
|
+
function getVariables(config, secretKey) {
|
|
6
|
+
const { cleanKeys, encryptedKeys } = config;
|
|
7
|
+
const variables = {};
|
|
8
|
+
for (const key in cleanKeys) {
|
|
9
|
+
const value = cleanKeys[key];
|
|
10
|
+
variables[key] = value;
|
|
11
|
+
}
|
|
12
|
+
for (const key in encryptedKeys) {
|
|
13
|
+
const encrypted = encryptedKeys[key];
|
|
14
|
+
try {
|
|
15
|
+
variables[key] = (0, crypto_1.decrypt)(secretKey, encrypted);
|
|
16
|
+
}
|
|
17
|
+
catch (error) {
|
|
18
|
+
throw new Error(`Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not the right key for "${key}"`);
|
|
19
|
+
}
|
|
20
|
+
}
|
|
21
|
+
return variables;
|
|
22
|
+
}
|
|
23
|
+
exports.getVariables = getVariables;
|
package/lib/environment/index.js
CHANGED
|
@@ -1,28 +1,26 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
exports.env = void 0;
|
|
4
|
-
const
|
|
5
|
-
const
|
|
6
|
-
|
|
3
|
+
exports.env = exports.readEnv = void 0;
|
|
4
|
+
const getConfig_1 = require("../cli/add/getConfig");
|
|
5
|
+
const getVariables_1 = require("./getVariables");
|
|
6
|
+
let variables = {};
|
|
7
7
|
const g = global;
|
|
8
|
-
|
|
9
|
-
|
|
8
|
+
const secretKey = process.env.ORION_ENV_SECRET_KEY;
|
|
9
|
+
const envFilePath = process.env.ORION_ENV_FILE_PATH;
|
|
10
|
+
const readEnv = () => {
|
|
11
|
+
const data = (0, getConfig_1.getConfig)(envFilePath);
|
|
12
|
+
return (0, getVariables_1.getVariables)(data, secretKey);
|
|
13
|
+
};
|
|
14
|
+
exports.readEnv = readEnv;
|
|
15
|
+
if (g.__orion_env_final__) {
|
|
16
|
+
variables = g.__orion_env_final__;
|
|
17
|
+
}
|
|
18
|
+
else if (envFilePath) {
|
|
10
19
|
if (!secretKey) {
|
|
11
20
|
throw new Error('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
|
|
12
21
|
}
|
|
13
|
-
|
|
14
|
-
const encryptedKeys = g.__orion_env__.encryptedKeys;
|
|
15
|
-
for (const key in cleanKeys) {
|
|
16
|
-
const value = cleanKeys[key];
|
|
17
|
-
env[key] = value;
|
|
18
|
-
}
|
|
19
|
-
for (const key in encryptedKeys) {
|
|
20
|
-
const encrypted = encryptedKeys[key];
|
|
21
|
-
try {
|
|
22
|
-
env[key] = crypto_1.asymmetric.decrypt(secretKey, encrypted);
|
|
23
|
-
}
|
|
24
|
-
catch (error) {
|
|
25
|
-
throw new Error(`Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not the right key for "${key}"`);
|
|
26
|
-
}
|
|
27
|
-
}
|
|
22
|
+
variables = (0, exports.readEnv)();
|
|
28
23
|
}
|
|
24
|
+
g.__orion_env_final__ = variables;
|
|
25
|
+
const env = variables;
|
|
26
|
+
exports.env = env;
|
|
@@ -1 +1 @@
|
|
|
1
|
-
|
|
1
|
+
export {};
|
|
@@ -1,15 +1,17 @@
|
|
|
1
1
|
"use strict";
|
|
2
2
|
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
-
require("
|
|
4
|
-
const
|
|
3
|
+
const crypto_1 = require("../crypto");
|
|
4
|
+
const getVariables_1 = require("./getVariables");
|
|
5
5
|
describe('Environment', () => {
|
|
6
6
|
beforeEach(() => {
|
|
7
|
+
;
|
|
8
|
+
global.__orion_env_final__ = undefined;
|
|
7
9
|
jest.resetModules();
|
|
8
10
|
});
|
|
9
11
|
it('should define all environment variables', async () => {
|
|
10
12
|
const secretKey = 'QShwQT1+d5wk/F6FVpT5VmZFXm50aFRt9/LaDbwSEGo=';
|
|
11
13
|
const secretValue = 'this_is_secret';
|
|
12
|
-
|
|
14
|
+
const data = {
|
|
13
15
|
version: '1.0',
|
|
14
16
|
publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
|
|
15
17
|
cleanKeys: {
|
|
@@ -20,41 +22,24 @@ describe('Environment', () => {
|
|
|
20
22
|
}
|
|
21
23
|
};
|
|
22
24
|
process.env.ORION_ENV_SECRET_KEY = secretKey;
|
|
23
|
-
const
|
|
25
|
+
const env = (0, getVariables_1.getVariables)(data, secretKey);
|
|
24
26
|
expect(env).toEqual({
|
|
25
27
|
a_key: 'a_value',
|
|
26
28
|
secret1: secretValue
|
|
27
29
|
});
|
|
28
30
|
});
|
|
29
|
-
it('should thow an error when the secret key is not present', () => {
|
|
30
|
-
global.__orion_env__ = {
|
|
31
|
-
version: '1.0',
|
|
32
|
-
publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
|
|
33
|
-
encryptedKeys: {
|
|
34
|
-
secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
|
|
35
|
-
}
|
|
36
|
-
};
|
|
37
|
-
try {
|
|
38
|
-
process.env.ORION_ENV_SECRET_KEY = '';
|
|
39
|
-
const { env } = require('./index');
|
|
40
|
-
console.log(env);
|
|
41
|
-
}
|
|
42
|
-
catch (error) {
|
|
43
|
-
expect(error.message).toEqual('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
|
|
44
|
-
}
|
|
45
|
-
expect.assertions(1);
|
|
46
|
-
});
|
|
47
31
|
it('should thow an error when the secret key is not the one used to encrypt', () => {
|
|
48
|
-
|
|
32
|
+
const data = {
|
|
49
33
|
version: '1.0',
|
|
50
34
|
publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
|
|
35
|
+
cleanKeys: {},
|
|
51
36
|
encryptedKeys: {
|
|
52
37
|
secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
|
|
53
38
|
}
|
|
54
39
|
};
|
|
55
40
|
try {
|
|
56
|
-
|
|
57
|
-
|
|
41
|
+
const key = (0, crypto_1.generateKeys)().decryptKey;
|
|
42
|
+
(0, getVariables_1.getVariables)(data, key);
|
|
58
43
|
}
|
|
59
44
|
catch (error) {
|
|
60
45
|
expect(error.message).toEqual('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not the right key for "secret1"');
|
|
@@ -0,0 +1,27 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
var __importDefault = (this && this.__importDefault) || function (mod) {
|
|
3
|
+
return (mod && mod.__esModule) ? mod : { "default": mod };
|
|
4
|
+
};
|
|
5
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
6
|
+
exports.ensureDirectory = exports.writeFile = exports.readFile = void 0;
|
|
7
|
+
const fs_1 = __importDefault(require("fs"));
|
|
8
|
+
const path_1 = __importDefault(require("path"));
|
|
9
|
+
function readFile(filePath) {
|
|
10
|
+
if (!fs_1.default.existsSync(filePath))
|
|
11
|
+
return null;
|
|
12
|
+
return fs_1.default.readFileSync(filePath).toString();
|
|
13
|
+
}
|
|
14
|
+
exports.readFile = readFile;
|
|
15
|
+
function writeFile(path, content) {
|
|
16
|
+
ensureDirectory(path);
|
|
17
|
+
fs_1.default.writeFileSync(path, content);
|
|
18
|
+
}
|
|
19
|
+
exports.writeFile = writeFile;
|
|
20
|
+
function ensureDirectory(filePath) {
|
|
21
|
+
const dirname = path_1.default.dirname(filePath);
|
|
22
|
+
if (fs_1.default.existsSync(dirname))
|
|
23
|
+
return true;
|
|
24
|
+
ensureDirectory(dirname);
|
|
25
|
+
fs_1.default.mkdirSync(dirname);
|
|
26
|
+
}
|
|
27
|
+
exports.ensureDirectory = ensureDirectory;
|
package/lib/index.d.ts
CHANGED
package/lib/index.js
CHANGED
|
@@ -0,0 +1 @@
|
|
|
1
|
+
export declare const internalGetEnv: (orionEnvName: string, processEnvName: string) => string | null;
|
|
@@ -0,0 +1,14 @@
|
|
|
1
|
+
"use strict";
|
|
2
|
+
Object.defineProperty(exports, "__esModule", { value: true });
|
|
3
|
+
exports.internalGetEnv = void 0;
|
|
4
|
+
const _1 = require(".");
|
|
5
|
+
const internalGetEnv = (orionEnvName, processEnvName) => {
|
|
6
|
+
if (_1.env[orionEnvName]) {
|
|
7
|
+
return _1.env[orionEnvName];
|
|
8
|
+
}
|
|
9
|
+
if (process.env[processEnvName]) {
|
|
10
|
+
return process.env[processEnvName];
|
|
11
|
+
}
|
|
12
|
+
return null;
|
|
13
|
+
};
|
|
14
|
+
exports.internalGetEnv = internalGetEnv;
|
package/package.json
CHANGED
|
@@ -1,9 +1,12 @@
|
|
|
1
1
|
{
|
|
2
2
|
"name": "@orion-js/env",
|
|
3
|
-
"version": "3.1.
|
|
3
|
+
"version": "3.1.20",
|
|
4
4
|
"main": "lib/index.js",
|
|
5
5
|
"author": "nicolaslopezj",
|
|
6
6
|
"license": "MIT",
|
|
7
|
+
"bin": {
|
|
8
|
+
"orion-env": "./lib/cli/index.js"
|
|
9
|
+
},
|
|
7
10
|
"scripts": {
|
|
8
11
|
"test": "ORION_DEV=1 jest",
|
|
9
12
|
"prepare": "yarn run build",
|
|
@@ -13,17 +16,20 @@
|
|
|
13
16
|
"upgrade-interactive": "yarn upgrade-interactive"
|
|
14
17
|
},
|
|
15
18
|
"dependencies": {
|
|
16
|
-
"
|
|
19
|
+
"colors": "^1.4.0",
|
|
20
|
+
"commander": "^9.1.0",
|
|
21
|
+
"prompts": "^2.4.2",
|
|
22
|
+
"tweetnacl-util": "0.15.1",
|
|
23
|
+
"yaml": "^2.0.0"
|
|
17
24
|
},
|
|
18
25
|
"devDependencies": {
|
|
19
26
|
"@types/jest": "^27.0.2",
|
|
20
27
|
"jest": "27.3.1",
|
|
21
|
-
"reflect-metadata": "0.1.13",
|
|
22
28
|
"ts-jest": "27.0.7",
|
|
23
29
|
"typescript": "^4.4.4"
|
|
24
30
|
},
|
|
25
31
|
"publishConfig": {
|
|
26
32
|
"access": "public"
|
|
27
33
|
},
|
|
28
|
-
"gitHead": "
|
|
34
|
+
"gitHead": "57400314f6605eb78cb5eed36430806ee4428e28"
|
|
29
35
|
}
|