From c8cca3cc82c3c446c49bf23ff6e2805f2aaeef48 Mon Sep 17 00:00:00 2001 From: Ben Howard Date: Mon, 27 Aug 2012 16:26:46 -0600 Subject: Fixed critical bug where user and group creation was being done after SSH configurations were applied. The result of this bug was that cloud-config supplied SSH public keys would fail to apply since the configured user may or may not exist. (LP: #1042459). cloudinit/config/cc_ssh_import_id.py: ssh_import_id.py now handles all user SSH import IDs. cloudinit/distros/ubuntu.py: Removed create_user class override as cruft, since ssh_import_id now handles all users. config/cloud.cfg: Moved users_groups to run under cloud_init_modules. doc/examples/cloud-config.txt: Added missing documentation on user and group creation. --- cloudinit/config/cc_ssh_import_id.py | 40 ++++++++++++++++++++++++++---------- 1 file changed, 29 insertions(+), 11 deletions(-) (limited to 'cloudinit/config') diff --git a/cloudinit/config/cc_ssh_import_id.py b/cloudinit/config/cc_ssh_import_id.py index cd97b99b..4a8849c8 100644 --- a/cloudinit/config/cc_ssh_import_id.py +++ b/cloudinit/config/cc_ssh_import_id.py @@ -19,6 +19,7 @@ # along with this program. If not, see . from cloudinit import util +import pwd # The ssh-import-id only seems to exist on ubuntu (for now) # https://launchpad.net/ssh-import-id @@ -26,30 +27,47 @@ distros = ['ubuntu'] def handle(name, cfg, cloud, log, args): + + # import for "user: XXXXX" if len(args) != 0: user = args[0] ids = [] if len(args) > 1: ids = args[1:] - else: - user = cloud.distro.get_default_user() - if 'users' in cfg: - user_zero = cfg['users'].keys()[0] + import_ssh_ids(ids, user, log) - if user_zero != "default": - user = user_zero + # import for cloudinit created users + for user in cfg['users'].keys(): + if user == "default": + distro_user = cloud.distro.get_default_user() + d_ids = util.get_cfg_option_list(cfg, "ssh_import_id", []) + import_ssh_ids(d_ids, distro_user, log) - ids = util.get_cfg_option_list(cfg, "ssh_import_id", []) + user_cfg = cfg['users'][user] + if not isinstance(user_cfg, dict): + user_cfg = None - if len(ids) == 0: - log.debug("Skipping module named %s, no ids found to import", name) - return + if user_cfg: + ids = util.get_cfg_option_list(user_cfg, "ssh_import_id", []) + import_ssh_ids(ids, user, log) + + +def import_ssh_ids(ids, user): if not user: - log.debug("Skipping module named %s, no user found to import", name) + log.debug("Skipping ssh-import-ids, no user for ids") return + if len(ids) == 0: + log.debug("Skipping ssh-import-ids for %s, no ids to import" % user) + return + + try: + check = pwd.getpwnam(user) + except KeyError: + log.debug("Skipping ssh-import-ids for %s, user not found" % user) + cmd = ["sudo", "-Hu", user, "ssh-import-id"] + ids log.debug("Importing ssh ids for user %s.", user) -- cgit v1.2.3