| 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_create |
| 13 |
version_added: 6.2.0 |
| 14 |
author: |
| 15 |
- Martin Chmielewski (@M4rt1nCh) |
| 16 |
short_description: Create or update a static role |
| 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 a new or updates an existing static role,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 |
db_username: |
| 46 |
description: The database username - Note that the user must exist in the target database! |
| 47 |
type: str |
| 48 |
required: True |
| 49 |
rotation_statements: |
| 50 |
description: SQL statements to rotate the password for the given O(db_username) |
| 51 |
type: list |
| 52 |
required: True |
| 53 |
elements: str |
| 54 |
rotation_period: |
| 55 |
description: Password rotation period in seconds (defaults to 24hs) |
| 56 |
type: int |
| 57 |
required: False |
| 58 |
default: 86400 |
| 59 |
""" |
| 60 |
|
| 61 |
EXAMPLES = r""" |
| 62 |
- name: Generate rotation statement |
| 63 |
ansible.builtin.set_fact: |
| 64 |
rotation_statements = ["ALTER USER \"{{name}}\" WITH PASSWORD '{{password}}';"] |
| 65 |
|
| 66 |
- name: Create / update Static Role with the default mount point |
| 67 |
community.hashi_vault.vault_database_static_role_create: |
| 68 |
url: https://vault:8201 |
| 69 |
auth_method: userpass |
| 70 |
username: '{{ user }}' |
| 71 |
password: '{{ passwd }}' |
| 72 |
connection_name: SomeConnection |
| 73 |
role_name: SomeRole |
| 74 |
db_username: '{{ db_username}}' |
| 75 |
rotation_statements: '{{ rotation_statements }}' |
| 76 |
register: response |
| 77 |
|
| 78 |
- name: Display the result of the operation |
| 79 |
ansible.builtin.debug: |
| 80 |
msg: "{{ result }}" |
| 81 |
|
| 82 |
- name: Create / update Static Role with a custom mount point |
| 83 |
community.hashi_vault.vault_database_static_role_create: |
| 84 |
url: https://vault:8201 |
| 85 |
auth_method: userpass |
| 86 |
username: '{{ user }}' |
| 87 |
password: '{{ passwd }}' |
| 88 |
engine_mount_point: db1 |
| 89 |
connection_name: SomeConnection |
| 90 |
role_name: SomeRole |
| 91 |
db_username: '{{ db_username}}' |
| 92 |
rotation_statements: '{{ rotation_statements }}' |
| 93 |
register: response |
| 94 |
|
| 95 |
- name: Display the result of the operation |
| 96 |
ansible.builtin.debug: |
| 97 |
msg: "{{ result }}" |
| 98 |
""" |
| 99 |
|
| 100 |
RETURN = r"""""" |
| 101 |
|
| 102 |
import traceback |
| 103 |
|
| 104 |
from ansible.module_utils.common.text.converters import to_text |
| 105 |
|
| 106 |
from ..module_utils._hashi_vault_module import HashiVaultModule |
| 107 |
from ..module_utils._hashi_vault_common import HashiVaultValueError |
| 108 |
|
| 109 |
|
| 110 |
def run_module(): |
| 111 |
argspec = HashiVaultModule.generate_argspec( |
| 112 |
engine_mount_point=dict(type="str", required=False), |
| 113 |
connection_name=dict(type="str", required=True), |
| 114 |
role_name=dict(type="str", required=True), |
| 115 |
db_username=dict(type="str", required=True), |
| 116 |
rotation_statements=dict(type="list", required=True, elements="str"), |
| 117 |
rotation_period=dict(type="int", required=False, default=86400), |
| 118 |
) |
| 119 |
|
| 120 |
module = HashiVaultModule(argument_spec=argspec, supports_check_mode=True) |
| 121 |
|
| 122 |
if module.check_mode is True: |
| 123 |
module.exit_json(changed=True) |
| 124 |
|
| 125 |
parameters = {} |
| 126 |
engine_mount_point = module.params.get("engine_mount_point", None) |
| 127 |
if engine_mount_point is not None: |
| 128 |
parameters["mount_point"] = engine_mount_point |
| 129 |
parameters["db_name"] = module.params.get("connection_name") |
| 130 |
parameters["name"] = module.params.get("role_name") |
| 131 |
parameters["username"] = module.params.get("db_username") |
| 132 |
parameters["rotation_statements"] = module.params.get("rotation_statements") |
| 133 |
rotation_period = module.params.get("rotation_period", None) |
| 134 |
parameters["rotation_period"] = rotation_period |
| 135 |
|
| 136 |
module.connection_options.process_connection_options() |
| 137 |
client_args = module.connection_options.get_hvac_connection_options() |
| 138 |
client = module.helper.get_vault_client(**client_args) |
| 139 |
hvac_exceptions = module.helper.get_hvac().exceptions |
| 140 |
|
| 141 |
try: |
| 142 |
module.authenticator.validate() |
| 143 |
module.authenticator.authenticate(client) |
| 144 |
except (NotImplementedError, HashiVaultValueError) as e: |
| 145 |
module.fail_json(msg=to_text(e), exception=traceback.format_exc()) |
| 146 |
|
| 147 |
try: |
| 148 |
client.secrets.database.create_static_role(**parameters) |
| 149 |
except hvac_exceptions.Forbidden as e: |
| 150 |
module.fail_json( |
| 151 |
msg="Forbidden: Permission Denied to path ['%s']." % engine_mount_point |
| 152 |
or "database", |
| 153 |
exception=traceback.format_exc(), |
| 154 |
) |
| 155 |
except hvac_exceptions.InvalidPath as e: |
| 156 |
module.fail_json( |
| 157 |
msg="Invalid or missing path ['%s/static-roles/%s']." |
| 158 |
% (engine_mount_point or "database", parameters["name"]), |
| 159 |
exception=traceback.format_exc(), |
| 160 |
) |
| 161 |
except hvac_exceptions.InvalidRequest as e: |
| 162 |
module.fail_json( |
| 163 |
msg="Cannot update static role ['%s/static-roles/%s']. Please verify that the user exists on the database." |
| 164 |
% (engine_mount_point or "database", parameters["name"]), |
| 165 |
exception=traceback.format_exc(), |
| 166 |
) |
| 167 |
else: |
| 168 |
module.exit_json(changed=True) |
| 169 |
|
| 170 |
|
| 171 |
def main(): |
| 172 |
run_module() |
| 173 |
|
| 174 |
|
| 175 |
if __name__ == "__main__": |
| 176 |
main() |