Skip to content

Commit

Permalink
Update pki CLI
Browse files Browse the repository at this point in the history
  • Loading branch information
edewata committed Jan 20, 2025
1 parent 8f8f733 commit d2fb5ce
Showing 1 changed file with 92 additions and 118 deletions.
210 changes: 92 additions & 118 deletions base/common/python/pki/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
# All rights reserved.
#

import argparse
import logging
import os
import shlex
Expand Down Expand Up @@ -51,13 +52,53 @@ def __init__(self):
self.add_module(pki.cli.password.PasswordCLI())
self.add_module(pki.cli.pkcs12.PKCS12CLI())

def create_parser(self, subparsers=None):

# create main parser
self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)
self.parser.add_argument(
'--client-type',
default='java')
self.parser.add_argument(
'-D',
action='append')
self.parser.add_argument('-d')
self.parser.add_argument('-c')
self.parser.add_argument('-C')
self.parser.add_argument('-f')
self.parser.add_argument('--token')
self.parser.add_argument(
'--ignore-banner',
action='store_true')
self.parser.add_argument(
'-v',
'--verbose',
action='store_true')
self.parser.add_argument(
'--debug',
action='store_true')
self.parser.add_argument(
'--help',
action='store_true')

self.parser.add_argument(
'remainder',
nargs=argparse.REMAINDER)

# create subparsers
#subparsers = self.parser.add_subparsers(dest='command')
#super().create_parser(subparsers=subparsers)

def get_full_module_name(self, module_name):
return module_name

def print_help(self):
print('Usage: pki [OPTIONS]')
print()
print(' --client-type <type> PKI client type (default: java)')
print(' -D <name>=<value> System propery')
print(' -d <path> NSS database location ' +
'(default: ~/.dogtag/nssdb)')
print(' -c <password> NSS database password ' +
Expand All @@ -67,6 +108,7 @@ def print_help(self):
print(' -f <password config> NSS database password configuration ' +
'(mutually exclusive to -c and -C options)')
print(' --token <name> Security token name')
print(' --ignore-banner Ignore banner')
print()
print(' -v, --verbose Run in verbose mode.')
print(' --debug Show debug messages.')
Expand Down Expand Up @@ -157,133 +199,65 @@ def execute_java(self, args, properties=None, stdout=sys.stdout):

def execute(self, argv, args=None):

print('Parsing %s' % ' '.join(argv))

# append global options
value = os.getenv('PKI_CLI_OPTIONS')
args = shlex.split(value)
args.extend(argv)

client_type = 'java'

properties = {}
pki_options = []
command = None
cmd_args = []

# read pki options before the command
# remove options for Python module

i = 0
while i < len(args):
# if arg is a command, stop
if args[i][0] != '-':
command = args[i]
break

# get properties
if args[i] == '-D':
try:
name, value = args[i + 1].split('=', 1)
properties[name] = value
except IndexError:
break
i = i + 2

# get database path
if args[i] == '-d':
try:
self.database = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

# get database password
elif args[i] == '-c':
try:
self.password = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

# get database password file path
elif args[i] == '-C':
try:
self.password_file = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

# get database password config path
elif args[i] == '-f':
try:
self.password_conf = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

# get token name
elif args[i] == '--token':
try:
self.token = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

# check ignore banner option
elif args[i] == '--ignore-banner':
self.ignore_banner = True
pki_options.append(args[i])
i = i + 1

# check verbose option
elif args[i] == '-v' or args[i] == '--verbose':
logging.getLogger().setLevel(logging.INFO)
pki_options.append(args[i])
i = i + 1

# check debug option
elif args[i] == '--debug':
logging.getLogger().setLevel(logging.DEBUG)
pki_options.append(args[i])
i = i + 1

# get client type
elif args[i] == '--client-type':
try:
client_type = args[i + 1]
except IndexError:
break
pki_options.append(args[i])
pki_options.append(args[i + 1])
i = i + 2

else: # otherwise, save the arg for the next module
cmd_args.append(args[i])
i = i + 1

# save the rest of the args
while i < len(args):
cmd_args.append(args[i])
i = i + 1

logger.debug('PKI options: %s', ' '.join(pki_options))
logger.debug('PKI command: %s %s', command, ' '.join(cmd_args))
#args, unknown = self.parser.parse_known_args(args=argv)
args = self.parser.parse_args(args=argv)

print('Known args: %s' % str(args))
#print('Unknown args: %s' % str(unknown))
print('Remainder: %s' % str(args.remainder))

if args.help:
self.print_help()
return

if args.debug:
logging.getLogger().setLevel(logging.DEBUG)

elif args.verbose:
logging.getLogger().setLevel(logging.INFO)

client_type = args.client_type

self.properties = {}
if args.D:
for param in args.D:
name, value = param.split('=', 1)
self.properties[name] = value

self.database = args.d
self.password = args.c
self.password_file = args.C
self.password_conf = args.f
self.token = args.token

self.ignore_banner = args.ignore_banner

#command = args.command
command = args.remainder[0]
print('Command: %s' % command)

#cmd_args = []
cmd_args = args.remainder[1:]
print('Arguments: %s' % ' '.join(cmd_args))

if client_type == 'python' or command in PYTHON_COMMANDS:
(module, module_args) = self.parse_args(cmd_args)
module.execute(module_args)
#(module, module_args) = self.parse_args(cmd_args)
#module.execute(module_args)
module = self.find_module(command)
print('Module: %s' % module.get_full_name())

module.execute(cmd_args)

elif client_type == 'java':
self.execute_java(cmd_args, properties)
#self.execute_java(cmd_args, self.properties)
self.execute_java(command, self.properties)

else:
raise Exception('Unsupported client type: ' + client_type)
Expand Down

0 comments on commit d2fb5ce

Please sign in to comment.