Skip to content

Commit 58654d6

Browse files
Merge pull request #612 from adorton-adobe/fix/encrypt-cli
CLI Updates
2 parents 9304afe + 42fe69a commit 58654d6

File tree

7 files changed

+96
-27
lines changed

7 files changed

+96
-27
lines changed

.gitignore

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,7 @@ user_sync/resources/*
105105
!user_sync/resources/__init__.py
106106
!user_sync/resources/README.md
107107
!user_sync/resources/manual_url
108+
!user_sync/resources/shell_scripts
108109
.DS_Store
109110
!user_sync/resources/default_flags.cfg
110111
test_config/

user-sync.spec

Lines changed: 6 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -4,10 +4,14 @@ block_cipher = None
44

55
a = Analysis(['user_sync/app.py'],
66
binaries=[],
7-
datas=[('user_sync/resources/*.cfg', 'resources'),
7+
datas=[
8+
('user_sync/resources/*.cfg', 'resources'),
89
('user_sync/resources/manual_url', 'resources'),
910
('user_sync/resources/README.md', 'resources'),
10-
('user_sync/resources/examples/*', 'resources/examples')],
11+
('user_sync/resources/examples/*', 'resources/examples'),
12+
('user_sync/resources/shell_scripts/win', 'resources/shell_scripts/win'),
13+
('user_sync/resources/shell_scripts/linux', 'resources/shell_scripts/linux'),
14+
],
1115
hiddenimports=['win32timezone', 'pkg_resources.py2_warn', 'keyring'],
1216
hookspath=['.build'],
1317
runtime_hooks=[],

user_sync/app.py

Lines changed: 76 additions & 25 deletions
Original file line numberDiff line numberDiff line change
@@ -23,6 +23,7 @@
2323
import shutil
2424
import sys
2525
from datetime import datetime
26+
from pathlib import Path
2627

2728
import click
2829
import six
@@ -211,28 +212,43 @@ def sync(**kwargs):
211212
run_stats.log_end(logger)
212213

213214

214-
@main.command(help='Generates configuration files, an X509 certificate/keypair, and the batch '
215-
'files for running the user-sync tool in test and live mode.')
215+
@main.command(short_help="Generate conf files, certificates and shell scripts")
216+
@click.help_option('-h', '--help')
216217
@click.pass_context
217218
def init(ctx):
219+
"""
220+
Generates configuration files, an X509 certificate/keypair, and the batch files for running the user-sync tool
221+
in test and live mode.
222+
"""
218223
ctx.forward(certgen, randomize=True)
219-
220-
with open('Run_UST_Test_Mode.bat', 'w') as OPATH:
221-
OPATH.writelines(['mode 155,50', '\ncd /D "%~dp0"', '\nuser-sync.exe --process-groups --users mapped -t',
222-
'\npause'])
223-
with open("Run_UST_Live.bat", 'w') as OPATH:
224-
OPATH.writelines(
225-
['mode 155,50', '\ncd /D "%~dp0"', '\nuser-sync.exe --process-groups --users mapped'])
224+
ctx.forward(shell_scripts, platform=None)
226225

227226
sync = 'user-sync-config.yml'
228227
umapi = 'connector-umapi.yml'
229228
ldap = 'connector-ldap.yml'
230-
existing = "\n".join({f for f in (sync, umapi, ldap) if os.path.exists(f)})
231-
if existing and not click.confirm('\nWarning: files already exist: \n{}\nOverwrite?'.format(existing)):
232-
return
233229
ctx.forward(example_config, root=sync, umapi=umapi, ldap=ldap)
234230

235231

232+
@main.command(short_help="Generate invocation scripts")
233+
@click.help_option('-h', '--help')
234+
@click.option('-p', '--platform', help="Platform for which to generate scripts [default: current system platform]",
235+
type=click.Choice(['win', 'linux'], case_sensitive=False))
236+
def shell_scripts(platform):
237+
"""Generate invocation shell scripts for the given platform."""
238+
if platform is None:
239+
platform = 'win' if 'win' in sys.platform.lower() else 'linux'
240+
shell_scripts = user_sync.resource.get_resource_dir('shell_scripts/{}'.format(platform))
241+
for script in shell_scripts:
242+
with open(script, 'r') as fh:
243+
content = fh.read()
244+
target = Path.cwd()/Path(script).parts[-1]
245+
if target.exists() and not click.confirm('\nWarning - file already exists: \n{}\nOverwrite?'.format(target)):
246+
continue
247+
with open(target, 'w') as fh:
248+
fh.write(content)
249+
click.echo("Wrote shell script: {}".format(target))
250+
251+
236252
@main.command()
237253
@click.help_option('-h', '--help')
238254
@click.option('--root', help="Filename of root user sync config file",
@@ -250,13 +266,16 @@ def example_config(**kwargs):
250266
}
251267

252268
for k, fname in kwargs.items():
269+
target = Path.cwd() / fname
253270
assert k in res_files, "Invalid option specified"
254271
res_file = user_sync.resource.get_resource(res_files[k])
255272
assert res_file is not None, "Resource file '{}' not found".format(res_files[k])
273+
if target.exists() and not click.confirm('\nWarning - file already exists: \n{}\nOverwrite?'.format(target)):
274+
continue
256275
click.echo("Generating file '{}'".format(fname))
257276
with open(res_file, 'r') as file:
258277
content = file.read()
259-
with open(fname, 'w') as file:
278+
with open(target, 'w') as file:
260279
file.write(content)
261280

262281

@@ -422,39 +441,71 @@ def begin_work(config_loader):
422441
post_sync_manager.run(rule_processor.post_sync_data)
423442

424443

425-
@main.command(help='Encrypt an existing RSA private key file with a passphrase')
444+
@main.command(short_help="Encrypt RSA private key")
445+
@click.help_option('-h', '--help')
426446
@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)
427449
@click.option('--password', '-p', prompt='Create password', hide_input=True, confirmation_prompt=True)
428-
def encrypt(password, key_path):
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
429461
try:
430462
data = user_sync.encryption.encrypt_file(password, key_path)
431-
user_sync.encryption.write_key(data, key_path)
432-
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)))
433466
except AssertionException as e:
434467
click.echo(str(e))
435468

436469

437-
@main.command(help='Decrypt an RSA private key file with a passphrase')
470+
@main.command(short_help="Decrypt RSA private key")
471+
@click.help_option('-h', '--help')
438472
@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)
439475
@click.option('--password', '-p', prompt='Enter password', hide_input=True)
440-
def decrypt(password, key_path):
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
441487
try:
442488
data = user_sync.encryption.decrypt_file(password, key_path)
443-
user_sync.encryption.write_key(data, key_path)
444-
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)))
445492
except AssertionException as e:
446493
click.echo(str(e))
447494

448495

449-
@main.command(help='Generates an X509 certificate/keypair with random or user-specified subject. '
450-
'User Sync Tool can use these files to communicate with the admin console. '
451-
'Please visit https://console.adobe.io to complete the integration process. '
452-
'Use the --randomize argument to create a secure keypair with no user input.')
496+
@main.command(short_help="Generate service integration certificates")
497+
@click.help_option('-h', '--help')
453498
@click.option('--overwrite', '-o', '-y', help='Overwrite existing files without being asked to confirm', is_flag=True)
454499
@click.option('--randomize', '-r', help='Randomize the values rather than entering credentials', is_flag=True)
455500
@click.option('--key', '-k', help='Set a custom output path for private key', default='private.key')
456501
@click.option('--certificate', '-c', help='Set a custom output path for certificate', default='certificate_pub.crt')
457502
def certgen(randomize, key, certificate, overwrite):
503+
"""
504+
Generates an X509 certificate/keypair with random or user-specified subject.
505+
User Sync Tool can use these files to communicate with the admin console.
506+
Please visit https://console.adobe.io to complete the integration process.
507+
Use the --randomize argument to create a secure keypair with no user input.
508+
"""
458509
key = os.path.abspath(key)
459510
certificate = os.path.abspath(certificate)
460511
existing = "\n".join({f for f in (key, certificate) if os.path.exists(f)})
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
./user-sync --process-groups --users mapped
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
#!/bin/sh
2+
3+
./user-sync --process-groups --users mapped -t
Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,3 @@
1+
mode 155,50
2+
cd /D "%~dp0"
3+
user-sync.exe --process-groups --users mapped
Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,4 @@
1+
mode 155,50
2+
cd /D "%~dp0"
3+
user-sync.exe --process-groups --users mapped -t
4+
pause

0 commit comments

Comments
 (0)