def create_group(self, name, members=None): group_add_cmd = ['groupadd', name] if util.system_is_snappy(): group_add_cmd.append('--extrausers') if not members: members = [] # Check if group exists, and then add it doesn't if util.is_group(name): LOG.warning("Skipping creation of existing group '%s'", name) else: try: subp.subp(group_add_cmd) LOG.info("Created new group %s", name) except Exception: util.logexc(LOG, "Failed to create group %s", name) # Add members to the group, if so defined if len(members) > 0: for member in members: if not util.is_user(member): LOG.warning( "Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name) continue subp.subp(['usermod', '-a', '-G', name, member]) LOG.info("Added user '%s' to group '%s'", member, name)
def create_group(self, name, members=None): if util.is_group(name): LOG.warning("Skipping creation of existing group '%s'", name) else: group_add_cmd = self.group_add_cmd_prefix + [name] try: subp.subp(group_add_cmd) LOG.info("Created new group %s", name) except Exception: util.logexc(LOG, "Failed to create group %s", name) if not members: members = [] for member in members: if not util.is_user(member): LOG.warning( "Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name, ) continue try: subp.subp(self._get_add_member_to_group_cmd(member, name)) LOG.info("Added user '%s' to group '%s'", member, name) except Exception: util.logexc(LOG, "Failed to add user '%s' to group '%s'", member, name)
def create_group(self, name, members): group_add_cmd = ['pw', '-n', name] if util.is_group(name): LOG.warning("Skipping creation of existing group '%s'", name) else: try: util.subp(group_add_cmd) LOG.info("Created new group %s", name) except Exception as e: util.logexc(LOG, "Failed to create group %s", name) raise e if len(members) > 0: for member in members: if not util.is_user(member): LOG.warning( "Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name) continue try: util.subp(['pw', 'usermod', '-n', name, '-G', member]) LOG.info("Added user '%s' to group '%s'", member, name) except Exception: util.logexc(LOG, "Failed to add user '%s' to group '%s'", member, name)
def create_group(self, name, members=None): group_add_cmd = ['groupadd', name] if not members: members = [] # Check if group exists, and then add it doesn't if util.is_group(name): LOG.warn("Skipping creation of existing group '%s'" % name) else: try: util.subp(group_add_cmd) LOG.info("Created new group %s" % name) except Exception: util.logexc(LOG, "Failed to create group %s", name) # Add members to the group, if so defined if len(members) > 0: for member in members: if not util.is_user(member): LOG.warn("Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name) continue util.subp(['usermod', '-a', '-G', name, member]) LOG.info("Added user '%s' to group '%s'" % (member, name))
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False adduser_cmd = ['pw', 'useradd', '-n', name] log_adduser_cmd = ['pw', 'useradd', '-n', name] adduser_opts = { "homedir": '-d', "gecos": '-c', "primary_group": '-g', "groups": '-G', "passwd": '-h', "shell": '-s', "inactive": '-E', } adduser_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } redact_opts = ['passwd'] for key, val in kwargs.items(): if (key in adduser_opts and val and isinstance(val, six.string_types)): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) if 'no_create_home' in kwargs or 'system' in kwargs: adduser_cmd.append('-d/nonexistent') log_adduser_cmd.append('-d/nonexistent') else: adduser_cmd.append('-d/usr/home/%s' % name) adduser_cmd.append('-m') log_adduser_cmd.append('-d/usr/home/%s' % name) log_adduser_cmd.append('-m') # Run the command LOG.info("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False pw_useradd_cmd = ["pw", "useradd", "-n", name] log_pw_useradd_cmd = ["pw", "useradd", "-n", name] pw_useradd_opts = { "homedir": "-d", "gecos": "-c", "primary_group": "-g", "groups": "-G", "shell": "-s", "inactive": "-E", } pw_useradd_flags = { "no_user_group": "--no-user-group", "system": "--system", "no_log_init": "--no-log-init", } for key, val in kwargs.items(): if key in pw_useradd_opts and val and isinstance(val, str): pw_useradd_cmd.extend([pw_useradd_opts[key], val]) elif key in pw_useradd_flags and val: pw_useradd_cmd.append(pw_useradd_flags[key]) log_pw_useradd_cmd.append(pw_useradd_flags[key]) if "no_create_home" in kwargs or "system" in kwargs: pw_useradd_cmd.append("-d/nonexistent") log_pw_useradd_cmd.append("-d/nonexistent") else: pw_useradd_cmd.append("-d{home_dir}/{name}".format( home_dir=self.home_dir, name=name)) pw_useradd_cmd.append("-m") log_pw_useradd_cmd.append("-d{home_dir}/{name}".format( home_dir=self.home_dir, name=name)) log_pw_useradd_cmd.append("-m") # Run the command LOG.info("Adding user %s", name) try: subp.subp(pw_useradd_cmd, logstring=log_pw_useradd_cmd) except Exception: util.logexc(LOG, "Failed to create user %s", name) raise # Set the password if it is provided # For security consideration, only hashed passwd is assumed passwd_val = kwargs.get("passwd", None) if passwd_val is not None: self.set_passwd(name, passwd_val, hashed=True)
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False adduser_cmd = ['pw', 'useradd', '-n', name] log_adduser_cmd = ['pw', 'useradd', '-n', name] adduser_opts = { "homedir": '-d', "gecos": '-c', "primary_group": '-g', "groups": '-G', "shell": '-s', "inactive": '-E', } adduser_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } for key, val in kwargs.items(): if (key in adduser_opts and val and isinstance(val, six.string_types)): adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) if 'no_create_home' in kwargs or 'system' in kwargs: adduser_cmd.append('-d/nonexistent') log_adduser_cmd.append('-d/nonexistent') else: adduser_cmd.append('-d/usr/home/%s' % name) adduser_cmd.append('-m') log_adduser_cmd.append('-d/usr/home/%s' % name) log_adduser_cmd.append('-m') # Run the command LOG.info("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e # Set the password if it is provided # For security consideration, only hashed passwd is assumed passwd_val = kwargs.get('passwd', None) if passwd_val is not None: self.set_passwd(name, passwd_val, hashed=True)
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False adduser_cmd = ['/usr/sbin/useradd'] log_adduser_cmd = ['/usr/sbin/useradd'] adduser_opts = { "homedir": '-d', "gecos": '-c', "primary_group": '-g', "groups": '-G', "shell": '-s', "expiredate": '-e', } redact_opts = ['passwd'] for key, val in kwargs.iteritems(): if key in adduser_opts and val and isinstance(val, basestring): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) if 'no_create_home' in kwargs or 'system' in kwargs: adduser_cmd.append('-d/nonexistent') log_adduser_cmd.append('-d/nonexistent') else: adduser_cmd.append('-m') adduser_cmd.append('-m') log_adduser_cmd.append('-m') log_adduser_cmd.append('-m') adduser_cmd.append(name) log_adduser_cmd.append(name) # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False adduser_cmd = ["useradd"] log_adduser_cmd = ["useradd"] adduser_opts = { "homedir": "-d", "gecos": "-c", "primary_group": "-g", "groups": "-G", "shell": "-s", } adduser_flags = { "no_user_group": "--no-user-group", "system": "--system", "no_log_init": "--no-log-init", } for key, val in kwargs.items(): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) if "no_create_home" not in kwargs or "system" not in kwargs: adduser_cmd += ["-m"] log_adduser_cmd += ["-m"] adduser_cmd += [name] log_adduser_cmd += [name] # Run the command LOG.info("Adding user %s", name) try: subp.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception: util.logexc(LOG, "Failed to create user %s", name) raise # Set the password if it is provided # For security consideration, only hashed passwd is assumed passwd_val = kwargs.get("passwd", None) if passwd_val is not None: self.set_passwd(name, passwd_val, hashed=True)
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return False adduser_cmd = ['/usr/sbin/useradd'] log_adduser_cmd = ['/usr/sbin/useradd'] adduser_opts = { "homedir": '-d', "gecos": '-c', "primary_group": '-g', "groups": '-G', "shell": '-s', "expiredate" : '-e', } redact_opts = ['passwd'] for key, val in kwargs.iteritems(): if key in adduser_opts and val and isinstance(val, basestring): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) if 'no_create_home' in kwargs or 'system' in kwargs: adduser_cmd.append('-d/nonexistent') log_adduser_cmd.append('-d/nonexistent') else: adduser_cmd.append('-m') log_adduser_cmd.append('-m') adduser_cmd.append(name) log_adduser_cmd.append(name) # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def create_group(self, name, members): group_add_cmd = ['groupadd', name] # Check if group exists, and then add it doesn't if util.is_group(name): LOG.warn("Skipping creation of existing group '%s'" % name) else: try: util.subp(group_add_cmd) LOG.info("Created new group %s" % name) except Exception: util.logexc("Failed to create group %s", name) # Add members to the group, if so defined if len(members) > 0: for member in members: if not util.is_user(member): LOG.warn("Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name) continue util.subp(['usermod', '-a', '-G', name, member]) LOG.info("Added user '%s' to group '%s'" % (member, name))
def create_group(self, name, members): group_add_cmd = ['pw', '-n', name] if util.is_group(name): LOG.warn("Skipping creation of existing group '%s'", name) else: try: util.subp(group_add_cmd) LOG.info("Created new group %s", name) except Exception as e: util.logexc(LOG, "Failed to create group %s", name) raise e if len(members) > 0: for member in members: if not util.is_user(member): LOG.warn("Unable to add group member '%s' to group '%s'" "; user does not exist.", member, name) continue try: util.subp(['pw', 'usermod', '-n', name, '-G', member]) LOG.info("Added user '%s' to group '%s'", member, name) except Exception: util.logexc(LOG, "Failed to add user '%s' to group '%s'", member, name)
def add_user(self, name, **kwargs): """ Add a user to the system using standard GNU tools """ if util.is_user(name): LOG.info("User %s already exists, skipping." % name) return if 'create_groups' in kwargs: create_groups = kwargs.pop('create_groups') else: create_groups = True adduser_cmd = ['useradd', name] log_adduser_cmd = ['useradd', name] # Since we are creating users, we want to carefully validate the # inputs. If something goes wrong, we can end up with a system # that nobody can login to. adduser_opts = { "gecos": '--comment', "homedir": '--home', "primary_group": '--gid', "uid": '--uid', "groups": '--groups', "passwd": '--password', "shell": '--shell', "expiredate": '--expiredate', "inactive": '--inactive', "selinux_user": '******', } adduser_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } redact_opts = ['passwd'] # 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 = groups.split(",") primary_group = kwargs.get('primary_group') if primary_group: groups.append(primary_group) if create_groups and groups: for group in groups: if not util.is_group(group): self.create_group(group) LOG.debug("created group %s for user %s", name, group) # Check the values and create the command for key, val in kwargs.items(): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) # 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: adduser_cmd.append('-M') log_adduser_cmd.append('-M') else: adduser_cmd.append('-m') log_adduser_cmd.append('-m') # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): """ Add a user to the system using standard GNU tools """ if util.is_user(name): LOG.info("User %s already exists, skipping." % name) return adduser_cmd = ['useradd', name] log_adduser_cmd = ['useradd', name] # Since we are creating users, we want to carefully validate the # inputs. If something goes wrong, we can end up with a system # that nobody can login to. adduser_opts = { "gecos": '--comment', "homedir": '--home', "primary_group": '--gid', "uid": '--uid', "groups": '--groups', "passwd": '--password', "shell": '--shell', "expiredate": '--expiredate', "inactive": '--inactive', "selinux_user": '******', } adduser_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } redact_opts = ['passwd'] # Check the values and create the command for key, val in kwargs.items(): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) # 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: adduser_cmd.append('-M') log_adduser_cmd.append('-M') else: adduser_cmd.append('-m') log_adduser_cmd.append('-m') # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): """ Add a user to the system using standard GNU tools """ if util.is_user(name): LOG.info("User %s already exists, skipping." % name) return adduser_cmd = ['useradd', name] log_adduser_cmd = ['useradd', name] # Since we are creating users, we want to carefully validate the # inputs. If something goes wrong, we can end up with a system # that nobody can login to. adduser_opts = { "gecos": '--comment', "homedir": '--home', "primary_group": '--gid', "groups": '--groups', "passwd": '--password', "shell": '--shell', "expiredate": '--expiredate', "inactive": '--inactive', "selinux_user": '******', } adduser_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } redact_opts = ['passwd'] # Check the values and create the command for key, val in kwargs.iteritems(): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) # 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: adduser_cmd.append('-M') log_adduser_cmd.append('-M') else: adduser_cmd.append('-m') log_adduser_cmd.append('-m') # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): """ Add a user to the system using standard GNU tools """ # XXX need to make add_user idempotent somehow as we # still want to add groups or modify SSH keys on pre-existing # users in the image. if util.is_user(name): LOG.info("User %s already exists, skipping.", name) return if 'create_groups' in kwargs: create_groups = kwargs.pop('create_groups') else: create_groups = True useradd_cmd = ['useradd', name] log_useradd_cmd = ['useradd', name] if util.system_is_snappy(): useradd_cmd.append('--extrausers') log_useradd_cmd.append('--extrausers') # Since we are creating users, we want to carefully validate the # inputs. If something goes wrong, we can end up with a system # that nobody can login to. useradd_opts = { "gecos": '--comment', "homedir": '--home', "primary_group": '--gid', "uid": '--uid', "groups": '--groups', "passwd": '--password', "shell": '--shell', "expiredate": '--expiredate', "inactive": '--inactive', "selinux_user": '******', } useradd_flags = { "no_user_group": '--no-user-group', "system": '--system', "no_log_init": '--no-log-init', } redact_opts = ['passwd'] # support kwargs having groups=[list] or groups="g1,g2" groups = kwargs.get('groups') if groups: if isinstance(groups, str): 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: groups.append(primary_group) if create_groups and groups: for group in groups: if not util.is_group(group): self.create_group(group) LOG.debug("created group '%s' for user '%s'", group, name) # Check the values and create the command for key, val in sorted(kwargs.items()): if key in useradd_opts and val and isinstance(val, str): useradd_cmd.extend([useradd_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_useradd_cmd.extend([useradd_opts[key], 'REDACTED']) else: log_useradd_cmd.extend([useradd_opts[key], val]) elif key in useradd_flags and val: useradd_cmd.append(useradd_flags[key]) log_useradd_cmd.append(useradd_flags[key]) # Don't create the home directory if directed so or if the user is a # system user if kwargs.get('no_create_home') or kwargs.get('system'): useradd_cmd.append('-M') log_useradd_cmd.append('-M') else: useradd_cmd.append('-m') log_useradd_cmd.append('-m') # Run the command LOG.debug("Adding user %s", name) try: subp.subp(useradd_cmd, logstring=log_useradd_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e
def add_user(self, name, **kwargs): if util.is_user(name): LOG.info("User %s already exists, skipping." % name) return adduser_cmd = ['adduser', name, '-D'] log_adduser_cmd = ['adduser', name, '-D'] # Since we are creating users, we want to carefully validate the # inputs. If something goes wrong, we can end up with a system # that nobody can login to. adduser_opts = { "gecos": '-g', "homedir": '-h', "uid": '-u', "shell": '-s', } adduser_flags = { "system": '-S', } redact_opts = ['passwd'] # Check the values and create the command for key, val in kwargs.items(): if key in adduser_opts and val and isinstance(val, str): adduser_cmd.extend([adduser_opts[key], val]) # Redact certain fields from the logs if key in redact_opts: log_adduser_cmd.extend([adduser_opts[key], 'REDACTED']) else: log_adduser_cmd.extend([adduser_opts[key], val]) elif key in adduser_flags and val: adduser_cmd.append(adduser_flags[key]) log_adduser_cmd.append(adduser_flags[key]) # 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: adduser_cmd.append('-H') log_adduser_cmd.append('-H') # Run the command LOG.debug("Adding user %s", name) try: util.subp(adduser_cmd, logstring=log_adduser_cmd) except Exception as e: util.logexc(LOG, "Failed to create user %s", name) raise e # Unlock the user LOG.debug("Unlocking user %s", name) try: util.subp(['passwd', '-u', name], logstring=['passwd', '-u', name]) except Exception as e: util.logexc(LOG, "Failed to unlock user %s", name) raise e if 'groups' in kwargs: groups = kwargs['groups'] if groups and isinstance(groups, str): # Why are these even a single string in the first place? groups = groups.split(',') for group in groups: try: util.subp(['adduser', name, group], logstring=['adduser', name, group]) except Exception as e: util.logexc(LOG, "Failed to add user %s to group %s", name, group) raise e