Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

decryption specific file and create file #20

Open
wants to merge 1 commit into
base: master
Choose a base branch
from

Conversation

sarawee
Copy link

@sarawee sarawee commented Apr 22, 2021

I can't decryption by specific file with cli.
I was try npx secure-env myEnvironment.enc -s mySecretPassword -d found error "ERROR OCCURED .env.enc does not exist"

Closes #12 #18 #19

@@ -14,7 +14,8 @@ const log = require('./utils/log')
module.exports.decrypt = (options) => {
try {
const secret = options.secret || 'mySecret'
const inputFile = options.file || '.env.enc'
const inputFile = options.inputFile || '.env.enc'
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

This would break https://github.com/kunalpanchal/secure-env/blob/master/lib/index.js#L18
Can we also update it there to send it as inputFile instead of file

if (fs.existsSync(outputFilePath)) {
fs.copyFileSync(outputFilePath, `${outputFilePath}.bak`)
}
fs.writeFileSync(outputFilePath, decrypted)
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Please note that this same function is used in https://github.com/kunalpanchal/secure-env/blob/master/lib/index.js

so we should not create or write any file if its used within the code.

All of this should only happen if the outputFilePath exists. Otherwise just log it the screen. If -o option is not sent via the CLI

@@ -14,7 +14,8 @@ const log = require('./utils/log')
module.exports.decrypt = (options) => {
try {
const secret = options.secret || 'mySecret'
const inputFile = options.file || '.env.enc'
const inputFile = options.inputFile || '.env.enc'
const outputFilePath = options.outputFile || inputFile.replace('.enc', '')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Suggested change
const outputFilePath = options.outputFile || inputFile.replace('.enc', '')
const outputFilePath = options.outputFile

Use the path only if specified explicitly lets not do the replace thing

@@ -28,7 +29,12 @@ module.exports.decrypt = (options) => {
const decipher = crypto.createDecipheriv(decryptionAlgo, key, iv)
let decrypted = decipher.update(ciphertext, 'hex', 'utf8')
decrypted += decipher.final('utf8')
return decrypted
if (fs.existsSync(outputFilePath)) {
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not create a backup file in case the file exist instead throw an error and return.

}
fs.writeFileSync(outputFilePath, decrypted)
log(`The Environment file "${inputFile}" has been decrypted to "${outputFilePath}".`, 'info')
log(`And then backup file "${inputFile}".bak.`, 'info')
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let's not have the backup file option.


if (argv.decrypt || argv.d) log(cryptography.decrypt({secret, outputFile, encryptionAlgo}),'info')
else cryptography.encrypt({ secret, inputFile, outputFile, encryptionAlgo });
if (argv.decrypt || argv.d) cryptography.decrypt({ secret, inputFile, outputFile, encryptionAlgo })
Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Let the log be here, please do not remove it from here since we need the log to be happening only in case of cli. Not in the decrypt

Copy link
Owner

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

All the logging for decrypt should happen in this file and not in lib/cryptography.js unless its an error.

@kunalpanchal
Copy link
Owner

Hey @sarawee thank you for this PR to address the issue regarding decryption using CLI. Appreciate the efforts. I have just added a few comments which needs to be addressed after which I will merge this PR and release a new version.

@kunalpanchal kunalpanchal mentioned this pull request May 27, 2021
@kunalpanchal kunalpanchal added this to in progress in v1.3.0 May 27, 2021
@kunalpanchal
Copy link
Owner

No updates here

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment
Labels
Projects
v1.3.0
in progress
Development

Successfully merging this pull request may close these issues.

Problem in Decrypting
2 participants