Secure your code as it's written. Use Snyk Code to scan source code in minutes - no build needed - and fix issues immediately.
def add_public_key_to_user(self, key, user):
# type: (str, str) -> None
sql_user = User.get(self.session, name=user)
assert sql_user
public_key = SSHKey(key, strict=True)
public_key.parse()
sql_public_key = PublicKey(
user_id=sql_user.id,
public_key=public_key.keydata.strip(),
fingerprint=public_key.hash_md5().replace("MD5:", ""),
fingerprint_sha256=public_key.hash_sha256().replace("SHA256:", ""),
key_size=public_key.bits,
key_type=public_key.key_type,
comment=public_key.comment,
)
sql_public_key.add(self.session)
def _get_ssh_fingerprint(self, public_key):
key = sshpubkeys.SSHKey(public_key)
if hasattr(key, 'hash_md5'):
return key.hash_md5().replace(to_native('MD5:'), to_native(''))
return key.hash()
def min_bits(self):
"""
The minimum number of bits considered secure for the type of this key.
"""
k = sshpubkeys.SSHKey(self.key)
return settings.SSH_KEY_MIN_BITS_FOR_TYPE.get(k.key_type,
settings.SSH_KEY_MIN_BITS_DEFAULT)
def public_key_obj(self):
class PubKey(object):
def __getattr__(self, item):
return ''
if self.public_key:
import sshpubkeys
try:
return sshpubkeys.SSHKey(self.public_key)
except (TabError, TypeError):
pass
return PubKey()
def check_ssh_key(key_path: str) -> bool:
if key_path and os.path.exists(key_path):
with open(key_path) as key:
contents = key.read()
key = sshpubkeys.SSHKey(contents)
try:
key.parse()
except (sshpubkeys.InvalidKeyError, NotImplementedError) as e:
return False
return True
return False
def pubkey_to_fingerprint(pubkey): # done
# converts pubkey in ssh-rsa BASE64SHIT to fingerprint
try:
msg_debug("Creating SSH key fingerprint")
ssh = SSHKey(pubkey)
fingerprint = ssh.hash()
return fingerprint
except Exception, e:
msg_debug(e)
msg_fail("ssh fingerprint generation failed.")