# Buggy code
def pkg(pkg_path, pkg_sum, hash_type, test=None, **kwargs):
    """
    Execute a packaged state run, the packaged state run will exist in a
    tarball available locally. This packaged state
    can be generated using salt-ssh.

    CLI Example:

    .. code-block:: bash

        salt '*' state.pkg /tmp/salt_state.tgz 760a9353810e36f6d81416366fc426dc md5
    """
    # TODO - Add ability to download from salt master or other source
    popts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
    if not os.path.isfile(pkg_path):
        return {}
    if not salt.utils.hashutils.get_hash(pkg_path, hash_type) == pkg_sum:
        return {}
    root = tempfile.mkdtemp()
    s_pkg = tarfile.open(pkg_path, "r:gz")
    # Verify that the tarball does not extract outside of the intended root
    members = s_pkg.getmembers()
    for member in members:
        if salt.utils.stringutils.to_unicode(member.path).startswith(
            (os.sep, "..{0}".format(os.sep))
        ):
            return {}
        elif "..{0}".format(os.sep) in salt.utils.stringutils.to_unicode(member.path):
            return {}
    s_pkg.extractall(root)
    s_pkg.close()
    lowstate_json = os.path.join(root, "lowstate.json")
    with salt.utils.files.fopen(lowstate_json, "r") as fp_:
        lowstate = salt.utils.json.load(fp_)
    # Check for errors in the lowstate
    for chunk in lowstate:
        if not isinstance(chunk, dict):
            return lowstate
    pillar_json = os.path.join(root, "pillar.json")
    if os.path.isfile(pillar_json):
        with salt.utils.files.fopen(pillar_json, "r") as fp_:
            pillar_override = salt.utils.json.load(fp_)
    else:
        pillar_override = None

    roster_grains_json = os.path.join(root, "roster_grains.json")
    if os.path.isfile(roster_grains_json):
        with salt.utils.files.fopen(roster_grains_json, "r") as fp_:
            roster_grains = salt.utils.json.load(fp_)

    if os.path.isfile(roster_grains_json):
        popts["grains"] = roster_grains
    popts["fileclient"] = "local"
    popts["file_roots"] = {}
    popts["test"] = _get_test_value(test, **kwargs)
    envs = os.listdir(root)
    for fn_ in envs:
        full = os.path.join(root, fn_)
        if not os.path.isdir(full):
            continue
        popts["file_roots"][fn_] = [full]
    st_ = salt.state.State(popts, pillar_override=pillar_override)
    snapper_pre = _snapper_pre(popts, kwargs.get("__pub_jid", "called localy"))
    ret = st_.call_chunks(lowstate)
    ret = st_.call_listen(lowstate, ret)
    try:
        shutil.rmtree(root)
    except (IOError, OSError):
        pass
    _set_retcode(ret)
    _snapper_post(popts, kwargs.get("__pub_jid", "called localy"), snapper_pre)
    return ret


# Traceback (KeyError)
$ salt --async $id pkg.info_installed
Executed command with job ID: 20191006202640409841
$ salt-run jobs.lookup_jid 20191006202640409841
hyEowqOAC7PQnONCkaMI:
The minion function caused an exception: Traceback (most recent call last):
File "/usr/lib/python2.7/dist-packages/salt/minion.py", line 1663, in _thread_return
return_data = minion_instance.executors[fname](opts, data, func, args, kwargs)
File "/usr/lib/python2.7/dist-packages/salt/executors/direct_call.py", line 12, in execute
return func(*args, **kwargs)
File "/usr/lib/python2.7/dist-packages/salt/modules/aptpkg.py", line 2792, in info_installed
for pkg_name, pkg_nfo in __salt__['lowpkg.info'](*names, failhard=failhard).items():
File "/usr/lib/python2.7/dist-packages/salt/modules/dpkg_lowpkg.py", line 420, in info
for pkg_ext_k, pkg_ext_v in dselect_pkg_avail.get(pkg['package'], {}).items():
KeyError: u'package'

# Fixed code
def pkg(pkg_path, pkg_sum, hash_type, test=None, **kwargs):
    """
    Execute a packaged state run, the packaged state run will exist in a
    tarball available locally. This packaged state
    can be generated using salt-ssh.

    CLI Example:

    .. code-block:: bash

        salt '*' state.pkg /tmp/salt_state.tgz 760a9353810e36f6d81416366fc426dc md5
    """
    # TODO - Add ability to download from salt master or other source
    popts = salt.utils.state.get_sls_opts(__opts__, **kwargs)
    if not os.path.isfile(pkg_path):
        return {}
    if not salt.utils.hashutils.get_hash(pkg_path, hash_type) == pkg_sum:
        return {}
    root = tempfile.mkdtemp()
    s_pkg = tarfile.open(pkg_path, "r:gz")
    # Verify that the tarball does not extract outside of the intended root
    members = s_pkg.getmembers()
    for member in members:
        if salt.utils.stringutils.to_unicode(member.path).startswith(
            (os.sep, "..{0}".format(os.sep))
        ):
            return {}
        elif "..{0}".format(os.sep) in salt.utils.stringutils.to_unicode(member.path):
            return {}
    s_pkg.extractall(root)
    s_pkg.close()
    lowstate_json = os.path.join(root, "lowstate.json")
    with salt.utils.files.fopen(lowstate_json, "r") as fp_:
        lowstate = salt.utils.json.load(fp_)
    # Check for errors in the lowstate
    for chunk in lowstate:
        if not isinstance(chunk, dict):
            return lowstate
    pillar_json = os.path.join(root, "pillar.json")
    if os.path.isfile(pillar_json):
        with salt.utils.files.fopen(pillar_json, "r") as fp_:
            pillar_override = salt.utils.json.load(fp_)
    else:
        pillar_override = None

    roster_grains_json = os.path.join(root, "roster_grains.json")
    if os.path.isfile(roster_grains_json):
        with salt.utils.files.fopen(roster_grains_json, "r") as fp_:
            roster_grains = _format_cached_grains(salt.utils.json.load(fp_))

    if os.path.isfile(roster_grains_json):
        popts["grains"] = roster_grains
    popts["fileclient"] = "local"
    popts["file_roots"] = {}
    popts["test"] = _get_test_value(test, **kwargs)
    envs = os.listdir(root)
    for fn_ in envs:
        full = os.path.join(root, fn_)
        if not os.path.isdir(full):
            continue
        popts["file_roots"][fn_] = [full]
    st_ = salt.state.State(popts, pillar_override=pillar_override)
    snapper_pre = _snapper_pre(popts, kwargs.get("__pub_jid", "called localy"))
    ret = st_.call_chunks(lowstate)
    ret = st_.call_listen(lowstate, ret)
    try:
        shutil.rmtree(root)
    except (IOError, OSError):
        pass
    _set_retcode(ret)
    _snapper_post(popts, kwargs.get("__pub_jid", "called localy"), snapper_pre)
    return ret