ssh: ignore empty lines when importing multiple SSH keys

ssh-import-id will include empty lines when multiple keys get imported.
These empty lines end up included in the array of authorized keys that
Subiquity manages and subsequently get passed to cloud-init and get
stored in autoinstall-user-data:

  authorized_keys = [
    'ssh-rsa AAAA[...] user@hostname',
    '',
    'ssh-ed255129 AAAA[...] user@hostname2',
  ]

Although cloud-init successfully ignores empty lines, it seems cleaner
to filter those out in Subiquity.

Signed-off-by: Olivier Gayot <olivier.gayot@canonical.com>
This commit is contained in:
Olivier Gayot 2022-11-21 11:25:59 +01:00
parent fd3a856bc4
commit 69c120e235
2 changed files with 11 additions and 7 deletions

View File

@ -107,13 +107,14 @@ class SSHController(SubiquityTuiController):
"# ssh-import-id {}".format(ssh_import_id), "# ssh-import-id {}".format(ssh_import_id),
"").strip().splitlines() "").strip().splitlines()
authorized_keys = [key for key in key_material.splitlines() if key]
if 'ssh-import-id' in self.app.answers.get("Identity", {}): if 'ssh-import-id' in self.app.answers.get("Identity", {}):
ssh_data.authorized_keys = key_material.splitlines() ssh_data.authorized_keys = authorized_keys
self.done(ssh_data) self.done(ssh_data)
else: else:
if isinstance(self.ui.body, SSHView): if isinstance(self.ui.body, SSHView):
self.ui.body.confirm_ssh_keys( self.ui.body.confirm_ssh_keys(
ssh_data, ssh_import_id, key_material, fingerprints) ssh_data, ssh_import_id, authorized_keys, fingerprints)
else: else:
log.debug("ui.body of unexpected instance: %s", log.debug("ui.body of unexpected instance: %s",
type(self.ui.body).__name__) type(self.ui.body).__name__)

View File

@ -15,6 +15,7 @@
import logging import logging
import re import re
from typing import List
from urwid import ( from urwid import (
connect_signal, connect_signal,
@ -177,10 +178,11 @@ class FetchingSSHKeys(WidgetWrap):
class ConfirmSSHKeys(Stretchy): class ConfirmSSHKeys(Stretchy):
def __init__(self, parent, ssh_data, key_material, fingerprints): def __init__(self, parent, ssh_data, authorized_keys: List[str],
fingerprints: List[str]):
self.parent = parent self.parent = parent
self.ssh_data = ssh_data self.ssh_data = ssh_data
self.key_material = key_material self.authorized_keys: List[str] = authorized_keys
ok = ok_btn(label=_("Yes"), on_press=self.ok) ok = ok_btn(label=_("Yes"), on_press=self.ok)
cancel = cancel_btn(label=_("No"), on_press=self.cancel) cancel = cancel_btn(label=_("No"), on_press=self.cancel)
@ -211,7 +213,7 @@ class ConfirmSSHKeys(Stretchy):
self.parent.remove_overlay() self.parent.remove_overlay()
def ok(self, sender): def ok(self, sender):
self.ssh_data.authorized_keys = self.key_material.splitlines() self.ssh_data.authorized_keys = self.authorized_keys
self.parent.controller.done(self.ssh_data) self.parent.controller.done(self.ssh_data)
@ -288,10 +290,11 @@ class SSHView(BaseView):
def cancel(self, result=None): def cancel(self, result=None):
self.controller.cancel() self.controller.cancel()
def confirm_ssh_keys(self, ssh_data, ssh_import_id, ssh_key, fingerprints): def confirm_ssh_keys(self, ssh_data, ssh_import_id,
authorized_keys: List[str], fingerprints):
self.remove_overlay() self.remove_overlay()
self.show_stretchy_overlay( self.show_stretchy_overlay(
ConfirmSSHKeys(self, ssh_data, ssh_key, fingerprints)) ConfirmSSHKeys(self, ssh_data, authorized_keys, fingerprints))
def fetching_ssh_keys_failed(self, msg, stderr): def fetching_ssh_keys_failed(self, msg, stderr):
self.remove_overlay() self.remove_overlay()