summaryrefslogtreecommitdiff
diff options
context:
space:
mode:
authorBrett Holman <bpholman5@gmail.com>2022-04-19 17:14:43 -0500
committerGitHub <noreply@github.com>2022-04-20 00:14:43 +0200
commit437cb0a01bc3f8758caa7b87dcd077c101a55675 (patch)
treee46987db256e9ac59316853dcb7558f72cf187c8
parentb80d6d14d1c777e1d2c10a89d5f01d85d4ed9484 (diff)
downloadcloud-init-git-437cb0a01bc3f8758caa7b87dcd077c101a55675.tar.gz
declare dependency on ssh-import-id (#1334)
Add ssh-import-id as a suggested package dependency for debs
-rwxr-xr-xcloudinit/config/cc_ssh_import_id.py35
-rw-r--r--packages/debian/control.in1
2 files changed, 34 insertions, 2 deletions
diff --git a/cloudinit/config/cc_ssh_import_id.py b/cloudinit/config/cc_ssh_import_id.py
index e5864878..b6bca841 100755
--- a/cloudinit/config/cc_ssh_import_id.py
+++ b/cloudinit/config/cc_ssh_import_id.py
@@ -18,6 +18,7 @@ from cloudinit.settings import PER_INSTANCE
# https://launchpad.net/ssh-import-id
distros = ["ubuntu", "debian"]
+SSH_IMPORT_ID_BINARY = "ssh-import-id"
MODULE_DESCRIPTION = """\
This module imports SSH keys from either a public keyserver, usually launchpad
or github using ``ssh-import-id``. Keys are referenced by the username they are
@@ -49,6 +50,19 @@ __doc__ = get_meta_doc(meta)
def handle(_name, cfg, cloud, log, args):
+ if not is_key_in_nested_dict(cfg, "ssh_import_id"):
+ log.debug(
+ "Skipping module named ssh-import-id, no 'ssh_import_id'"
+ " directives found."
+ )
+ return
+ elif not subp.which(SSH_IMPORT_ID_BINARY):
+ log.warn(
+ "ssh-import-id is not installed, but module ssh_import_id is "
+ "configured. Skipping module."
+ )
+ return
+
# import for "user: XXXXX"
if len(args) != 0:
user = args[0]
@@ -138,7 +152,7 @@ def import_ssh_ids(ids, user, log):
"--preserve-env=https_proxy",
"-Hu",
user,
- "ssh-import-id",
+ SSH_IMPORT_ID_BINARY,
] + ids
log.debug("Importing SSH ids for user %s.", user)
@@ -149,4 +163,21 @@ def import_ssh_ids(ids, user, log):
raise exc
-# vi: ts=4 expandtab
+def is_key_in_nested_dict(config: dict, search_key: str) -> bool:
+ """Search for key nested in config.
+
+ Note: A dict embedded in a list of lists will not be found walked - but in
+ this case we don't need it.
+ """
+ for config_key in config.keys():
+ if search_key == config_key:
+ return True
+ if isinstance(config[config_key], dict):
+ return is_key_in_nested_dict(config[config_key], search_key)
+ if isinstance(config[config_key], list):
+ # this code could probably be generalized to walking the whole
+ # config by iterating lists in search of dictionaries
+ for item in config[config_key]:
+ if isinstance(item, dict):
+ return is_key_in_nested_dict(item, search_key)
+ return False
diff --git a/packages/debian/control.in b/packages/debian/control.in
index 72895b47..7cded051 100644
--- a/packages/debian/control.in
+++ b/packages/debian/control.in
@@ -14,6 +14,7 @@ Depends: ${misc:Depends},
iproute2,
isc-dhcp-client
Recommends: eatmydata, sudo, software-properties-common, gdisk
+Suggests: ssh-import-id
Description: Init scripts for cloud instances
Cloud instances need special scripts to run during initialisation
to retrieve and install ssh keys and to let the user run various scripts.