Merge "Validate type of given ssh key"

This commit is contained in:
Jenkins 2013-12-05 06:13:05 +00:00 committed by Gerrit Code Review
commit cf3b6285d2
3 changed files with 23 additions and 3 deletions

View File

@ -22,7 +22,7 @@ __all__ = ('ParamValidationError', 'validate_integer', 'validate_float',
'validate_regexp', 'validate_port', 'validate_not_empty',
'validate_options', 'validate_ip', 'validate_multi_ip',
'validate_file', 'validate_ping', 'validate_ssh',
'validate_multi_ssh')
'validate_multi_ssh', 'validate_sshkey')
def validate_integer(param, options=None):
@ -244,3 +244,21 @@ def validate_multi_ssh(param, options=None):
options = options or []
for host in param.split(","):
validate_ssh(host)
def validate_sshkey(param, options=None):
"""
Raises ParamValidationError if provided sshkey file is not public key.
"""
if not param:
return
with open(param) as sshkey:
line = sshkey.readline()
msg = None
if not re.search('ssh-|ecdsa', line):
msg = ('Invalid content header in %s, Public SSH key is required.'
% param)
if re.search('BEGIN [RD]SA PRIVATE KEY', line):
msg = 'Public SSH key is required. You passed private key.'
if msg:
raise ParamValidationError(msg)

View File

@ -33,7 +33,8 @@ def initConfig(controllerObject):
"USAGE" : "Path to a Public key to install on servers. If a usable key has not been installed on the remote servers the user will be prompted for a password and this key will be installed so the password will not be required again",
"PROMPT" : "Enter the path to your ssh Public key to install on servers",
"OPTION_LIST" : [],
"VALIDATORS" : [validators.validate_file],
"VALIDATORS" : [validators.validate_file,
validators.validate_sshkey],
"PROCESSORS" : [processors.process_ssh_key],
"DEFAULT_VALUE" : (glob.glob(os.path.join(os.environ["HOME"], ".ssh/*.pub"))+[""])[0],
"MASK_INPUT" : False,

View File

@ -51,7 +51,8 @@ class CommandLineTestCase(PackstackTestCaseMixin, TestCase):
# create a dummy public key
dummy_public_key = os.path.join(self.tempdir, 'id_rsa.pub')
open(dummy_public_key, 'w').close()
with open(dummy_public_key, 'w') as dummy:
dummy.write('ssh-rsa AAAAblablabla')
# Save sys.argv and replace it with the args we want optparse to use
orig_argv = sys.argv