@orion-js/env 3.1.12 → 3.1.23

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/README.md ADDED
@@ -0,0 +1,48 @@
1
+ # Orion Env
2
+
3
+ > This package works as standlaone package. It does not depend on any other orionjs package.
4
+
5
+ Orion Env is a utility for managing a collection of secrets in source control. The secrets are encrypted using public key, elliptic curve cryptography.
6
+
7
+ It's like dotEnv but saved in source control.
8
+
9
+ ## Creating a new Env config file
10
+
11
+ Run the following script in a project that has the package installed. Save the password output because you will need to use it to decrypt the keys.
12
+
13
+ yarn orion-env init --path=<path>
14
+
15
+ ## Adding a new env variable
16
+
17
+ Run the following script in a project that has the package installed and you will be prompted for the variable name and value.
18
+
19
+ yarn orion-env add --path=<path>
20
+
21
+ ## Using in your app
22
+
23
+ Define the following environment variables (in the old way):
24
+
25
+ - `ORION_ENV_FILE_PATH=<path>` The path to the env file.
26
+ - `ORION_ENV_SECRET_KEY=<password>` The password to decrypt the keys.
27
+
28
+ Then you can access the variables by importing the package and all the env variables defined will be the env object.
29
+
30
+ import {env} from '@orion-js/env'
31
+
32
+ env.XX
33
+
34
+ ## Setting environment variables (process.env)
35
+
36
+ To add backwards compatibility you can also use this libary to set the environment variables.
37
+
38
+ import {loadEnv} from '@orion-js/env'
39
+
40
+ loadEnv(options)
41
+
42
+ This will set the environment variables for the current process.
43
+
44
+ Options:
45
+
46
+ - `secretKey` Secret password used to decrypt the encrypted env file. Default: process.env.ORION_ENV_SECRET_KEY
47
+ - `envFilePath` Location of the file to read. Default: process.env.ORION_ENV_FILE_PATH
48
+ - `override` Set to true to set the environment variables even if the variable was already set. Default: process.env.ORION_ENV_OVERRIDE
@@ -0,0 +1,2 @@
1
+ import { Config } from '../../environment/getVariables';
2
+ export declare const encryptValue: (key: string, value: string, config: Config) => void;
@@ -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,2 @@
1
+ import { Config } from '../../environment/getVariables';
2
+ export declare const getConfig: (envPath: string) => Config;
@@ -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,5 @@
1
+ import { Config } from '../../environment/getVariables';
2
+ export declare const getParams: (config: Config) => Promise<{
3
+ key: string;
4
+ value: string;
5
+ }>;
@@ -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,3 @@
1
+ export default function envAdd({ envPath }: {
2
+ envPath: any;
3
+ }): Promise<void>;
@@ -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;
@@ -0,0 +1,2 @@
1
+ #!/usr/bin/env node
2
+ export {};
@@ -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,3 @@
1
+ export default function envInit({ envPath }: {
2
+ envPath: any;
3
+ }): Promise<void>;
@@ -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,4 @@
1
+ import { Config } from './getVariables';
2
+ export declare function getDts(config: Config): string;
3
+ export declare function writeDtsFile(config: Config, path: string): void;
4
+ export declare function writeDtsFileFromConfigFile(configFilePath: string, path: string): void;
@@ -0,0 +1,28 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.writeDtsFileFromConfigFile = exports.writeDtsFile = exports.getDts = void 0;
4
+ const getConfig_1 = require("../cli/add/getConfig");
5
+ const files_1 = require("../files");
6
+ function getDts(config) {
7
+ const keys = [...Object.keys(config.cleanKeys), ...Object.keys(config.encryptedKeys)];
8
+ return `declare module '@orion-js/env' {
9
+ export const env: {
10
+ ${keys.map(key => ` ${key}: string;`).join('\n')}
11
+ }
12
+ }
13
+ `;
14
+ }
15
+ exports.getDts = getDts;
16
+ function writeDtsFile(config, path) {
17
+ const currentFile = (0, files_1.readFile)(path);
18
+ const dts = getDts(config);
19
+ if (currentFile !== dts) {
20
+ (0, files_1.writeFile)(path, dts);
21
+ }
22
+ }
23
+ exports.writeDtsFile = writeDtsFile;
24
+ function writeDtsFileFromConfigFile(configFilePath, path) {
25
+ const config = (0, getConfig_1.getConfig)(configFilePath);
26
+ writeDtsFile(config, path);
27
+ }
28
+ exports.writeDtsFileFromConfigFile = writeDtsFileFromConfigFile;
@@ -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;
@@ -1,2 +1,8 @@
1
- declare const env: {};
1
+ export * from './getDts';
2
+ export * from './load';
3
+ export interface Variables {
4
+ [key: string]: string;
5
+ }
6
+ export declare const readEnv: () => import("./getVariables").Variables;
7
+ declare const env: Variables;
2
8
  export { env };
@@ -1,32 +1,37 @@
1
1
  "use strict";
2
+ var __createBinding = (this && this.__createBinding) || (Object.create ? (function(o, m, k, k2) {
3
+ if (k2 === undefined) k2 = k;
4
+ Object.defineProperty(o, k2, { enumerable: true, get: function() { return m[k]; } });
5
+ }) : (function(o, m, k, k2) {
6
+ if (k2 === undefined) k2 = k;
7
+ o[k2] = m[k];
8
+ }));
9
+ var __exportStar = (this && this.__exportStar) || function(m, exports) {
10
+ for (var p in m) if (p !== "default" && !Object.prototype.hasOwnProperty.call(exports, p)) __createBinding(exports, m, p);
11
+ };
2
12
  Object.defineProperty(exports, "__esModule", { value: true });
3
- exports.env = void 0;
4
- const crypto_1 = require("@orion-js/crypto");
13
+ exports.env = exports.readEnv = void 0;
14
+ const getConfig_1 = require("../cli/add/getConfig");
15
+ const getVariables_1 = require("./getVariables");
16
+ __exportStar(require("./getDts"), exports);
17
+ __exportStar(require("./load"), exports);
5
18
  let variables = {};
6
19
  const g = global;
20
+ const secretKey = process.env.ORION_ENV_SECRET_KEY;
21
+ const envFilePath = process.env.ORION_ENV_FILE_PATH;
22
+ const readEnv = () => {
23
+ const data = (0, getConfig_1.getConfig)(envFilePath);
24
+ return (0, getVariables_1.getVariables)(data, secretKey);
25
+ };
26
+ exports.readEnv = readEnv;
7
27
  if (g.__orion_env_final__) {
8
28
  variables = g.__orion_env_final__;
9
29
  }
10
- else if (g.__orion_env__) {
11
- const secretKey = process.env.ORION_ENV_SECRET_KEY;
30
+ else if (envFilePath) {
12
31
  if (!secretKey) {
13
32
  throw new Error('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
14
33
  }
15
- const cleanKeys = g.__orion_env__.cleanKeys;
16
- const encryptedKeys = g.__orion_env__.encryptedKeys;
17
- for (const key in cleanKeys) {
18
- const value = cleanKeys[key];
19
- variables[key] = value;
20
- }
21
- for (const key in encryptedKeys) {
22
- const encrypted = encryptedKeys[key];
23
- try {
24
- variables[key] = crypto_1.asymmetric.decrypt(secretKey, encrypted);
25
- }
26
- catch (error) {
27
- throw new Error(`Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not the right key for "${key}"`);
28
- }
29
- }
34
+ variables = (0, exports.readEnv)();
30
35
  }
31
36
  g.__orion_env_final__ = variables;
32
37
  const env = variables;
@@ -1 +1 @@
1
- import 'reflect-metadata';
1
+ export {};
@@ -1,7 +1,7 @@
1
1
  "use strict";
2
2
  Object.defineProperty(exports, "__esModule", { value: true });
3
- require("reflect-metadata");
4
- const crypto_1 = require("@orion-js/crypto");
3
+ const crypto_1 = require("../crypto");
4
+ const getVariables_1 = require("./getVariables");
5
5
  describe('Environment', () => {
6
6
  beforeEach(() => {
7
7
  ;
@@ -11,7 +11,7 @@ describe('Environment', () => {
11
11
  it('should define all environment variables', async () => {
12
12
  const secretKey = 'QShwQT1+d5wk/F6FVpT5VmZFXm50aFRt9/LaDbwSEGo=';
13
13
  const secretValue = 'this_is_secret';
14
- global.__orion_env__ = {
14
+ const data = {
15
15
  version: '1.0',
16
16
  publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
17
17
  cleanKeys: {
@@ -22,41 +22,24 @@ describe('Environment', () => {
22
22
  }
23
23
  };
24
24
  process.env.ORION_ENV_SECRET_KEY = secretKey;
25
- const { env } = require('./index');
25
+ const env = (0, getVariables_1.getVariables)(data, secretKey);
26
26
  expect(env).toEqual({
27
27
  a_key: 'a_value',
28
28
  secret1: secretValue
29
29
  });
30
30
  });
31
- it('should thow an error when the secret key is not present', () => {
32
- global.__orion_env__ = {
33
- version: '1.0',
34
- publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
35
- encryptedKeys: {
36
- secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
37
- }
38
- };
39
- try {
40
- process.env.ORION_ENV_SECRET_KEY = '';
41
- const { env } = require('./index');
42
- console.log(env);
43
- }
44
- catch (error) {
45
- expect(error.message).toEqual('Orion encrypted env was passed but process.env.ORION_ENV_SECRET_KEY is not defined');
46
- }
47
- expect.assertions(1);
48
- });
49
31
  it('should thow an error when the secret key is not the one used to encrypt', () => {
50
- global.__orion_env__ = {
32
+ const data = {
51
33
  version: '1.0',
52
34
  publicKey: 'quyw/56O1P/BmjlHGfguZD27zKbjOtxNBDOTz+FOYho=',
35
+ cleanKeys: {},
53
36
  encryptedKeys: {
54
37
  secret1: 'nQCxsZxjVkOABeQSdIhYK7jSMYKUggUm9IWUGLpY3i4=:9gvH5IOhV/q5R4ngUIk2onf5oEZM5dIU89PRZ5TGjnnfcnrwkssLqsACNDmr0m4jQZVo0nBL'
55
38
  }
56
39
  };
57
40
  try {
58
- process.env.ORION_ENV_SECRET_KEY = crypto_1.asymmetric.generateKeys().decryptKey;
59
- require('./index');
41
+ const key = (0, crypto_1.generateKeys)().decryptKey;
42
+ (0, getVariables_1.getVariables)(data, key);
60
43
  }
61
44
  catch (error) {
62
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,6 @@
1
+ export interface LoadEnvOptions {
2
+ secretKey?: string;
3
+ envFilePath?: string;
4
+ override?: boolean;
5
+ }
6
+ export declare function loadEnv(passedOptions?: LoadEnvOptions): void;
@@ -0,0 +1,33 @@
1
+ "use strict";
2
+ Object.defineProperty(exports, "__esModule", { value: true });
3
+ exports.loadEnv = void 0;
4
+ const getConfig_1 = require("../cli/add/getConfig");
5
+ const getVariables_1 = require("./getVariables");
6
+ const defaultOptions = {
7
+ secretKey: process.env.ORION_ENV_SECRET_KEY,
8
+ envFilePath: process.env.ORION_ENV_FILE_PATH,
9
+ override: !!process.env.ORION_ENV_OVERRIDE
10
+ };
11
+ function loadEnv(passedOptions = {}) {
12
+ const options = { ...defaultOptions, ...passedOptions };
13
+ const data = (0, getConfig_1.getConfig)(options.envFilePath);
14
+ const variables = (0, getVariables_1.getVariables)(data, options.secretKey);
15
+ for (const key in variables) {
16
+ const variable = variables[key];
17
+ if (!Object.prototype.hasOwnProperty.call(process.env, key)) {
18
+ process.env[key] = variable;
19
+ }
20
+ else {
21
+ if (options.override) {
22
+ process.env[key] = variable;
23
+ }
24
+ if (options.override) {
25
+ console.log(`"${key}" is already defined in \`process.env\` and WAS overwritten`);
26
+ }
27
+ else {
28
+ console.log(`"${key}" is already defined in \`process.env\` and was NOT overwritten`);
29
+ }
30
+ }
31
+ }
32
+ }
33
+ exports.loadEnv = loadEnv;
@@ -0,0 +1,3 @@
1
+ export declare function readFile(filePath: string): string;
2
+ export declare function writeFile(path: string, content: string): void;
3
+ export declare function ensureDirectory(filePath: any): boolean;
@@ -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/package.json CHANGED
@@ -1,9 +1,12 @@
1
1
  {
2
2
  "name": "@orion-js/env",
3
- "version": "3.1.12",
3
+ "version": "3.1.23",
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
- "@orion-js/crypto": "^3.1.12"
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": "a6b82a931bea3179dfac4099579096c4f7a780bb"
34
+ "gitHead": "0194171f6f326dfa921cf6e9f341e5b733b7d11e"
29
35
  }