Comparing version 0.1.1 to 0.1.2
{ | ||
"name": "cryptoenv", | ||
"version": "0.1.1", | ||
"version": "0.1.2", | ||
"description": "Manage encrypted env variables", | ||
@@ -5,0 +5,0 @@ "homepage": "https://github.com/secrez/cryptoenv#readme", |
@@ -40,2 +40,8 @@ # CryptoEnv | ||
Install it as usual | ||
``` | ||
npm i cryptoenv | ||
``` | ||
Let's do the case of Hardhat. You have a conf file called `hardhat.config.js`. At the beginning of that file you can read the env variable with, for example Dotenv, and after requiring CryptoEnv, like here: | ||
@@ -55,3 +61,3 @@ | ||
accounts: [ | ||
process.env.OWNER_KEY || process.env.OTHER_PRIVATE_KEY | ||
process.env.OWNER_KEY | ||
], | ||
@@ -63,8 +69,8 @@ chainId: 3 | ||
This way, if the key is decrypted and available, Hardhat will use it, if not it will use some other key. | ||
If you just press enter when asked for the password, the decryption will be ignored. CryptoEnv will throw an error only if the password is wrong. | ||
Notice that, while Hardhood allows different passwords for different keys, putting the data in the `~/hardhood` folder, CryptoEnv manages an `.env` file, and it makes more sense to use the same password for all the variables. | ||
To avoid that Hardhat gives you an error when you skip the decryption, you can set up a variable OWNER_KEY in the `.env` file, with a testing key. When you will use CryptoEnv, the variable will be overwritten. | ||
Notice that after saving the first encrypted key, for all the others you must use the same password. | ||
### Multiple apps | ||
@@ -119,4 +125,5 @@ | ||
~~~~~~~~MIT — enjoy it :-) | ||
```MIT — enjoy it :-) | ||
``` |
@@ -66,19 +66,2 @@ const path = require("path"); | ||
filter(variables, how) { | ||
let filtered = {}; | ||
for (let key in variables) { | ||
if (typeof how === "function") { | ||
if (!how(key)) { | ||
continue; | ||
} | ||
} else if (Object.prototype.toString.call(how) === "[object RegExp]") { | ||
if (!how.test(key)) { | ||
continue; | ||
} | ||
} | ||
filtered[key] = variables[key]; | ||
} | ||
return filtered; | ||
} | ||
async newKey() { | ||
@@ -157,8 +140,9 @@ let { variable } = await inquirer.prompt([ | ||
key = key.split(this.prefix)[1]; | ||
if (filter && ((typeof filter === "function" && !filter(key)) || ( | ||
Object.prototype.toString.call(filter) && !filter.test(key) | ||
))) { | ||
continue; | ||
} | ||
this.keys[key]= value; | ||
if ( | ||
!filter || | ||
((typeof filter === "function" && filter(key)) || | ||
(Object.prototype.toString.call(filter) && filter.test && filter.test(key))) | ||
) { | ||
this.keys[key] = value; | ||
} | ||
} | ||
@@ -170,7 +154,11 @@ } | ||
if (!password) { | ||
const prompt = require('prompt-sync')({}); | ||
console.log(chalk.green("CryptoEnv > Type your password to decrypt the env")); | ||
const prompt = require("prompt-sync")({}); | ||
console.log( | ||
chalk.green( | ||
"CryptoEnv > Type your password to decrypt the env, or press enter to skip it" | ||
) | ||
); | ||
password = prompt.hide(); | ||
if (!password) { | ||
return console.log(chalk.grey("CryptoEnv > decription skipped")); | ||
return console.log(chalk.grey("CryptoEnv > decryption skipped")); | ||
} | ||
@@ -184,15 +172,17 @@ } | ||
for (let key in this.keys) { | ||
try { | ||
process.env[key] = Crypto.decrypt( | ||
this.keys[key], | ||
Crypto.SHA3(password) | ||
); | ||
found++; | ||
} catch (e) { | ||
console.log(chalk.red("Wrong password")); | ||
process.exit(1); | ||
} | ||
try { | ||
process.env[key] = Crypto.decrypt( | ||
this.keys[key], | ||
Crypto.SHA3(password) | ||
); | ||
found++; | ||
} catch (e) { | ||
console.log(chalk.red("Wrong password")); | ||
process.exit(1); | ||
} | ||
} | ||
if (found) { | ||
console.info(chalk.green(`CryptoEnv > ${found} key${found > 1 ? "s" : ""} decrypted`)); | ||
console.info( | ||
chalk.green(`CryptoEnv > ${found} key${found > 1 ? "s" : ""} decrypted`) | ||
); | ||
} else { | ||
@@ -199,0 +189,0 @@ console.info(chalk.grey(`CryptoEnv > no encrypted keys found`)); |
@@ -11,2 +11,3 @@ const { assert, expect } = require("chai"); | ||
let password = "some-very-strong-password"; | ||
let value = "8s8s8s8s87w7w7wydydydyd6d6d6d6"; | ||
@@ -33,3 +34,2 @@ before(async function () {}); | ||
); | ||
let value = "8s8s8s8s87w7w7wydydydyd6d6d6d6"; | ||
expect( | ||
@@ -47,5 +47,5 @@ CryptoEnv.Crypto.decrypt( | ||
let newKey = "privateKey"; | ||
let value = "7fys8f7ywbfwbyef8sbfs8dfysd8cysdchsdcysc"; | ||
let value1 = "7fys8f7ywbfwbyef8sbfs8dfysd8cysdchsdcysc"; | ||
let cryptoEnv = new CryptoEnv({ envPath, newKey }); | ||
await cryptoEnv.encryptAndSave(value, password); | ||
await cryptoEnv.encryptAndSave(value1, password); | ||
const { variables } = cryptoEnv.list(true); | ||
@@ -61,5 +61,5 @@ expect(Object.keys(variables).length).equal(2); | ||
let newKey = "myKey"; | ||
let value = "workdansdaBANK9987"; | ||
let value1 = "workdansdaBANK9987"; | ||
let cryptoEnv = new CryptoEnv({ envPath, newKey }); | ||
await cryptoEnv.encryptAndSave(value, password); | ||
await cryptoEnv.encryptAndSave(value1, password); | ||
const { variables } = cryptoEnv.list(true); | ||
@@ -75,6 +75,6 @@ expect(Object.keys(variables).length).equal(1); | ||
let newKey = "privateKey"; | ||
let value = "7fys8f7ywbfwbyef8sbfs8dfysd8cysdchsdcysc"; | ||
let value1 = "7fys8f7ywbfwbyef8sbfs8dfysd8cysdchsdcysc"; | ||
let cryptoEnv = new CryptoEnv({ envPath, newKey }); | ||
assertThrowsMessage( | ||
cryptoEnv.encryptAndSave(value, "a-new-strong-password"), | ||
cryptoEnv.encryptAndSave(value1, "a-new-strong-password"), | ||
"This is not the password used in the past" | ||
@@ -91,6 +91,6 @@ ); | ||
cryptoEnv.parse(undefined, password); | ||
expect(process.env.myKey).equal("8s8s8s8s87w7w7wydydydyd6d6d6d6"); | ||
expect(process.env.myKey).equal(value); | ||
}); | ||
it("should parse with a filter", async function () { | ||
it("should parse with a filter (regex)", async function () { | ||
delete process.env.myKey; | ||
@@ -101,5 +101,8 @@ require("dotenv").config({ path: envPath }); | ||
expect(process.env.myKey).equal(undefined); | ||
cryptoEnv.parse(/key/i, password); | ||
expect(process.env.myKey).equal(value); | ||
}); | ||
it("should parse with a filter", async function () { | ||
it("should parse with a filter (function)", async function () { | ||
delete process.env.myKey; | ||
@@ -109,7 +112,9 @@ process.env.nodeENV = "test"; | ||
let cryptoEnv = new CryptoEnv(); | ||
cryptoEnv.parse(() => process.env.nodeENV !== "test" , password); | ||
cryptoEnv.parse(() => process.env.nodeENV !== "test", password); | ||
expect(process.env.myKey).equal(undefined); | ||
cryptoEnv.parse(() => process.env.nodeENV === "test", password); | ||
expect(process.env.myKey).equal(value); | ||
}); | ||
it.skip("should parse the .env file and decrypt the variables", async function () { | ||
@@ -122,5 +127,5 @@ delete process.env.myKey; | ||
cryptoEnv.parse(); | ||
expect(process.env.myKey, "8s8s8s8s87w7w7wydydydyd6d6d6d6"); | ||
expect(process.env.myKey, value); | ||
}); | ||
}); | ||
}); |
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
License Policy Violation
LicenseThis package is not allowed per your license policy. Review the package's license to ensure compliance.
Found 1 instance in 1 package
25954
22
126
373
19