hvac.utilsยถ

Misc utility functions and constants

Functions

comma_delimited_to_list(list_param)

Convert comma-delimited list / string into a list of strings

deprecated_method(to_be_removed_in_version)

This is a decorator which can be used to mark methods as deprecated.

format_url(format_str,ย *args,ย **kwargs)

Creates a URL using the specified format after escaping the provided arguments.

generate_method_deprecation_message(โ€ฆ[,ย โ€ฆ])

Generate a message to be used when warning about the use of deprecated methods.

generate_property_deprecation_message(โ€ฆ[,ย โ€ฆ])

Generate a message to be used when warning about the use of deprecated properties.

get_token_from_env()

Get the token from env var, VAULT_TOKEN.

getattr_with_deprecated_properties(obj,ย โ€ฆ)

Helper method to use in the getattr method of a class with deprecated properties.

list_to_comma_delimited(list_param)

Convert a list of strings into a comma-delimited list / string.

raise_for_error(method,ย url,ย status_code[,ย โ€ฆ])

Helper method to raise exceptions based on the status code of a response received back from Vault.

remove_nones(params)

Removes None values from optional arguments in a parameter dictionary.

validate_list_of_strings_param(param_name,ย โ€ฆ)

Validate that an argument is a list of strings.

validate_pem_format(param_name,ย param_argument)

Validate that an argument is a PEM-formatted public key or certificate

hvac.utils.comma_delimited_to_list(list_param)[source]ยถ

Convert comma-delimited list / string into a list of strings

Parameters

list_param (str | unicode) โ€“ Comma-delimited string

Returns

A list of strings

Return type

list

hvac.utils.deprecated_method(to_be_removed_in_version, new_method=None)[source]ยถ

This is a decorator which can be used to mark methods as deprecated. It will result in a warning being emitted when the function is used.

Parameters
  • to_be_removed_in_version (str) โ€“ Version of this module the decorated method will be removed in.

  • new_method (function) โ€“ Method intended to replace the decorated method. This methodโ€™s docstrings are included in the decorated methodโ€™s docstring.

Returns

Wrapped function that includes a deprecation warning and update docstrings from the replacement method.

Return type

types.FunctionType

hvac.utils.format_url(format_str, *args, **kwargs)[source]ยถ

Creates a URL using the specified format after escaping the provided arguments.

Parameters
  • format_str (str) โ€“ The URL containing replacement fields.

  • kwargs (dict) โ€“ Positional replacement field values.

  • kwargs โ€“ Named replacement field values.

Returns

The formatted URL path with escaped replacement fields.

Return type

str

hvac.utils.generate_method_deprecation_message(to_be_removed_in_version, old_method_name, method_name=None, module_name=None)[source]ยถ

Generate a message to be used when warning about the use of deprecated methods.

Parameters
  • to_be_removed_in_version (str) โ€“ Version of this module the deprecated method will be removed in.

  • old_method_name (str) โ€“ Deprecated method name.

  • method_name (str) โ€“ Method intended to replace the deprecated method indicated. This methodโ€™s docstrings are included in the decorated methodโ€™s docstring.

  • module_name (str) โ€“ Name of the module containing the new method to use.

Returns

Full deprecation warning message for the indicated method.

Return type

str

hvac.utils.generate_property_deprecation_message(to_be_removed_in_version, old_name, new_name, new_attribute, module_name='Client')[source]ยถ

Generate a message to be used when warning about the use of deprecated properties.

Parameters
  • to_be_removed_in_version (str) โ€“ Version of this module the deprecated property will be removed in.

  • old_name (str) โ€“ Deprecated property name.

  • new_name (str) โ€“ Name of the new property name to use.

  • new_attribute (str) โ€“ The new attribute where the new property can be found.

  • module_name (str) โ€“ Name of the module containing the new method to use.

Returns

Full deprecation warning message for the indicated property.

Return type

str

hvac.utils.get_token_from_env()[source]ยถ

Get the token from env var, VAULT_TOKEN. If not set, attempt to get the token from, ~/.vault-token

Returns

The vault token if set, else None

Return type

str | None

hvac.utils.getattr_with_deprecated_properties(obj, item, deprecated_properties)[source]ยถ

Helper method to use in the getattr method of a class with deprecated properties.

Parameters
  • obj (object) โ€“ Instance of the Class containing the deprecated properties in question.

  • item (str) โ€“ Name of the attribute being requested.

  • deprecated_properties (List[dict]) โ€“ List of deprecated properties. Each item in the list is a dict with at least a โ€œto_be_removed_in_versionโ€ and โ€œclient_propertyโ€ key to be used in the displayed deprecation warning.

Returns

The new property indicated where available.

Return type

object

hvac.utils.list_to_comma_delimited(list_param)[source]ยถ

Convert a list of strings into a comma-delimited list / string.

Parameters

list_param (list) โ€“ A list of strings.

Returns

Comma-delimited string.

Return type

str

hvac.utils.raise_for_error(method, url, status_code, message=None, errors=None)[source]ยถ

Helper method to raise exceptions based on the status code of a response received back from Vault.

Parameters
  • method (str) โ€“ HTTP method of a request to Vault.

  • url (str) โ€“ URL of the endpoint requested in Vault.

  • status_code (int) โ€“ Status code received in a response from Vault.

  • message (str) โ€“ Optional message to include in a resulting exception.

  • errors (list | str) โ€“ Optional errors to include in a resulting exception.

Raises

hvac.exceptions.InvalidRequest | hvac.exceptions.Unauthorized | hvac.exceptions.Forbidden | hvac.exceptions.InvalidPath | hvac.exceptions.RateLimitExceeded | hvac.exceptions.InternalServerError | hvac.exceptions.VaultNotInitialized | hvac.exceptions.BadGateway | hvac.exceptions.VaultDown | hvac.exceptions.UnexpectedError

hvac.utils.remove_nones(params)[source]ยถ

Removes None values from optional arguments in a parameter dictionary.

Parameters

params (dict) โ€“ The dictionary of parameters to be filtered.

Returns

A filtered copy of the parameter dictionary.

Return type

dict

hvac.utils.validate_list_of_strings_param(param_name, param_argument)[source]ยถ

Validate that an argument is a list of strings.

Parameters
  • param_name (str | unicode) โ€“ The name of the parameter being validated. Used in any resulting exception messages.

  • param_argument (list) โ€“ The argument to validate.

Returns

True if the argument is validated, False otherwise.

Return type

bool

hvac.utils.validate_pem_format(param_name, param_argument)[source]ยถ

Validate that an argument is a PEM-formatted public key or certificate

Parameters
  • param_name (str | unicode) โ€“ The name of the parameter being validate. Used in any resulting exception messages.

  • param_argument (str | unicode) โ€“ The argument to validate

Returns

True if the argument is validate False otherwise

Return type

bool