Beispiel #1
0
 def test_quote(self):
     quoted_str = QuoteString("quote me!")
     cmd = StringCommand("hello", quoted_str)
     assert str(cmd) == cmd.get_raw_value() == "hello 'quote me!'"
Beispiel #2
0
 def test_mixed_masked(self):
     cmd = StringCommand("some", "stuff", MaskString("mask me"), "other",
                         "stuff")
     assert cmd.get_raw_value() == "some stuff mask me other stuff"
     assert str(cmd) == "some stuff *** other stuff"
Beispiel #3
0
 def test_nested(self):
     nested_cmd = StringCommand("some", "stuff")
     cmd = StringCommand("hello", nested_cmd, "world")
     assert str(cmd) == cmd.get_raw_value() == "hello some stuff world"
Beispiel #4
0
 def test_normal(self):
     cmd = StringCommand("hello", "world")
     assert str(cmd) == cmd.get_raw_value() == "hello world"
Beispiel #5
0
 def test_masked(self):
     cmd = StringCommand(MaskString("adsfg"))
     assert cmd.get_raw_value() == "adsfg"
     assert str(cmd) == "***"
Beispiel #6
0
 def test_mixed_masked(self):
     cmd = StringCommand('some', 'stuff', MaskString('mask me'), 'other',
                         'stuff')
     assert cmd.get_raw_value() == 'some stuff mask me other stuff'
     assert str(cmd) == 'some stuff *** other stuff'
Beispiel #7
0
 def test_nested(self):
     nested_cmd = StringCommand('some', 'stuff')
     cmd = StringCommand('hello', nested_cmd, 'world')
     assert str(cmd) == cmd.get_raw_value() == 'hello some stuff world'
Beispiel #8
0
 def test_masked(self):
     cmd = StringCommand(MaskString('adsfg'))
     assert cmd.get_raw_value() == 'adsfg'
     assert str(cmd) == '***'
Beispiel #9
0
 def test_normal(self):
     cmd = StringCommand('hello', 'world')
     assert str(cmd) == cmd.get_raw_value() == 'hello world'
Beispiel #10
0
def make_unix_command(
    command,
    env=None,
    su_user=Config.SU_USER,
    use_su_login=Config.USE_SU_LOGIN,
    sudo=Config.SUDO,
    sudo_user=Config.SUDO_USER,
    use_sudo_login=Config.USE_SUDO_LOGIN,
    use_sudo_password=Config.USE_SUDO_PASSWORD,
    preserve_sudo_env=Config.PRESERVE_SUDO_ENV,
    shell_executable=Config.SHELL,
    raw=True,
):
    '''
    Builds a shell command with various kwargs.
    '''

    if shell_executable is None or not isinstance(shell_executable,
                                                  six.string_types):
        shell_executable = 'sh'

    command_bits = []

    # Use sudo (w/user?)
    if sudo:
        if use_sudo_password:
            askpass_filename, sudo_password = use_sudo_password
            command_bits.extend([
                'env',
                'SUDO_ASKPASS={0}'.format(askpass_filename),
                MaskString('{0}={1}'.format(SUDO_ASKPASS_ENV_VAR,
                                            sudo_password)),
            ])

        sudo_bits = ['sudo', '-H']

        if use_sudo_password:
            sudo_bits.extend(['-A', '-k'])  # use askpass, disable cache
        else:
            sudo_bits.append('-n')  # disable prompt/interactivity

        if use_sudo_login:
            sudo_bits.append('-i')

        if preserve_sudo_env:
            sudo_bits.append('-E')

        if sudo_user:
            sudo_bits.extend(('-u', sudo_user))

        command_bits.extend(sudo_bits)

    # Switch user with su
    if su_user:
        su_bits = ['su']

        if use_su_login:
            su_bits.append('-l')

        # note `which <shell>` usage here - su requires an absolute path
        command_bits.extend(su_bits)
        command_bits.extend(
            [su_user, '-s', '`which {0}`'.format(shell_executable), '-c'])

    else:
        # Otherwise just sh wrap the command
        command_bits.extend([shell_executable, '-c'])

    #
    # OK, now parse the command!
    #

    command = printable_command = command

    if isinstance(command, six.binary_type):
        command = printable_command = command.decode('utf-8')

    if isinstance(command, StringCommand):
        printable_command = command.get_masked_value()
        command = command.get_raw_value()

    # Use env & build our actual command
    if env:
        env_string = ' '.join([
            '{0}={1}'.format(key, value) for key, value in six.iteritems(env)
        ])
        command = 'env {0} {1}'.format(env_string, command)
        printable_command = 'env {0} {1}'.format(env_string, printable_command)

    # Quote the command as a string
    command = shlex_quote(command)

    command_bits = StringCommand(*command_bits)

    command = '{0} {1}'.format(command_bits.get_raw_value(), command)
    printable_command = '{0} {1}'.format(command_bits.get_masked_value(),
                                         printable_command)

    return command, printable_command
Beispiel #11
0
    def test_one(self):
        ts = StringCommand(MaskString('adsfg'))

        assert ts.get_raw_value() == 'adsfg'
        assert str(ts) == '***'
Beispiel #12
0
    def test_two(self):
        ts = StringCommand('some', 'stuff', MaskString('mask me'), 'other',
                           'stuff')

        assert ts.get_raw_value() == 'some stuff mask me other stuff'
        assert str(ts) == 'some stuff *** other stuff'