Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def validate_ssh_public_key(text):
ssh = sshpubkeys.SSHKey(text)
try:
ssh.parse()
except (sshpubkeys.InvalidKeyException, UnicodeDecodeError):
return False
except NotImplementedError as e:
return False
return True
def validate_ssh_public_key(text):
ssh = sshpubkeys.SSHKey(text)
try:
ssh.parse()
except (sshpubkeys.InvalidKeyException, UnicodeDecodeError):
return False
except NotImplementedError as e:
return False
return True
if (request.user.username != user.username and
not user.is_sudo_user(request)):
return Response('Forbidden', status=403)
ldap_data = user.ldap.fetch(user.get_dn(), filters=user.ldap_filter,
attrs=['objectClass'], scope=ldap.SCOPE_BASE)
if SSHKey.LDAP_OBJCLS not in ldap_data['objectClass']:
user.ldap.op_modify(user.get_dn(),
[(ldap.MOD_ADD, 'objectClass', SSHKey.LDAP_OBJCLS)])
with transaction.atomic():
try:
ssh_key = SSHKey(user=user, title=request.DATA['title'],
key=request.DATA['key'])
ssh_key.save()
except (sshpubkeys.InvalidKeyException, ValidationError) as e:
return Response(str(e), status=status.HTTP_400_BAD_REQUEST)
user.ldap.op_modify(user.get_dn(),
[(ldap.MOD_ADD, SSHKey.LDAP_ATTR, str(ssh_key.key))])
changes_save(None, ssh_key, True)
return Response('', status=200)
def validate_ssh_public_key(text):
ssh = sshpubkeys.SSHKey(text)
try:
ssh.parse()
except (sshpubkeys.InvalidKeyException, UnicodeDecodeError):
return False
except NotImplementedError as e:
return False
return True
def validate_pks(key_list):
err = 0
msg = ""
bad_keys = []
for k in key_list:
if not k:
return 1, "No Key was passed in."
ssh = SSHKey(k, strict_mode=True)
try:
ssh.parse()
except InvalidKeyException as e:
err += 1
bad_keys.append(k)
if err > 0:
msg = "Invalid SSH keys: {}".format(",".join(bad_keys))
return err, msg
def run(self, session, dry_run=True):
for key in session.query(PublicKey):
pubkey = sshpubkeys.SSHKey(key.public_key, strict=True)
logging.info("Processing Key (id={})".format(key.id))
try:
pubkey.parse()
except sshpubkeys.InvalidKeyException as e:
logging.error("Invalid Key (id={}): {}".format(key.id, str(e)))
continue
try:
get_plugin_proxy().will_add_public_key(pubkey)
except PluginRejectedPublicKey as e:
logging.error("Bad Key (id={}): {}".format(key.id, str(e)))
continue
def public_key_validator(ssh_key):
ssh = sshpubkeys.SSHKey(ssh_key)
try:
ssh.parse()
except (sshpubkeys.InvalidKeyException, UnicodeDecodeError):
return False
except NotImplementedError as e:
return False
return True
user: User model of user in question
public_key_str: public key to add
Throws:
DuplicateKey if key is already in use
PublicKeyParseError if key can't be parsed
BadPublicKey if a plugin rejects the key
Returns:
PublicKey model object representing the key
"""
pubkey = sshpubkeys.SSHKey(public_key_str, strict=True)
try:
pubkey.parse()
except sshpubkeys.InvalidKeyException as e:
raise PublicKeyParseError(str(e))
try:
get_plugin_proxy().will_add_public_key(pubkey)
except PluginRejectedPublicKey as e:
raise BadPublicKey(str(e))
db_pubkey = PublicKey(
user=user,
public_key=pubkey.keydata.strip(),
fingerprint=pubkey.hash_md5().replace("MD5:", ""),
fingerprint_sha256=pubkey.hash_sha256().replace("SHA256:", ""),
key_size=pubkey.bits,
key_type=pubkey.key_type,
comment=pubkey.comment,
)