Distinguish between a key lookup error in the SSH agent and other communication/runtime errors
Marco Ricci

Marco Ricci commited on 2024-06-30 16:37:52
Zeige 1 geänderte Dateien mit 8 Einfügungen und 8 Löschungen.

... ...
@@ -134,11 +134,11 @@ def _get_suitable_ssh_keys(
134 134
             passphrase derivation.
135 135
 
136 136
     Raises:
137
-        RuntimeError:
138
-            There was an error communicating with the SSH agent.
139
-        RuntimeError:
137
+        LookupError:
140 138
             No keys usable for passphrase derivation are loaded into the
141 139
             SSH agent.
140
+        RuntimeError:
141
+            There was an error communicating with the SSH agent.
142 142
 
143 143
     """
144 144
     client: ssh_agent_client.SSHAgentClient
... ...
@@ -166,7 +166,7 @@ def _get_suitable_ssh_keys(
166 166
         if dpp.Vault._is_suitable_ssh_key(key):
167 167
             yield pair
168 168
     if not suitable_keys:  # pragma: no cover
169
-        raise RuntimeError('No usable SSH keys were found')
169
+        raise IndexError('No usable SSH keys were found')
170 170
 
171 171
 
172 172
 def _prompt_for_selection(
... ...
@@ -263,11 +263,11 @@ def _select_ssh_key(
263 263
         IndexError:
264 264
             The user made an invalid or empty selection, or requested an
265 265
             abort.
266
-        RuntimeError:
267
-            There was an error communicating with the SSH agent.
268
-        RuntimeError:
266
+        LookupError:
269 267
             No keys usable for passphrase derivation are loaded into the
270 268
             SSH agent.
269
+        RuntimeError:
270
+            There was an error communicating with the SSH agent.
271 271
     """
272 272
     suitable_keys = list(_get_suitable_ssh_keys(conn))
273 273
     key_listing: list[str] = []
... ...
@@ -825,7 +825,7 @@ def derivepassphrase(
825 825
                     _select_ssh_key()).decode('ASCII')
826 826
             except IndexError:
827 827
                 ctx.fail('no valid SSH key selected')
828
-            except RuntimeError as e:
828
+            except (LookupError, RuntimeError) as e:
829 829
                 ctx.fail(str(e))
830 830
         elif use_phrase:
831 831
             maybe_phrase = _prompt_for_passphrase()
832 832