|
| 1 | +From 4d57b7b3a3977cac020728027590daf42326ed09 Mon Sep 17 00:00:00 2001 |
| 2 | +From: Johannes Helmold <johannes.helmold@greenbone.net> |
| 3 | +Date: Wed, 22 Dec 2021 16:22:09 +0100 |
| 4 | +Subject: [PATCH 1/3] Don't fail if the known_hosts file does not exists. |
| 5 | + |
| 6 | +Now the SSH authentication process does not fail / abort |
| 7 | +if the known_hosts file does not exists. It still aborts |
| 8 | +in other cases, e.g. when the permission for that file is |
| 9 | +denied. |
| 10 | +--- |
| 11 | + gvm/connections.py | 10 ++++++---- |
| 12 | + 1 file changed, 6 insertions(+), 4 deletions(-) |
| 13 | + |
| 14 | +diff --git a/gvm/connections.py b/gvm/connections.py |
| 15 | +index 149f6047..ae964ecf 100644 |
| 16 | +--- a/gvm/connections.py |
| 17 | ++++ b/gvm/connections.py |
| 18 | +@@ -25,6 +25,7 @@ |
| 19 | + import ssl |
| 20 | + import sys |
| 21 | + import time |
| 22 | ++import errno |
| 23 | + |
| 24 | + |
| 25 | + from pathlib import Path |
| 26 | +@@ -328,10 +329,11 @@ def _ssh_authentication(self) -> None: |
| 27 | + # load the keys into paramiko and check if remote is in the list |
| 28 | + self._socket.load_host_keys(filename=self.known_hosts_file) |
| 29 | + except OSError as e: |
| 30 | +- raise GvmError( |
| 31 | +- 'Something went wrong with reading ' |
| 32 | +- f'the known_hosts file: {e}' |
| 33 | +- ) from None |
| 34 | ++ if e.errno != errno.ENOENT: |
| 35 | ++ raise GvmError( |
| 36 | ++ 'Something went wrong with reading ' |
| 37 | ++ f'the known_hosts file: {e}' |
| 38 | ++ ) from None |
| 39 | + hostkeys = self._socket.get_host_keys() |
| 40 | + if not hostkeys.lookup(self.hostname): |
| 41 | + # Key not found, so connect to remote and fetch the key |
| 42 | + |
| 43 | +From cd54af9c87724916477cf61e066d3ac9375ea06f Mon Sep 17 00:00:00 2001 |
| 44 | +From: Johannes Helmold <johannes.helmold@greenbone.net> |
| 45 | +Date: Thu, 23 Dec 2021 14:43:48 +0100 |
| 46 | +Subject: [PATCH 2/3] Added a function for unit tests. |
| 47 | + |
| 48 | +Added a function to test the enhancement of the known_hosts file |
| 49 | +issue via unit tests. |
| 50 | +--- |
| 51 | + tests/connections/test_ssh_connection.py | 17 +++++++++++++++++ |
| 52 | + 1 file changed, 17 insertions(+) |
| 53 | + |
| 54 | +diff --git a/tests/connections/test_ssh_connection.py b/tests/connections/test_ssh_connection.py |
| 55 | +index 7159cbca..534e1740 100644 |
| 56 | +--- a/tests/connections/test_ssh_connection.py |
| 57 | ++++ b/tests/connections/test_ssh_connection.py |
| 58 | +@@ -17,6 +17,7 @@ |
| 59 | + # along with this program. If not, see <http://www.gnu.org/licenses/>. |
| 60 | + |
| 61 | + from io import StringIO |
| 62 | ++import os |
| 63 | + import unittest |
| 64 | + from unittest.mock import patch, Mock |
| 65 | + from pathlib import Path |
| 66 | +@@ -103,6 +104,22 @@ def test_connect_unknown_host(self): |
| 67 | + ): |
| 68 | + ssh_connection.connect() |
| 69 | + |
| 70 | ++ def test_connect_no_known_hosts_file(self): |
| 71 | ++ if os.path.exists(self.known_hosts_file): |
| 72 | ++ os.remove(self.known_hosts_file) |
| 73 | ++ |
| 74 | ++ ssh_connection = SSHConnection( |
| 75 | ++ hostname='0.0.0.1', known_hosts_file=self.known_hosts_file |
| 76 | ++ ) |
| 77 | ++ with self.assertRaises( |
| 78 | ++ GvmError, |
| 79 | ++ msg=( |
| 80 | ++ "Could'nt establish a connection to fetch the remote " |
| 81 | ++ "server key: [Errno 65] No route to host" |
| 82 | ++ ), |
| 83 | ++ ): |
| 84 | ++ ssh_connection.connect() |
| 85 | ++ |
| 86 | + @patch('builtins.input') |
| 87 | + def test_connect_adding_and_save_hostkey(self, input_mock): |
| 88 | + |
| 89 | + |
| 90 | +From b5709af942c0c3acac8ef2a66ab47390462f7fcc Mon Sep 17 00:00:00 2001 |
| 91 | +From: Johannes Helmold <johannes.helmold@greenbone.net> |
| 92 | +Date: Thu, 23 Dec 2021 15:22:15 +0100 |
| 93 | +Subject: [PATCH 3/3] Added an additional function for unit tests. |
| 94 | + |
| 95 | +Added an additional function to test the enhancement of the |
| 96 | +known_hosts file issue via unit tests. |
| 97 | +--- |
| 98 | + tests/connections/test_ssh_connection.py | 16 ++++++++++++++++ |
| 99 | + 1 file changed, 16 insertions(+) |
| 100 | + |
| 101 | +diff --git a/tests/connections/test_ssh_connection.py b/tests/connections/test_ssh_connection.py |
| 102 | +index 534e1740..bf2db5da 100644 |
| 103 | +--- a/tests/connections/test_ssh_connection.py |
| 104 | ++++ b/tests/connections/test_ssh_connection.py |
| 105 | +@@ -104,6 +104,22 @@ def test_connect_unknown_host(self): |
| 106 | + ): |
| 107 | + ssh_connection.connect() |
| 108 | + |
| 109 | ++ def test_connect_denied_known_hosts_file(self): |
| 110 | ++ if os.path.exists(self.known_hosts_file): |
| 111 | ++ os.chmod(self.known_hosts_file, 0000) |
| 112 | ++ |
| 113 | ++ ssh_connection = SSHConnection( |
| 114 | ++ hostname='0.0.0.1', known_hosts_file=self.known_hosts_file |
| 115 | ++ ) |
| 116 | ++ with self.assertRaises( |
| 117 | ++ GvmError, |
| 118 | ++ msg=( |
| 119 | ++ "Could'nt establish a connection to fetch the remote " |
| 120 | ++ "server key: [Errno 65] No route to host" |
| 121 | ++ ), |
| 122 | ++ ): |
| 123 | ++ ssh_connection.connect() |
| 124 | ++ |
| 125 | + def test_connect_no_known_hosts_file(self): |
| 126 | + if os.path.exists(self.known_hosts_file): |
| 127 | + os.remove(self.known_hosts_file) |
0 commit comments