Skip to content

Commit

Permalink
Merge branch 'master' into issues/161
Browse files Browse the repository at this point in the history
  • Loading branch information
chambridge authored Oct 31, 2017
2 parents 47fc2ab + 6a6a812 commit 8bfaca4
Show file tree
Hide file tree
Showing 19 changed files with 235 additions and 106 deletions.
18 changes: 10 additions & 8 deletions qpc/auth/add.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from qpc.clicommand import CliCommand
import qpc.auth as auth
from qpc.auth.utils import validate_sshkeyfile, build_credential_payload
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
Expand All @@ -36,22 +38,22 @@ def __init__(self, subparsers):
subparsers.add_parser(self.ACTION), POST,
auth.AUTH_URI, [codes.created])
self.parser.add_argument('--name', dest='name', metavar='NAME',
help='auth credential name', required=True)
help=_(messages.AUTH_NAME_HELP),
required=True)
self.parser.add_argument('--username', dest='username',
metavar='USERNAME',
help='user name for authenticating'
' against target system', required=True)
help=_(messages.AUTH_USER_HELP),
required=True)
group = self.parser.add_mutually_exclusive_group(required=True)
group.add_argument('--password', dest='password',
action='store_true',
help='password for authenticating against'
' target system')
help=_(messages.AUTH_PWD_HELP))
group.add_argument('--sshkeyfile', dest='filename',
metavar='FILENAME',
help='file containing SSH key')
help=_(messages.AUTH_SSH_HELP))
self.parser.add_argument('--sudo-password', dest='sudo_password',
action='store_true',
help='password for running sudo')
help=_(messages.AUTH_SUDO_HELP))

def _validate_args(self):
CliCommand._validate_args(self)
Expand All @@ -69,4 +71,4 @@ def _build_data(self):
self.req_payload = build_credential_payload(self.args)

def _handle_response_success(self):
print('Auth "%s" was added' % self.args.name)
print(_(messages.AUTH_ADDED % self.args.name))
20 changes: 10 additions & 10 deletions qpc/auth/clear.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from qpc.clicommand import CliCommand
import qpc.auth as auth
from qpc.request import GET, DELETE, request
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
Expand All @@ -37,9 +39,9 @@ def __init__(self, subparsers):
auth.AUTH_URI, [codes.ok])
group = self.parser.add_mutually_exclusive_group(required=True)
group.add_argument('--name', dest='name', metavar='NAME',
help='auth credential name')
help=_(messages.AUTH_NAME_HELP))
group.add_argument('--all', dest='all', action='store_true',
help='remove all credentials')
help=_(messages.AUTH_CLEAR_ALL_HELP))

def _build_req_params(self):
if self.args.name:
Expand All @@ -54,26 +56,26 @@ def _delete_entry(self, auth_entry, print_out=True):
if response.status_code == codes.no_content:
deleted = True
if print_out:
print('Auth "%s" was removed' % name)
print(_(messages.AUTH_REMOVED % name))
else:
handle_error_response(response)
if print_out:
print('Failed to remove credential "%s"' % name)
print(_(messages.AUTH_FAILED_TO_REMOVE % name))
return deleted

def _handle_response_success(self):
json_data = self.response.json()
response_len = len(json_data)
if self.args.name and response_len == 0:
print('Auth "%s" was not found' % self.args.name)
print(_(messages.AUTH_NOT_FOUND % self.args.name))
sys.exit(1)
elif self.args.name and response_len == 1:
# delete single credential
entry = json_data[0]
if self._delete_entry(entry) is False:
sys.exit(1)
elif response_len == 0:
print("No credentials exist to be removed")
print(_(messages.AUTH_NO_CREDS_TO_REMOVE))
sys.exit(1)
else:
# remove all entries
Expand All @@ -83,9 +85,7 @@ def _handle_response_success(self):
remove_error.append(entry['name'])
if remove_error != []:
cred_err = ','.join(remove_error)
print('Some credentials were removed, however an error'
' occurred removing the following credentials: %s'
% cred_err)
print(_(messages.AUTH_PARTIAL_REMOVE % cred_err))
sys.exit(1)
else:
print('All credentials were removed')
print(_(messages.AUTH_CLEAR_ALL_SUCCESS))
25 changes: 13 additions & 12 deletions qpc/auth/edit.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from qpc.clicommand import CliCommand
import qpc.auth as auth
from qpc.auth.utils import validate_sshkeyfile, build_credential_payload
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
Expand All @@ -37,30 +39,29 @@ def __init__(self, subparsers):
subparsers.add_parser(self.ACTION), PATCH,
auth.AUTH_URI, [codes.ok])
self.parser.add_argument('--name', dest='name', metavar='NAME',
help='auth credential name', required=True)
help=_(messages.AUTH_NAME_HELP),
required=True)
self.parser.add_argument('--username', dest='username',
metavar='USERNAME',
help='user name for authenticating'
' against target system', required=False)
help=_(messages.AUTH_USER_HELP),
required=False)
group = self.parser.add_mutually_exclusive_group(required=False)
group.add_argument('--password', dest='password',
action='store_true',
help='password for authenticating against'
' target system')
help=_(messages.AUTH_PWD_HELP))
group.add_argument('--sshkeyfile', dest='filename',
metavar='FILENAME',
help='file containing SSH key')
help=_(messages.AUTH_SSH_HELP))
self.parser.add_argument('--sudo-password', dest='sudo_password',
action='store_true',
help='password for running sudo')
help=_(messages.AUTH_SUDO_HELP))

def _validate_args(self):
CliCommand._validate_args(self)

if not(self.args.username or self.args.password or
self.args.sudo_password or self.args.filename):
print('No arguments provided to edit credential %s' %
(self.args.name))
print(_(messages.AUTH_EDIT_NO_ARGS % (self.args.name)))
self.parser.print_help()
sys.exit(1)

Expand All @@ -79,10 +80,10 @@ def _validate_args(self):
cred_entry = json_data[0]
self.req_path = self.req_path + str(cred_entry['id']) + '/'
else:
print('Auth "%s" does not exist' % self.args.name)
print(_(messages.AUTH_DOES_NOT_EXIST % self.args.name))
sys.exit(1)
else:
print('Auth "%s" does not exist' % self.args.name)
print(_(messages.AUTH_DOES_NOT_EXIST % self.args.name))
sys.exit(1)

def _build_data(self):
Expand All @@ -93,4 +94,4 @@ def _build_data(self):
self.req_payload = build_credential_payload(self.args, add_none=False)

def _handle_response_success(self):
print('Auth "%s" was updated' % self.args.name)
print(_(messages.AUTH_UPDATED % self.args.name))
4 changes: 3 additions & 1 deletion qpc/auth/list.py
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,8 @@
from qpc.clicommand import CliCommand
import qpc.auth as auth
from qpc.request import GET
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
Expand All @@ -39,7 +41,7 @@ def __init__(self, subparsers):
def _handle_response_success(self):
json_data = self.response.json()
if json_data == []:
print('No credentials exist yet.')
print(_(messages.AUTH_LIST_NO_CREDS))
else:
data = pretty_print(json_data)
print(data)
7 changes: 5 additions & 2 deletions qpc/auth/show.py
Original file line number Diff line number Diff line change
Expand Up @@ -20,6 +20,8 @@
from qpc.clicommand import CliCommand
import qpc.auth as auth
from qpc.request import GET
from qpc.translation import _
import qpc.messages as messages


# pylint: disable=too-few-public-methods
Expand All @@ -37,7 +39,8 @@ def __init__(self, subparsers):
subparsers.add_parser(self.ACTION), GET,
auth.AUTH_URI, [codes.ok])
self.parser.add_argument('--name', dest='name', metavar='NAME',
help='auth credential name', required=True)
help=_(messages.AUTH_NAME_HELP),
required=True)

def _build_req_params(self):
self.req_params = {'name': self.args.name}
Expand All @@ -49,5 +52,5 @@ def _handle_response_success(self):
data = pretty_print(cred_entry)
print(data)
else:
print('Auth "%s" does not exist' % self.args.name)
print(_(messages.AUTH_DOES_NOT_EXIST % self.args.name))
sys.exit(1)
10 changes: 5 additions & 5 deletions qpc/auth/utils.py
Original file line number Diff line number Diff line change
Expand Up @@ -15,6 +15,8 @@
import os
import sys
from getpass import getpass
from qpc.translation import _
import qpc.messages as messages


def validate_sshkeyfile(keyfile, parser):
Expand All @@ -26,9 +28,7 @@ def validate_sshkeyfile(keyfile, parser):
"""
keyfile_path = os.path.abspath(os.path.normpath(keyfile))
if not os.path.isfile(keyfile_path):
print('The file path provided, %s, could not be found on the '
'system. Please provide a valid location for the '
'"--sshkeyfile" argument.' % (keyfile))
print(_(messages.VALIDATE_SSHKEY % (keyfile)))
parser.print_help()
sys.exit(1)
else:
Expand All @@ -45,13 +45,13 @@ def get_password(args, req_payload, add_none=True):
:returns: the updated dictionary
"""
if args.password:
print('Provide connection password.')
print(_(messages.CONN_PASSWORD))
pass_prompt = getpass()
req_payload['password'] = pass_prompt or None
elif add_none:
req_payload['password'] = None
if args.sudo_password:
print('Provide password for sudo.')
print(_(messages.SUDO_PASSWORD))
pass_prompt = getpass()
req_payload['sudo_password'] = pass_prompt or None
elif add_none:
Expand Down
5 changes: 3 additions & 2 deletions qpc/cli.py
Original file line number Diff line number Diff line change
Expand Up @@ -31,6 +31,8 @@
from qpc.scan.start import ScanStartCommand
from qpc.scan.list import ScanListCommand
from qpc.scan.show import ScanShowCommand
from qpc.translation import _
import qpc.messages as messages
from . import __version__


Expand All @@ -48,8 +50,7 @@ def __init__(self, name="cli", usage=None, shortdesc=None,
self.parser.add_argument('--version', action='version',
version=__version__)
self.parser.add_argument('-v', dest='verbosity', action='count',
help='Verbose mode. Use up to -vvvv for '
'more verbosity.')
help=_(messages.VERBOSITY_HELP))
self.subparsers = self.parser.add_subparsers(dest='subcommand')
self.name = name
self.args = None
Expand Down
91 changes: 91 additions & 0 deletions qpc/messages.py
Original file line number Diff line number Diff line change
@@ -0,0 +1,91 @@
#
# Copyright (c) 2017 Red Hat, Inc.
#
# This software is licensed to you under the GNU General Public License,
# version 3 (GPLv3). There is NO WARRANTY for this software, express or
# implied, including the implied warranties of MERCHANTABILITY or FITNESS
# FOR A PARTICULAR PURPOSE. You should have received a copy of GPLv3
# along with this software; if not, see
# https://www.gnu.org/licenses/gpl-3.0.txt.
#
#

"""CLI messages for translation"""

AUTH_NAME_HELP = 'auth credential name'
AUTH_USER_HELP = 'user name for authenticating against target system'
AUTH_PWD_HELP = 'password for authenticating against target system'
AUTH_SSH_HELP = 'file containing SSH key'
AUTH_SUDO_HELP = 'password for running sudo'
AUTH_CLEAR_ALL_HELP = 'remove all credentials'

AUTH_ADDED = 'Auth "%s" was added'

AUTH_REMOVED = 'Auth "%s" was removed'
AUTH_FAILED_TO_REMOVE = 'Failed to remove auth "%s"'
AUTH_NOT_FOUND = 'Auth "%s" was not found'
AUTH_NO_CREDS_TO_REMOVE = "No credentials exist to be removed"
AUTH_PARTIAL_REMOVE = 'Some credentials were removed, however an error' \
' occurred removing the following credentials: %s'
AUTH_CLEAR_ALL_SUCCESS = 'All credentials were removed'

AUTH_EDIT_NO_ARGS = 'No arguments provided to edit credential "%s"'
AUTH_DOES_NOT_EXIST = 'Auth "%s" does not exist'
AUTH_UPDATED = 'Auth "%s" was updated'

AUTH_LIST_NO_CREDS = 'No credentials exist yet.'

PROFILE_NAME_HELP = 'profile name'
PROFILE_HOSTS_HELP = 'IP range to scan. See "man qpc" for supported formats.'
PROFILE_AUTHS_HELP = 'credentials to associate with profile'
PROFILE_SSH_PORT_HELP = 'SSH port for connection; default=22'
PROFILE_ADD_AUTHS_NOT_FOUND = 'An error occurred while processing the ' \
'"--auth" input values. References for the following auth could not' \
' be found: %s. Failed to add profile "%s".'
PROFILE_ADD_AUTH_PROCESS_ERR = 'An error occurred while processing the' \
' "--auth" input values. Failed to add profile "%s"'
PROFILE_ADDED = 'Profile "%s" was added'

PROFILE_CLEAR_ALL_HELP = 'remove all network profiles'
PROFILE_REMOVED = 'Profile "%s" was removed'
PROFILE_FAILED_TO_REMOVE = 'Failed to remove profile "%s"'
PROFILE_NOT_FOUND = 'Profile "%s" was not found'
PROFILE_NO_PROFILES_TO_REMOVE = "No profiles exist to be removed"
PROFILE_PARTIAL_REMOVE = 'Some profiles were removed, however an error' \
' occurred removing the following profiles: %s'
PROFILE_CLEAR_ALL_SUCCESS = 'All profiles were removed'
PROFILE_EDIT_NO_ARGS = 'No arguments provided to edit profile %s'
PROFILE_DOES_NOT_EXIST = 'Profile "%s" does not exist'

PROFILE_EDIT_AUTHS_NOT_FOUND = 'An error occurred while processing the' \
' "--auth" input values. References for the following auth ' \
'could not be found: %s. Failed to edit profile "%s".'
PROFILE_EDIT_AUTH_PROCESS_ERR = 'An error occurred while processing the ' \
'"--auth" input values. Failed to edit profile "%s"'
PROFILE_UPDATED = 'Profile "%s" was updated'
PROFILE_LIST_NO_PROFILES = 'No profiles exist yet.'

SCAN_ID_HELP = 'scan identifier'
SCAN_DOES_NOT_EXIST = 'Scan "%s" does not exist'
SCAN_LIST_NO_SCANS = 'No scans exist yet.'
SCAN_STARTED = 'Scan "%s" started'

VERBOSITY_HELP = 'Verbose mode. Use up to -vvvv for more verbosity.'


CONNECTION_ERROR_MSG = 'A connection error has occurred attempting to' \
' communicate with the server. Check the ' \
'configuration and/or the status of the server.'

SSL_ERROR_MSG = 'A connection error has occurred attempting to' \
' communicate with the server over "https". Check the' \
' configuration and/or the status of the server.'

READ_FILE_ERROR = 'Error reading from %s: %s'
NOT_A_FILE = 'Input %s was not a file.'

VALIDATE_SSHKEY = 'The file path provided, %s, could not be found on the ' \
'system. Please provide a valid location for the "--sshkeyfile" argument.'

CONN_PASSWORD = 'Provide connection password.'
SUDO_PASSWORD = 'Provide password for sudo.'
Loading

0 comments on commit 8bfaca4

Please sign in to comment.