-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathkeystore-generator.py
executable file
·594 lines (445 loc) · 19.1 KB
/
keystore-generator.py
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
#! /usr/bin/env python3
'''
# -----------------------------------------------------------------------------
# keystore-generator.py
# -----------------------------------------------------------------------------
'''
# Import from standard library. https://docs.python.org/3/library/
import argparse
import base64
import json
import linecache
import logging
import os
import signal
import sys
import time
from shutil import which
# Import from https://pypi.org/
import boto3
# Metadata
__all__ = []
__version__ = "1.0.0" # See https://www.python.org/dev/peps/pep-0396/
__date__ = '2022-09-01'
__updated__ = '2022-09-01'
SENZING_PRODUCT_ID = "5032" # See https://github.com/senzing-garage/knowledge-base/blob/main/lists/senzing-product-ids.md
LOG_FORMAT = '%(asctime)s %(message)s'
# The "configuration_locator" describes where configuration variables are in:
# 1) Command line options, 2) Environment variables, 3) Configuration files, 4) Default values
CONFIGURATION_LOCATOR = {
"debug": {
"default": False,
"env": "SENZING_DEBUG",
"cli": "debug"
},
"delay_in_seconds": {
"default": 0,
"env": "SENZING_DELAY_IN_SECONDS",
"cli": "delay-in-seconds"
},
"etc_dir": {
"default": "/etc/opt/senzing",
"env": "SENZING_ETC_DIR",
"cli": "etc-dir"
},
"sleep_time_in_seconds": {
"default": 0,
"env": "SENZING_SLEEP_TIME_IN_SECONDS",
"cli": "sleep-time-in-seconds"
},
"subcommand": {
"default": None,
"env": "SENZING_SUBCOMMAND",
}
}
# Enumerate keys in 'configuration_locator' that should not be printed to the log.
KEYS_TO_REDACT = []
# -----------------------------------------------------------------------------
# Define argument parser
# -----------------------------------------------------------------------------
def get_parser():
''' Parse commandline arguments. '''
subcommands = {
'aws': {
"help": 'Generate a java keystore for ssl client authentication in an AWS environment',
"argument_aspects": ["common"],
"arguments": {
"--etc-dir": {
"dest": "etc_dir",
"metavar": "SENZING_ETC_DIR",
"help": "Location of senzing etc directory. Default: /etc/opt/senzing"
}
}
},
'sleep': {
"help": 'Do nothing but sleep. For Docker testing.',
"arguments": {
"--sleep-time-in-seconds": {
"dest": "sleep_time_in_seconds",
"metavar": "SENZING_SLEEP_TIME_IN_SECONDS",
"help": "Sleep time in seconds. DEFAULT: 0 (infinite)"
},
},
},
'version': {
"help": 'Print version of program.',
},
'docker-acceptance-test': {
"help": 'For Docker acceptance testing.',
},
}
# Define argument_aspects.
argument_aspects = {
"common": {
"--debug": {
"dest": "debug",
"action": "store_true",
"help": "Enable debugging. (SENZING_DEBUG) Default: False"
},
"--delay-in-seconds": {
"dest": "delay_in_seconds",
"metavar": "SENZING_DELAY_IN_SECONDS",
"help": "Delay before processing in seconds. DEFAULT: 0"
}
}
}
# Augment "subcommands" variable with arguments specified by aspects.
for subcommand_value in subcommands.values():
if 'argument_aspects' in subcommand_value:
for aspect in subcommand_value['argument_aspects']:
if 'arguments' not in subcommand_value:
subcommand_value['arguments'] = {}
arguments = argument_aspects.get(aspect, {})
for argument, argument_value in arguments.items():
subcommand_value['arguments'][argument] = argument_value
# Parse command line arguments.
parser = argparse.ArgumentParser(prog="keystore-generator.py", description="Initialize Senzing installation. For more information, see https://github.com/senzing-garage/keystore-generator")
subparsers = parser.add_subparsers(dest='subcommand', help='Subcommands (SENZING_SUBCOMMAND):')
for subcommand_key, subcommand_values in subcommands.items():
subcommand_help = subcommand_values.get('help', "")
subcommand_arguments = subcommand_values.get('arguments', {})
subparser = subparsers.add_parser(subcommand_key, help=subcommand_help)
for argument_key, argument_values in subcommand_arguments.items():
subparser.add_argument(argument_key, **argument_values)
return parser
# -----------------------------------------------------------------------------
# Message handling
# -----------------------------------------------------------------------------
# 1xx Informational (i.e. logging.info())
# 3xx Warning (i.e. logging.warning())
# 5xx User configuration issues (either logging.warning() or logging.err() for Client errors)
# 7xx Internal error (i.e. logging.error for Server errors)
# 9xx Debugging (i.e. logging.debug())
MESSAGE_INFO = 100
MESSAGE_WARN = 300
MESSAGE_ERROR = 700
MESSAGE_DEBUG = 900
MESSAGE_DICTIONARY = {
"100": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}I",
"157": "{0} - Creating file",
"293": "For information on warnings and errors, see https://github.com/senzing-garage/keystore-generator#errors",
"294": "Version: {0} Updated: {1}",
"295": "Sleeping infinitely.",
"296": "Sleeping {0} seconds.",
"297": "Enter {0}",
"298": "Exit {0}",
"299": "{0}",
"300": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}W",
"499": "{0}",
"500": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"697": "No processing done.",
"698": "Program terminated with error.",
"699": "{0}",
"700": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}E",
"898": "Environment variable / command-line option not set: {0}",
"899": "{0}",
"900": "senzing-" + SENZING_PRODUCT_ID + "{0:04d}D",
"950": "Enter function: {0}",
"951": "Exit function: {0}",
"998": "Debugging enabled.",
"999": "{0}",
}
def message(index, *args):
''' Return an instantiated message. '''
index_string = str(index)
template = MESSAGE_DICTIONARY.get(index_string, "No message for index {0}.".format(index_string))
return template.format(*args)
def message_generic(generic_index, index, *args):
''' Return a formatted message. '''
return "{0} {1}".format(message(generic_index, index), message(index, *args))
def message_info(index, *args):
''' Return an info message. '''
return message_generic(MESSAGE_INFO, index, *args)
def message_warning(index, *args):
''' Return a warning message. '''
return message_generic(MESSAGE_WARN, index, *args)
def message_error(index, *args):
''' Return an error message. '''
return message_generic(MESSAGE_ERROR, index, *args)
def message_debug(index, *args):
''' Return a debug message. '''
return message_generic(MESSAGE_DEBUG, index, *args)
def get_exception():
''' Get details about an exception. '''
exception_type, exception_object, traceback = sys.exc_info()
frame = traceback.tb_frame
line_number = traceback.tb_lineno
filename = frame.f_code.co_filename
linecache.checkcache(filename)
line = linecache.getline(filename, line_number, frame.f_globals)
return {
"filename": filename,
"line_number": line_number,
"line": line.strip(),
"exception": exception_object,
"type": exception_type,
"traceback": traceback,
}
# -----------------------------------------------------------------------------
# Configuration
# -----------------------------------------------------------------------------
def get_configuration(subcommand, args):
''' Order of precedence: CLI, OS environment variables, INI file, default. '''
result = {}
# Copy default values into configuration dictionary.
for key, value in list(CONFIGURATION_LOCATOR.items()):
result[key] = value.get('default', None)
# "Prime the pump" with command line args. This will be done again as the last step.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Copy OS environment variables into configuration dictionary.
for key, value in list(CONFIGURATION_LOCATOR.items()):
os_env_var = value.get('env', None)
if os_env_var:
os_env_value = os.getenv(os_env_var, None)
if os_env_value:
result[key] = os_env_value
# Copy 'args' into configuration dictionary.
for key, value in list(args.__dict__.items()):
new_key = key.format(subcommand.replace('-', '_'))
if value:
result[new_key] = value
# Add program information.
result['program_version'] = __version__
result['program_updated'] = __updated__
# Special case: subcommand from command-line
if args.subcommand:
result['subcommand'] = args.subcommand
# Special case: Change boolean strings to booleans.
booleans = [
'debug',
]
for boolean in booleans:
boolean_value = result.get(boolean)
if isinstance(boolean_value, str):
boolean_value_lower_case = boolean_value.lower()
if boolean_value_lower_case in ['true', '1', 't', 'y', 'yes']:
result[boolean] = True
else:
result[boolean] = False
# Special case: Change integer strings to integers.
integers = [
'delay_in_seconds',
'sleep_time_in_seconds',
]
for integer in integers:
integer_string = result.get(integer)
result[integer] = int(integer_string)
return result
def redact_configuration(config):
''' Return a shallow copy of config with certain keys removed. '''
result = config.copy()
for key in KEYS_TO_REDACT:
try:
result.pop(key)
except Exception:
pass
return result
# -----------------------------------------------------------------------------
# Utility functions
# -----------------------------------------------------------------------------
def bootstrap_signal_handler(signal_number, frame):
''' Exit on signal error. '''
logging.debug(message_debug(901, signal_number, frame))
sys.exit(0)
def create_signal_handler_function(args):
''' Tricky code. Uses currying technique. Create a function for signal handling.
that knows about "args".
'''
def result_function(signal_number, frame):
logging.info(message_info(298, args))
logging.debug(message_debug(901, signal_number, frame))
sys.exit(0)
return result_function
def delay(config):
''' Sleep for a period of time. '''
delay_in_seconds = config.get('delay_in_seconds')
if delay_in_seconds > 0:
logging.info(message_info(296, delay_in_seconds))
time.sleep(delay_in_seconds)
def entry_template(config):
''' Format of entry message. '''
debug = config.get("debug", False)
config['start_time'] = time.time()
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(297, config_json)
def exit_template(config):
''' Format of exit message. '''
debug = config.get("debug", False)
stop_time = time.time()
config['stop_time'] = stop_time
config['elapsed_time'] = stop_time - config.get('start_time', stop_time)
if debug:
final_config = config
else:
final_config = redact_configuration(config)
config_json = json.dumps(final_config, sort_keys=True)
return message_info(298, config_json)
def exit_error(index, *args):
''' Log error message and exit program. '''
logging.error(message_error(index, *args))
logging.error(message_error(698))
sys.exit(1)
def exit_silently():
''' Exit program. '''
sys.exit(0)
# -----------------------------------------------------------------------------
# worker functions
# -----------------------------------------------------------------------------
def create_keystore_truststore(config):
''' Create key stores and trust stores, which are used by Senzing API server'''
etc_dir = config.get("etc_dir")
# default keystore password is change-it
server_keystore_password = "change-it" if os.getenv("SENZING_API_SERVER_KEYSTORE_PASSWORD") is None else os.getenv("SENZING_API_SERVER_KEYSTORE_PASSWORD")
client_keystore_password = "change-it" if os.getenv("SENZING_API_CLIENT_KEYSTORE_PASSWORD") is None else os.getenv("SENZING_API_CLIENT_KEYSTORE_PASSWORD")
# Create server key store
os.system("keytool -genkey -alias sz-api-server -keystore {0}/sz-api-server-store.p12 -storetype PKCS12 -keyalg RSA -storepass {1} -validity 730 -keysize 2048 -dname 'CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown'".format(etc_dir, server_keystore_password))
logging.info(message_info(157, "created sz-api-server-store.p12"))
# Create client key store
os.system("keytool -genkey -alias my-client -keystore {0}/my-client-key-store.p12 -storetype PKCS12 -keyalg RSA -storepass {1} -validity 730 -keysize 2048 -dname 'CN=Unknown, OU=Unknown, O=Unknown, L=Unknown, ST=Unknown, C=Unknown'".format(etc_dir, client_keystore_password))
logging.info(message_info(157, "created my-client-key-store.p12"))
# Create client certificate with client key store
os.system("keytool -export -keystore {0}/my-client-key-store.p12 -storepass {1} -storetype PKCS12 -alias my-client -file {0}/my-client.cer".format(etc_dir, client_keystore_password))
logging.info(message_info(157, "created my-client.cer"))
# Add client certificate to client trust store
os.system("keytool -import -file {0}/my-client.cer -alias my-client -keystore {0}/my-client-trust-store.p12 -storetype PKCS12 -storepass {1} -noprompt".format(etc_dir, client_keystore_password))
logging.info(message_info(157, "created my-client-trust-store.p12"))
# base64 encode client key store
encoded_keystore = ""
with open("{0}/my-client-key-store.p12".format(etc_dir), "rb") as keystore:
encoded_keystore_bytes = base64.b64encode(keystore.read())
encoded_keystore = encoded_keystore_bytes.decode('ascii')
return encoded_keystore
def upload_aws_secrets_manager(base64_client_keystore):
''' Upload client keystore to AWS secrets manager '''
current_region = os.getenv("AWS_REGION")
client_keystore_arn = os.getenv("SENZING_API_CLIENT_KEYSTORE_SECRET")
client = boto3.Session(region_name=current_region).client('secretsmanager')
response = client.update_secret(
Description='Base64 representation of Senzing Api client key store',
SecretId=client_keystore_arn,
SecretString=base64_client_keystore
)
logging.info(message_info(299, response))
# -----------------------------------------------------------------------------
# do_* functions
# Common function signature: do_XXX(args)
# -----------------------------------------------------------------------------
def do_docker_acceptance_test(subcommand, args):
''' For use with Docker acceptance testing. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(subcommand, args)
# Prolog.
logging.info(entry_template(config))
# Epilog.
logging.info(exit_template(config))
def do_aws(subcommand, args):
''' Do a task. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(subcommand, args)
# Prolog.
logging.info(entry_template(config))
# If requested, create sz-api-server-store.p12 my-client-key-store.p12 my-client.cer my-client-trust-store.p12
if which("keytool") is not None:
base64_client_keystore = create_keystore_truststore(config)
upload_aws_secrets_manager(base64_client_keystore)
# Epilog.
logging.info(exit_template(config))
def do_sleep(subcommand, args):
''' Sleep. Used for debugging. '''
# Get context from CLI, environment variables, and ini files.
config = get_configuration(subcommand, args)
# Prolog.
logging.info(entry_template(config))
# Pull values from configuration.
sleep_time_in_seconds = config.get('sleep_time_in_seconds')
# Sleep.
if sleep_time_in_seconds > 0:
logging.info(message_info(296, sleep_time_in_seconds))
time.sleep(sleep_time_in_seconds)
else:
sleep_time_in_seconds = 3600
while True:
logging.info(message_info(295))
time.sleep(sleep_time_in_seconds)
# Epilog.
logging.info(exit_template(config))
def do_version(subcommand, args):
''' Log version information. '''
logging.info(message_info(294, __version__, __updated__))
logging.debug(message_debug(902, subcommand, args))
# -----------------------------------------------------------------------------
# Main
# -----------------------------------------------------------------------------
if __name__ == "__main__":
# Configure logging. See https://docs.python.org/2/library/logging.html#levels
LOG_LEVEL_MAP = {
"notset": logging.NOTSET,
"debug": logging.DEBUG,
"info": logging.INFO,
"fatal": logging.FATAL,
"warning": logging.WARNING,
"error": logging.ERROR,
"critical": logging.CRITICAL
}
LOG_LEVEL_PARAMETER = os.getenv("SENZING_LOG_LEVEL", "info").lower()
LOG_LEVEL = LOG_LEVEL_MAP.get(LOG_LEVEL_PARAMETER, logging.INFO)
logging.basicConfig(format=LOG_FORMAT, level=LOG_LEVEL)
logging.debug(message_debug(998))
# Trap signals temporarily until args are parsed.
signal.signal(signal.SIGTERM, bootstrap_signal_handler)
signal.signal(signal.SIGINT, bootstrap_signal_handler)
# Parse the command line arguments.
SUBCOMMAND = os.getenv("SENZING_SUBCOMMAND", None)
PARSER = get_parser()
if len(sys.argv) > 1:
ARGS = PARSER.parse_args()
SUBCOMMAND = ARGS.subcommand
elif SUBCOMMAND:
ARGS = argparse.Namespace(subcommand=SUBCOMMAND)
else:
PARSER.print_help()
if len(os.getenv("SENZING_DOCKER_LAUNCHED", "")) > 0:
SUBCOMMAND = "sleep"
ARGS = argparse.Namespace(subcommand=SUBCOMMAND)
do_sleep(SUBCOMMAND, ARGS)
exit_silently()
# Catch interrupts. Tricky code: Uses currying.
SIGNAL_HANDLER = create_signal_handler_function(ARGS)
signal.signal(signal.SIGINT, SIGNAL_HANDLER)
signal.signal(signal.SIGTERM, SIGNAL_HANDLER)
# Transform subcommand from CLI parameter to function name string.
SUBCOMMAND_FUNCTION_NAME = "do_{0}".format(SUBCOMMAND.replace('-', '_'))
# Test to see if function exists in the code.
if SUBCOMMAND_FUNCTION_NAME not in globals():
logging.warning(message_warning(596, SUBCOMMAND))
PARSER.print_help()
exit_silently()
# Tricky code for calling function based on string.
globals()[SUBCOMMAND_FUNCTION_NAME](SUBCOMMAND, ARGS)