| 1 |
# (c) 2022, Brian Scholer (@briantist) |
| 2 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 3 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 4 |
|
| 5 |
from __future__ import (absolute_import, division, print_function) |
| 6 |
__metaclass__ = type |
| 7 |
|
| 8 |
DOCUMENTATION = """ |
| 9 |
name: vault_write |
| 10 |
version_added: 2.4.0 |
| 11 |
author: |
| 12 |
- Brian Scholer (@briantist) |
| 13 |
short_description: Perform a write operation against HashiCorp Vault |
| 14 |
requirements: |
| 15 |
- C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html)) |
| 16 |
- For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements). |
| 17 |
description: |
| 18 |
- Performs a generic write operation against a given path in HashiCorp Vault, returning any output. |
| 19 |
seealso: |
| 20 |
- module: community.hashi_vault.vault_write |
| 21 |
- module: community.hashi_vault.vault_kv2_write |
| 22 |
- ref: community.hashi_vault.vault_read lookup <ansible_collections.community.hashi_vault.vault_read_lookup> |
| 23 |
description: The official documentation for the C(community.hashi_vault.vault_read) lookup plugin. |
| 24 |
- module: community.hashi_vault.vault_read |
| 25 |
- ref: community.hashi_vault Lookup Guide <ansible_collections.community.hashi_vault.docsite.lookup_guide> |
| 26 |
description: Guidance on using lookups in C(community.hashi_vault). |
| 27 |
notes: |
| 28 |
- C(vault_write) is a generic plugin to do operations that do not yet have a dedicated plugin. Where a specific plugin exists, that should be used instead. |
| 29 |
- In the vast majority of cases, it will be better to do writes as a task, with the M(community.hashi_vault.vault_write) module. |
| 30 |
- The lookup can be used in cases where you need a value directly in templating, but there is risk of executing the write many times unintentionally. |
| 31 |
- The lookup is best used for endpoints that directly manipulate the input data and return a value, while not changing state in Vault. |
| 32 |
- See the R(Lookup Guide,ansible_collections.community.hashi_vault.docsite.lookup_guide) for more information. |
| 33 |
extends_documentation_fragment: |
| 34 |
- community.hashi_vault.connection |
| 35 |
- community.hashi_vault.connection.plugins |
| 36 |
- community.hashi_vault.auth |
| 37 |
- community.hashi_vault.auth.plugins |
| 38 |
- community.hashi_vault.wrapping |
| 39 |
- community.hashi_vault.wrapping.plugins |
| 40 |
options: |
| 41 |
_terms: |
| 42 |
description: Vault path(s) to be written to. |
| 43 |
type: str |
| 44 |
required: true |
| 45 |
data: |
| 46 |
description: |
| 47 |
- A dictionary to be serialized to JSON and then sent as the request body. |
| 48 |
- If the dictionary contains keys named C(path) or C(wrap_ttl), the call will fail with C(hvac<1.2). |
| 49 |
type: dict |
| 50 |
required: false |
| 51 |
default: {} |
| 52 |
""" |
| 53 |
|
| 54 |
EXAMPLES = """ |
| 55 |
# These examples show some uses that might work well as a lookup. |
| 56 |
# For most uses, the vault_write module should be used. |
| 57 |
|
| 58 |
- name: Retrieve and display random data |
| 59 |
vars: |
| 60 |
data: |
| 61 |
format: hex |
| 62 |
num_bytes: 64 |
| 63 |
ansible.builtin.debug: |
| 64 |
msg: "{{ lookup('community.hashi_vault.vault_write', 'sys/tools/random/' ~ num_bytes, data=data) }}" |
| 65 |
|
| 66 |
- name: Hash some data and display the hash |
| 67 |
vars: |
| 68 |
input: | |
| 69 |
Lorem ipsum dolor sit amet, consectetur adipiscing elit. |
| 70 |
Pellentesque posuere dui a ipsum dapibus, et placerat nibh bibendum. |
| 71 |
data: |
| 72 |
input: '{{ input | b64encode }}' |
| 73 |
hash_algo: sha2-256 |
| 74 |
ansible.builtin.debug: |
| 75 |
msg: "The hash is {{ lookup('community.hashi_vault.vault_write', 'sys/tools/hash/' ~ hash_algo, data=data) }}" |
| 76 |
|
| 77 |
|
| 78 |
# In this next example, the Ansible controller's token does not have permission to read the secrets we need. |
| 79 |
# It does have permission to generate new secret IDs for an approle which has permission to read the secrets, |
| 80 |
# however the approle is configured to: |
| 81 |
# 1) allow a maximum of 1 use per secret ID |
| 82 |
# 2) restrict the IPs allowed to use login using the approle to those of the remote hosts |
| 83 |
# |
| 84 |
# Normally, the fact that a new secret ID would be generated on every loop iteration would not be desirable, |
| 85 |
# but here it's quite convenient. |
| 86 |
|
| 87 |
- name: Retrieve secrets from the remote host with one-time-use approle creds |
| 88 |
vars: |
| 89 |
role_id: "{{ lookup('community.hashi_vault.vault_read', 'auth/approle/role/role-name/role-id') }}" |
| 90 |
secret_id: "{{ lookup('community.hashi_vault.vault_write', 'auth/approle/role/role-name/secret-id') }}" |
| 91 |
community.hashi_vault.vault_read: |
| 92 |
auth_method: approle |
| 93 |
role_id: '{{ role_id }}' |
| 94 |
secret_id: '{{ secret_id }}' |
| 95 |
path: '{{ item }}' |
| 96 |
register: secret_data |
| 97 |
loop: |
| 98 |
- secret/data/secret1 |
| 99 |
- secret/data/app/deploy-key |
| 100 |
- secret/data/access-codes/self-destruct |
| 101 |
|
| 102 |
|
| 103 |
# This time we have a secret values on the controller, and we need to run a command the remote host, |
| 104 |
# that is expecting to a use single-use token as input, so we need to use wrapping to send the data. |
| 105 |
|
| 106 |
- name: Run a command that needs wrapped secrets |
| 107 |
vars: |
| 108 |
secrets: |
| 109 |
secret1: '{{ my_secret_1 }}' |
| 110 |
secret2: '{{ second_secret }}' |
| 111 |
wrapped: "{{ lookup('community.hashi_vault.vault_write', 'sys/wrapping/wrap', data=secrets) }}" |
| 112 |
ansible.builtin.command: 'vault unwrap {{ wrapped }}' |
| 113 |
""" |
| 114 |
|
| 115 |
RETURN = """ |
| 116 |
_raw: |
| 117 |
description: The raw result of the write against the given path. |
| 118 |
type: list |
| 119 |
elements: dict |
| 120 |
""" |
| 121 |
|
| 122 |
from ansible.errors import AnsibleError |
| 123 |
from ansible.utils.display import Display |
| 124 |
|
| 125 |
from ..plugin_utils._hashi_vault_lookup_base import HashiVaultLookupBase |
| 126 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 127 |
|
| 128 |
display = Display() |
| 129 |
|
| 130 |
|
| 131 |
class LookupModule(HashiVaultLookupBase): |
| 132 |
def run(self, terms, variables=None, **kwargs): |
| 133 |
ret = [] |
| 134 |
|
| 135 |
self.set_options(direct=kwargs, var_options=variables) |
| 136 |
# TODO: remove process_deprecations() if backported fix is available (see method definition) |
| 137 |
self.process_deprecations() |
| 138 |
|
| 139 |
self.connection_options.process_connection_options() |
| 140 |
client_args = self.connection_options.get_hvac_connection_options() |
| 141 |
client = self.helper.get_vault_client(**client_args) |
| 142 |
hvac_exceptions = self.helper.get_hvac().exceptions |
| 143 |
|
| 144 |
data = self._options_adapter.get_option('data') |
| 145 |
wrap_ttl = self._options_adapter.get_option_default('wrap_ttl') |
| 146 |
|
| 147 |
try: |
| 148 |
self.authenticator.validate() |
| 149 |
self.authenticator.authenticate(client) |
| 150 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 151 |
raise AnsibleError(e) from e |
| 152 |
|
| 153 |
for term in terms: |
| 154 |
try: |
| 155 |
try: |
| 156 |
# TODO: write_data will eventually turn back into write |
| 157 |
# see: https://github.com/hvac/hvac/issues/1034 |
| 158 |
response = client.write_data(path=term, wrap_ttl=wrap_ttl, data=data) |
| 159 |
except AttributeError as e: |
| 160 |
# https://github.com/ansible-collections/community.hashi_vault/issues/389 |
| 161 |
if "path" in data or "wrap_ttl" in data: |
| 162 |
raise AnsibleError("To use 'path' or 'wrap_ttl' as data keys, use hvac >= 1.2") from e |
| 163 |
else: |
| 164 |
response = client.write(path=term, wrap_ttl=wrap_ttl, **data) |
| 165 |
except hvac_exceptions.Forbidden as e: |
| 166 |
raise AnsibleError("Forbidden: Permission Denied to path '%s'." % term) from e |
| 167 |
except hvac_exceptions.InvalidPath as e: |
| 168 |
raise AnsibleError("The path '%s' doesn't seem to exist." % term) from e |
| 169 |
except hvac_exceptions.InternalServerError as e: |
| 170 |
raise AnsibleError("Internal Server Error: %s" % str(e)) from e |
| 171 |
|
| 172 |
# https://github.com/hvac/hvac/issues/797 |
| 173 |
# HVAC returns a raw response object when the body is not JSON. |
| 174 |
# That includes 204 responses, which are successful with no body. |
| 175 |
# So we will try to detect that and a act accordingly. |
| 176 |
# A better way may be to implement our own adapter for this |
| 177 |
# collection, but it's a little premature to do that. |
| 178 |
if hasattr(response, 'json') and callable(response.json): |
| 179 |
if response.status_code == 204: |
| 180 |
output = {} |
| 181 |
else: |
| 182 |
display.warning('Vault returned status code %i and an unparsable body.' % response.status_code) |
| 183 |
output = response.content |
| 184 |
else: |
| 185 |
output = response |
| 186 |
|
| 187 |
ret.append(output) |
| 188 |
|
| 189 |
return ret |