backups: Fix issue with verifying SSH host keys

Ensure that the fingerprint accepted is the one verified by user. If they
fingerprints and public keys are retrieved separately, there is chance that what
was verified by the user is not what is added to the known hosts file.

- Avoid creating a temporary file when fetching keys

Signed-off-by: Sunil Mohan Adapa <sunil@medhas.org>
Reviewed-by: Joseph Nuthalapati <njoseph@thoughtworks.com>
This commit is contained in:
Sunil Mohan Adapa authored and Joseph Nuthalapati committed 2019-07-03 12:44:43 +05:30
1 parent 463c620c65
commit fa3e2ea86b
2 files changed
+13 -22

No files matched your search

+9 -13
View File
@@ -199,18 +199,14 @@ class VerifySshHostkeyForm(forms.Form):
def _get_all_public_keys(hostname):
"""Use ssh-keyscan to get all the SSH public keys of a host."""
# Fetch public keys of ssh remote
res1 = subprocess.run(['ssh-keyscan', hostname],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL, check=True)
with tempfile.NamedTemporaryFile(delete=False) as tmpfil:
tmpfil.write(res1.stdout)
keyscan = subprocess.run(['ssh-keyscan', hostname],
stdout=subprocess.PIPE,
stderr=subprocess.DEVNULL)
keys = keyscan.stdout.decode().splitlines()
# Generate user-friendly fingerprints of public keys
res2 = subprocess.run(['ssh-keygen', '-l', '-f', tmpfil.name],
stdout=subprocess.PIPE)
os.remove(tmpfil.name)
keys = res2.stdout.decode().splitlines()
keygen = subprocess.run(['ssh-keygen', '-l', '-f', '-'],
input=keyscan.stdout,
stdout=subprocess.PIPE)
fingerprints = keygen.stdout.decode().splitlines()
# Create a list of tuples of (algorithm, fingerprint)
return [(key.rsplit(' ', 1)[-1].strip('()'), key) for key in keys]
return zip(keys, fingerprints)