Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/modules/vault_database_static_role_read.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_static_role_read
13 version_added: 6.2.0
14 author:
15 - Martin Chmielewski (@M4rt1nCh)
16 short_description: Queries a static role definition
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(Queries a static role definition,L(reads a static role,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#read-static-role),
22 - identified by its O(role_name)
23 notes:
24 - This module always reports C(changed) as False as it is a read operation that doesn't modify data.
25 - Use C(changed_when) to control that in cases where the operation is known to not change state.
26 extends_documentation_fragment:
27 - community.hashi_vault.attributes
28 - community.hashi_vault.attributes.action_group
29 - community.hashi_vault.attributes.check_mode_read_only
30 - community.hashi_vault.connection
31 - community.hashi_vault.auth
32 - community.hashi_vault.engine_mount
33 options:
34 role_name:
35 description: The role name to be read from Hashicorp Vault.
36 type: str
37 required: True
38 """
39
40 EXAMPLES = r"""
41 - name: Read Static Role with a default mount point
42 community.hashi_vault.vault_database_static_role_read:
43 url: https://vault:8201
44 auth_method: userpass
45 username: '{{ user }}'
46 password: '{{ passwd }}'
47 role_name: SomeRole
48 register: result
49
50 - name: Display the result of the operation
51 ansible.builtin.debug:
52 msg: "{{ result }}"
53
54 - name: Read Static Role with a custom moint point
55 community.hashi_vault.vault_database_static_role_read:
56 url: https://vault:8201
57 auth_method: userpass
58 username: '{{ user }}'
59 password: '{{ passwd }}'
60 engine_mount_point: db1
61 role_name: SomeRole
62 register: result
63
64 - name: Display the result of the operation
65 ansible.builtin.debug:
66 msg: "{{ result }}"
67
68 """
69
70 RETURN = r"""
71 data: &data
72 description: The C(data) field of raw result. This can also be accessed via RV(raw.data).
73 returned: success
74 type: dict
75 sample: &data_sample
76 credential_type: "password"
77 db_name: "SomeConnection"
78 last_vault_rotation": "2024-01-01T09:00:00 +01:00"
79 rotation_period": 86400
80 rotation_statements": [
81 "ALTER USER \"{{name}}\" WITH PASSWORD '{{password}}';"
82 ]
83 raw:
84 description: The raw result of the operation.
85 returned: success
86 type: dict
87 contains:
88 data: *data
89 sample:
90 auth: null
91 data: *data_sample
92 username: "SomeUser"
93 lease_duration": 0
94 lease_id: ""
95 renewable: false
96 request_id: "123456"
97 warnings: null
98 wrap_info: null
99 """
100
101 import traceback
102
103 from ansible.module_utils.common.text.converters import to_text
104
105 from ..module_utils._hashi_vault_module import HashiVaultModule
106 from ..module_utils._hashi_vault_common import HashiVaultValueError
107
108
109 def run_module():
110 argspec = HashiVaultModule.generate_argspec(
111 engine_mount_point=dict(type="str", required=False),
112 role_name=dict(type="str", required=True),
113 )
114
115 module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True)
116
117 parameters = {}
118 engine_mount_point = module.params.get("engine_mount_point", None)
119 if engine_mount_point is not None:
120 parameters["mount_point"] = engine_mount_point
121 parameters["name"] = module.params.get("role_name")
122
123 module.connection_options.process_connection_options()
124 client_args = module.connection_options.get_hvac_connection_options()
125 client = module.helper.get_vault_client(**client_args)
126 hvac_exceptions = module.helper.get_hvac().exceptions
127
128 try:
129 module.authenticator.validate()
130 module.authenticator.authenticate(client)
131 except (NotImplementedError, HashiVaultValueError) as e:
132 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
133
134 try:
135 raw = client.secrets.database.read_static_role(**parameters)
136 except hvac_exceptions.Forbidden as e:
137 module.fail_json(
138 msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point
139 or "database",
140 exception=traceback.format_exc(),
141 )
142 except hvac_exceptions.InvalidPath as e:
143 module.fail_json(
144 msg="Invalid or missing path ['%s/static-roles/%s']."
145 % (engine_mount_point or "database", parameters["name"]),
146 exception=traceback.format_exc(),
147 )
148
149 data = raw["data"]
150
151 module.exit_json(data=data, raw=raw, changed=False)
152
153
154 def main():
155 run_module()
156
157
158 if __name__ == "__main__":
159 main()