| 1 |
#!/usr/bin/python |
| 2 |
# Copyright (c) 2021, Ansible Project |
| 3 |
# Heavily influenced from Fran Fitzpatrick <[email protected]> ipa_config module |
| 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 annotations |
| 8 |
|
| 9 |
DOCUMENTATION = r""" |
| 10 |
module: ipa_otpconfig |
| 11 |
author: justchris1 (@justchris1) |
| 12 |
short_description: Manage FreeIPA OTP Configuration Settings |
| 13 |
version_added: 2.5.0 |
| 14 |
description: |
| 15 |
- Modify global configuration settings of a FreeIPA Server with respect to OTP (One Time Passwords). |
| 16 |
attributes: |
| 17 |
check_mode: |
| 18 |
support: full |
| 19 |
diff_mode: |
| 20 |
support: none |
| 21 |
options: |
| 22 |
ipatokentotpauthwindow: |
| 23 |
description: TOTP authentication window in seconds. |
| 24 |
aliases: ["totpauthwindow"] |
| 25 |
type: int |
| 26 |
ipatokentotpsyncwindow: |
| 27 |
description: TOTP synchronization window in seconds. |
| 28 |
aliases: ["totpsyncwindow"] |
| 29 |
type: int |
| 30 |
ipatokenhotpauthwindow: |
| 31 |
description: HOTP authentication window in number of hops. |
| 32 |
aliases: ["hotpauthwindow"] |
| 33 |
type: int |
| 34 |
ipatokenhotpsyncwindow: |
| 35 |
description: HOTP synchronization window in hops. |
| 36 |
aliases: ["hotpsyncwindow"] |
| 37 |
type: int |
| 38 |
extends_documentation_fragment: |
| 39 |
- community.general._ipa.documentation |
| 40 |
- community.general._ipa.connection_notes |
| 41 |
- community.general._attributes |
| 42 |
""" |
| 43 |
|
| 44 |
EXAMPLES = r""" |
| 45 |
- name: Ensure the TOTP authentication window is set to 300 seconds |
| 46 |
community.general.ipa_otpconfig: |
| 47 |
ipatokentotpauthwindow: '300' |
| 48 |
ipa_host: localhost |
| 49 |
ipa_user: admin |
| 50 |
ipa_pass: supersecret |
| 51 |
|
| 52 |
- name: Ensure the TOTP synchronization window is set to 86400 seconds |
| 53 |
community.general.ipa_otpconfig: |
| 54 |
ipatokentotpsyncwindow: '86400' |
| 55 |
ipa_host: localhost |
| 56 |
ipa_user: admin |
| 57 |
ipa_pass: supersecret |
| 58 |
|
| 59 |
- name: Ensure the HOTP authentication window is set to 10 hops |
| 60 |
community.general.ipa_otpconfig: |
| 61 |
ipatokenhotpauthwindow: '10' |
| 62 |
ipa_host: localhost |
| 63 |
ipa_user: admin |
| 64 |
ipa_pass: supersecret |
| 65 |
|
| 66 |
- name: Ensure the HOTP synchronization window is set to 100 hops |
| 67 |
community.general.ipa_otpconfig: |
| 68 |
ipatokenhotpsyncwindow: '100' |
| 69 |
ipa_host: localhost |
| 70 |
ipa_user: admin |
| 71 |
ipa_pass: supersecret |
| 72 |
""" |
| 73 |
|
| 74 |
RETURN = r""" |
| 75 |
otpconfig: |
| 76 |
description: OTP configuration as returned by IPA API. |
| 77 |
returned: always |
| 78 |
type: dict |
| 79 |
""" |
| 80 |
|
| 81 |
import traceback |
| 82 |
|
| 83 |
from ansible.module_utils.basic import AnsibleModule |
| 84 |
|
| 85 |
from ansible_collections.community.general.plugins.module_utils._ipa import IPAClient, ipa_argument_spec |
| 86 |
|
| 87 |
|
| 88 |
class OTPConfigIPAClient(IPAClient): |
| 89 |
def __init__(self, module, host, port, protocol): |
| 90 |
super().__init__(module, host, port, protocol) |
| 91 |
|
| 92 |
def otpconfig_show(self): |
| 93 |
return self._post_json(method="otpconfig_show", name=None) |
| 94 |
|
| 95 |
def otpconfig_mod(self, name, item): |
| 96 |
return self._post_json(method="otpconfig_mod", name=name, item=item) |
| 97 |
|
| 98 |
|
| 99 |
def get_otpconfig_dict( |
| 100 |
ipatokentotpauthwindow=None, ipatokentotpsyncwindow=None, ipatokenhotpauthwindow=None, ipatokenhotpsyncwindow=None |
| 101 |
): |
| 102 |
config = {} |
| 103 |
if ipatokentotpauthwindow is not None: |
| 104 |
config["ipatokentotpauthwindow"] = str(ipatokentotpauthwindow) |
| 105 |
if ipatokentotpsyncwindow is not None: |
| 106 |
config["ipatokentotpsyncwindow"] = str(ipatokentotpsyncwindow) |
| 107 |
if ipatokenhotpauthwindow is not None: |
| 108 |
config["ipatokenhotpauthwindow"] = str(ipatokenhotpauthwindow) |
| 109 |
if ipatokenhotpsyncwindow is not None: |
| 110 |
config["ipatokenhotpsyncwindow"] = str(ipatokenhotpsyncwindow) |
| 111 |
|
| 112 |
return config |
| 113 |
|
| 114 |
|
| 115 |
def get_otpconfig_diff(client, ipa_config, module_config): |
| 116 |
return client.get_diff(ipa_data=ipa_config, module_data=module_config) |
| 117 |
|
| 118 |
|
| 119 |
def ensure(module, client): |
| 120 |
module_otpconfig = get_otpconfig_dict( |
| 121 |
ipatokentotpauthwindow=module.params.get("ipatokentotpauthwindow"), |
| 122 |
ipatokentotpsyncwindow=module.params.get("ipatokentotpsyncwindow"), |
| 123 |
ipatokenhotpauthwindow=module.params.get("ipatokenhotpauthwindow"), |
| 124 |
ipatokenhotpsyncwindow=module.params.get("ipatokenhotpsyncwindow"), |
| 125 |
) |
| 126 |
ipa_otpconfig = client.otpconfig_show() |
| 127 |
diff = get_otpconfig_diff(client, ipa_otpconfig, module_otpconfig) |
| 128 |
|
| 129 |
changed = False |
| 130 |
new_otpconfig = {} |
| 131 |
for module_key in diff: |
| 132 |
if module_otpconfig.get(module_key) != ipa_otpconfig.get(module_key, None): |
| 133 |
changed = True |
| 134 |
new_otpconfig.update({module_key: module_otpconfig.get(module_key)}) |
| 135 |
|
| 136 |
if changed and not module.check_mode: |
| 137 |
client.otpconfig_mod(name=None, item=new_otpconfig) |
| 138 |
|
| 139 |
return changed, client.otpconfig_show() |
| 140 |
|
| 141 |
|
| 142 |
def main(): |
| 143 |
argument_spec = ipa_argument_spec() |
| 144 |
argument_spec.update( |
| 145 |
ipatokentotpauthwindow=dict(type="int", aliases=["totpauthwindow"], no_log=False), |
| 146 |
ipatokentotpsyncwindow=dict(type="int", aliases=["totpsyncwindow"], no_log=False), |
| 147 |
ipatokenhotpauthwindow=dict(type="int", aliases=["hotpauthwindow"], no_log=False), |
| 148 |
ipatokenhotpsyncwindow=dict(type="int", aliases=["hotpsyncwindow"], no_log=False), |
| 149 |
) |
| 150 |
|
| 151 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 152 |
|
| 153 |
client = OTPConfigIPAClient( |
| 154 |
module=module, |
| 155 |
host=module.params["ipa_host"], |
| 156 |
port=module.params["ipa_port"], |
| 157 |
protocol=module.params["ipa_prot"], |
| 158 |
) |
| 159 |
|
| 160 |
try: |
| 161 |
client.login(username=module.params["ipa_user"], password=module.params["ipa_pass"]) |
| 162 |
changed, otpconfig = ensure(module, client) |
| 163 |
except Exception as e: |
| 164 |
module.fail_json(msg=f"{e}", exception=traceback.format_exc()) |
| 165 |
|
| 166 |
module.exit_json(changed=changed, otpconfig=otpconfig) |
| 167 |
|
| 168 |
|
| 169 |
if __name__ == "__main__": |
| 170 |
main() |