| 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_role_read |
| 13 |
version_added: 6.2.0 |
| 14 |
author: |
| 15 |
- Martin Chmielewski (@M4rt1nCh) |
| 16 |
short_description: Queries a dynamic 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 role definition,L(reads a static role,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#read-a-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 Role with a default mount point |
| 42 |
community.hashi_vault.vault_database_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_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 |
creation_statements: [ |
| 77 |
"CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';", |
| 78 |
"GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" |
| 79 |
] |
| 80 |
credential_type: "password" |
| 81 |
db_name: "database" |
| 82 |
default_ttl: 3600 |
| 83 |
max_ttl: 86400 |
| 84 |
renew_statements: [] |
| 85 |
revocation_statements: [] |
| 86 |
rollback_statements: [] |
| 87 |
raw: |
| 88 |
description: The raw result of the operation. |
| 89 |
returned: success |
| 90 |
type: dict |
| 91 |
contains: |
| 92 |
data: *data |
| 93 |
sample: |
| 94 |
auth: null |
| 95 |
data: *data_sample |
| 96 |
username: "SomeUser" |
| 97 |
lease_duration": 0 |
| 98 |
lease_id: "" |
| 99 |
renewable: false |
| 100 |
request_id: "123456" |
| 101 |
warnings: null |
| 102 |
wrap_info: null |
| 103 |
""" |
| 104 |
|
| 105 |
import traceback |
| 106 |
|
| 107 |
from ansible.module_utils.common.text.converters import to_text |
| 108 |
|
| 109 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 110 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 111 |
|
| 112 |
|
| 113 |
def run_module(): |
| 114 |
argspec = HashiVaultModule.generate_argspec( |
| 115 |
engine_mount_point=dict(type="str", required=False), |
| 116 |
role_name=dict(type="str", required=True), |
| 117 |
) |
| 118 |
|
| 119 |
module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True) |
| 120 |
|
| 121 |
parameters = {} |
| 122 |
engine_mount_point = module.params.get("engine_mount_point", None) |
| 123 |
if engine_mount_point is not None: |
| 124 |
parameters["mount_point"] = engine_mount_point |
| 125 |
parameters["name"] = module.params.get("role_name") |
| 126 |
|
| 127 |
module.connection_options.process_connection_options() |
| 128 |
client_args = module.connection_options.get_hvac_connection_options() |
| 129 |
client = module.helper.get_vault_client(**client_args) |
| 130 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 131 |
|
| 132 |
try: |
| 133 |
module.authenticator.validate() |
| 134 |
module.authenticator.authenticate(client) |
| 135 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 136 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 137 |
|
| 138 |
try: |
| 139 |
raw = client.secrets.database.read_role(**parameters) |
| 140 |
except hvac_exceptions.Forbidden as e: |
| 141 |
module.fail_json( |
| 142 |
msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point |
| 143 |
or "database", |
| 144 |
exception=traceback.format_exc(), |
| 145 |
) |
| 146 |
except hvac_exceptions.InvalidPath as e: |
| 147 |
module.fail_json( |
| 148 |
msg="Invalid or missing path ['%s/roles/%s']." |
| 149 |
% (engine_mount_point or "database", parameters["name"]), |
| 150 |
exception=traceback.format_exc(), |
| 151 |
) |
| 152 |
|
| 153 |
data = raw["data"] |
| 154 |
|
| 155 |
module.exit_json(data=data, raw=raw, changed=False) |
| 156 |
|
| 157 |
|
| 158 |
def main(): |
| 159 |
run_module() |
| 160 |
|
| 161 |
|
| 162 |
if __name__ == "__main__": |
| 163 |
main() |