Debian Codegraph @

ansible_collections/community/general/plugins/modules/ipa_getkeytab.py

raw
1 #!/usr/bin/python
2 # Copyright (c) 2024 Alexander Bakanovskii <[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_getkeytab
10 short_description: Manage keytab file in FreeIPA
11 version_added: 9.5.0
12 description:
13 - Manage keytab file with C(ipa-getkeytab) utility.
14 - See U(https://manpages.ubuntu.com/manpages/jammy/man1/ipa-getkeytab.1.html) for reference.
15 author: "Alexander Bakanovskii (@abakanovskii)"
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21 options:
22 path:
23 description:
24 - The base path where to put generated keytab file.
25 type: path
26 aliases: ["keytab"]
27 required: true
28 principal:
29 description:
30 - The non-realm part of the full principal name.
31 type: str
32 required: true
33 ipa_host:
34 description:
35 - The IPA server to retrieve the keytab from (FQDN).
36 type: str
37 ldap_uri:
38 description:
39 - LDAP URI. If V(ldap://) is specified, STARTTLS is initiated by default.
40 - Can not be used with the O(ipa_host) option.
41 type: str
42 bind_dn:
43 description:
44 - The LDAP DN to bind as when retrieving a keytab without Kerberos credentials.
45 - Generally used with the O(bind_pw) option.
46 type: str
47 bind_pw:
48 description:
49 - The LDAP password to use when not binding with Kerberos.
50 type: str
51 password:
52 description:
53 - Use this password for the key instead of one randomly generated.
54 type: str
55 ca_cert:
56 description:
57 - The path to the IPA CA certificate used to validate LDAPS/STARTTLS connections.
58 type: path
59 sasl_mech:
60 description:
61 - SASL mechanism to use if O(bind_dn) and O(bind_pw) are not specified.
62 choices: ["GSSAPI", "EXTERNAL"]
63 type: str
64 retrieve_mode:
65 description:
66 - Retrieve an existing key from the server instead of generating a new one.
67 - This is incompatible with the O(password), and works only against a IPA server more recent than version 3.3.
68 - The user requesting the keytab must have access to the keys for this operation to succeed.
69 - Be aware that if set V(true), a new keytab is generated.
70 - This invalidates all previously retrieved keytabs for this service principal.
71 type: bool
72 encryption_types:
73 description:
74 - The list of encryption types to use to generate keys.
75 - It uses local client defaults if not provided.
76 - Valid values depend on the Kerberos library version and configuration.
77 type: str
78 state:
79 description:
80 - The state of the keytab file.
81 - V(present) only check for existence of a file, if you want to recreate keytab with other parameters you should set
82 O(force=true).
83 type: str
84 default: present
85 choices: ["present", "absent"]
86 force:
87 description:
88 - Force recreation if exists already.
89 type: bool
90 requirements:
91 - freeipa-client
92 - Managed host is FreeIPA client
93 extends_documentation_fragment:
94 - community.general._attributes
95 """
96
97 EXAMPLES = r"""
98 - name: Get Kerberos ticket using default principal
99 community.general.krb_ticket:
100 password: "{{ aldpro_admin_password }}"
101
102 - name: Create keytab
103 community.general.ipa_getkeytab:
104 path: /etc/ipa/test.keytab
105 principal: HTTP/freeipa-dc02.ipa.test
106 ipa_host: freeipa-dc01.ipa.test
107
108 - name: Retrieve already existing keytab
109 community.general.ipa_getkeytab:
110 path: /etc/ipa/test.keytab
111 principal: HTTP/freeipa-dc02.ipa.test
112 ipa_host: freeipa-dc01.ipa.test
113 retrieve_mode: true
114
115 - name: Force keytab recreation
116 community.general.ipa_getkeytab:
117 path: /etc/ipa/test.keytab
118 principal: HTTP/freeipa-dc02.ipa.test
119 ipa_host: freeipa-dc01.ipa.test
120 force: true
121 """
122
123 import os
124
125 from ansible.module_utils.basic import AnsibleModule
126
127 from ansible_collections.community.general.plugins.module_utils._cmd_runner import CmdRunner, cmd_runner_fmt
128
129
130 class IPAKeytab:
131 def __init__(self, module, **kwargs):
132 self.module = module
133 self.path = kwargs["path"]
134 self.state = kwargs["state"]
135 self.principal = kwargs["principal"]
136 self.ipa_host = kwargs["ipa_host"]
137 self.ldap_uri = kwargs["ldap_uri"]
138 self.bind_dn = kwargs["bind_dn"]
139 self.bind_pw = kwargs["bind_pw"]
140 self.password = kwargs["password"]
141 self.ca_cert = kwargs["ca_cert"]
142 self.sasl_mech = kwargs["sasl_mech"]
143 self.retrieve_mode = kwargs["retrieve_mode"]
144 self.encryption_types = kwargs["encryption_types"]
145
146 self.runner = CmdRunner(
147 module,
148 command="ipa-getkeytab",
149 arg_formats=dict(
150 retrieve_mode=cmd_runner_fmt.as_bool("--retrieve"),
151 path=cmd_runner_fmt.as_opt_val("--keytab"),
152 ipa_host=cmd_runner_fmt.as_opt_val("--server"),
153 principal=cmd_runner_fmt.as_opt_val("--principal"),
154 ldap_uri=cmd_runner_fmt.as_opt_val("--ldapuri"),
155 bind_dn=cmd_runner_fmt.as_opt_val("--binddn"),
156 bind_pw=cmd_runner_fmt.as_opt_val("--bindpw"),
157 password=cmd_runner_fmt.as_opt_val("--password"),
158 ca_cert=cmd_runner_fmt.as_opt_val("--cacert"),
159 sasl_mech=cmd_runner_fmt.as_opt_val("--mech"),
160 encryption_types=cmd_runner_fmt.as_opt_val("--enctypes"),
161 ),
162 )
163
164 def _exec(self, check_rc=True):
165 with self.runner(
166 "retrieve_mode path ipa_host principal ldap_uri bind_dn bind_pw password ca_cert sasl_mech encryption_types",
167 check_rc=check_rc,
168 ) as ctx:
169 rc, out, err = ctx.run()
170 return out
171
172
173 def main():
174 arg_spec = dict(
175 path=dict(type="path", required=True, aliases=["keytab"]),
176 state=dict(default="present", choices=["present", "absent"]),
177 principal=dict(type="str", required=True),
178 ipa_host=dict(type="str"),
179 ldap_uri=dict(type="str"),
180 bind_dn=dict(type="str"),
181 bind_pw=dict(type="str"),
182 password=dict(type="str", no_log=True),
183 ca_cert=dict(type="path"),
184 sasl_mech=dict(type="str", choices=["GSSAPI", "EXTERNAL"]),
185 retrieve_mode=dict(type="bool"),
186 encryption_types=dict(type="str"),
187 force=dict(type="bool"),
188 )
189 module = AnsibleModule(
190 argument_spec=arg_spec,
191 mutually_exclusive=[("ipa_host", "ldap_uri"), ("retrieve_mode", "password")],
192 supports_check_mode=True,
193 )
194
195 path = module.params["path"]
196 state = module.params["state"]
197 force = module.params["force"]
198
199 keytab = IPAKeytab(
200 module,
201 path=path,
202 state=state,
203 principal=module.params["principal"],
204 ipa_host=module.params["ipa_host"],
205 ldap_uri=module.params["ldap_uri"],
206 bind_dn=module.params["bind_dn"],
207 bind_pw=module.params["bind_pw"],
208 password=module.params["password"],
209 ca_cert=module.params["ca_cert"],
210 sasl_mech=module.params["sasl_mech"],
211 retrieve_mode=module.params["retrieve_mode"],
212 encryption_types=module.params["encryption_types"],
213 )
214
215 changed = False
216 if state == "present":
217 if os.path.exists(path):
218 if force and not module.check_mode:
219 try:
220 os.remove(path)
221 except OSError as e:
222 module.fail_json(msg=f"Error deleting: {e.filename} - {e.strerror}.")
223 keytab._exec()
224 changed = True
225 if force and module.check_mode:
226 changed = True
227 else:
228 changed = True
229 keytab._exec()
230
231 if state == "absent":
232 if os.path.exists(path):
233 changed = True
234 if not module.check_mode:
235 try:
236 os.remove(path)
237 except OSError as e:
238 module.fail_json(msg=f"Error deleting: {e.filename} - {e.strerror}.")
239
240 module.exit_json(changed=changed)
241
242
243 if __name__ == "__main__":
244 main()