Skip to content

Commit 6886ba1

Browse files
committed
add output path parameters to encrypt/decrypt
1 parent 6f4edda commit 6886ba1

File tree

1 file changed

+34
-10
lines changed

1 file changed

+34
-10
lines changed

user_sync/app.py

Lines changed: 34 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -441,30 +441,54 @@ def begin_work(config_loader):
441441
post_sync_manager.run(rule_processor.post_sync_data)
442442

443443

444-
@main.command(short_help="Encrypt an existing RSA private key")
444+
@main.command(short_help="Encrypt RSA private key")
445445
@click.help_option('-h', '--help')
446446
@click.argument('key-path', default='private.key', type=click.Path(exists=True))
447+
@click.option('-o', '--output-file', help="Path of encrypted file [default: key specified by KEY_PATH will be overwritten]",
448+
default=None)
447449
@click.option('--password', '-p', prompt='Create password', hide_input=True, confirmation_prompt=True)
448-
def encrypt(password, key_path):
449-
"""Encrypt an existing RSA private key file with a passphrase"""
450+
def encrypt(output_file, password, key_path):
451+
"""Encrypt RSA private key specified by KEY_PATH.
452+
453+
KEY_PATH default: private.key
454+
455+
A passphrase is required to encrypt the file"""
456+
if output_file is None:
457+
output_file = key_path
458+
if output_file != key_path and Path(output_file).exists() \
459+
and not click.confirm('\nWarning - file already exists: \n{}\nOverwrite?'.format(output_file)):
460+
return
450461
try:
451462
data = user_sync.encryption.encrypt_file(password, key_path)
452-
user_sync.encryption.write_key(data, key_path)
453-
click.echo('Encryption was successful.\n{0}'.format(os.path.abspath(key_path)))
463+
user_sync.encryption.write_key(data, output_file)
464+
click.echo('Encryption was successful.')
465+
click.echo('Wrote file: {}'.format(os.path.abspath(output_file)))
454466
except AssertionException as e:
455467
click.echo(str(e))
456468

457469

458-
@main.command()
470+
@main.command(short_help="Decrypt RSA private key")
459471
@click.help_option('-h', '--help')
460472
@click.argument('key-path', default='private.key', type=click.Path(exists=True))
473+
@click.option('-o', '--output-file', help="Path of decrypted file [default: key specified by KEY_PATH will be overwritten]",
474+
default=None)
461475
@click.option('--password', '-p', prompt='Enter password', hide_input=True)
462-
def decrypt(password, key_path):
463-
"""Decrypt an RSA private key file with a passphrase"""
476+
def decrypt(output_file, password, key_path):
477+
"""Decrypt RSA private key specified by KEY_PATH.
478+
479+
KEY_PATH default: private.key
480+
481+
A passphrase is required to decrypt the file"""
482+
if output_file is None:
483+
output_file = key_path
484+
if output_file != key_path and Path(output_file).exists() \
485+
and not click.confirm('\nWarning - file already exists: \n{}\nOverwrite?'.format(output_file)):
486+
return
464487
try:
465488
data = user_sync.encryption.decrypt_file(password, key_path)
466-
user_sync.encryption.write_key(data, key_path)
467-
click.echo('Decryption was successful.\n{0}'.format(os.path.abspath(key_path)))
489+
user_sync.encryption.write_key(data, output_file)
490+
click.echo('Decryption was successful.')
491+
click.echo('Wrote file: {}'.format(os.path.abspath(output_file)))
468492
except AssertionException as e:
469493
click.echo(str(e))
470494

0 commit comments

Comments
 (0)