Exemplo n.º 1
0
 def test_cmd_run_whoami(self):
     """
     test return of whoami
     """
     if not salt.utils.platform.is_windows():
         user = RUNTIME_VARS.RUNTIME_CONFIGS["master"]["user"]
     else:
         user = salt.utils.user.get_specific_user()
     if user.startswith("sudo_"):
         user = user.replace("sudo_", "")
     cmd = self.run_function("cmd.run", ["whoami"])
     self.assertEqual(user.lower(), cmd.lower())
Exemplo n.º 2
0
def mk_key(opts, user):
    if HAS_PWD:
        uid = None
        try:
            uid = pwd.getpwnam(user).pw_uid
        except KeyError:
            # User doesn't exist in the system
            if opts['client_acl_verify']:
                return None
    if salt.utils.platform.is_windows():
        # The username may contain '\' if it is in Windows
        # 'DOMAIN\username' format. Fix this for the keyfile path.
        keyfile = os.path.join(opts['cachedir'],
                               '.{0}_key'.format(user.replace('\\', '_')))
    else:
        keyfile = os.path.join(opts['cachedir'], '.{0}_key'.format(user))

    if os.path.exists(keyfile):
        log.debug('Removing stale keyfile: {0}'.format(keyfile))
        if salt.utils.platform.is_windows() and not os.access(
                keyfile, os.W_OK):
            # Cannot delete read-only files on Windows.
            os.chmod(keyfile, stat.S_IRUSR | stat.S_IWUSR)
        os.unlink(keyfile)

    key = salt.crypt.Crypticle.generate_key_string()
    cumask = os.umask(191)
    with salt.utils.files.fopen(keyfile, 'w+') as fp_:
        fp_.write(key)
    os.umask(cumask)
    # 600 octal: Read and write access to the owner only.
    # Write access is necessary since on subsequent runs, if the file
    # exists, it needs to be written to again. Windows enforces this.
    os.chmod(keyfile, 0o600)
    if HAS_PWD and uid is not None:
        try:
            os.chown(keyfile, uid, -1)
        except OSError:
            # The master is not being run as root and can therefore not
            # chown the key file
            pass
    return key