Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/modules/vault_database_rotate_root_credentials.py

raw
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_rotate_root_credentials
13 version_added: 6.2.0
14 author:
15 - Martin Chmielewski (@M4rt1nCh)
16 short_description: Rotates the root credentials stored for the database connection. This user must have permissions to update its own password.
17 requirements:
18 - C(hvac) (L(Python library,https://hvac.readthedocs.io/en/stable/overview.html)) >= 2.0.0
19 - For detailed requirements, see R(the collection requirements page,ansible_collections.community.hashi_vault.docsite.user_guide.requirements).
20 description:
21 - Trigger L(root credential rotation,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#rotate-root-credentials)
22 of a Database Connection identified by its O(connection_name).
23 notes:
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, a sample response will be returned, but the credential rotation will not be performed in Hashicorp Vault.
31 extends_documentation_fragment:
32 - community.hashi_vault.attributes
33 - community.hashi_vault.attributes.action_group
34 - community.hashi_vault.connection
35 - community.hashi_vault.auth
36 - community.hashi_vault.engine_mount
37 options:
38 connection_name:
39 description: The connection name where the root credential rotation should be triggered.
40 type: str
41 required: True
42 """
43
44 EXAMPLES = r"""
45 - name: Trigger root credentiaL rotation with the default mount point
46 community.hashi_vault.vault_database_rotate_root_credentials:
47 url: https://vault:8201
48 auth_method: userpass
49 username: '{{ user }}'
50 password: '{{ passwd }}'
51 connection_name: SomeName
52 register: result
53
54 - name: Display the result of the operation
55 ansible.builtin.debug:
56 msg: "{{ result }}"
57
58 - name: Trigger root credential rotation with the default mount point
59 community.hashi_vault.vault_database_rotate_root_credentials:
60 url: https://vault:8201
61 auth_method: userpass
62 username: '{{ user }}'
63 password: '{{ passwd }}'
64 engine_mount_point: db1
65 connection_name: SomeName
66 register: result
67
68 - name: Display the result of the operation
69 ansible.builtin.debug:
70 msg: "{{ result }}"
71 """
72
73 RETURN = r""""""
74
75 import traceback
76
77 from ansible.module_utils.common.text.converters import to_text
78
79 from ..module_utils._hashi_vault_module import HashiVaultModule
80 from ..module_utils._hashi_vault_common import HashiVaultValueError
81
82
83 def run_module():
84 argspec = HashiVaultModule.generate_argspec(
85 engine_mount_point=dict(type="str", required=False),
86 connection_name=dict(type="str", required=True),
87 )
88
89 module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True)
90
91 if module.check_mode is True:
92 module.exit_json(changed=True)
93
94 parameters = {}
95 engine_mount_point = module.params.get("engine_mount_point", None)
96 if engine_mount_point is not None:
97 parameters["mount_point"] = engine_mount_point
98 parameters["name"] = module.params.get("connection_name")
99
100 module.connection_options.process_connection_options()
101 client_args = module.connection_options.get_hvac_connection_options()
102 client = module.helper.get_vault_client(**client_args)
103 hvac_exceptions = module.helper.get_hvac().exceptions
104
105 try:
106 module.authenticator.validate()
107 module.authenticator.authenticate(client)
108 except (NotImplementedError, HashiVaultValueError) as e:
109 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
110
111 try:
112 client.secrets.database.rotate_root_credentials(**parameters)
113 except AttributeError as e:
114 module.fail_json(
115 msg="hvac>=2.0.0 is required", exception=traceback.format_exc()
116 )
117 except hvac_exceptions.Forbidden as e:
118 module.fail_json(
119 msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point
120 or "database",
121 exception=traceback.format_exc(),
122 )
123 except hvac_exceptions.InvalidPath as e:
124 module.fail_json(
125 msg="Invalid or missing path ['%s/rotate-root/%s']"
126 % (engine_mount_point or "database", parameters["name"]),
127 exception=traceback.format_exc(),
128 )
129 else:
130 module.exit_json(changed=True)
131
132
133 def main():
134 run_module()
135
136
137 if __name__ == "__main__":
138 main()