Exemple #1
0
def generate_modify_user_command(task=None, manage_home=None):
    """Generate command to modify existing user to become the proposed user.

    args:
        task (dict): A proposed user and the differences between it and the existing user

    returns:
        list: The command string split into shell-like syntax
    """
    name = task['proposed_user'].name
    comparison_result = task['user_comparison']['result']
    command = None
    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1}'.format(sudo_check(), LINUX_CMD_USERMOD)
        if comparison_result.get('replacement_uid_value'):
            command = '{0} -u {1}'.format(
                command, comparison_result.get('replacement_uid_value'))
        if comparison_result.get('replacement_gid_value'):
            command = '{0} -g {1}'.format(
                command, comparison_result.get('replacement_gid_value'))
        if comparison_result.get('replacement_gecos_value'):
            command = '{0} -c {1}'.format(
                command, comparison_result.get('replacement_gecos_value'))
        if comparison_result.get('replacement_shell_value'):
            command = '{0} -s {1}'.format(
                command, comparison_result.get('replacement_shell_value'))
        if manage_home and comparison_result.get('replacement_home_dir_value'):
            command = '{0} -d {1}'.format(
                command, comparison_result.get('replacement_home_dir_value'))
        command = '{0} {1}'.format(command, name)
    if get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} usermod'.format(sudo_check(), FREEBSD_CMD_PW)
        if comparison_result.get('replacement_uid_value'):
            command = '{0} -u {1}'.format(
                command, comparison_result.get('replacement_uid_value'))
        if comparison_result.get('replacement_gid_value'):
            command = '{0} -g {1}'.format(
                command, comparison_result.get('replacement_gid_value'))
        if comparison_result.get('replacement_gecos_value'):
            command = '{0} -c {1}'.format(
                command, comparison_result.get('replacement_gecos_value'))
        if comparison_result.get('replacement_shell_value'):
            command = '{0} -s {1}'.format(
                command, comparison_result.get('replacement_shell_value'))
        if manage_home and comparison_result.get('replacement_home_dir_value'):
            command = '{0} -d {1}'.format(
                command, comparison_result.get('replacement_home_dir_value'))
        command = '{0} -n {1}'.format(command, name)
    if command:
        return shlex.split(str(command))
Exemple #2
0
def generate_add_user_command(proposed_user=None, manage_home=None):
    """Generate command to add a user.

    args:
        proposed_user (User): User
        manage_home: bool

    returns:
        list: The command string split into shell-like syntax
    """
    command = None
    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1}'.format(sudo_check(), LINUX_CMD_USERADD)
        if proposed_user.uid:
            command = '{0} -u {1}'.format(command, proposed_user.uid)
        if proposed_user.gid:
            command = '{0} -g {1}'.format(command, proposed_user.gid)
        if proposed_user.gecos:
            command = '{0} -c \'{1}\''.format(command, proposed_user.gecos)
        if manage_home:
            if proposed_user.home_dir:
                if os.path.exists(proposed_user.home_dir):
                    command = '{0} -d {1}'.format(command,
                                                  proposed_user.home_dir)
            elif not os.path.exists('/home/{0}'.format(proposed_user.name)):
                command = '{0} -m'.format(command)
        if proposed_user.shell:
            command = '{0} -s {1}'.format(command, proposed_user.shell)
        command = '{0} {1}'.format(command, proposed_user.name)
    elif get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} useradd'.format(sudo_check(), FREEBSD_CMD_PW)
        if proposed_user.uid:
            command = '{0} -u {1}'.format(command, proposed_user.uid)
        if proposed_user.gid:
            command = '{0} -g {1}'.format(command, proposed_user.gid)
        if proposed_user.gecos:
            command = '{0} -c \'{1}\''.format(command, proposed_user.gecos)
        if manage_home:
            if proposed_user.home_dir:
                command = '{0} -d {1}'.format(command, proposed_user.home_dir)
            else:
                command = '{0} -m'.format(command)
        if proposed_user.shell:
            command = '{0} -s {1}'.format(command, proposed_user.shell)
        command = '{0} -n {1}'.format(command, proposed_user.name)

    if command:
        return shlex.split(str(command))
Exemple #3
0
def generate_add_user_command(proposed_user=None, manage_home=None):
    """Generate command to add a user.

    args:
        proposed_user (User): User
        manage_home: bool

    returns:
        list: The command string split into shell-like syntax
    """
    command = None
    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1}'.format(sudo_check(), LINUX_CMD_USERADD)
        if proposed_user.uid:
            command = '{0} -u {1}'.format(command, proposed_user.uid)
        if proposed_user.gid:
            command = '{0} -g {1}'.format(command, proposed_user.gid)
        if proposed_user.gecos:
            command = '{0} -c \'{1}\''.format(command, proposed_user.gecos)
        if manage_home:
            if proposed_user.home_dir:
                if os.path.exists(proposed_user.home_dir):
                    command = '{0} -d {1}'.format(command, proposed_user.home_dir)
            elif not os.path.exists('/home/{0}'.format(proposed_user.name)):
                command = '{0} -m'.format(command)
        if proposed_user.shell:
            command = '{0} -s {1}'.format(command, proposed_user.shell)
        command = '{0} {1}'.format(command, proposed_user.name)
    elif get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} useradd'.format(sudo_check(), FREEBSD_CMD_PW)
        if proposed_user.uid:
            command = '{0} -u {1}'.format(command, proposed_user.uid)
        if proposed_user.gid:
            command = '{0} -g {1}'.format(command, proposed_user.gid)
        if proposed_user.gecos:
            command = '{0} -c \'{1}\''.format(command, proposed_user.gecos)
        if manage_home:
            if proposed_user.home_dir:
                command = '{0} -d {1}'.format(command, proposed_user.home_dir)
            else:
                command = '{0} -m'.format(command)
        if proposed_user.shell:
            command = '{0} -s {1}'.format(command, proposed_user.shell)
        command = '{0} -n {1}'.format(command, proposed_user.name)

    if command:
        return shlex.split(str(command))
Exemple #4
0
def generate_delete_user_command(username=None, manage_home=None):
    """Generate command to delete a user.

    args:
        username (str): user name
        manage_home (bool): manage home directory

    returns:
        list: The user delete command string split into shell-like syntax
    """
    command = None
    remove_home = '-r' if manage_home else ''

    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1} {2} {3}'.format(sudo_check(), LINUX_CMD_USERDEL, remove_home, username)
    elif get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} userdel {2} -n {3}'.format(sudo_check(), FREEBSD_CMD_PW, remove_home, username)
    if command:
        return shlex.split(str(command))
Exemple #5
0
def generate_modify_user_command(task=None, manage_home=None):
    """Generate command to modify existing user to become the proposed user.

    args:
        task (dict): A proposed user and the differences between it and the existing user

    returns:
        list: The command string split into shell-like syntax
    """
    name = task['proposed_user'].name
    comparison_result = task['user_comparison']['result']
    command = None
    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1}'.format(sudo_check(), LINUX_CMD_USERMOD)
        if comparison_result.get('replacement_uid_value'):
            command = '{0} -u {1}'.format(command, comparison_result.get('replacement_uid_value'))
        if comparison_result.get('replacement_gid_value'):
            command = '{0} -g {1}'.format(command, comparison_result.get('replacement_gid_value'))
        if comparison_result.get('replacement_gecos_value'):
            command = '{0} -c {1}'.format(command, comparison_result.get('replacement_gecos_value'))
        if comparison_result.get('replacement_shell_value'):
            command = '{0} -s {1}'.format(command, comparison_result.get('replacement_shell_value'))
        if manage_home and comparison_result.get('replacement_home_dir_value'):
                command = '{0} -d {1}'.format(command, comparison_result.get('replacement_home_dir_value'))
        command = '{0} {1}'.format(command, name)
    if get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} usermod'.format(sudo_check(), FREEBSD_CMD_PW)
        if comparison_result.get('replacement_uid_value'):
            command = '{0} -u {1}'.format(command, comparison_result.get('replacement_uid_value'))
        if comparison_result.get('replacement_gid_value'):
            command = '{0} -g {1}'.format(command, comparison_result.get('replacement_gid_value'))
        if comparison_result.get('replacement_gecos_value'):
            command = '{0} -c {1}'.format(command, comparison_result.get('replacement_gecos_value'))
        if comparison_result.get('replacement_shell_value'):
            command = '{0} -s {1}'.format(command, comparison_result.get('replacement_shell_value'))
        if manage_home and comparison_result.get('replacement_home_dir_value'):
            command = '{0} -d {1}'.format(command, comparison_result.get('replacement_home_dir_value'))
        command = '{0} -n {1}'.format(command, name)
    if command:
        return shlex.split(str(command))
Exemple #6
0
def generate_delete_user_command(username=None, manage_home=None):
    """Generate command to delete a user.

    args:
        username (str): user name
        manage_home (bool): manage home directory

    returns:
        list: The user delete command string split into shell-like syntax
    """
    command = None
    remove_home = '-r' if manage_home else ''

    if get_platform() in ('Linux', 'OpenBSD'):
        command = '{0} {1} {2} {3}'.format(sudo_check(), LINUX_CMD_USERDEL,
                                           remove_home, username)
    elif get_platform() == 'FreeBSD':  # pragma: FreeBSD
        command = '{0} {1} userdel {2} -n {3}'.format(sudo_check(),
                                                      FREEBSD_CMD_PW,
                                                      remove_home, username)
    if command:
        return shlex.split(str(command))
Exemple #7
0
    def __init__(self, oktypes=User):
        """Create instance of Users collection.

        args:
            oktypes (type): The acceptable types of instances..
        """
        platform = get_platform()
        # Check platform is supported
        if not platform in SUPPORTED_PLATFORMS:
            sys.exit('Linux, FreeBSD and OpenBSD are currently the only supported platforms for this library.')
        # Check OS commands are available for managing users
        missing_commands = get_missing_commands(platform)
        if missing_commands:
            sys.exit('Unable to find commands: {0}.\nPlease check PATH.'.format(', '.join(missing_commands)))

        self.oktypes = oktypes
        self._user_list = list()
Exemple #8
0
    def __init__(self, oktypes=User):
        """Create instance of Users collection.

        args:
            oktypes (type): The acceptable types of instances..
        """
        platform = get_platform()
        # Check platform is supported
        if not platform in SUPPORTED_PLATFORMS:
            sys.exit(
                'Linux, FreeBSD and OpenBSD are currently the only supported platforms for this library.'
            )
        # Check OS commands are available for managing users
        missing_commands = get_missing_commands(platform)
        if missing_commands:
            sys.exit(
                'Unable to find commands: {0}.\nPlease check PATH.'.format(
                    ', '.join(missing_commands)))

        self.oktypes = oktypes
        self._user_list = list()
Exemple #9
0
from creds.plan import (create_plan, execute_plan)
from creds.ssh import PublicKey
from creds.users import (Users, User)
from creds.utils import (execute_command, sudo_check, get_platform,
                         remove_sudoers_entry)
from external.six import text_type
from .sample_data import PUBLIC_KEYS

# TODO: Detect based on OS
USERMOD = '/usr/sbin/usermod'
USERADD = '/usr/sbin/useradd'
USERDEL = '/usr/sbin/userdel'
GROUPADD = '/usr/sbin/groupadd'
GROUPDEL = '/usr/sbin/groupdel'

PLATFORM = get_platform()

CURRENT_USER = getpass.getuser()


@mock_s3
def test_execute_plan_to_create_user_with_downloaded_yaml():
    """ Create a new user from downloaded YAML file """
    delete_test_user_and_group()
    session = Session()
    s3_client = session.client('s3')
    test_user_1 = open(
        os.path.join(os.path.dirname(__file__), 'test_user_1.yml')).read()
    s3_client.create_bucket(Bucket='test')
    s3_client.put_object(Bucket='test', Key='test.yml', Body=test_user_1)
    response = s3_client.get_object(Bucket='test', Key='test.yml')