From ca3ae67211d907b4cfdcd685c0ae4f9530cb7da1 Mon Sep 17 00:00:00 2001 From: Scott Moser Date: Fri, 16 Dec 2016 13:29:36 -0500 Subject: user-groups: fix bug when groups was provided as string and had spaces Cloud-config provided like: users: - default - name: foobar groups: sudo, adm Would result in adduser being called as: useradd foobar --groups 'sudo, adm' -m Which would cause error: useradd: group ' adm' does not exist The fix here is just to always normalize groups and remove whitespace. Additionally a fix and unit tests to explicitly set system=False or no_create_home=True. Previously those paths did not test the value of the entry, only the presense of the entry. LP: #1354694 --- cloudinit/distros/__init__.py | 22 +++++++++++++--------- 1 file changed, 13 insertions(+), 9 deletions(-) (limited to 'cloudinit') diff --git a/cloudinit/distros/__init__.py b/cloudinit/distros/__init__.py index 1f731951..94f31f86 100755 --- a/cloudinit/distros/__init__.py +++ b/cloudinit/distros/__init__.py @@ -398,12 +398,16 @@ class Distro(object): # support kwargs having groups=[list] or groups="g1,g2" groups = kwargs.get('groups') if groups: - if isinstance(groups, (list, tuple)): - # kwargs.items loop below wants a comma delimeted string - # that can go right through to the command. - kwargs['groups'] = ",".join(groups) - else: - groups = [group.strip() for group in groups.split(",")] + if isinstance(groups, six.string_types): + groups = groups.split(",") + + # remove any white spaces in group names, most likely + # that came in as a string like: groups: group1, group2 + groups = [g.strip() for g in groups] + + # kwargs.items loop below wants a comma delimeted string + # that can go right through to the command. + kwargs['groups'] = ",".join(groups) primary_group = kwargs.get('primary_group') if primary_group: @@ -413,10 +417,10 @@ class Distro(object): for group in groups: if not util.is_group(group): self.create_group(group) - LOG.debug("created group %s for user %s", name, group) + LOG.debug("created group '%s' for user '%s'", group, name) # Check the values and create the command - for key, val in kwargs.items(): + for key, val in sorted(kwargs.items()): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) @@ -433,7 +437,7 @@ class Distro(object): # Don't create the home directory if directed so or if the user is a # system user - if 'no_create_home' in kwargs or 'system' in kwargs: + if kwargs.get('no_create_home') or kwargs.get('system'): adduser_cmd.append('-M') log_adduser_cmd.append('-M') else: -- cgit v1.2.3