Debian Codegraph @

ansible_collections/community/proxmox/plugins/modules/proxmox_domain.py

raw
1 #!/usr/bin/python
2 #
3 # Copyright (c) 2026, (@teslamania) <[email protected]>
4 # GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt)
5 # SPDX-License-Identifier: GPL-3.0-or-later
6
7
8 DOCUMENTATION = r"""
9 module: proxmox_domain
10 version_added: 2.0.0
11 short_description: Manage authentication realms.
12 description:
13 - Add, modify or delete domain authentication realms.
14 - See L(Authentication Realms,https://pve.proxmox.com/pve-docs/chapter-pveum.html#user-realms-pam)
15 author: Vial Nicolas (@teslamania)
16 attributes:
17 check_mode:
18 support: full
19 diff_mode:
20 support: none
21
22 options:
23 openid_acr_values:
24 description:
25 - Defines the Authentication Context Class Reference values requested from the Authorization Server for the Authentication Request.
26 - Supported for O(type=openid).
27 type: str
28 openid_autocreate:
29 description:
30 - Automatically create users if they do not exist.
31 - Supported for O(type=openid).
32 type: bool
33 ldap_base_dn:
34 description:
35 - LDAP base domain name.
36 - Required when O(type=ldap).
37 type: str
38 ldap_bind_dn:
39 description:
40 - LDAP user bind domain name.
41 - Supported for O(type=ldap) and O(type=ad).
42 type: str
43 aliases: ["ad_bind_dn"]
44 ad_case_sensitive:
45 description:
46 - Choose if username is case-sensitive or not.
47 - Supported for O(type=ad).
48 type: bool
49 default: True
50 comment:
51 description: Description of the realm.
52 type: str
53 openid_client_id:
54 description:
55 - OpenID Client ID.
56 - Required when O(type=openid).
57 type: str
58 openid_client_key:
59 description:
60 - OpenID Client key.
61 - Supported for O(type=openid).
62 type: str
63 default:
64 description: Use as default realm.
65 type: bool
66 ad_domain:
67 description:
68 - AD domain name.
69 - Required when O(type=ad).
70 type: str
71 ldap_filter:
72 description:
73 - LDAP filter for user sync.
74 - Supported for O(type=ldap) and O(type=ad).
75 type: str
76 aliases: ["ad_filter"]
77 openid_issuer_url:
78 description:
79 - OpenID Issuer Url.
80 - Required when O(type=openid).
81 type: str
82 openid_groups_autocreate:
83 description:
84 - Automatically create users if they do not exist.
85 - Supported for O(type=openid).
86 type: bool
87 openid_groups_claim:
88 description:
89 - OpenID claim used to retrieve groups with.
90 - Supported for O(type=openid).
91 type: str
92 openid_groups_overwrite:
93 description:
94 - All groups will be overwritten for the user on login.
95 - Supported for O(type=openid).
96 type: bool
97 ldap_group_classes:
98 description:
99 - The object class for group (groupOfNames, group, univentionGroup, ipausergroup).
100 - Supported for O(type=ldap) and O(type=ad).
101 type: str
102 aliases: ["ad_group_classes"]
103 ldap_group_filter:
104 description:
105 - LDAP filter for group sync.
106 - Supported for O(type=ldap) and O(type=ad).
107 type: str
108 aliases: ["ad_group_filter"]
109 ldap_group_name_attr:
110 description:
111 - LDAP group attribute name.
112 - Supported for O(type=ldap) and O(type=ad).
113 type: str
114 aliases: ["ad_group_name_attr"]
115 ldap_mode:
116 description:
117 - LDAP protocol mode.
118 - Supported for O(type=ldap) and O(type=ad).
119 choices: ['ldap', 'ldaps', 'ldap+starttls']
120 type: str
121 aliases: ["ad_mode"]
122 ldap_password:
123 description:
124 - LDAP bind password.
125 - Supported for O(type=ldap) and O(type=ad).
126 type: str
127 aliases: ["ad_password"]
128 ldap_port:
129 description:
130 - Server port.
131 - Supported for O(type=ldap) and O(type=ad).
132 type: int
133 aliases: ["ad_port"]
134 openid_prompt:
135 description:
136 - Specifies whether the Authorization Server prompts the End-User for reauthentication and consent.
137 - Supported for with O(type=openid).
138 type: str
139 openid_query_userinfo:
140 description:
141 - Enables querying the userinfo endpoint for claims values.
142 - Supported for with O(type=openid).
143 type: bool
144 realm:
145 description: Arbitrary string used to identify the login realm in Proxmox.
146 required: true
147 type: str
148 openid_scopes:
149 description:
150 - Specifies the scopes (user details) that should be authorized and returned, for example 'email' or 'profile'.
151 - Supported for O(type=openid).
152 type: str
153 ldap_primary_server:
154 description:
155 - Server ip address or dns name.
156 - Required when O(type=ldap) or O(type=ad).
157 type: str
158 aliases: ["ad_primary_server"]
159 ldap_secondary_server:
160 description:
161 - Fallback server ip address or dns name.
162 - Supported for O(type=ldap) and O(type=ad).
163 type: str
164 aliases: ["ad_secondary_server"]
165 state:
166 description:
167 - Indicates if the realm should be present or absent.
168 required: true
169 choices: ['present', 'absent']
170 type: str
171 ldap_sync_defaults_options:
172 description:
173 - The defaults options for behavior of synchronizations.
174 - Supported for O(type=ldap) and O(type=ad).
175 type: dict
176 aliases: ["ad_sync_defaults_options"]
177 suboptions:
178 scope:
179 description: Select what to sync.
180 choices: ['users', 'groups', 'both']
181 type: str
182 enable_new:
183 description: Enable creation of new users.
184 type: bool
185 remove_vanished:
186 description:
187 - Semicolon-separated list of things to remove when they or the user vanishes during a sync.
188 - The following values are possible
189 - C(remove_vanished=acl) removes acls when the user/group is not returned from the sync.
190 - C(remove_vanished=properties) removes the set properties on existing user/group that do not appear in the source (even custom ones).
191 - C(remove_vanished=entry) removes the user/group when not returned from the sync.
192 - Instead of a list it also can be C(remove_vanishe=none).
193 - Example C(remove_vanished="acl;properties;entry").
194 type: str
195 type:
196 description:
197 - Realm type.
198 - Required when O(state=present).
199 choices: ['ad', 'ldap', 'openid']
200 type: str
201 openid_username_claim:
202 description:
203 - OpenID claim used to generate the unique username.
204 - Supported for O(type=openid).
205 type: str
206 ldap_user_attr:
207 description:
208 - LDAP user attribute name.
209 - Required when O(type=ldap).
210 type: str
211 ldap_user_classes:
212 description:
213 - The object class for user (inetorgperson, posixaccount, person, user).
214 - Supported for O(type=ldap) and O(type=ad).
215 type: str
216 aliases: ["ad_user_classes"]
217 ldap_validate_certs:
218 description:
219 - Verify the server's SSL certificate.
220 - Supported for O(type=ldap) and O(type=ad).
221 type: bool
222 aliases: ["ad_validate_certs"]
223
224 extends_documentation_fragment:
225 - community.proxmox.proxmox.actiongroup_proxmox
226 - community.proxmox.proxmox.documentation
227 - community.proxmox.attributes
228 - community.proxmox.attributes.info_module
229 """
230
231
232 EXAMPLES = r"""
233 - name: Add LDAP domain
234 community.proxmox.proxmox_domain:
235 realm: "example.test"
236 state: present
237 type: "ldap"
238 default: True
239 ldap_base_dn: "cn=accounts,dc=example,dc=test"
240 ldap_bind_dn: "uid=sa-proxmox,cn=users,cn=accounts,dc=example,dc=test"
241 ldap_filter: "memberof=cn=admins-proxmox,cn=groups,cn=accounts,dc=example,dc=test"
242 ldap_group_filter: "cn=admins-proxmox"
243 ldap_group_name_attr: "cn"
244 ldap_mode: "ldaps"
245 ldap_password: XXXXX
246 ldap_primary_server: "ipa.example.test"
247 ldap_user_attr: "uid"
248 ldap_validate_certs: False
249 ldap_sync_defaults_options:
250 scope: "both"
251 enable_new: True
252 remove_vanished: "acl;properties;entry"
253
254 - name: Add AD domain
255 community.proxmox.proxmox_domain:
256 ad_domain: "ADDOMAIN"
257 realm: "ad"
258 state: present
259 type: "ad"
260 comment: "AD"
261 ad_mode: "ldap"
262 ad_password: XXXXXXXX
263 ad_primary_server: "ad.exemple.test"
264 ad_sync_defaults_options:
265 scope: "both"
266 enable_new: True
267 remove_vanished: "acl;properties;entry"
268
269 - name: Add OpenID domain
270 community.proxmox.proxmox_domain:
271 realm: "openid"
272 state: present
273 type: "openid"
274 openid_client_id: idoftheclient
275 openid_client_key: keyoftheclient
276 openid_issuer_url: "https://example.test/openid-server"
277
278 - name: Remove domain
279 community.proxmox.proxmox_domain:
280 realm: "ipa.example.test"
281 state: absent
282 """
283
284 RETURN = r"""
285 msg:
286 description: The output message that the module generates.
287 type: str
288 returned: always
289 """
290
291
292 from ansible_collections.community.proxmox.plugins.module_utils.proxmox import (
293 ProxmoxAnsible,
294 ansible_to_proxmox_bool,
295 create_proxmox_module,
296 )
297
298
299 def module_args():
300 return dict(
301 realm=dict(type="str", required=True),
302 state=dict(choices=["present", "absent"], required=True),
303 type=dict(choices=["ad", "ldap", "openid"]),
304 comment=dict(type="str"),
305 default=dict(type="bool"),
306 ad_domain=dict(type="str"),
307 ad_case_sensitive=dict(type="bool", default=True),
308 ldap_base_dn=dict(type="str"),
309 ldap_user_attr=dict(type="str"),
310 ldap_bind_dn=dict(aliases=["ad_bind_dn"], type="str"),
311 ldap_filter=dict(aliases=["ad_filter"], type="str"),
312 ldap_group_filter=dict(aliases=["ad_group_filter"], type="str"),
313 ldap_group_classes=dict(aliases=["ad_group_classes"], type="str"),
314 ldap_group_name_attr=dict(aliases=["ad_group_name_attr"], type="str"),
315 ldap_mode=dict(aliases=["ad_mode"], choices=["ldap", "ldaps", "ldap+starttls"]),
316 ldap_password=dict(aliases=["ad_password"], type="str", no_log=True),
317 ldap_port=dict(aliases=["ad_port"], type="int"),
318 ldap_primary_server=dict(aliases=["ad_primary_server"], type="str"),
319 ldap_secondary_server=dict(aliases=["ad_secondary_server"], type="str"),
320 ldap_user_classes=dict(aliases=["ad_user_classes"], type="str"),
321 ldap_validate_certs=dict(aliases=["ad_validate_certs"], type="bool"),
322 ldap_sync_defaults_options=dict(
323 aliases=["ad_sync_defaults_options"],
324 type="dict",
325 options={
326 "enable_new": dict(type="bool"),
327 "remove_vanished": dict(type="str"),
328 "scope": dict(choices=["users", "groups", "both"]),
329 },
330 ),
331 openid_acr_values=dict(type="str"),
332 openid_autocreate=dict(type="bool"),
333 openid_client_id=dict(type="str"),
334 openid_client_key=dict(type="str", no_log=True),
335 openid_groups_autocreate=dict(type="bool"),
336 openid_groups_claim=dict(type="str"),
337 openid_groups_overwrite=dict(type="bool"),
338 openid_issuer_url=dict(type="str"),
339 openid_prompt=dict(type="str"),
340 openid_query_userinfo=dict(type="bool"),
341 openid_scopes=dict(type="str"),
342 openid_username_claim=dict(type="str"),
343 )
344
345
346 def module_options():
347 return dict(
348 required_if=[
349 ("type", "ldap", ["ldap_base_dn"]),
350 ("type", "ldap", ["ldap_user_attr"]),
351 ("type", "ldap", ["ldap_primary_server"]),
352 ("type", "ad", ["ldap_primary_server"]),
353 ("type", "ad", ["ad_domain"]),
354 ("type", "openid", ["openid_issuer_url"]),
355 ("type", "openid", ["openid_client_id"]),
356 ("state", "present", ["type"]),
357 ],
358 )
359
360
361 class ProxmoxDomainAnsible(ProxmoxAnsible):
362 def __init__(self, module):
363 super().__init__(module)
364 self.params = module.params
365
366 def check_domain(self, realm):
367 domains = self.proxmox_api.access.domains.get()
368 return realm in [item["realm"] for item in domains]
369
370 def get_domain(self, realm):
371 return self.proxmox_api.access.domains.get(realm)
372
373 def is_equal(self, params, current):
374 return all(params[k] == current.get(k) for k in params if k not in ("password", "realm"))
375
376 def get_params_from_list(self, params_list):
377 params = {
378 k: ansible_to_proxmox_bool(v) if isinstance(v, bool) else v
379 for k, v in self.params.items()
380 if v is not None and k in params_list
381 }
382 return params
383
384 def get_params_from_dict(self, params_dict):
385 params = {
386 params_dict[k]: ansible_to_proxmox_bool(v) if isinstance(v, bool) else v
387 for k, v in self.params.items()
388 if v is not None and k in params_dict
389 }
390 return params
391
392 def get_domain_params(self):
393 list_domain_params = [
394 "comment",
395 "default",
396 "type",
397 "realm",
398 ]
399 return self.get_params_from_list(list_domain_params)
400
401 def get_ldap_params(self):
402 ldap_params = {
403 "base_dn": self.params["ldap_base_dn"],
404 "user_attr": self.params["ldap_user_attr"],
405 }
406 return ldap_params
407
408 def get_ad_params(self):
409 ad_params = {
410 "domain": self.params["ad_domain"],
411 "case-sensitive": ansible_to_proxmox_bool(self.params["ad_case_sensitive"]),
412 }
413 return ad_params
414
415 def get_sync_params(self):
416 dict_sync_params = {
417 "ldap_bind_dn": "bind_dn",
418 "ldap_filter": "filter",
419 "ldap_group_classes": "group_classes",
420 "ldap_group_filter": "group_filter",
421 "ldap_group_name_attr": "group_name_attr",
422 "ldap_mode": "mode",
423 "ldap_port": "port",
424 "ldap_password": "password",
425 "ldap_primary_server": "server1",
426 "ldap_secondary_server": "server2",
427 "ldap_user_classes": "user_classes",
428 "ldap_validate_certs": "verify",
429 }
430 return self.get_params_from_dict(dict_sync_params)
431
432 def get_sync_options_param(self):
433 sync_option = self.params["ldap_sync_defaults_options"]
434 if sync_option is not None:
435 options = []
436 if sync_option.get("enable_new"):
437 options.append(f"enable-new={ansible_to_proxmox_bool(sync_option.get('enable_new'))}")
438 if sync_option.get("remove_vanished") is not None:
439 options.append(f"remove-vanished={sync_option.get('remove_vanished')}")
440 if sync_option.get("scope") is not None:
441 options.append(f"scope={sync_option.get('scope')}")
442 param = {"sync-defaults-options": ",".join(options)}
443 else:
444 param = {}
445 return param
446
447 def get_openid_params(self):
448 list_openid_params = [
449 "openid_acr_values",
450 "openid_autocreate",
451 "openid_client_id",
452 "openid_client_key",
453 "openid_groups_autocreate",
454 "openid_groups_claim",
455 "openid_groups_overwrite",
456 "openid_issuer_url",
457 "openid_prompt",
458 "openid_query_userinfo",
459 "openid_scopes",
460 "openid_username_claim",
461 ]
462 openid_param = self.get_params_from_list(list_openid_params)
463 # Remove the openid_
464 # On the api all the parameters are with a - and not a _
465 openid_params = {k.replace("openid_", "").replace("_", "-"): v for k, v in openid_param.items()}
466 return openid_params
467
468 def add_domain(self):
469 domain_params = self.get_domain_params()
470 if self.params["type"] == "ldap":
471 type_params = {
472 **self.get_ldap_params(),
473 **self.get_sync_params(),
474 **self.get_sync_options_param(),
475 }
476 elif self.params["type"] == "ad":
477 type_params = {
478 **self.get_ad_params(),
479 **self.get_sync_params(),
480 **self.get_sync_options_param(),
481 }
482 elif self.params["type"] == "openid":
483 type_params = self.get_openid_params()
484
485 params = {**domain_params, **type_params}
486
487 if self.check_domain(self.params["realm"]):
488 current = self.get_domain(self.params["realm"])
489 if self.is_equal(params, current):
490 self.module.exit_json(
491 changed=False,
492 msg=f"Domain {self.params['realm']} already exists.",
493 )
494 else:
495 if not self.module.check_mode:
496 params.pop("type")
497 self.proxmox_api.access.domains(self.params["realm"]).put(**params)
498 msg = f"Domain {self.params['realm']} edited."
499 else:
500 msg = f"Domain {self.params['realm']} would be edited."
501 self.module.exit_json(changed=True, msg=msg)
502 else:
503 if not self.module.check_mode:
504 self.proxmox_api.access.domains.create(**params)
505 msg = f"Domain {self.params['realm']} added."
506 else:
507 msg = f"Domain {self.params['realm']} would be added."
508 self.module.exit_json(changed=True, msg=msg)
509
510 def del_domain(self):
511 if self.check_domain(self.params["realm"]):
512 if not self.module.check_mode:
513 self.proxmox_api.access.domains(self.params["realm"]).delete()
514 msg = f"Domain {self.params['realm']} deleted."
515 else:
516 msg = f"Domain {self.params['realm']} would be deleted."
517
518 self.module.exit_json(changed=True, msg=msg)
519 else:
520 self.module.exit_json(changed=False, msg=f"Domain {self.params['realm']} not present.")
521
522
523 def main():
524 module = create_proxmox_module(module_args(), **module_options())
525 proxmox = ProxmoxDomainAnsible(module)
526 state = module.params["state"]
527
528 if state == "present":
529 proxmox.add_domain()
530
531 elif state == "absent":
532 proxmox.del_domain()
533
534
535 if __name__ == "__main__":
536 main()