| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2018, Juan Manuel Parrilla <[email protected]> |
| 3 |
# GNU General Public License v3.0+ (see LICENSES/GPL-3.0-or-later.txt or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 4 |
# SPDX-License-Identifier: GPL-3.0-or-later |
| 5 |
|
| 6 |
from __future__ import annotations |
| 7 |
|
| 8 |
DOCUMENTATION = r""" |
| 9 |
module: ipa_vault |
| 10 |
author: Juan Manuel Parrilla (@jparrill) |
| 11 |
short_description: Manage FreeIPA vaults |
| 12 |
description: |
| 13 |
- Add, modify and delete vaults and secret vaults. |
| 14 |
- KRA service should be enabled to use this module. |
| 15 |
attributes: |
| 16 |
check_mode: |
| 17 |
support: full |
| 18 |
diff_mode: |
| 19 |
support: none |
| 20 |
options: |
| 21 |
cn: |
| 22 |
description: |
| 23 |
- Vault name. |
| 24 |
- Can not be changed as it is the unique identifier. |
| 25 |
required: true |
| 26 |
aliases: ["name"] |
| 27 |
type: str |
| 28 |
description: |
| 29 |
description: |
| 30 |
- Description. |
| 31 |
type: str |
| 32 |
ipavaulttype: |
| 33 |
description: |
| 34 |
- Vault types are based on security level. |
| 35 |
default: "symmetric" |
| 36 |
choices: ["asymmetric", "standard", "symmetric"] |
| 37 |
aliases: ["vault_type"] |
| 38 |
type: str |
| 39 |
ipavaultpublickey: |
| 40 |
description: |
| 41 |
- Public key. |
| 42 |
aliases: ["vault_public_key"] |
| 43 |
type: str |
| 44 |
ipavaultsalt: |
| 45 |
description: |
| 46 |
- Vault Salt. |
| 47 |
aliases: ["vault_salt"] |
| 48 |
type: str |
| 49 |
username: |
| 50 |
description: |
| 51 |
- Any user can own one or more user vaults. |
| 52 |
- Mutually exclusive with O(service). |
| 53 |
aliases: ["user"] |
| 54 |
type: list |
| 55 |
elements: str |
| 56 |
service: |
| 57 |
description: |
| 58 |
- Any service can own one or more service vaults. |
| 59 |
- Mutually exclusive with O(user). |
| 60 |
type: str |
| 61 |
state: |
| 62 |
description: |
| 63 |
- State to ensure. |
| 64 |
default: "present" |
| 65 |
choices: ["absent", "present"] |
| 66 |
type: str |
| 67 |
replace: |
| 68 |
description: |
| 69 |
- Force replace the existent vault on IPA server. |
| 70 |
type: bool |
| 71 |
default: false |
| 72 |
choices: ["True", "False"] |
| 73 |
validate_certs: |
| 74 |
description: |
| 75 |
- Validate IPA server certificates. |
| 76 |
type: bool |
| 77 |
default: true |
| 78 |
extends_documentation_fragment: |
| 79 |
- community.general._ipa.documentation |
| 80 |
- community.general._ipa.connection_notes |
| 81 |
- community.general._attributes |
| 82 |
""" |
| 83 |
|
| 84 |
EXAMPLES = r""" |
| 85 |
- name: Ensure vault is present |
| 86 |
community.general.ipa_vault: |
| 87 |
name: vault01 |
| 88 |
vault_type: standard |
| 89 |
user: user01 |
| 90 |
ipa_host: ipa.example.com |
| 91 |
ipa_user: admin |
| 92 |
ipa_pass: topsecret |
| 93 |
|
| 94 |
- name: Ensure vault is present for Admin user |
| 95 |
community.general.ipa_vault: |
| 96 |
name: vault01 |
| 97 |
vault_type: standard |
| 98 |
ipa_host: ipa.example.com |
| 99 |
ipa_user: admin |
| 100 |
ipa_pass: topsecret |
| 101 |
|
| 102 |
- name: Ensure vault is absent |
| 103 |
community.general.ipa_vault: |
| 104 |
name: vault01 |
| 105 |
vault_type: standard |
| 106 |
user: user01 |
| 107 |
state: absent |
| 108 |
ipa_host: ipa.example.com |
| 109 |
ipa_user: admin |
| 110 |
ipa_pass: topsecret |
| 111 |
|
| 112 |
- name: Modify vault if already exists |
| 113 |
community.general.ipa_vault: |
| 114 |
name: vault01 |
| 115 |
vault_type: standard |
| 116 |
description: "Vault for test" |
| 117 |
ipa_host: ipa.example.com |
| 118 |
ipa_user: admin |
| 119 |
ipa_pass: topsecret |
| 120 |
replace: true |
| 121 |
|
| 122 |
- name: Get vault info if already exists |
| 123 |
community.general.ipa_vault: |
| 124 |
name: vault01 |
| 125 |
ipa_host: ipa.example.com |
| 126 |
ipa_user: admin |
| 127 |
ipa_pass: topsecret |
| 128 |
""" |
| 129 |
|
| 130 |
RETURN = r""" |
| 131 |
vault: |
| 132 |
description: Vault as returned by IPA API. |
| 133 |
returned: always |
| 134 |
type: dict |
| 135 |
""" |
| 136 |
|
| 137 |
import traceback |
| 138 |
|
| 139 |
from ansible.module_utils.basic import AnsibleModule |
| 140 |
|
| 141 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 142 |
|
| 143 |
|
| 144 |
class VaultIPAClient(IPAClient): |
| 145 |
def __init__(self, module, host, port, protocol): |
| 146 |
super().__init__(module, host, port, protocol) |
| 147 |
|
| 148 |
def vault_find(self, name): |
| 149 |
return self._post_json(method="vault_find", name=None, item={"all": True, "cn": name}) |
| 150 |
|
| 151 |
def vault_add_internal(self, name, item): |
| 152 |
return self._post_json(method="vault_add_internal", name=name, item=item) |
| 153 |
|
| 154 |
def vault_mod_internal(self, name, item): |
| 155 |
return self._post_json(method="vault_mod_internal", name=name, item=item) |
| 156 |
|
| 157 |
def vault_del(self, name): |
| 158 |
return self._post_json(method="vault_del", name=name) |
| 159 |
|
| 160 |
|
| 161 |
def get_vault_dict(description=None, vault_type=None, vault_salt=None, vault_public_key=None, service=None): |
| 162 |
vault = {} |
| 163 |
|
| 164 |
if description is not None: |
| 165 |
vault["description"] = description |
| 166 |
if vault_type is not None: |
| 167 |
vault["ipavaulttype"] = vault_type |
| 168 |
if vault_salt is not None: |
| 169 |
vault["ipavaultsalt"] = vault_salt |
| 170 |
if vault_public_key is not None: |
| 171 |
vault["ipavaultpublickey"] = vault_public_key |
| 172 |
if service is not None: |
| 173 |
vault["service"] = service |
| 174 |
return vault |
| 175 |
|
| 176 |
|
| 177 |
def get_vault_diff(client, ipa_vault, module_vault, module): |
| 178 |
return client.get_diff(ipa_data=ipa_vault, module_data=module_vault) |
| 179 |
|
| 180 |
|
| 181 |
def ensure(module, client): |
| 182 |
state = module.params["state"] |
| 183 |
name = module.params["cn"] |
| 184 |
# user = module.params["username"] TODO is this really not needed? |
| 185 |
replace = module.params["replace"] |
| 186 |
|
| 187 |
module_vault = get_vault_dict( |
| 188 |
description=module.params["description"], |
| 189 |
vault_type=module.params["ipavaulttype"], |
| 190 |
vault_salt=module.params["ipavaultsalt"], |
| 191 |
vault_public_key=module.params["ipavaultpublickey"], |
| 192 |
service=module.params["service"], |
| 193 |
) |
| 194 |
ipa_vault = client.vault_find(name=name) |
| 195 |
|
| 196 |
changed = False |
| 197 |
if state == "present": |
| 198 |
if not ipa_vault: |
| 199 |
# New vault |
| 200 |
changed = True |
| 201 |
if not module.check_mode: |
| 202 |
ipa_vault = client.vault_add_internal(name, item=module_vault) |
| 203 |
else: |
| 204 |
# Already exists |
| 205 |
if replace: |
| 206 |
diff = get_vault_diff(client, ipa_vault, module_vault, module) |
| 207 |
if len(diff) > 0: |
| 208 |
changed = True |
| 209 |
if not module.check_mode: |
| 210 |
data = {} |
| 211 |
for key in diff: |
| 212 |
data[key] = module_vault.get(key) |
| 213 |
client.vault_mod_internal(name=name, item=data) |
| 214 |
|
| 215 |
else: |
| 216 |
if ipa_vault: |
| 217 |
changed = True |
| 218 |
if not module.check_mode: |
| 219 |
client.vault_del(name) |
| 220 |
|
| 221 |
return changed, client.vault_find(name=name) |
| 222 |
|
| 223 |
|
| 224 |
def main(): |
| 225 |
argument_spec = ipa_argument_spec() |
| 226 |
argument_spec.update( |
| 227 |
cn=dict(type="str", required=True, aliases=["name"]), |
| 228 |
description=dict(type="str"), |
| 229 |
ipavaulttype=dict( |
| 230 |
type="str", default="symmetric", choices=["standard", "symmetric", "asymmetric"], aliases=["vault_type"] |
| 231 |
), |
| 232 |
ipavaultsalt=dict(type="str", aliases=["vault_salt"]), |
| 233 |
ipavaultpublickey=dict(type="str", aliases=["vault_public_key"]), |
| 234 |
service=dict(type="str"), |
| 235 |
replace=dict(type="bool", default=False, choices=[True, False]), |
| 236 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 237 |
username=dict(type="list", elements="str", aliases=["user"]), |
| 238 |
) |
| 239 |
|
| 240 |
module = AnsibleModule( |
| 241 |
argument_spec=argument_spec, supports_check_mode=True, mutually_exclusive=[["username", "service"]] |
| 242 |
) |
| 243 |
|
| 244 |
client = VaultIPAClient( |
| 245 |
module=module, |
| 246 |
host=module.params["ipa_host"], |
| 247 |
port=module.params["ipa_port"], |
| 248 |
protocol=module.params["ipa_prot"], |
| 249 |
) |
| 250 |
try: |
| 251 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 252 |
changed, vault = ensure(module, client) |
| 253 |
module.exit_json(changed=changed, vault=vault) |
| 254 |
except Exception as e: |
| 255 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 256 |
|
| 257 |
|
| 258 |
if __name__ == "__main__": |
| 259 |
main() |