| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2017, Abhijeet Kasurde ([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_subca |
| 10 |
author: Abhijeet Kasurde (@Akasurde) |
| 11 |
short_description: Manage FreeIPA Lightweight Sub Certificate Authorities |
| 12 |
description: |
| 13 |
- Add, modify, enable, disable and delete an IPA Lightweight Sub Certificate Authorities using IPA API. |
| 14 |
attributes: |
| 15 |
check_mode: |
| 16 |
support: full |
| 17 |
diff_mode: |
| 18 |
support: none |
| 19 |
options: |
| 20 |
subca_name: |
| 21 |
description: |
| 22 |
- The Sub Certificate Authority name which needs to be managed. |
| 23 |
required: true |
| 24 |
aliases: ["name"] |
| 25 |
type: str |
| 26 |
subca_subject: |
| 27 |
description: |
| 28 |
- The Sub Certificate Authority's Subject, for example V(CN=SampleSubCA1,O=testrelm.test). |
| 29 |
required: true |
| 30 |
type: str |
| 31 |
subca_desc: |
| 32 |
description: |
| 33 |
- The Sub Certificate Authority's description. |
| 34 |
type: str |
| 35 |
state: |
| 36 |
description: |
| 37 |
- State to ensure. |
| 38 |
- States V(disable) and V(enable) are available for FreeIPA 4.4.2 version and onwards. |
| 39 |
default: present |
| 40 |
choices: ["absent", "disabled", "enabled", "present"] |
| 41 |
type: str |
| 42 |
extends_documentation_fragment: |
| 43 |
- community.general._ipa.documentation |
| 44 |
- community.general._ipa.connection_notes |
| 45 |
- community.general._attributes |
| 46 |
""" |
| 47 |
|
| 48 |
EXAMPLES = r""" |
| 49 |
- name: Ensure IPA Sub CA is present |
| 50 |
community.general.ipa_subca: |
| 51 |
ipa_host: spider.example.com |
| 52 |
ipa_pass: Passw0rd! |
| 53 |
state: present |
| 54 |
subca_name: AnsibleSubCA1 |
| 55 |
subca_subject: 'CN=AnsibleSubCA1,O=example.com' |
| 56 |
subca_desc: Ansible Sub CA |
| 57 |
|
| 58 |
- name: Ensure that IPA Sub CA is removed |
| 59 |
community.general.ipa_subca: |
| 60 |
ipa_host: spider.example.com |
| 61 |
ipa_pass: Passw0rd! |
| 62 |
state: absent |
| 63 |
subca_name: AnsibleSubCA1 |
| 64 |
|
| 65 |
- name: Ensure that IPA Sub CA is disabled |
| 66 |
community.general.ipa_subca: |
| 67 |
ipa_host: spider.example.com |
| 68 |
ipa_pass: Passw0rd! |
| 69 |
state: disable |
| 70 |
subca_name: AnsibleSubCA1 |
| 71 |
""" |
| 72 |
|
| 73 |
RETURN = r""" |
| 74 |
subca: |
| 75 |
description: IPA Sub CA record as returned by IPA API. |
| 76 |
returned: always |
| 77 |
type: dict |
| 78 |
""" |
| 79 |
|
| 80 |
from ansible.module_utils.basic import AnsibleModule |
| 81 |
|
| 82 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 83 |
from ansible_collections.community.general.plugins.module_utils._version import LooseVersion |
| 84 |
|
| 85 |
|
| 86 |
class SubCAIPAClient(IPAClient): |
| 87 |
def __init__(self, module, host, port, protocol): |
| 88 |
super().__init__(module, host, port, protocol) |
| 89 |
|
| 90 |
def subca_find(self, subca_name): |
| 91 |
return self._post_json(method="ca_find", name=subca_name, item=None) |
| 92 |
|
| 93 |
def subca_add(self, subca_name=None, subject_dn=None, details=None): |
| 94 |
item = dict(ipacasubjectdn=subject_dn) |
| 95 |
subca_desc = details.get("description", None) |
| 96 |
if subca_desc is not None: |
| 97 |
item.update(description=subca_desc) |
| 98 |
return self._post_json(method="ca_add", name=subca_name, item=item) |
| 99 |
|
| 100 |
def subca_mod(self, subca_name=None, diff=None, details=None): |
| 101 |
item = get_subca_dict(details) |
| 102 |
for change in diff: |
| 103 |
update_detail = dict() |
| 104 |
if item[change] is not None: |
| 105 |
update_detail.update(setattr=f"{change}={item[change]}") |
| 106 |
self._post_json(method="ca_mod", name=subca_name, item=update_detail) |
| 107 |
|
| 108 |
def subca_del(self, subca_name=None): |
| 109 |
return self._post_json(method="ca_del", name=subca_name) |
| 110 |
|
| 111 |
def subca_disable(self, subca_name=None): |
| 112 |
return self._post_json(method="ca_disable", name=subca_name) |
| 113 |
|
| 114 |
def subca_enable(self, subca_name=None): |
| 115 |
return self._post_json(method="ca_enable", name=subca_name) |
| 116 |
|
| 117 |
|
| 118 |
def get_subca_dict(details=None): |
| 119 |
module_subca = dict() |
| 120 |
if details["description"] is not None: |
| 121 |
module_subca["description"] = details["description"] |
| 122 |
if details["subca_subject"] is not None: |
| 123 |
module_subca["ipacasubjectdn"] = details["subca_subject"] |
| 124 |
return module_subca |
| 125 |
|
| 126 |
|
| 127 |
def get_subca_diff(client, ipa_subca, module_subca): |
| 128 |
details = get_subca_dict(module_subca) |
| 129 |
return client.get_diff(ipa_data=ipa_subca, module_data=details) |
| 130 |
|
| 131 |
|
| 132 |
def ensure(module, client): |
| 133 |
subca_name = module.params["subca_name"] |
| 134 |
subca_subject_dn = module.params["subca_subject"] |
| 135 |
subca_desc = module.params["subca_desc"] |
| 136 |
|
| 137 |
state = module.params["state"] |
| 138 |
|
| 139 |
ipa_subca = client.subca_find(subca_name) |
| 140 |
module_subca = dict(description=subca_desc, subca_subject=subca_subject_dn) |
| 141 |
|
| 142 |
changed = False |
| 143 |
if state == "present": |
| 144 |
if not ipa_subca: |
| 145 |
changed = True |
| 146 |
if not module.check_mode: |
| 147 |
client.subca_add(subca_name=subca_name, subject_dn=subca_subject_dn, details=module_subca) |
| 148 |
else: |
| 149 |
diff = get_subca_diff(client, ipa_subca, module_subca) |
| 150 |
# IPA does not allow to modify Sub CA's subject DN |
| 151 |
# So skip it for now. |
| 152 |
if "ipacasubjectdn" in diff: |
| 153 |
diff.remove("ipacasubjectdn") |
| 154 |
del module_subca["subca_subject"] |
| 155 |
|
| 156 |
if len(diff) > 0: |
| 157 |
changed = True |
| 158 |
if not module.check_mode: |
| 159 |
client.subca_mod(subca_name=subca_name, diff=diff, details=module_subca) |
| 160 |
elif state == "absent": |
| 161 |
if ipa_subca: |
| 162 |
changed = True |
| 163 |
if not module.check_mode: |
| 164 |
client.subca_del(subca_name=subca_name) |
| 165 |
elif state == "disable": |
| 166 |
ipa_version = client.get_ipa_version() |
| 167 |
if LooseVersion(ipa_version) < LooseVersion("4.4.2"): |
| 168 |
module.fail_json( |
| 169 |
msg="Current version of IPA server [%s] does not support 'CA disable' option. Please upgrade to " |
| 170 |
"version greater than 4.4.2" |
| 171 |
) |
| 172 |
if ipa_subca: |
| 173 |
changed = True |
| 174 |
if not module.check_mode: |
| 175 |
client.subca_disable(subca_name=subca_name) |
| 176 |
elif state == "enable": |
| 177 |
ipa_version = client.get_ipa_version() |
| 178 |
if LooseVersion(ipa_version) < LooseVersion("4.4.2"): |
| 179 |
module.fail_json( |
| 180 |
msg="Current version of IPA server [%s] does not support 'CA enable' option. Please upgrade to " |
| 181 |
"version greater than 4.4.2" |
| 182 |
) |
| 183 |
if ipa_subca: |
| 184 |
changed = True |
| 185 |
if not module.check_mode: |
| 186 |
client.subca_enable(subca_name=subca_name) |
| 187 |
|
| 188 |
return changed, client.subca_find(subca_name) |
| 189 |
|
| 190 |
|
| 191 |
def main(): |
| 192 |
argument_spec = ipa_argument_spec() |
| 193 |
argument_spec.update( |
| 194 |
subca_name=dict(type="str", required=True, aliases=["name"]), |
| 195 |
subca_subject=dict(type="str", required=True), |
| 196 |
subca_desc=dict(type="str"), |
| 197 |
state=dict(type="str", default="present", choices=["present", "absent", "enabled", "disabled"]), |
| 198 |
) |
| 199 |
|
| 200 |
module = AnsibleModule( |
| 201 |
argument_spec=argument_spec, |
| 202 |
supports_check_mode=True, |
| 203 |
) |
| 204 |
|
| 205 |
client = SubCAIPAClient( |
| 206 |
module=module, |
| 207 |
host=module.params["ipa_host"], |
| 208 |
port=module.params["ipa_port"], |
| 209 |
protocol=module.params["ipa_prot"], |
| 210 |
) |
| 211 |
|
| 212 |
try: |
| 213 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 214 |
changed, record = ensure(module, client) |
| 215 |
module.exit_json(changed=changed, record=record) |
| 216 |
except Exception as exc: |
| 217 |
module.fail_json(msg=f"{exc}") |
| 218 |
|
| 219 |
|
| 220 |
if __name__ == "__main__": |
| 221 |
main() |