Skip to content

Commit

Permalink
Merge pull request #7 from lkubb/add-missing-tests
Browse files Browse the repository at this point in the history
Add tests that were missed during migration from Salt PR 62684
  • Loading branch information
nicholasmhughes authored Dec 11, 2023
2 parents c4cadc3 + 1ef008b commit 88f46c4
Show file tree
Hide file tree
Showing 2 changed files with 347 additions and 148 deletions.
148 changes: 0 additions & 148 deletions src/saltext/vault/utils/vault/__init__.py
Original file line number Diff line number Diff line change
Expand Up @@ -327,151 +327,3 @@ def _check_clear(config, client):
return not client.token_valid(remote=True)
except VaultAuthExpired:
return True


####################################################################################
# The following functions were available in previous versions and are deprecated
# TODO: remove deprecated functions after v3008 (Argon)
####################################################################################


def get_vault_connection():
"""
Get the connection details for calling Vault, from local configuration if
it exists, or from the master otherwise
"""
salt.utils.versions.warn_until(
"Argon",
"salt.utils.vault.get_vault_connection is deprecated, "
"please use salt.utils.vault.get_authd_client.",
)

opts = globals().get("__opts__", {})
context = globals().get("__context__", {})

try:
vault = get_authd_client(opts, context)
except salt.exceptions.InvalidConfigError as err:
# This exception class was raised previously
raise salt.exceptions.CommandExecutionError(err) from err

token = vault.auth.get_token()
server_config = vault.get_config()

ret = {
"url": server_config["url"],
"namespace": server_config["namespace"],
"token": str(token),
"verify": server_config["verify"],
"issued": token.creation_time,
}

if hlp._get_salt_run_type(opts) in [
hlp.SALT_RUNTYPE_MASTER_IMPERSONATING,
hlp.SALT_RUNTYPE_MASTER_PEER_RUN,
hlp.SALT_RUNTYPE_MINION_REMOTE,
]:
ret["lease_duration"] = token.explicit_max_ttl
ret["uses"] = token.num_uses
else:
ret["ttl"] = token.explicit_max_ttl

return ret


def del_cache():
"""
Delete cache file
"""
salt.utils.versions.warn_until(
"Argon",
"salt.utils.vault.del_cache is deprecated, please use salt.utils.vault.clear_cache.",
)
clear_cache(
globals().get("__opts__", {}),
globals().get("__context__", {}),
connection=False,
)


def write_cache(connection): # pylint: disable=unused-argument
"""
Write the vault token to cache
"""
salt.utils.versions.warn_until(
"Argon",
"salt.utils.vault.write_cache is deprecated without replacement.",
)
# always return false since cache is managed internally
return False


def get_cache():
"""
Return connection information from vault cache file
"""
salt.utils.versions.warn_until(
"Argon",
"salt.utils.vault.get_cache is deprecated, please use salt.utils.vault.get_authd_client.",
)
return get_vault_connection()


def make_request(
method,
resource,
token=None,
vault_url=None,
namespace=None,
get_token_url=False,
retry=False,
**args,
):
"""
Make a request to Vault
"""
salt.utils.versions.warn_until(
"Argon",
"salt.utils.vault.make_request is deprecated, please use "
"salt.utils.vault.query or salt.utils.vault.query_raw."
"To override token/url/namespace, please make use of the "
"provided classes directly.",
)

def _get_client(token, vault_url, namespace, args):
vault = get_authd_client(opts, context)
if token is not None:
vault.session = requests.Session()
vault.auth.cache = None
vault.auth.token = VaultToken(
client_token=token, renewable=False, lease_duration=60, num_uses=1
)
if vault_url is not None:
vault.session = requests.Session()
vault.url = vault_url
if namespace is not None:
vault.namespace = namespace
if "verify" in args:
vault.verify = args.pop("verify")

return vault

opts = globals().get("__opts__", {})
context = globals().get("__context__", {})
endpoint = resource.lstrip("/").lstrip("v1/")
payload = args.pop("json", None)

if "data" in args:
payload = salt.utils.json.loads(args.pop("data"))

vault = _get_client(token, vault_url, namespace, args)
res = vault.request_raw(method, endpoint, payload=payload, wrap=False, **args)
if res.status_code == 403 and not retry:
# retry was used to indicate to only try once more
clear_cache(opts, context)
vault = _get_client(token, vault_url, namespace, args)
res = vault.request_raw(method, endpoint, payload=payload, wrap=False, **args)

if get_token_url:
return res, str(vault.auth.token), vault.get_config()["url"]
return res
Loading

0 comments on commit 88f46c4

Please sign in to comment.