23
23
import shutil
24
24
import sys
25
25
from datetime import datetime
26
+ from pathlib import Path
26
27
27
28
import click
28
29
import six
@@ -211,28 +212,43 @@ def sync(**kwargs):
211
212
run_stats .log_end (logger )
212
213
213
214
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 ' )
216
217
@click .pass_context
217
218
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
+ """
218
223
ctx .forward (certgen , randomize = True )
219
-
220
- with open ('Run_UST_Test_Mode.bat' , 'w' ) as OPATH :
221
- OPATH .writelines (['mode 155,50' , '\n cd /D "%~dp0"' , '\n user-sync.exe --process-groups --users mapped -t' ,
222
- '\n pause' ])
223
- with open ("Run_UST_Live.bat" , 'w' ) as OPATH :
224
- OPATH .writelines (
225
- ['mode 155,50' , '\n cd /D "%~dp0"' , '\n user-sync.exe --process-groups --users mapped' ])
224
+ ctx .forward (shell_scripts , platform = None )
226
225
227
226
sync = 'user-sync-config.yml'
228
227
umapi = 'connector-umapi.yml'
229
228
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 ('\n Warning: files already exist: \n {}\n Overwrite?' .format (existing )):
232
- return
233
229
ctx .forward (example_config , root = sync , umapi = umapi , ldap = ldap )
234
230
235
231
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 ('\n Warning - file already exists: \n {}\n Overwrite?' .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
+
236
252
@main .command ()
237
253
@click .help_option ('-h' , '--help' )
238
254
@click .option ('--root' , help = "Filename of root user sync config file" ,
@@ -250,13 +266,16 @@ def example_config(**kwargs):
250
266
}
251
267
252
268
for k , fname in kwargs .items ():
269
+ target = Path .cwd () / fname
253
270
assert k in res_files , "Invalid option specified"
254
271
res_file = user_sync .resource .get_resource (res_files [k ])
255
272
assert res_file is not None , "Resource file '{}' not found" .format (res_files [k ])
273
+ if target .exists () and not click .confirm ('\n Warning - file already exists: \n {}\n Overwrite?' .format (target )):
274
+ continue
256
275
click .echo ("Generating file '{}'" .format (fname ))
257
276
with open (res_file , 'r' ) as file :
258
277
content = file .read ()
259
- with open (fname , 'w' ) as file :
278
+ with open (target , 'w' ) as file :
260
279
file .write (content )
261
280
262
281
@@ -422,39 +441,71 @@ def begin_work(config_loader):
422
441
post_sync_manager .run (rule_processor .post_sync_data )
423
442
424
443
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' )
426
446
@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 )
427
449
@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 ('\n Warning - file already exists: \n {}\n Overwrite?' .format (output_file )):
460
+ return
429
461
try :
430
462
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 )))
433
466
except AssertionException as e :
434
467
click .echo (str (e ))
435
468
436
469
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' )
438
472
@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 )
439
475
@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 ('\n Warning - file already exists: \n {}\n Overwrite?' .format (output_file )):
486
+ return
441
487
try :
442
488
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 )))
445
492
except AssertionException as e :
446
493
click .echo (str (e ))
447
494
448
495
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' )
453
498
@click .option ('--overwrite' , '-o' , '-y' , help = 'Overwrite existing files without being asked to confirm' , is_flag = True )
454
499
@click .option ('--randomize' , '-r' , help = 'Randomize the values rather than entering credentials' , is_flag = True )
455
500
@click .option ('--key' , '-k' , help = 'Set a custom output path for private key' , default = 'private.key' )
456
501
@click .option ('--certificate' , '-c' , help = 'Set a custom output path for certificate' , default = 'certificate_pub.crt' )
457
502
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
+ """
458
509
key = os .path .abspath (key )
459
510
certificate = os .path .abspath (certificate )
460
511
existing = "\n " .join ({f for f in (key , certificate ) if os .path .exists (f )})
0 commit comments