| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2022, Isaac Wagner (@idwagner) |
| 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_kv2_delete |
| 12 |
version_added: 3.4.0 |
| 13 |
author: |
| 14 |
- Isaac Wagner (@idwagner) |
| 15 |
short_description: Delete one or more versions of a secret from HashiCorp Vault's KV version 2 secret store |
| 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 |
- Delete one or more versions of a secret from HashiCorp Vault's KV version 2 secret store. |
| 21 |
notes: |
| 22 |
- This module always reports C(changed) status because it cannot guarantee idempotence. |
| 23 |
- Use C(changed_when) to control that in cases where the operation is known to not change state. |
| 24 |
attributes: |
| 25 |
check_mode: |
| 26 |
support: partial |
| 27 |
details: |
| 28 |
- In check mode, the module returns C(changed) status without contacting Vault. |
| 29 |
- Consider using M(community.hashi_vault.vault_kv2_get) to verify the existence of the secret first. |
| 30 |
seealso: |
| 31 |
- module: community.hashi_vault.vault_kv2_get |
| 32 |
- module: community.hashi_vault.vault_kv2_write |
| 33 |
- name: KV2 Secrets Engine |
| 34 |
description: Documentation for the Vault KV secrets engine, version 2. |
| 35 |
link: https://www.vaultproject.io/docs/secrets/kv/kv-v2 |
| 36 |
extends_documentation_fragment: |
| 37 |
- community.hashi_vault.attributes |
| 38 |
- community.hashi_vault.attributes.action_group |
| 39 |
- community.hashi_vault.connection |
| 40 |
- community.hashi_vault.auth |
| 41 |
- community.hashi_vault.engine_mount |
| 42 |
options: |
| 43 |
engine_mount_point: |
| 44 |
default: secret |
| 45 |
path: |
| 46 |
description: |
| 47 |
- Vault KV path to be deleted. |
| 48 |
- This is relative to the I(engine_mount_point), so the mount path should not be included. |
| 49 |
- For kv2, do not include C(/data/) or C(/metadata/). |
| 50 |
type: str |
| 51 |
required: True |
| 52 |
versions: |
| 53 |
description: |
| 54 |
- One or more versions of the secret to delete. |
| 55 |
- When omitted, the latest version of the secret is deleted. |
| 56 |
type: list |
| 57 |
elements: int |
| 58 |
required: False |
| 59 |
''' |
| 60 |
|
| 61 |
EXAMPLES = """ |
| 62 |
- name: Delete the latest version of the secret/mysecret secret. |
| 63 |
community.hashi_vault.vault_kv2_delete: |
| 64 |
url: https://vault:8201 |
| 65 |
path: secret/mysecret |
| 66 |
auth_method: userpass |
| 67 |
username: user |
| 68 |
password: '{{ passwd }}' |
| 69 |
register: result |
| 70 |
|
| 71 |
- name: Delete versions 1 and 3 of the secret/mysecret secret. |
| 72 |
community.hashi_vault.vault_kv2_delete: |
| 73 |
url: https://vault:8201 |
| 74 |
path: secret/mysecret |
| 75 |
versions: [1, 3] |
| 76 |
auth_method: userpass |
| 77 |
username: user |
| 78 |
password: '{{ passwd }}' |
| 79 |
""" |
| 80 |
|
| 81 |
RETURN = """ |
| 82 |
data: |
| 83 |
description: |
| 84 |
- The raw result of the delete against the given path. |
| 85 |
- This is usually empty, but may contain warnings or other information. |
| 86 |
returned: success |
| 87 |
type: dict |
| 88 |
""" |
| 89 |
|
| 90 |
import traceback |
| 91 |
|
| 92 |
from ansible.module_utils.common.text.converters import to_text |
| 93 |
|
| 94 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_module import HashiVaultModule |
| 95 |
from ansible_collections.community.hashi_vault.plugins.module_utils._hashi_vault_common import HashiVaultValueError |
| 96 |
|
| 97 |
|
| 98 |
def run_module(): |
| 99 |
|
| 100 |
argspec = HashiVaultModule.generate_argspec( |
| 101 |
engine_mount_point=dict(type='str', default='secret'), |
| 102 |
path=dict(type='str', required=True), |
| 103 |
versions=dict(type='list', elements='int', required=False) |
| 104 |
) |
| 105 |
|
| 106 |
module = HashiVaultModule( |
| 107 |
argument_spec=argspec, |
| 108 |
supports_check_mode=True |
| 109 |
) |
| 110 |
|
| 111 |
engine_mount_point = module.params.get('engine_mount_point') |
| 112 |
path = module.params.get('path') |
| 113 |
versions = module.params.get('versions') |
| 114 |
|
| 115 |
module.connection_options.process_connection_options() |
| 116 |
client_args = module.connection_options.get_hvac_connection_options() |
| 117 |
client = module.helper.get_vault_client(**client_args) |
| 118 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 119 |
|
| 120 |
try: |
| 121 |
module.authenticator.validate() |
| 122 |
module.authenticator.authenticate(client) |
| 123 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 124 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 125 |
|
| 126 |
try: |
| 127 |
# Vault has two separate methods, one for delete latest version, |
| 128 |
# and delete specific versions. |
| 129 |
if module.check_mode: |
| 130 |
response = {} |
| 131 |
elif not versions: |
| 132 |
response = client.secrets.kv.v2.delete_latest_version_of_secret( |
| 133 |
path=path, mount_point=engine_mount_point) |
| 134 |
else: |
| 135 |
response = client.secrets.kv.v2.delete_secret_versions( |
| 136 |
path=path, versions=versions, mount_point=engine_mount_point) |
| 137 |
|
| 138 |
except hvac_exceptions.Forbidden as e: |
| 139 |
module.fail_json(msg="Forbidden: Permission Denied to path ['%s']." % path, exception=traceback.format_exc()) |
| 140 |
|
| 141 |
# https://github.com/hvac/hvac/issues/797 |
| 142 |
# HVAC returns a raw response object when the body is not JSON. |
| 143 |
# That includes 204 responses, which are successful with no body. |
| 144 |
# So we will try to detect that and a act accordingly. |
| 145 |
# A better way may be to implement our own adapter for this |
| 146 |
# collection, but it's a little premature to do that. |
| 147 |
if hasattr(response, 'json') and callable(response.json): |
| 148 |
if response.status_code == 204: |
| 149 |
output = {} |
| 150 |
else: |
| 151 |
module.warn( |
| 152 |
'Vault returned status code %i and an unparsable body.' % response.status_code) |
| 153 |
output = response.content |
| 154 |
else: |
| 155 |
output = response |
| 156 |
|
| 157 |
module.exit_json(changed=True, data=output) |
| 158 |
|
| 159 |
|
| 160 |
def main(): |
| 161 |
run_module() |
| 162 |
|
| 163 |
|
| 164 |
if __name__ == '__main__': |
| 165 |
main() |