| 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_read |
| 13 |
version_added: 6.2.0 |
| 14 |
author: |
| 15 |
- Martin Chmielewski (@M4rt1nCh) |
| 16 |
short_description: Returns the configuration settings for a O(connection_name) |
| 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(Reads a Database Connection,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#read-configuration), |
| 22 |
identified by its O(connection_name) from Hashcorp Vault. |
| 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 |
connection_name: |
| 35 |
description: The connection name to be read. |
| 36 |
type: str |
| 37 |
required: True |
| 38 |
""" |
| 39 |
|
| 40 |
EXAMPLES = r""" |
| 41 |
- name: Read a Database Connection with the default mount point |
| 42 |
community.hashi_vault.vault_database_connection_read: |
| 43 |
url: https://vault:8201 |
| 44 |
auth_method: userpass |
| 45 |
username: '{{ user }}' |
| 46 |
password: '{{ passwd }}' |
| 47 |
connection_name: SomeName |
| 48 |
register: result |
| 49 |
|
| 50 |
- name: Display the result of the operation |
| 51 |
ansible.builtin.debug: |
| 52 |
msg: "{{ result }}" |
| 53 |
|
| 54 |
- name: Read a Database Connection with a custom mount point |
| 55 |
community.hashi_vault.vault_database_connection_read: |
| 56 |
url: https://vault:8201 |
| 57 |
auth_method: userpass |
| 58 |
username: '{{ user }}' |
| 59 |
password: '{{ passwd }}' |
| 60 |
engine_mount_point: db1 |
| 61 |
register: result |
| 62 |
|
| 63 |
- name: Display the result of the operation |
| 64 |
ansible.builtin.debug: |
| 65 |
msg: "{{ result }}" |
| 66 |
""" |
| 67 |
|
| 68 |
RETURN = r""" |
| 69 |
data: &data |
| 70 |
description: The C(data) field of the RV(raw) result. This can also be accessed via RV(raw.data). |
| 71 |
returned: success |
| 72 |
type: dict |
| 73 |
sample: &data_sample |
| 74 |
allowed_roles: [] |
| 75 |
connection_details: |
| 76 |
connection_url: "postgresql://{{username}}:{{password}}@postgres:5432/postgres?sslmode=disable" |
| 77 |
username: "UserName" |
| 78 |
password_policy": "" |
| 79 |
plugin_name": "postgresql-database-plugin" |
| 80 |
plugin_version": "" |
| 81 |
root_credentials_rotate_statements": [] |
| 82 |
raw: |
| 83 |
description: The raw result of the operation |
| 84 |
returned: success |
| 85 |
type: dict |
| 86 |
contains: |
| 87 |
data: *data |
| 88 |
sample: |
| 89 |
auth: null, |
| 90 |
data: *data_sample |
| 91 |
""" |
| 92 |
|
| 93 |
import traceback |
| 94 |
|
| 95 |
from ansible.module_utils.common.text.converters import to_text |
| 96 |
|
| 97 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 98 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 99 |
|
| 100 |
|
| 101 |
def run_module(): |
| 102 |
argspec = HashiVaultModule.generate_argspec( |
| 103 |
engine_mount_point=dict(type="str", required=False), |
| 104 |
connection_name=dict(type="str", required=True), |
| 105 |
) |
| 106 |
|
| 107 |
module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True) |
| 108 |
|
| 109 |
parameters = {} |
| 110 |
engine_mount_point = module.params.get("engine_mount_point", None) |
| 111 |
if engine_mount_point is not None: |
| 112 |
parameters["mount_point"] = engine_mount_point |
| 113 |
parameters["name"] = module.params.get("connection_name") |
| 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 |
raw = client.secrets.database.read_connection(**parameters) |
| 128 |
except hvac_exceptions.Forbidden as e: |
| 129 |
module.fail_json( |
| 130 |
msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point |
| 131 |
or "database", |
| 132 |
exception=traceback.format_exc(), |
| 133 |
) |
| 134 |
except hvac_exceptions.InvalidPath as e: |
| 135 |
module.fail_json( |
| 136 |
msg="Invalid or missing path ['%s/config/%s']." |
| 137 |
% (engine_mount_point or "database", parameters["name"]), |
| 138 |
exception=traceback.format_exc(), |
| 139 |
) |
| 140 |
|
| 141 |
data = raw["data"] |
| 142 |
module.exit_json(raw=raw, data=data, changed=False) |
| 143 |
|
| 144 |
|
| 145 |
def main(): |
| 146 |
run_module() |
| 147 |
|
| 148 |
|
| 149 |
if __name__ == "__main__": |
| 150 |
main() |