errors: Provide some helpful information when keypushes fail
This gives the user some actionable methods for resolving the problem.
Change-Id: I08ce95767b978d8dff1274038060d8958cb5c20c
diff --git a/mdt/command.py b/mdt/command.py
index 73f76f5..9158659 100644
--- a/mdt/command.py
+++ b/mdt/command.py
@@ -73,6 +73,15 @@
except sshclient.DefaultLoginError as e:
print("Can't login using default credentials: {0}".format(e))
return 1
+ except sshclient.NonLocalDeviceError as e:
+ print()
+ print("It looks like you're trying to connect to a device that isn't connected\n"
+ "to your workstation via USB and doesn't have the SSH key this MDT generated.\n"
+ "To connect with `mdt shell` you will need to first connect to your device\n"
+ "ONLY via USB.")
+ print()
+ print("Cowardly refusing to attempt to push a key to a public machine.\n")
+ return 1
except SSHException as e:
print("Couldn't establish ssh connection to device: {0}".format(e))
return 1
diff --git a/mdt/sshclient.py b/mdt/sshclient.py
index 9355e96..207de90 100644
--- a/mdt/sshclient.py
+++ b/mdt/sshclient.py
@@ -42,6 +42,10 @@
pass
+class NonLocalDeviceError(Exception):
+ pass
+
+
class SshClient:
def __init__(self, device, address):
self.config = config.Config()
@@ -82,11 +86,26 @@
return authorized_keys_line
def _pushKeyViaKeymaster(self):
+ if not self.address.startswith('192.168.100'):
+ raise NonLocalDeviceError()
+
connection = http.client.HTTPConnection(self.address, KEYMASTER_PORT)
try:
key_line = self._generateAuthorizedKeysLine()
- connection.request('PUT', '/', key_line + '\n')
+ connection.request('PUT', '/', key_line + '\r\n')
response = connection.getresponse()
+ except ConnectionRefusedError as e:
+ print()
+ print("Couldn't connect to keymaster on {0}: {1}.".format(self.device, e))
+ print()
+ print("Did you previously connect from a different machine? If so,\n"
+ "mdt-keymaster will not be running as it only accepts a single key.\n")
+ print("You will need to either remove the key from ~/.ssh/authorized_keys\n"
+ "via some other method (serial console, etc), or you will need to\n"
+ "connect from the other machine to push an SSH public key to the\n"
+ "authorized_keys file.")
+ print()
+ raise KeyPushError(e)
except ConnectionError as e:
raise KeyPushError(e)
finally:
@@ -117,7 +136,7 @@
try:
self._pushKeyViaKeymaster()
except KeyPushError as e:
- print('Failed to push via keymaster -- attempting password login')
+ print('Failed to push via keymaster -- will attempt password login as a fallback.')
self._pushKeyViaDefaultLogin()
time.sleep(1)