some self review

This commit is contained in:
Michael Hudson-Doyle 2018-05-10 16:22:14 +12:00
parent eb0fc05682
commit 80fffc723c
4 changed files with 37 additions and 28 deletions

View File

@ -71,19 +71,16 @@ class IdentityController(BaseController):
return None
status = run_command_summarize(p, stdout, stderr)
if status['status'] != 0:
if not stderr:
stderr = stdout
return False, _("Importing keys failed"), stderr
ssh_key = status['output']
return False, _("Importing keys failed"), stdout
key_material = status['output'].replace('\r', '')
p = subprocess.Popen(
['ssh-keygen', '-lf-'], stdout=subprocess.PIPE, stderr=subprocess.PIPE, stdin=subprocess.PIPE)
stdout, stderr = p.communicate(input=ssh_key.encode('latin-1'))
stdout, stderr = p.communicate(input=key_material.encode('latin-1'))
if p.returncode != 0:
# Do better here!
return False, _("ssh-keygen failed"), stderr.decode('utf-8')
fingerprint = stdout.decode('utf-8')
fingerprint = fingerprint.replace("# ssh-import-id {} ".format(ssh_import_id), "")
return True, result, ssh_key, fingerprint
fingerprints = stdout.decode('utf-8').replace("# ssh-import-id {} ".format(ssh_import_id), "").strip().splitlines()
return True, result, key_material, fingerprints
def _fetched_ssh_keys(self, fut):
result = fut.result()
@ -91,12 +88,12 @@ class IdentityController(BaseController):
if result is not None:
ok, rest = result[0], result[1:]
if ok:
rest = result, ssh_key, fingerprint
result, key_material, fingerprints = rest
if self.answers.get('accept-ssh-key'):
result['ssh_key'] = ssh_key
result['ssh_keys'] = key_material.splitlines()
self.loop.set_alarm_in(0.0, lambda loop, ud: self.done(result))
else:
self.ui.frame.body.confirm_ssh_keys(result, ssh_key, fingerprint)
self.ui.frame.body.confirm_ssh_keys(result, key_material, fingerprints)
else:
msg, stderr = rest
self.ui.frame.body.fetching_ssh_keys_failed(msg, stderr)

View File

@ -67,8 +67,8 @@ class SubiquityModel:
'groups': groups,
'lock-passwd': False,
}
if user.ssh_key is not None:
user_info['ssh_authorized_keys'] = user.ssh_key.replace('\r', '').splitlines()
if user.ssh_keys:
user_info['ssh_authorized_keys'] = user.ssh_keys
config = {
'growpart': {
'mode': 'off',

View File

@ -204,26 +204,38 @@ class FetchingSSHKeys(WidgetWrap):
self.parent.remove_overlay()
class ConfirmSSHKeys(WidgetWrap):
def __init__(self, parent, result, ssh_key, fingerprint):
class ConfirmSSHKeys(Stretchy):
def __init__(self, parent, result, key_material, fingerprints):
self.parent = parent
self.result = result
self.ssh_key = ssh_key
self.fingerprint = fingerprint
self.key_material = key_material
ok = ok_btn(label=_("Yes"), on_press=self.ok)
cancel = cancel_btn(label=_("No"), on_press=self.cancel)
if len(fingerprints) > 1:
title = _("Confirm SSH keys")
header = _('Keys with the following fingerprints were fetched. Do you want to use them?')
else:
title = _("Confirm SSH key")
header = _('A key with the following fingerprint was fetched. Do you want to use it?')
fingerprints = Pile([Text(fingerprint) for fingerprint in fingerprints])
super().__init__(
LineBox(
Pile([
('pack', Text('Key ok?')),
('pack', Text(fingerprint)),
('pack', button_pile([ok, cancel])),
]),
title=_("Confirm SSH keys")))
title,
[
Text(header),
Text(""),
fingerprints,
Text(""),
button_pile([ok, cancel]),
], 2, 4)
def cancel(self, sender):
self.parent.remove_overlay()
def ok(self, sender):
self.result['ssh_key'] = self.ssh_key
self.result['ssh_keys'] = self.key_material.splitlines()
self.parent.controller.done(self.result)
class FetchingSSHKeysFailed(Stretchy):
@ -303,9 +315,9 @@ class IdentityView(BaseView):
log.debug("User input: {}".format(result))
self.controller.done(result)
def confirm_ssh_keys(self, result, ssh_key, fingerprint):
def confirm_ssh_keys(self, result, ssh_key, fingerprints):
self.remove_overlay()
self.show_overlay(ConfirmSSHKeys(self, result, ssh_key, fingerprint))
self.show_stretchy_overlay(ConfirmSSHKeys(self, result, ssh_key, fingerprints))
def fetching_ssh_keys_failed(self, msg, stderr):
self.remove_overlay()

View File

@ -28,7 +28,7 @@ class User(object):
realname = attr.ib()
username = attr.ib()
password = attr.ib()
ssh_key = attr.ib(default=None)
ssh_keys = attr.ib(default=attr.Factory(list))
class IdentityModel(object):