| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2022, Brian Scholer (@briantist) |
| 4 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 6 |
|
| 7 |
from __future__ import (absolute_import, division, print_function) |
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
DOCUMENTATION = """ |
| 11 |
module: vault_write |
| 12 |
version_added: 2.4.0 |
| 13 |
author: |
| 14 |
- Brian Scholer (@briantist) |
| 15 |
short_description: Perform a write operation against HashiCorp Vault |
| 16 |
requirements: |
| 17 |
- C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html)) |
| 18 |
- For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements). |
| 19 |
description: |
| 20 |
- Performs a generic write operation against a given path in HashiCorp Vault, returning any output. |
| 21 |
notes: |
| 22 |
- C(vault_write) is a generic module to do operations that do not yet have a dedicated module. Where a specific module exists, that should be used instead. |
| 23 |
- The I(data) option is not treated as secret and may be logged. Use the C(no_log) keyword if I(data) contains sensitive values. |
| 24 |
- This module always reports C(changed) status because it cannot guarantee idempotence. |
| 25 |
- Use C(changed_when) to control that in cases where the operation is known to not change state. |
| 26 |
attributes: |
| 27 |
check_mode: |
| 28 |
support: partial |
| 29 |
details: |
| 30 |
- In check mode, an empty response will be returned and the write will not be performed. |
| 31 |
seealso: |
| 32 |
- ref: community.hashi_vault.vault_write lookup <ansible_collections.community.hashi_vault.vault_write_lookup> |
| 33 |
description: The official documentation for the C(community.hashi_vault.vault_write) lookup plugin. |
| 34 |
- module: community.hashi_vault.vault_read |
| 35 |
- ref: community.hashi_vault.vault_read lookup <ansible_collections.community.hashi_vault.vault_read_lookup> |
| 36 |
description: The official documentation for the C(community.hashi_vault.vault_read) lookup plugin. |
| 37 |
extends_documentation_fragment: |
| 38 |
- community.hashi_vault.attributes |
| 39 |
- community.hashi_vault.attributes.action_group |
| 40 |
- community.hashi_vault.connection |
| 41 |
- community.hashi_vault.auth |
| 42 |
- community.hashi_vault.wrapping |
| 43 |
options: |
| 44 |
path: |
| 45 |
description: Vault path to be written to. |
| 46 |
type: str |
| 47 |
required: True |
| 48 |
data: |
| 49 |
description: |
| 50 |
- A dictionary to be serialized to JSON and then sent as the request body. |
| 51 |
- If the dictionary contains keys named C(path) or C(wrap_ttl), the call will fail with C(hvac<1.2). |
| 52 |
type: dict |
| 53 |
required: false |
| 54 |
default: {} |
| 55 |
""" |
| 56 |
|
| 57 |
EXAMPLES = """ |
| 58 |
- name: Write a value to the cubbyhole via the remote host with userpass auth |
| 59 |
community.hashi_vault.vault_write: |
| 60 |
url: https://vault:8201 |
| 61 |
path: cubbyhole/mysecret |
| 62 |
data: |
| 63 |
key1: val1 |
| 64 |
key2: val2 |
| 65 |
auth_method: userpass |
| 66 |
username: user |
| 67 |
password: '{{ passwd }}' |
| 68 |
register: result |
| 69 |
|
| 70 |
- name: Display the result of the write (this can be empty) |
| 71 |
ansible.builtin.debug: |
| 72 |
msg: "{{ result.data }}" |
| 73 |
|
| 74 |
- name: Write secret to Vault using key value V2 engine |
| 75 |
community.hashi_vault.vault_write: |
| 76 |
path: secret/data/mysecret |
| 77 |
data: |
| 78 |
data: |
| 79 |
key1: val1 |
| 80 |
key2: val2 |
| 81 |
|
| 82 |
- name: Retrieve an approle role ID from Vault via the remote host |
| 83 |
community.hashi_vault.vault_read: |
| 84 |
url: https://vault:8201 |
| 85 |
path: auth/approle/role/role-name/role-id |
| 86 |
register: approle_id |
| 87 |
|
| 88 |
- name: Generate a secret-id for the given approle |
| 89 |
community.hashi_vault.vault_write: |
| 90 |
url: https://vault:8201 |
| 91 |
path: auth/approle/role/role-name/secret-id |
| 92 |
register: secret_id |
| 93 |
|
| 94 |
- name: Display the role ID and secret ID |
| 95 |
ansible.builtin.debug: |
| 96 |
msg: |
| 97 |
- "role-id: {{ approle_id.data.data.role_id }}" |
| 98 |
- "secret-id: {{ secret_id.data.data.secret_id }}" |
| 99 |
""" |
| 100 |
|
| 101 |
RETURN = """ |
| 102 |
data: |
| 103 |
description: The raw result of the write against the given path. |
| 104 |
returned: success |
| 105 |
type: dict |
| 106 |
""" |
| 107 |
|
| 108 |
import traceback |
| 109 |
|
| 110 |
from ansible.module_utils.common.text.converters import to_text |
| 111 |
|
| 112 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 113 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 114 |
|
| 115 |
|
| 116 |
def run_module(): |
| 117 |
argspec = HashiVaultModule.generate_argspec( |
| 118 |
path=dict(type='str', required=True), |
| 119 |
data=dict(type='dict', required=False, default={}), |
| 120 |
wrap_ttl=dict(type='str'), |
| 121 |
) |
| 122 |
|
| 123 |
module = HashiVaultModule( |
| 124 |
argument_spec=argspec, |
| 125 |
supports_check_mode=True |
| 126 |
) |
| 127 |
|
| 128 |
path = module.params.get('path') |
| 129 |
data = module.params.get('data') |
| 130 |
wrap_ttl = module.params.get('wrap_ttl') |
| 131 |
|
| 132 |
module.connection_options.process_connection_options() |
| 133 |
client_args = module.connection_options.get_hvac_connection_options() |
| 134 |
client = module.helper.get_vault_client(**client_args) |
| 135 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 136 |
|
| 137 |
try: |
| 138 |
module.authenticator.validate() |
| 139 |
module.authenticator.authenticate(client) |
| 140 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 141 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 142 |
|
| 143 |
try: |
| 144 |
if module.check_mode: |
| 145 |
response = {} |
| 146 |
else: |
| 147 |
try: |
| 148 |
# TODO: write_data will eventually turn back into write |
| 149 |
# see: https://github.com/hvac/hvac/issues/1034 |
| 150 |
response = client.write_data(path=path, wrap_ttl=wrap_ttl, data=data) |
| 151 |
except AttributeError: |
| 152 |
# https://github.com/ansible-collections/community.hashi_vault/issues/389 |
| 153 |
if "path" in data or "wrap_ttl" in data: |
| 154 |
module.fail_json("To use 'path' or 'wrap_ttl' as data keys, use hvac >= 1.2") |
| 155 |
else: |
| 156 |
response = client.write(path=path, wrap_ttl=wrap_ttl, **data) |
| 157 |
except hvac_exceptions.Forbidden: |
| 158 |
module.fail_json(msg="Forbidden: Permission Denied to path '%s'." % path, exception=traceback.format_exc()) |
| 159 |
except hvac_exceptions.InvalidPath: |
| 160 |
module.fail_json(msg="The path '%s' doesn't seem to exist." % path, exception=traceback.format_exc()) |
| 161 |
except hvac_exceptions.InternalServerError as e: |
| 162 |
module.fail_json(msg="Internal Server Error: %s" % to_text(e), exception=traceback.format_exc()) |
| 163 |
|
| 164 |
# https://github.com/hvac/hvac/issues/797 |
| 165 |
# HVAC returns a raw response object when the body is not JSON. |
| 166 |
# That includes 204 responses, which are successful with no body. |
| 167 |
# So we will try to detect that and a act accordingly. |
| 168 |
# A better way may be to implement our own adapter for this |
| 169 |
# collection, but it's a little premature to do that. |
| 170 |
if hasattr(response, 'json') and callable(response.json): |
| 171 |
if response.status_code == 204: |
| 172 |
output = {} |
| 173 |
else: |
| 174 |
module.warn('Vault returned status code %i and an unparsable body.' % response.status_code) |
| 175 |
output = response.content |
| 176 |
else: |
| 177 |
output = response |
| 178 |
|
| 179 |
module.exit_json(changed=True, data=output) |
| 180 |
|
| 181 |
|
| 182 |
def main(): |
| 183 |
run_module() |
| 184 |
|
| 185 |
|
| 186 |
if __name__ == '__main__': |
| 187 |
main() |