| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
# (c) 2024, Martin Chmielewski (@M4rt1nCh) |
| 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 |
|
| 9 |
__metaclass__ = type |
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
module: vault_database_connection_reset |
| 13 |
version_added: 6.2.0 |
| 14 |
author: |
| 15 |
- Martin Chmielewski (@M4rt1nCh) |
| 16 |
short_description: Closes a O(connection_name) and its underlying plugin and restarts it with the configuration stored |
| 17 |
requirements: |
| 18 |
- C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html)) |
| 19 |
- For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements). |
| 20 |
description: |
| 21 |
- L(Resets a Database Connection,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#reset-connection). |
| 22 |
notes: |
| 23 |
- This module always reports C(changed) status because it cannot guarantee idempotence. |
| 24 |
- Use C(changed_when) to control that in cases where the operation is known to not change state. |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: partial |
| 28 |
details: |
| 29 |
- In check mode, a sample response will be returned, but the reset will not be performed in Hashicorp Vault. |
| 30 |
extends_documentation_fragment: |
| 31 |
- community.hashi_vault.attributes |
| 32 |
- community.hashi_vault.attributes.action_group |
| 33 |
- community.hashi_vault.connection |
| 34 |
- community.hashi_vault.auth |
| 35 |
- community.hashi_vault.engine_mount |
| 36 |
options: |
| 37 |
connection_name: |
| 38 |
description: The connection name to be resetted. |
| 39 |
type: str |
| 40 |
required: True |
| 41 |
""" |
| 42 |
|
| 43 |
EXAMPLES = r""" |
| 44 |
- name: Reset a Database Connection with the default mount point |
| 45 |
community.hashi_vault.vault_database_connection_reset: |
| 46 |
url: https://vault:8201 |
| 47 |
auth_method: userpass |
| 48 |
username: '{{ user }}' |
| 49 |
password: '{{ passwd }}' |
| 50 |
connection_name: SomeName |
| 51 |
register: result |
| 52 |
|
| 53 |
- name: Display the result of the operation |
| 54 |
ansible.builtin.debug: |
| 55 |
msg: "{{ result }}" |
| 56 |
|
| 57 |
- name: Reset a Database Connection with a custom mount point |
| 58 |
community.hashi_vault.vault_database_connection_reset: |
| 59 |
url: https://vault:8201 |
| 60 |
auth_method: userpass |
| 61 |
username: '{{ user }}' |
| 62 |
password: '{{ passwd }}' |
| 63 |
engine_mount_point: db1 |
| 64 |
connection_name: SomeName |
| 65 |
register: result |
| 66 |
|
| 67 |
- name: Display the result of the operation |
| 68 |
ansible.builtin.debug: |
| 69 |
msg: "{{ result }}" |
| 70 |
""" |
| 71 |
|
| 72 |
RETURN = r"""""" |
| 73 |
|
| 74 |
import traceback |
| 75 |
|
| 76 |
from ansible.module_utils.common.text.converters import to_text |
| 77 |
|
| 78 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 79 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 80 |
|
| 81 |
|
| 82 |
def run_module(): |
| 83 |
argspec = HashiVaultModule.generate_argspec( |
| 84 |
engine_mount_point=dict(type="str", required=False), |
| 85 |
connection_name=dict(type="str", required=True), |
| 86 |
) |
| 87 |
|
| 88 |
module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True) |
| 89 |
|
| 90 |
if module.check_mode is True: |
| 91 |
module.exit_json(changed=True) |
| 92 |
|
| 93 |
parameters = {} |
| 94 |
engine_mount_point = module.params.get("engine_mount_point", None) |
| 95 |
if engine_mount_point is not None: |
| 96 |
parameters["mount_point"] = engine_mount_point |
| 97 |
parameters["name"] = module.params.get("connection_name") |
| 98 |
|
| 99 |
module.connection_options.process_connection_options() |
| 100 |
client_args = module.connection_options.get_hvac_connection_options() |
| 101 |
client = module.helper.get_vault_client(**client_args) |
| 102 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 103 |
|
| 104 |
try: |
| 105 |
module.authenticator.validate() |
| 106 |
module.authenticator.authenticate(client) |
| 107 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 108 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 109 |
|
| 110 |
try: |
| 111 |
client.secrets.database.reset_connection(**parameters) |
| 112 |
except hvac_exceptions.Forbidden as e: |
| 113 |
module.fail_json( |
| 114 |
msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point |
| 115 |
or "database", |
| 116 |
exception=traceback.format_exc(), |
| 117 |
) |
| 118 |
except hvac_exceptions.InvalidPath as e: |
| 119 |
module.fail_json( |
| 120 |
msg="Invalid or missing path ['%s/reset/%s']." |
| 121 |
% (engine_mount_point or "database", parameters["name"]), |
| 122 |
exception=traceback.format_exc(), |
| 123 |
) |
| 124 |
else: |
| 125 |
module.exit_json(changed=True) |
| 126 |
|
| 127 |
|
| 128 |
def main(): |
| 129 |
run_module() |
| 130 |
|
| 131 |
|
| 132 |
if __name__ == "__main__": |
| 133 |
main() |