# Buggy code
def trust_key(keyid=None, fingerprint=None, trust_level=None, user=None):
    """
    Set the trust level for a key in GPG keychain

    keyid
        The keyid of the key to set the trust level for.

    fingerprint
        The fingerprint of the key to set the trust level for.

    trust_level
        The trust level to set for the specified key, must be one
        of the following:
        expired, unknown, not_trusted, marginally, fully, ultimately

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.trust_key keyid='3FAD9F1E' trust_level='marginally'
        salt '*' gpg.trust_key fingerprint='53C96788253E58416D20BCD352952C84C3252192' trust_level='not_trusted'
        salt '*' gpg.trust_key keys=3FAD9F1E trust_level='ultimately' user='username'

    """
    ret = {"res": True, "message": ""}

    _VALID_TRUST_LEVELS = [
        "expired",
        "unknown",
        "not_trusted",
        "marginally",
        "fully",
        "ultimately",
    ]

    if fingerprint and keyid:
        ret["res"] = False
        ret["message"] = "Only specify one argument, fingerprint or keyid"
        return ret

    if not fingerprint:
        if keyid:
            key = get_key(keyid, user=user)
            if key:
                if "fingerprint" not in key:
                    ret["res"] = False
                    ret["message"] = "Fingerprint not found for keyid {0}".format(keyid)
                    return ret
                fingerprint = key["fingerprint"]
            else:
                ret["res"] = False
                ret["message"] = "KeyID {0} not in GPG keychain".format(keyid)
                return ret
        else:
            ret["res"] = False
            ret["message"] = "Required argument, fingerprint or keyid"
            return ret

    if trust_level not in _VALID_TRUST_LEVELS:
        return "ERROR: Valid trust levels - {0}".format(",".join(_VALID_TRUST_LEVELS))

    stdin = "{0}:{1}\n".format(fingerprint, NUM_TRUST_DICT[trust_level])
    cmd = [_gpg(), "--import-ownertrust"]
    _user = user

    if user == "salt":
        homeDir = os.path.join(__salt__["config.get"]("config_dir"), "gpgkeys")
        cmd.extend(["--homedir", homeDir])
        _user = "root"
    res = __salt__["cmd.run_all"](cmd, stdin=stdin, runas=_user, python_shell=False)

    if not res["retcode"] == 0:
        ret["res"] = False
        ret["message"] = res["stderr"]
    else:
        if res["stderr"]:
            _match = re.findall(r"\d", res["stderr"])
            if len(_match) == 2:
                ret["fingerprint"] = fingerprint
                ret["message"] = "Changing ownership trust from {0} to {1}.".format(
                    INV_NUM_TRUST_DICT[_match[0]], INV_NUM_TRUST_DICT[_match[1]]
                )
            else:
                ret["fingerprint"] = fingerprint
                ret["message"] = "Setting ownership trust to {0}.".format(
                    INV_NUM_TRUST_DICT[_match[0]]
                )
        else:
            ret["message"] = res["stderr"]
    return ret


# Traceback (ValueError)
[root@master1 salt]# salt-call gpg.encrypt text='Hello there.  How are you?'
[ERROR   ] An un-handled exception was caught by salt's global exception handler:
ValueError: No recipients specified with asymmetric encryption
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000.1', 'console_scripts', 'salt-call')()
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 119, in run
ret = self.call()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 218, in call
ret['return'] = self.minion.executors[fname](self.opts, data, func, args, kwargs)
File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/gpg.py", line 1191, in encrypt
result = gpg.encrypt(text, recipients, passphrase=gpg_passphrase)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1506, in encrypt
result = self.encrypt_file(data, recipients, **kwargs)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1440, in encrypt_file
raise ValueError('No recipients specified with asymmetric '
ValueError: No recipients specified with asymmetric encryption
Traceback (most recent call last):
File "/usr/bin/salt-call", line 11, in <module>
load_entry_point('salt==3000.1', 'console_scripts', 'salt-call')()
File "/usr/lib/python2.7/site-packages/salt/scripts.py", line 445, in salt_call
client.run()
File "/usr/lib/python2.7/site-packages/salt/cli/call.py", line 57, in run
caller.run()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 119, in run
ret = self.call()
File "/usr/lib/python2.7/site-packages/salt/cli/caller.py", line 218, in call
ret['return'] = self.minion.executors[fname](self.opts, data, func, args, kwargs)
File "/usr/lib/python2.7/site-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python2.7/site-packages/salt/modules/gpg.py", line 1191, in encrypt
result = gpg.encrypt(text, recipients, passphrase=gpg_passphrase)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1506, in encrypt
result = self.encrypt_file(data, recipients, **kwargs)
File "/usr/lib/python2.7/site-packages/gnupg.py", line 1440, in encrypt_file
raise ValueError('No recipients specified with asymmetric '
ValueError: No recipients specified with asymmetric encryption

# Fixed code
def trust_key(keyid=None, fingerprint=None, trust_level=None, user=None):
    """
    Set the trust level for a key in GPG keychain

    keyid
        The keyid of the key to set the trust level for.

    fingerprint
        The fingerprint of the key to set the trust level for.

    trust_level
        The trust level to set for the specified key, must be one
        of the following:
        expired, unknown, not_trusted, marginally, fully, ultimately

    user
        Which user's keychain to access, defaults to user Salt is running as.
        Passing the user as ``salt`` will set the GnuPG home directory to the
        ``/etc/salt/gpgkeys``.

    CLI Example:

    .. code-block:: bash

        salt '*' gpg.trust_key keyid='3FAD9F1E' trust_level='marginally'
        salt '*' gpg.trust_key fingerprint='53C96788253E58416D20BCD352952C84C3252192' trust_level='not_trusted'
        salt '*' gpg.trust_key keys=3FAD9F1E trust_level='ultimately' user='username'

    """
    ret = {"res": True, "message": ""}

    _VALID_TRUST_LEVELS = [
        "expired",
        "unknown",
        "not_trusted",
        "marginally",
        "fully",
        "ultimately",
    ]

    if fingerprint and keyid:
        ret["res"] = False
        ret["message"] = "Only specify one argument, fingerprint or keyid"
        return ret

    if not fingerprint:
        if keyid:
            key = get_key(keyid, user=user)
            if key:
                if "fingerprint" not in key:
                    ret["res"] = False
                    ret["message"] = "Fingerprint not found for keyid {}".format(keyid)
                    return ret
                fingerprint = key["fingerprint"]
            else:
                ret["res"] = False
                ret["message"] = "KeyID {} not in GPG keychain".format(keyid)
                return ret
        else:
            ret["res"] = False
            ret["message"] = "Required argument, fingerprint or keyid"
            return ret

    if trust_level not in _VALID_TRUST_LEVELS:
        return "ERROR: Valid trust levels - {}".format(",".join(_VALID_TRUST_LEVELS))

    stdin = "{}:{}\n".format(fingerprint, NUM_TRUST_DICT[trust_level])
    cmd = [_gpg(), "--import-ownertrust"]
    _user = user

    if user == "salt":
        homeDir = os.path.join(__salt__["config.get"]("config_dir"), "gpgkeys")
        cmd.extend(["--homedir", homeDir])
        _user = "root"
    res = __salt__["cmd.run_all"](cmd, stdin=stdin, runas=_user, python_shell=False)

    if not res["retcode"] == 0:
        ret["res"] = False
        ret["message"] = res["stderr"]
    else:
        if res["stderr"]:
            _match = re.findall(r"\d", res["stderr"])
            if len(_match) == 2:
                ret["fingerprint"] = fingerprint
                ret["message"] = "Changing ownership trust from {} to {}.".format(
                    INV_NUM_TRUST_DICT[_match[0]], INV_NUM_TRUST_DICT[_match[1]]
                )
            else:
                ret["fingerprint"] = fingerprint
                ret["message"] = "Setting ownership trust to {}.".format(
                    INV_NUM_TRUST_DICT[_match[0]]
                )
        else:
            ret["message"] = res["stderr"]
    return ret