yaml-crypt
Advanced tools
Comparing version 0.4.8 to 0.5.0
@@ -22,2 +22,3 @@ #!/usr/bin/env node | ||
const { UsageError, safeDumpAll, tryDecrypt } = require("../lib/utils"); | ||
const { walk } = require("../lib/yaml-crypt-helper"); | ||
@@ -152,4 +153,14 @@ require("pkginfo")(module); | ||
help: | ||
"Allows to pass directories as input, will process all files within the given directories (non-recursive)" | ||
"Allows to pass directories as input, process all files within the given directories (non-recursive)" | ||
}); | ||
parser.addArgument(["-R", "--recursive"], { | ||
action: "storeTrue", | ||
help: | ||
"Allows to pass directories as input, process all files within the given directories and subdirectories recursively" | ||
}); | ||
parser.addArgument(["--continue"], { | ||
action: "storeTrue", | ||
help: | ||
"Continue processing even when encryption/decryption of one or more files failed" | ||
}); | ||
parser.addArgument(["--keep"], { | ||
@@ -232,2 +243,5 @@ action: "storeTrue", | ||
} | ||
if (args.dir && args.recursive) { | ||
throw new UsageError("cannot combine --dir and --recursive!"); | ||
} | ||
if (args.edit && !args.file.length) { | ||
@@ -314,3 +328,3 @@ throw new UsageError("option --edit used, but no files given!"); | ||
for (const file of args.file) { | ||
processFileArg(file, keys, encryptionKey, algorithm, args); | ||
processFileArg(file, keys, encryptionKey, algorithm, args, options); | ||
} | ||
@@ -513,30 +527,44 @@ } else { | ||
function processFileArg(file, keys, encryptionKey, algorithm, args) { | ||
function processFileArg(file, keys, encryptionKey, algorithm, args, options) { | ||
const stat = fs.statSync(file); | ||
if (stat.isDirectory()) { | ||
if (args.dir) { | ||
fs.readdirSync(file) | ||
.filter(f => { | ||
if (args.encrypt) { | ||
return plaintextFile(f); | ||
} else if (args.decrypt) { | ||
return encryptedFile(f); | ||
} else { | ||
return plaintextFile(f) || encryptedFile(f); | ||
} | ||
}) | ||
.forEach(f => | ||
processFile(file + "/" + f, keys, encryptionKey, algorithm, args) | ||
); | ||
if (args.dir || args.recursive) { | ||
walk(file, args.recursive, f => { | ||
if (args.encrypt && !plaintextFile(f)) { | ||
return; | ||
} else if (args.decrypt && !encryptedFile(f)) { | ||
return; | ||
} else if (!plaintextFile(f) && !encryptedFile(f)) { | ||
return; | ||
} else { | ||
processFile(f, keys, encryptionKey, algorithm, args, options); | ||
} | ||
}); | ||
} else { | ||
throw new UsageError( | ||
`directories will be skipped unless --dir given: ${file}` | ||
`directories will be skipped unless --dir or --recursive given: ${file}` | ||
); | ||
} | ||
} else { | ||
processFile(file, keys, encryptionKey, algorithm, args); | ||
processFile(file, keys, encryptionKey, algorithm, args, options); | ||
} | ||
} | ||
function processFile(file, keys, encryptionKey, algorithm, args) { | ||
function processFile(file, keys, encryptionKey, algorithm, args, options) { | ||
try { | ||
doProcessFile(file, keys, encryptionKey, algorithm, args); | ||
} catch (e) { | ||
if (args["continue"]) { | ||
if (options.stderr) { | ||
options.stderr.write(`error: ${e.message}`); | ||
} else { | ||
console.error(`error: ${e.message}`); | ||
} | ||
} else { | ||
throw e; | ||
} | ||
} | ||
} | ||
function doProcessFile(file, keys, encryptionKey, algorithm, args) { | ||
let encrypting; | ||
@@ -543,0 +571,0 @@ if (plaintextFile(file)) { |
@@ -0,1 +1,4 @@ | ||
const fs = require("fs"); | ||
const path = require("path"); | ||
const yaml = require("js-yaml"); | ||
@@ -34,3 +37,3 @@ | ||
if (!key) { | ||
throw new Error("No matching key to decrypt the given data!"); | ||
throw new Error("no matching key to decrypt the given data!"); | ||
} | ||
@@ -117,3 +120,18 @@ | ||
function walk(dir, recursive, callback) { | ||
const entries = fs.readdirSync(dir, { withFileTypes: true }); | ||
for (const entry of entries) { | ||
const p = path.resolve(dir, entry.name); | ||
if (entry.isDirectory()) { | ||
if (recursive) { | ||
walk(p, true, callback); | ||
} | ||
} else { | ||
callback(p); | ||
} | ||
} | ||
} | ||
module.exports.safeDumpAll = safeDumpAll; | ||
module.exports.transform = transform; | ||
module.exports.walk = walk; |
{ | ||
"name": "yaml-crypt", | ||
"version": "0.4.8", | ||
"version": "0.5.0", | ||
"description": "Encrypt and decrypt YAML documents", | ||
@@ -36,8 +36,8 @@ "license": "MIT", | ||
"chai": "^4.1.2", | ||
"coveralls": "^3.0.3", | ||
"coveralls": "^3.0.4", | ||
"eslint": "^5.16.0", | ||
"mocha": "^6.1.4", | ||
"nyc": "^14.1.1", | ||
"prettier": "^1.17.1" | ||
"prettier": "^1.18.2" | ||
} | ||
} |
@@ -79,3 +79,5 @@ const fs = require("fs"); | ||
it("should correctly identify valid string tokens (branca)", () => { | ||
const token = fs.readFileSync("./test/test-7.yaml-crypt").toString(); | ||
const token = fs | ||
.readFileSync("./test/resources/test-7.yaml-crypt") | ||
.toString(); | ||
expect(crypto.isToken(token)).to.equal(true); | ||
@@ -85,3 +87,3 @@ }); | ||
it("should correctly identify valid Buffer tokens (branca)", () => { | ||
const token = fs.readFileSync("./test/test-7.yaml-crypt"); | ||
const token = fs.readFileSync("./test/resources/test-7.yaml-crypt"); | ||
expect(crypto.isToken(token)).to.equal(true); | ||
@@ -88,0 +90,0 @@ }); |
@@ -122,6 +122,6 @@ const fs = require("fs"); | ||
const input = tmp.fileSync({ postfix: ".yaml" }); | ||
fs.copyFileSync("./test/test-2.yaml", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml", input.name); | ||
runWithKeyFile([input.name], {}, { stdout: new Out() }); | ||
const output = fs.readFileSync(input.name + "-crypt"); | ||
const expected = fs.readFileSync("./test/test-2.yaml-crypt"); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml-crypt"); | ||
expect(output.toString("utf8")).to.equal(expected.toString("utf8")); | ||
@@ -132,3 +132,3 @@ }); | ||
const input = tmp.fileSync({ postfix: ".yaml" }); | ||
fs.copyFileSync("./test/test-2.yaml", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml", input.name); | ||
runWithKeyFile(["-a", "branca", input.name], {}, { stdout: new Out() }); | ||
@@ -141,3 +141,3 @@ const output = fs.readFileSync(input.name + "-crypt"); | ||
const input = tmp.fileSync({ postfix: ".yaml-crypt" }); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml-crypt", input.name); | ||
runWithKeyFile([input.name], {}, { stdout: new Out() }); | ||
@@ -147,3 +147,3 @@ const output = fs.readFileSync( | ||
); | ||
const expected = fs.readFileSync("./test/test-2.yaml"); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml"); | ||
expect(output.toString("utf8")).to.equal(expected.toString("utf8")); | ||
@@ -154,4 +154,7 @@ }); | ||
const tmpdir = tmp.dirSync(); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", `${tmpdir.name}/2.yaml-crypt`); | ||
fs.copyFileSync("./test/test-2.yaml", `${tmpdir.name}/2.yaml`); | ||
fs.copyFileSync( | ||
"./test/resources/test-2.yaml-crypt", | ||
`${tmpdir.name}/2.yaml-crypt` | ||
); | ||
fs.copyFileSync("./test/resources/test-2.yaml", `${tmpdir.name}/2.yaml`); | ||
expect(() => | ||
@@ -168,4 +171,7 @@ runWithKeyFile( | ||
const tmpdir = tmp.dirSync(); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", `${tmpdir.name}/2.yaml-crypt`); | ||
fs.copyFileSync("./test/test-2.yaml", `${tmpdir.name}/2.yaml`); | ||
fs.copyFileSync( | ||
"./test/resources/test-2.yaml-crypt", | ||
`${tmpdir.name}/2.yaml-crypt` | ||
); | ||
fs.copyFileSync("./test/resources/test-2.yaml", `${tmpdir.name}/2.yaml`); | ||
runWithKeyFile( | ||
@@ -180,6 +186,12 @@ ["-d", "--force", `${tmpdir.name}/2.yaml-crypt`], | ||
const tmpdir = tmp.dirSync(); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", `${tmpdir.name}/1.yaml-crypt`); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", `${tmpdir.name}/2.yml-crypt`); | ||
fs.copyFileSync( | ||
"./test/resources/test-2.yaml-crypt", | ||
`${tmpdir.name}/1.yaml-crypt` | ||
); | ||
fs.copyFileSync( | ||
"./test/resources/test-2.yaml-crypt", | ||
`${tmpdir.name}/2.yml-crypt` | ||
); | ||
runWithKeyFile(["--dir", tmpdir.name], {}, { stdout: new Out() }); | ||
const expected = fs.readFileSync("./test/test-2.yaml"); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml"); | ||
const output1 = fs.readFileSync(`${tmpdir.name}/1.yaml`); | ||
@@ -199,3 +211,3 @@ const output2 = fs.readFileSync(`${tmpdir.name}/2.yml`); | ||
const output = fs.readFileSync(input.name + "-crypt"); | ||
const expected = fs.readFileSync("./test/test-3.yaml-crypt"); | ||
const expected = fs.readFileSync("./test/resources/test-3.yaml-crypt"); | ||
expect(output.toString("utf8")).to.equal(expected.toString("utf8")); | ||
@@ -206,3 +218,3 @@ }); | ||
const input = tmp.fileSync({ postfix: ".yaml" }); | ||
fs.copyFileSync("./test/test-2.yaml", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml", input.name); | ||
runWithKeyFile([input.name], {}, { stdout: new Out() }); | ||
@@ -214,3 +226,3 @@ expect(fs.existsSync(input.name)).to.equal(false); | ||
const input = tmp.fileSync({ postfix: ".yaml" }); | ||
fs.copyFileSync("./test/test-2.yaml", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml", input.name); | ||
runWithKeyFile(["--keep", input.name], {}, { stdout: new Out() }); | ||
@@ -232,3 +244,3 @@ expect(fs.existsSync(input.name)).to.equal(true); | ||
const input = tmp.fileSync({ postfix: ".yaml-crypt" }); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml-crypt", input.name); | ||
const keyFile = tmp.fileSync(); | ||
@@ -245,3 +257,3 @@ fs.writeFileSync(keyFile.name, "aehae5Ui0Eechaeghau9Yoh9jufiep7H"); | ||
); | ||
const expected = fs.readFileSync("./test/test-2.yaml"); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml"); | ||
expect(output.toString("utf8")).to.equal(expected.toString("utf8")); | ||
@@ -254,3 +266,3 @@ }); | ||
const input = tmp.fileSync({ postfix: ".yaml-crypt" }); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml-crypt", input.name); | ||
expect(() => | ||
@@ -265,2 +277,14 @@ yamlcryptcli.run( | ||
it("should not throw an error when --continue is given", () => { | ||
const keyFile = tmp.fileSync(); | ||
fs.writeSync(keyFile.fd, "INVALID_KEYchaeghau9Yoh9jufiep7H"); | ||
const input = tmp.fileSync({ postfix: ".yaml-crypt" }); | ||
fs.copyFileSync("./test/resources/test-2.yaml-crypt", input.name); | ||
yamlcryptcli.run( | ||
["--continue", "-k", keyFile.name, input.name], | ||
{}, | ||
{ stderr: new Out() } | ||
); | ||
}); | ||
it("should throw an error when no named key is available in the config file", () => { | ||
@@ -300,7 +324,7 @@ const config = { | ||
const options = { | ||
stdin: fs.readFileSync("./test/test-2.yaml-crypt"), | ||
stdin: fs.readFileSync("./test/resources/test-2.yaml-crypt"), | ||
stdout: new Out() | ||
}; | ||
yamlcryptcli.run(["-d"], config, options); | ||
const expected = fs.readFileSync("./test/test-2.yaml").toString(); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml").toString(); | ||
expect(options.stdout.str).to.equal(expected); | ||
@@ -349,3 +373,3 @@ }); | ||
const input = tmp.fileSync({ postfix: ".yaml-crypt" }); | ||
fs.copyFileSync("./test/test-2.yaml-crypt", input.name); | ||
fs.copyFileSync("./test/resources/test-2.yaml-crypt", input.name); | ||
@@ -359,5 +383,5 @@ yamlcryptcli.run( | ||
const output = fs.readFileSync(input.name); | ||
const expected = fs.readFileSync("./test/test-2.yaml-crypt"); | ||
const expected = fs.readFileSync("./test/resources/test-2.yaml-crypt"); | ||
expect(output.toString("utf8")).to.equal(expected.toString("utf8")); | ||
}); | ||
}); |
@@ -62,3 +62,5 @@ const fs = require("fs"); | ||
const config = { keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }; | ||
const result = await loadFile("./test/test-1b.yaml-crypt", { config }); | ||
const result = await loadFile("./test/resources/test-1b.yaml-crypt", { | ||
config | ||
}); | ||
expect(result.key1.toString()).to.equal("Hello, world!"); | ||
@@ -70,3 +72,3 @@ expect(result.key2.toString()).to.equal("Hello, world!"); | ||
const config = { keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }; | ||
const result = await loadFile("./test/test-2.yaml-crypt", { | ||
const result = await loadFile("./test/resources/test-2.yaml-crypt", { | ||
config, | ||
@@ -81,3 +83,3 @@ loadAll: true | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-1b.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-1b.yaml-crypt"); | ||
const result = yaml.decrypt(content); | ||
@@ -90,3 +92,3 @@ expect(result.key1.toString()).to.equal("Hello, world!"); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-7.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-7.yaml-crypt"); | ||
const result = yaml.decrypt(content); | ||
@@ -98,3 +100,3 @@ expect(result).to.equal("Hello!"); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-7.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-7.yaml-crypt"); | ||
const result = yaml.decryptAll(content.toString()); | ||
@@ -111,3 +113,3 @@ expect(result[0]).to.equal("Hello!"); | ||
const expected = fs | ||
.readFileSync("./test/test-1a.yaml-crypt") | ||
.readFileSync("./test/resources/test-1a.yaml-crypt") | ||
.toString("utf8"); | ||
@@ -133,3 +135,3 @@ expect(result).to.equal(expected); | ||
const expected = fs | ||
.readFileSync("./test/test-4.yaml-crypt") | ||
.readFileSync("./test/resources/test-4.yaml-crypt") | ||
.toString("utf8"); | ||
@@ -141,3 +143,3 @@ expect(result).to.equal(expected); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-4.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-4.yaml-crypt"); | ||
const result = yaml.decryptAll(content, { base64: true })[0]; | ||
@@ -149,3 +151,3 @@ expect(result.base64.toString()).to.equal("Hello, world!"); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-5.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-5.yaml-crypt"); | ||
let decrypted = null; | ||
@@ -158,3 +160,3 @@ yaml.transform(content, str => (decrypted = str)); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content1 = fs.readFileSync("./test/test-6.yaml-crypt"); | ||
const content1 = fs.readFileSync("./test/resources/test-6.yaml-crypt"); | ||
const transformed = yaml.transform(content1, str => | ||
@@ -168,3 +170,3 @@ str.replace("Hello!", "Hello, world!") | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const expected = fs.readFileSync("./test/test-1a.yaml-crypt"); | ||
const expected = fs.readFileSync("./test/resources/test-1a.yaml-crypt"); | ||
const newContent = | ||
@@ -178,3 +180,3 @@ "key1: !<!yaml-crypt/fernet> Hello, world!\nkey2: !<!yaml-crypt/fernet> Hello, world!"; | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-7.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-7.yaml-crypt"); | ||
const transformed = yaml.transform(content, str => str); | ||
@@ -186,3 +188,3 @@ expect(transformed).to.equal(content); | ||
const yaml = yamlcrypt({ keys: "aehae5Ui0Eechaeghau9Yoh9jufiep7H" }); | ||
const content = fs.readFileSync("./test/test-7.yaml-crypt"); | ||
const content = fs.readFileSync("./test/resources/test-7.yaml-crypt"); | ||
const transformed = yaml.transform(content, str => | ||
@@ -189,0 +191,0 @@ str.replace("Hello!", "Hello, world!") |
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
75261
26
2112
8