Commit 5e98cfed authored by Michal Čihař's avatar Michal Čihař

Share logic for checking key line validity

Signed-off-by: default avatarMichal Čihař <michal@cihar.com>
parent 6654ca5b
...@@ -45,6 +45,21 @@ KNOWN_HOSTS_FILE = os.path.expanduser('~/.ssh/known_hosts') ...@@ -45,6 +45,21 @@ KNOWN_HOSTS_FILE = os.path.expanduser('~/.ssh/known_hosts')
RSA_KEY_FILE = os.path.expanduser('~/.ssh/id_rsa.pub') RSA_KEY_FILE = os.path.expanduser('~/.ssh/id_rsa.pub')
def is_key_line(key):
"""
Checks whether this line looks like a valid known_hosts line.
"""
if not key:
return False
if key[0] == '#':
return False
return (
' ssh-rsa ' in key
or ' ecdsa-sha2-nistp256 ' in key
or ' ssh-ed25519 ' in key
)
@staff_member_required @staff_member_required
def report(request): def report(request):
""" """
...@@ -214,7 +229,8 @@ def get_host_keys(): ...@@ -214,7 +229,8 @@ def get_host_keys():
result = [] result = []
with open(KNOWN_HOSTS_FILE, 'r') as handle: with open(KNOWN_HOSTS_FILE, 'r') as handle:
for line in handle: for line in handle:
if ' ssh-rsa ' in line or ' ecdsa-sha2-nistp256 ' in line: line = line.strip()
if is_key_line(line):
result.append(parse_hosts_line(line)) result.append(parse_hosts_line(line))
except IOError: except IOError:
return [] return []
...@@ -293,9 +309,8 @@ def add_host_key(request): ...@@ -293,9 +309,8 @@ def add_host_key(request):
) )
keys = [] keys = []
for key in output.splitlines(): for key in output.splitlines():
if (' ssh-rsa ' not in key key = key.strip()
and ' ecdsa-sha2-nistp256 ' not in key if not is_key_line(key):
and ' ssh-ed25519 ' not in key):
continue continue
keys.append(key) keys.append(key)
host, keytype, fingerprint = parse_hosts_line(key) host, keytype, fingerprint = parse_hosts_line(key)
......
Markdown is supported
0%
or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment