Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix CLI help messages #4948

Merged
merged 2 commits into from
Feb 4, 2025
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
27 changes: 13 additions & 14 deletions .github/workflows/ca-existing-ds-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -124,6 +124,19 @@ jobs:
--cert admin.crt \
caadmin

- name: Check pki-server ca CLI help message
run: |
docker exec pki pki-server ca
docker exec pki pki-server ca --help

# TODO: validate output

- name: Check pki-server ca-create CLI help message
run: |
docker exec pki pki-server ca-create --help

# TODO: validate output

- name: Create CA subsystem
run: |
docker exec pki pki-server ca-create -v
Expand Down Expand Up @@ -402,17 +415,3 @@ jobs:
if: always()
run: |
docker exec pki find /var/lib/pki/pki-tomcat/logs/ca -name "debug.*" -exec cat {} \;

- name: Gather artifacts
if: always()
run: |
tests/bin/ds-artifacts-save.sh ds
tests/bin/pki-artifacts-save.sh pki
continue-on-error: true

- name: Upload artifacts
if: always()
uses: actions/upload-artifact@v4
with:
name: ca-existing-ds
path: /tmp/artifacts
70 changes: 70 additions & 0 deletions .github/workflows/pki-basic-test.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,70 @@
name: Basic PKI CLI
# https://github.com/dogtagpki/pki/wiki/PKI-CLI

on: workflow_call

env:
DS_IMAGE: ${{ vars.DS_IMAGE || 'quay.io/389ds/dirsrv' }}

jobs:
test:
name: Test
runs-on: ubuntu-latest
env:
SHARED: /tmp/workdir/pki
steps:
- name: Clone repository
uses: actions/checkout@v4

- name: Retrieve PKI images
uses: actions/cache@v4
with:
key: pki-images-${{ github.sha }}
path: pki-images.tar

- name: Load PKI images
run: docker load --input pki-images.tar

- name: Set up runner container
run: |
tests/bin/runner-init.sh \
--hostname=pki.example.com \
pki

- name: Check pki CLI help message
run: |
docker exec pki pki
docker exec pki pki --help

# TODO: validate output

- name: Check pki CLI version
run: |
docker exec pki pki --version

# TODO: validate output

- name: Check pki CLI with wrong option
run: |
docker exec pki pki --wrong \
> >(tee stdout) 2> >(tee stderr >&2) || true

# TODO: validate output

- name: Check pki CLI with wrong sub-command
run: |
docker exec pki pki wrong \
> >(tee stdout) 2> >(tee stderr >&2) || true

cat > expected << EOF
ERROR: Invalid module "wrong".
EOF

diff expected stderr

- name: Check pki nss CLI help message
run: |
docker exec pki pki nss
docker exec pki pki nss --help

# TODO: validate output
59 changes: 38 additions & 21 deletions .github/workflows/server-basic-test.yml
Original file line number Diff line number Diff line change
@@ -1,4 +1,5 @@
name: Basic server
# https://github.com/dogtagpki/pki/wiki/PKI-Server-CLI

on: workflow_call

Expand Down Expand Up @@ -37,6 +38,43 @@ jobs:
- name: Connect server container to network
run: docker network connect example pki --alias pki.example.com

- name: Check pki-server CLI help message
run: |
docker exec pki pki-server
docker exec pki pki-server --help

# TODO: validate output

- name: Check pki-server CLI version
run: |
docker exec pki pki-server --version

# TODO: validate output

- name: Check pki-server CLI with wrong option
run: |
docker exec pki pki-server --wrong \
> >(tee stdout) 2> >(tee stderr >&2) || true

# TODO: validate output

- name: Check pki-server CLI with wrong sub-command
run: |
docker exec pki pki-server wrong \
> >(tee stdout) 2> >(tee stderr >&2) || true

cat > expected << EOF
ERROR: Invalid module "wrong".
EOF

diff expected stderr

- name: Check pki-server create CLI help message
run: |
docker exec pki pki-server create --help

# TODO: validate output

- name: Create pki-tomcat server
run: |
docker exec pki pki-server create -v
Expand Down Expand Up @@ -412,24 +450,3 @@ jobs:
EOF

diff expected output

- name: Gather artifacts from server container
if: always()
run: |
tests/bin/pki-artifacts-save.sh pki

docker exec pki ls -la /var/lib/tomcats/pki/conf
mkdir -p /tmp/artifacts/pki/var/lib/tomcats/pki/conf
docker cp pki:/var/lib/tomcats/pki/conf/* /tmp/artifacts/var/lib/tomcats/pki/conf

docker exec pki ls -la /var/lib/tomcats/pki/logs
mkdir -p /tmp/artifacts/pki/var/lib/tomcats/pki/logs
docker cp pki:/var/lib/tomcats/pki/logs/* /tmp/artifacts/var/lib/tomcats/pki/logs
continue-on-error: true

- name: Upload artifacts from server container
if: always()
uses: actions/upload-artifact@v4
with:
name: server-basic-test
path: /tmp/artifacts
27 changes: 13 additions & 14 deletions .github/workflows/server-https-nss-test.yml
Original file line number Diff line number Diff line change
Expand Up @@ -41,6 +41,19 @@ jobs:
run: |
docker exec pki pki-server create -v

- name: Check pki-server nss CLI help message
run: |
docker exec pki pki-server nss
docker exec pki pki-server nss --help

# TODO: validate output

- name: Check pki-server nss-create CLI help message
run: |
docker exec pki pki-server nss-create --help

# TODO: validate output

- name: Create NSS database in PKI server
run: |
docker exec pki pki-server nss-create --no-password
Expand Down Expand Up @@ -371,17 +384,3 @@ jobs:
if: always()
run: |
docker exec pki find /var/lib/pki/pki-tomcat/logs/pki -name "debug.*" -exec cat {} \;

- name: Gather artifacts from server container
if: always()
run: |
tests/bin/pki-artifacts-save.sh pki
continue-on-error: true

- name: Upload artifacts from server container
if: always()
uses: actions/upload-artifact@v4
with:
name: server-https-nss-test
path: |
/tmp/artifacts/pki
5 changes: 5 additions & 0 deletions .github/workflows/tools-tests.yml
Original file line number Diff line number Diff line change
Expand Up @@ -8,6 +8,11 @@ jobs:
uses: ./.github/workflows/wait-for-build.yml
secrets: inherit

pki-basic-test:
name: Basic PKI CLI
needs: build
uses: ./.github/workflows/pki-basic-test.yml

PKICertImport-test:
name: PKICertImport
needs: build
Expand Down
75 changes: 61 additions & 14 deletions base/common/python/pki/cli/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@
# All rights reserved.
#

import argparse
import collections
import logging
from six import itervalues
Expand Down Expand Up @@ -73,7 +74,7 @@ def print_help(self):
continue

full_name = module.get_full_name()
print(' {:30}{:30}'.format(full_name, module.description))
print(' {:32}{:30}'.format(full_name, module.description))

first = True

Expand All @@ -88,7 +89,7 @@ def print_help(self):
first = False

full_name = module.get_full_name()
print(' {:30}{:30}'.format(full_name, module.description))
print(' {:32}{:30}'.format(full_name, module.description))

def find_module(self, command):

Expand All @@ -102,6 +103,29 @@ def find_module(self, command):

def create_parser(self, subparsers=None):

if not self.parser:
# create default parser
self.parser = argparse.ArgumentParser(
prog=self.name,
add_help=False)

# add basic arguments
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')

# capture sub-command and args
self.parser.add_argument(
'remainder',
nargs=argparse.REMAINDER)

for module in self.modules.values():
module.create_parser(subparsers=subparsers)

Expand Down Expand Up @@ -162,31 +186,54 @@ def parse_command(self, command):

return (module, sub_command)

def parse_args(self, args):
def execute(self, argv, args=None):
'''
:param argv: Argument values
:param args: Parsed arguments
'''

if not args:
args = self.parser.parse_args(args=argv)

if args.help:
self.print_help()
return

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

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

command = None
if len(args.remainder) > 0:
command = args.remainder[0]
logger.debug('Command: %s', command)

if not command:
self.print_help()
return

command = args[0]
(module, sub_command) = self.parse_command(command)

if not module:
raise Exception('Invalid module "%s".' % command)

logger.debug('Module: %s', module.get_full_name())

# Prepare module arguments.
if sub_command:
# If module command exists, include it as arguments:
# <module command> <args>...
module_args = [sub_command] + args[1:]
module_args = [sub_command] + args.remainder[1:]

else:
# Otherwise, pass the original arguments: <args>...
module_args = args[1:]
module_args = args.remainder[1:]

return (module, module_args)
module.execute(module_args)

def execute(self, argv, args=None): # pylint: disable=W0613
'''
:param argv: Argument values
:param args: Parsed arguments
'''
(module, module_argv) = self.parse_args(argv)

module.execute(module_argv)
class CLIException(Exception):

pass
16 changes: 11 additions & 5 deletions base/common/python/pki/cli/main.py
Original file line number Diff line number Diff line change
Expand Up @@ -174,10 +174,10 @@ def print_help(self):
'certificate validity statuses')
print(' --ignore-banner Ignore banner')
print()
print(' -v, --verbose Run in verbose mode.')
print(' --debug Show debug messages.')
print(' --help Show help message.')
print(' --version Show version number.')
print(' -v, --verbose Run in verbose mode.')
print(' --debug Show debug messages.')
print(' --help Show help message.')
print(' --version Show version number.')
print()

super(PKICLI, self).print_help()
Expand Down Expand Up @@ -358,9 +358,15 @@ def execute(self, argv, args=None):
self.ignore_cert_status = args.ignore_cert_status
self.ignore_banner = args.ignore_banner

command = args.remainder[0]
command = None
if len(args.remainder) > 0:
command = args.remainder[0]
logger.debug('Command: %s', command)

if not command:
self.print_help()
return

if client_type == 'python' or command in PYTHON_COMMANDS:
module = self.find_module(command)
logger.debug('Module: %s', module.get_full_name())
Expand Down
Loading
Loading