| 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_create |
| 13 |
version_added: 6.2.0 |
| 14 |
author: |
| 15 |
- Martin Chmielewski (@M4rt1nCh) |
| 16 |
short_description: Creates or updates 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(Creates or updates a dynamic role definition,https://hvac.readthedocs.io/en/stable/usage/secrets_engines/database.html#create-static-role). |
| 22 |
notes: |
| 23 |
- This module always reports C(changed) status because it cannot guarantee idempotence. |
| 24 |
- Use C(changed_when) to control that in cases where the operation is known to not change state. |
| 25 |
attributes: |
| 26 |
check_mode: |
| 27 |
support: partial |
| 28 |
details: |
| 29 |
- In check mode, a sample response will be returned, but the role creation will not be performed in Hashicorp Vault. |
| 30 |
extends_documentation_fragment: |
| 31 |
- community.hashi_vault.attributes |
| 32 |
- community.hashi_vault.attributes.action_group |
| 33 |
- community.hashi_vault.connection |
| 34 |
- community.hashi_vault.auth |
| 35 |
- community.hashi_vault.engine_mount |
| 36 |
options: |
| 37 |
connection_name: |
| 38 |
description: The connection name under which the role should be created. |
| 39 |
type: str |
| 40 |
required: True |
| 41 |
role_name: |
| 42 |
description: The name of the role that should be created. |
| 43 |
type: str |
| 44 |
required: True |
| 45 |
creation_statements: |
| 46 |
description: Specifies the database statements executed to create and configure a user. |
| 47 |
type: list |
| 48 |
required: True |
| 49 |
elements: str |
| 50 |
revocation_statements: |
| 51 |
description: Specifies the database statements to be executed to revoke a user. |
| 52 |
type: list |
| 53 |
required: False |
| 54 |
elements: str |
| 55 |
rollback_statements: |
| 56 |
description: Specifies the database statements to be executed to rollback a create operation in the event of an error. |
| 57 |
type: list |
| 58 |
required: False |
| 59 |
elements: str |
| 60 |
renew_statements: |
| 61 |
description: Specifies the database statements to be executed to renew a user |
| 62 |
type: list |
| 63 |
required: False |
| 64 |
elements: str |
| 65 |
default_ttl: |
| 66 |
description: Default TTL for the role. |
| 67 |
type: int |
| 68 |
required: False |
| 69 |
default: 3600 |
| 70 |
max_ttl: |
| 71 |
description: Max TTL for the role. |
| 72 |
type: int |
| 73 |
required: False |
| 74 |
default: 86400 |
| 75 |
""" |
| 76 |
|
| 77 |
EXAMPLES = r""" |
| 78 |
- name: Generate creation statement |
| 79 |
ansible.builtin.set_fact: |
| 80 |
creation_statements = [ |
| 81 |
"CREATE ROLE \"{{name}}\" WITH LOGIN PASSWORD '{{password}}' VALID UNTIL '{{expiration}}';", |
| 82 |
"GRANT SELECT ON ALL TABLES IN SCHEMA public TO \"{{name}}\";" |
| 83 |
] |
| 84 |
|
| 85 |
- name: Create / update Role with the default mount point |
| 86 |
community.hashi_vault.vault_database_role_create: |
| 87 |
url: https://vault:8201 |
| 88 |
auth_method: userpass |
| 89 |
username: '{{ user }}' |
| 90 |
password: '{{ passwd }}' |
| 91 |
connection_name: SomeConnection |
| 92 |
role_name: SomeRole |
| 93 |
db_username: '{{ db_username}}' |
| 94 |
creation_statements: '{{ creation_statements }}' |
| 95 |
register: result |
| 96 |
|
| 97 |
- name: Display the result of the operation |
| 98 |
ansible.builtin.debug: |
| 99 |
msg: "{{ result }}" |
| 100 |
|
| 101 |
- name: Create / update Role with a custom mount point |
| 102 |
community.hashi_vault.vault_database_role_create: |
| 103 |
url: https://vault:8201 |
| 104 |
auth_method: userpass |
| 105 |
username: '{{ user }}' |
| 106 |
password: '{{ passwd }}' |
| 107 |
engine_mount_point: db1 |
| 108 |
connection_name: SomeConnection |
| 109 |
role_name: SomeRole |
| 110 |
db_username: '{{ db_username}}' |
| 111 |
creation_statements: '{{ creation_statements }}' |
| 112 |
register: result |
| 113 |
|
| 114 |
- name: Display the result of the operation |
| 115 |
ansible.builtin.debug: |
| 116 |
msg: "{{ result }}" |
| 117 |
""" |
| 118 |
|
| 119 |
RETURN = r"""""" |
| 120 |
|
| 121 |
import traceback |
| 122 |
|
| 123 |
from ansible.module_utils.common.text.converters import to_text |
| 124 |
|
| 125 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 126 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 127 |
|
| 128 |
|
| 129 |
def run_module(): |
| 130 |
argspec = HashiVaultModule.generate_argspec( |
| 131 |
engine_mount_point=dict(type="str", required=False), |
| 132 |
connection_name=dict(type="str", required=True), |
| 133 |
role_name=dict(type="str", required=True), |
| 134 |
creation_statements=dict(type="list", required=True, elements="str"), |
| 135 |
revocation_statements=dict(type="list", required=False, elements="str"), |
| 136 |
rollback_statements=dict(type="list", required=False, elements="str"), |
| 137 |
renew_statements=dict(type="list", required=False, elements="str"), |
| 138 |
default_ttl=dict(type="int", required=False, default=3600), |
| 139 |
max_ttl=dict(type="int", required=False, default=86400), |
| 140 |
) |
| 141 |
|
| 142 |
module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True) |
| 143 |
|
| 144 |
if module.check_mode is True: |
| 145 |
module.exit_json(changed=True) |
| 146 |
|
| 147 |
parameters = { |
| 148 |
"name": module.params.get("role_name"), |
| 149 |
"db_name": module.params.get("connection_name"), |
| 150 |
"creation_statements": module.params.get("creation_statements"), |
| 151 |
"revocation_statements": module.params.get("revocation_statements"), |
| 152 |
"rollback_statements": module.params.get("rollback_statements"), |
| 153 |
"renew_statements": module.params.get("renew_statements"), |
| 154 |
"default_ttl": module.params.get("default_ttl"), |
| 155 |
"max_ttl": module.params.get("max_ttl"), |
| 156 |
} |
| 157 |
engine_mount_point = module.params.get("engine_mount_point", None) |
| 158 |
if engine_mount_point is not None: |
| 159 |
parameters["mount_point"] = engine_mount_point |
| 160 |
|
| 161 |
module.connection_options.process_connection_options() |
| 162 |
client_args = module.connection_options.get_hvac_connection_options() |
| 163 |
client = module.helper.get_vault_client(**client_args) |
| 164 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 165 |
|
| 166 |
try: |
| 167 |
module.authenticator.validate() |
| 168 |
module.authenticator.authenticate(client) |
| 169 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 170 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 171 |
|
| 172 |
try: |
| 173 |
client.secrets.database.create_role(**parameters) |
| 174 |
except hvac_exceptions.Forbidden as e: |
| 175 |
module.fail_json( |
| 176 |
msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point |
| 177 |
or "database", |
| 178 |
exception=traceback.format_exc(), |
| 179 |
) |
| 180 |
except hvac_exceptions.InvalidPath as e: |
| 181 |
module.fail_json( |
| 182 |
msg="Invalid or missing path ['%s/roles/%s']." |
| 183 |
% (engine_mount_point or "database", parameters["name"]), |
| 184 |
exception=traceback.format_exc(), |
| 185 |
) |
| 186 |
else: |
| 187 |
module.exit_json(changed=True) |
| 188 |
|
| 189 |
|
| 190 |
def main(): |
| 191 |
run_module() |
| 192 |
|
| 193 |
|
| 194 |
if __name__ == "__main__": |
| 195 |
main() |