Debian Codegraph @

ansible_collections/community/hashi_vault/plugins/modules/vault_database_connection_configure.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_connection_configure
13 version_added: 6.2.0
14 author:
15 - Martin Chmielewski (@M4rt1nCh)
16 short_description: Configures the database engine
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 - Creates a L(new database connection for a database secrets engine,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#configuration),
22 identified by its O(engine_mount_point) in HashiCorp Vault.
23 notes:
24 - The database needs to be created and available to connect before you can configure the database secrets engine using the above configure method.
25 - This module always reports C(changed) status because it cannot guarantee idempotence.
26 - Use C(changed_when) to control that in cases where the operation is known to not change state.
27 attributes:
28 check_mode:
29 support: partial
30 details:
31 - In check mode, a sample response will be returned, but the create / update will not be performed in Hashicorp Vault.
32 extends_documentation_fragment:
33 - community.hashi_vault.attributes
34 - community.hashi_vault.attributes.action_group
35 - community.hashi_vault.connection
36 - community.hashi_vault.auth
37 - community.hashi_vault.engine_mount
38 options:
39 connection_name:
40 description: Name of the database connection.
41 type: str
42 required: True
43 plugin_name:
44 description: Plugin name used to connect to the database
45 type: str
46 required: True
47 allowed_roles:
48 description: Allowed roles
49 type: list
50 elements: str
51 required: True
52 connection_url:
53 description: Connection URL to the database
54 type: str
55 required: True
56 connection_username:
57 description: Username to connect to the database
58 type: str
59 required: True
60 connection_password:
61 description: Password to connect to the database
62 type: str
63 required: True
64 """
65
66 EXAMPLES = r"""
67 - name: Create a new Database Connection with the default mount point
68 community.hashi_vault.vault_database_connection_configure:
69 url: https://vault:8201
70 auth_method: userpass
71 username: '{{ user }}'
72 password: '{{ passwd }}'
73 connection_name: MyName
74 plugin_name: postgresql-database-plugin
75 connection_url: postgresql://{{'{{username}}'}}:{{'{{password}}'}}@postgres:5432/postgres?sslmode=disable
76 connection_username: SomeUser
77 connection_password: SomePass
78 register: result
79
80 - name: Display the result of the operation
81 ansible.builtin.debug:
82 msg: "{{ result }}"
83
84
85 - name: Create a new Database Connection with a custom mount point
86 community.hashi_vault.vault_database_connection_configure:
87 url: https://vault:8201
88 auth_method: userpass
89 username: '{{ user }}'
90 password: '{{ passwd }}'
91 engine_mount_point: db1
92 connection_name: MyName
93 plugin_name: postgresql-database-plugin
94 connection_url: postgresql://{{'{{username}}'}}:{{'{{password}}'}}@postgres:5432/postgres?sslmode=disable
95 connection_username: SomeUser
96 connection_password: SomePass
97 register: result
98
99 - name: Display the result of the operation
100 ansible.builtin.debug:
101 msg: "{{ result }}"
102 """
103
104 RETURN = r""""""
105
106 import traceback
107
108 from ansible.module_utils.common.text.converters import to_text
109
110 from ..module_utils._hashi_vault_module import HashiVaultModule
111 from ..module_utils._hashi_vault_common import HashiVaultValueError
112
113
114 def run_module():
115 argspec = HashiVaultModule.generate_argspec(
116 engine_mount_point=dict(type="str", required=False),
117 plugin_name=dict(type="str", required=True),
118 allowed_roles=dict(type="list", required=True, elements="str"),
119 connection_name=dict(type="str", required=True),
120 connection_url=dict(type="str", required=True),
121 connection_username=dict(type="str", required=True),
122 connection_password=dict(type="str", required=True, no_log=True),
123 )
124
125 module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True)
126
127 if module.check_mode is True:
128 module.exit_json(changed=True)
129
130 parameters = {}
131 engine_mount_point = module.params.get("engine_mount_point", None)
132 if engine_mount_point is not None:
133 parameters["mount_point"] = engine_mount_point
134 parameters["plugin_name"] = module.params.get("plugin_name")
135 parameters["allowed_roles"] = module.params.get("allowed_roles")
136 parameters["connection_url"] = module.params.get("connection_url")
137 parameters["name"] = module.params.get("connection_name")
138 parameters["username"] = module.params.get("connection_username")
139 parameters["password"] = module.params.get("connection_password")
140
141 module.connection_options.process_connection_options()
142 client_args = module.connection_options.get_hvac_connection_options()
143 client = module.helper.get_vault_client(**client_args)
144 hvac_exceptions = module.helper.get_hvac().exceptions
145
146 try:
147 module.authenticator.validate()
148 module.authenticator.authenticate(client)
149 except (NotImplementedError, HashiVaultValueError) as e:
150 module.fail_json(msg=to_text(e), exception=traceback.format_exc())
151
152 try:
153 client.secrets.database.configure(**parameters)
154 except hvac_exceptions.Forbidden as e:
155 module.fail_json(
156 msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point
157 or "database",
158 exception=traceback.format_exc(),
159 )
160 except hvac_exceptions.InvalidPath as e:
161 module.fail_json(
162 msg="Invalid or missing path ['%s/config/%s']."
163 % (engine_mount_point or "database", parameters["name"]),
164 exception=traceback.format_exc(),
165 )
166 except hvac_exceptions.InvalidRequest as e:
167 module.fail_json(
168 msg="Error creating database connection ['%s/config/%s']. Please analyze the traceback for further details."
169 % (engine_mount_point or "database", parameters["name"]),
170 exception=traceback.format_exc(),
171 )
172 else:
173 module.exit_json(changed=True)
174
175
176 def main():
177 run_module()
178
179
180 if __name__ == "__main__":
181 main()