| 1 |
#!/usr/bin/python |
| 2 |
# -*- coding: utf-8 -*- |
| 3 |
|
| 4 |
# GNU General Public License v3.0+ (see COPYING or https://www.gnu.org/licenses/gpl-3.0.txt) |
| 5 |
|
| 6 |
from __future__ import absolute_import, division, print_function |
| 7 |
|
| 8 |
__metaclass__ = type |
| 9 |
|
| 10 |
|
| 11 |
DOCUMENTATION = r""" |
| 12 |
--- |
| 13 |
module: zabbix_user_directory |
| 14 |
short_description: Create/update/delete Zabbix user directories |
| 15 |
description: |
| 16 |
- This module allows you to create, modify and delete Zabbix user directories. |
| 17 |
author: |
| 18 |
- Evgeny Yurchenko (@BGmot) |
| 19 |
requirements: |
| 20 |
- python >= 3.9 |
| 21 |
options: |
| 22 |
name: |
| 23 |
description: |
| 24 |
- Unique name of the user directory. |
| 25 |
required: true |
| 26 |
type: str |
| 27 |
idp_type: |
| 28 |
description: |
| 29 |
- Type of IdP. Only one user directory of type SAML can exist. |
| 30 |
- This parameter is available since Zabbix 6.4. |
| 31 |
required: false |
| 32 |
type: str |
| 33 |
choices: ["ldap", "saml"] |
| 34 |
provision_status: |
| 35 |
description: |
| 36 |
- User directory provisioning status. |
| 37 |
- if I(false) Provisioning of users created by this user directory is disabled |
| 38 |
- if I(true) Provisioning of users created by this user directory is enabled. |
| 39 |
Additionally, the authentication status of C(ldap_jit_status) or C(saml_jit_status) should be enabled. |
| 40 |
- This parameter is available since Zabbix 6.4. |
| 41 |
required: false |
| 42 |
type: bool |
| 43 |
default: false |
| 44 |
user_username: |
| 45 |
description: |
| 46 |
- LDAP/SAML attribute name to use for users.name field when user is provisioned |
| 47 |
- This parameter is available since Zabbix 6.4. |
| 48 |
required: false |
| 49 |
type: str |
| 50 |
user_lastname: |
| 51 |
description: |
| 52 |
- LDAP/SAML attribute name to use for users.surname field when user is provisioned |
| 53 |
- This parameter is available since Zabbix 6.4. |
| 54 |
required: false |
| 55 |
type: str |
| 56 |
user_ref_attr: |
| 57 |
description: |
| 58 |
- LDAP user object attribute name. Will be set instead of the placeholder I(%{ref}) in c(group_filter) string. |
| 59 |
- This parameter is available since Zabbix 6.4. |
| 60 |
required: false |
| 61 |
type: str |
| 62 |
description: |
| 63 |
description: |
| 64 |
- User directory description. |
| 65 |
required: false |
| 66 |
type: str |
| 67 |
default: "" |
| 68 |
group_membership: |
| 69 |
description: |
| 70 |
- LDAP property containing groups of user. E.g. I(memberOf) |
| 71 |
- This parameter is available since Zabbix 6.4. |
| 72 |
required: false |
| 73 |
type: str |
| 74 |
group_basedn: |
| 75 |
description: |
| 76 |
- LDAP groups path in LDAP tree to search for groups data. |
| 77 |
- Used to configure user membership check in I(openLDAP). |
| 78 |
- Required if group_membership is not set. |
| 79 |
- This parameter is available since Zabbix 6.4. |
| 80 |
type: str |
| 81 |
group_name: |
| 82 |
description: |
| 83 |
- LDAP/SAML attribute name to get group name for group mapping between Zabbix and IdP. |
| 84 |
- Used to configure user membership check in LDAP. |
| 85 |
- Ignored when provisioning a user if group_membership is set. |
| 86 |
- This parameter is available since Zabbix 6.4. |
| 87 |
required: false |
| 88 |
type: str |
| 89 |
group_member: |
| 90 |
description: |
| 91 |
- LDAP tree attribute name containing group name received with C(group_filter) query. |
| 92 |
- Used to configure user membership check in openLDAP. |
| 93 |
- Ignored when provisioning a user if group_membership is set. |
| 94 |
- This parameter is available since Zabbix 6.4. |
| 95 |
type: str |
| 96 |
group_filter: |
| 97 |
description: |
| 98 |
- LDAP search filter to select groups when searching for specific user groups. |
| 99 |
- Used to configure user membership check in openLDAP. |
| 100 |
- Ignored when provisioning a user if group_membership is set. |
| 101 |
- This parameter is available since Zabbix 6.4. |
| 102 |
type: str |
| 103 |
required: false |
| 104 |
bind_password: |
| 105 |
description: |
| 106 |
- LDAP bind password. Can be empty for anonymous binding. |
| 107 |
required: false |
| 108 |
type: str |
| 109 |
search_filter: |
| 110 |
description: |
| 111 |
- LDAP custom filter string when authenticating user in LDAP. |
| 112 |
- Supported search_filter placeholders |
| 113 |
- I(%{attr}) search attribute name (uid, sAMAccountName); |
| 114 |
- I(%{user}) username value. |
| 115 |
default: (%{attr}=%{user}) |
| 116 |
required: false |
| 117 |
type: str |
| 118 |
start_tls: |
| 119 |
description: |
| 120 |
- LDAP startTLS option. It cannot be used with ldaps:// protocol hosts. |
| 121 |
required: false |
| 122 |
type: int |
| 123 |
default: 0 |
| 124 |
choices: [0, 1] |
| 125 |
host: |
| 126 |
description: |
| 127 |
- LDAP server host name, IP or URI. URI should contain schema, host and port (optional). |
| 128 |
- required if C(idp_type) is set to I(ldap). |
| 129 |
required: false |
| 130 |
type: str |
| 131 |
port: |
| 132 |
description: |
| 133 |
- LDAP server port. |
| 134 |
- required if C(idp_type) is set to I(ldap). |
| 135 |
required: false |
| 136 |
type: int |
| 137 |
base_dn: |
| 138 |
description: |
| 139 |
- LDAP base distinguished name string. |
| 140 |
- required if C(idp_type) is set to I(ldap). |
| 141 |
required: false |
| 142 |
type: str |
| 143 |
search_attribute: |
| 144 |
description: |
| 145 |
- LDAP attribute name to identify user by username in Zabbix database. |
| 146 |
- required if C(idp_type) is set to I(ldap). |
| 147 |
required: false |
| 148 |
type: str |
| 149 |
bind_dn: |
| 150 |
description: |
| 151 |
- LDAP bind distinguished name string. Can be empty for anonymous binding. |
| 152 |
required: false |
| 153 |
type: str |
| 154 |
default: "" |
| 155 |
idp_entityid: |
| 156 |
description: |
| 157 |
- SAML URI that identifies the IdP in SAML messages. |
| 158 |
- required if C(idp_type) is set to I(saml). |
| 159 |
- This parameter is available since Zabbix 6.4. |
| 160 |
required: false |
| 161 |
type: str |
| 162 |
sp_entityid: |
| 163 |
description: |
| 164 |
- SAML SP entity ID. |
| 165 |
- required if C(idp_type) is set to I(saml). |
| 166 |
- This parameter is available since Zabbix 6.4. |
| 167 |
required: false |
| 168 |
type: str |
| 169 |
sso_url: |
| 170 |
description: |
| 171 |
- SAML URL of the IdP"s SAML SSO service, to which Zabbix will send SAML authentication requests. |
| 172 |
- required if C(idp_type) is set to I(saml). |
| 173 |
- This parameter is available since Zabbix 6.4. |
| 174 |
required: false |
| 175 |
type: str |
| 176 |
slo_url: |
| 177 |
description: |
| 178 |
- SAML IdP service endpoint URL to which Zabbix will send SAML logout requests. |
| 179 |
- This parameter is available since Zabbix 6.4. |
| 180 |
required: false |
| 181 |
type: str |
| 182 |
username_attribute: |
| 183 |
description: |
| 184 |
- SAML username attribute to be used in comparison with Zabbix user.username value when authenticating. |
| 185 |
- required if C(idp_type) is set to I(saml). |
| 186 |
- This parameter is available since Zabbix 6.4. |
| 187 |
required: false |
| 188 |
type: str |
| 189 |
nameid_format: |
| 190 |
description: |
| 191 |
- SAML SP name ID format. |
| 192 |
- This parameter is available since Zabbix 6.4. |
| 193 |
required: false |
| 194 |
type: str |
| 195 |
scim_status: |
| 196 |
description: |
| 197 |
- Whether the SCIM provisioning for SAML is enabled or disabled. |
| 198 |
- This parameter is available since Zabbix 6.4. |
| 199 |
required: false |
| 200 |
type: bool |
| 201 |
default: false |
| 202 |
encrypt_nameid: |
| 203 |
description: |
| 204 |
- SAML encrypt name ID. Encrypts if I(true). |
| 205 |
- This parameter is available since Zabbix 6.4. |
| 206 |
required: false |
| 207 |
type: bool |
| 208 |
default: false |
| 209 |
encrypt_assertions: |
| 210 |
description: |
| 211 |
- SAML encrypt assertions. Encrypts if I(true). |
| 212 |
- This parameter is available since Zabbix 6.4. |
| 213 |
required: false |
| 214 |
type: bool |
| 215 |
default: false |
| 216 |
sign_messages: |
| 217 |
description: |
| 218 |
- SAML sign messages. Signs if I(true). |
| 219 |
- This parameter is available since Zabbix 6.4. |
| 220 |
required: false |
| 221 |
type: bool |
| 222 |
default: false |
| 223 |
sign_assertions: |
| 224 |
description: |
| 225 |
- SAML sign assertions. Signs if I(true). |
| 226 |
- This parameter is available since Zabbix 6.4. |
| 227 |
required: false |
| 228 |
type: bool |
| 229 |
default: false |
| 230 |
sign_authn_requests: |
| 231 |
description: |
| 232 |
- SAML sign AuthN requests. Signs if I(true). |
| 233 |
- This parameter is available since Zabbix 6.4. |
| 234 |
required: false |
| 235 |
type: bool |
| 236 |
default: false |
| 237 |
sign_logout_requests: |
| 238 |
description: |
| 239 |
- SAML sign logout requests. Signs if I(true). |
| 240 |
- This parameter is available since Zabbix 6.4. |
| 241 |
required: false |
| 242 |
type: bool |
| 243 |
default: false |
| 244 |
sign_logout_responses: |
| 245 |
description: |
| 246 |
- SAML sign logout responses. Signs if I(true). |
| 247 |
- This parameter is available since Zabbix 6.4. |
| 248 |
required: false |
| 249 |
type: bool |
| 250 |
default: false |
| 251 |
provision_media: |
| 252 |
type: list |
| 253 |
elements: dict |
| 254 |
description: |
| 255 |
- Array of the IdP media type mappings objects. |
| 256 |
- This parameter is available since Zabbix 6.4. |
| 257 |
suboptions: |
| 258 |
name: |
| 259 |
description: |
| 260 |
- Visible name in the list of media type mappings. |
| 261 |
type: str |
| 262 |
required: true |
| 263 |
mediatype: |
| 264 |
description: |
| 265 |
- Name of media type to be created. |
| 266 |
type: str |
| 267 |
required: true |
| 268 |
attribute: |
| 269 |
description: |
| 270 |
- Attribute name. Used as the value for the I(sendto) field. |
| 271 |
- If present in data received from IdP and the value is not empty, will trigger media creation for the provisioned user. |
| 272 |
type: str |
| 273 |
required: true |
| 274 |
provision_groups: |
| 275 |
type: list |
| 276 |
elements: dict |
| 277 |
description: |
| 278 |
- Array of the IdP media type mappings objects. |
| 279 |
- This parameter is available since Zabbix 6.4. |
| 280 |
suboptions: |
| 281 |
name: |
| 282 |
description: |
| 283 |
- IdP group full name. |
| 284 |
- Supports the wildcard character "*". Unique across all provisioning groups mappings. |
| 285 |
type: str |
| 286 |
required: true |
| 287 |
role: |
| 288 |
description: |
| 289 |
- User role name to assign to the user. |
| 290 |
- Note that if multiple provisioning groups mappings are matched, the role of the highest user type will be assigned to the user. |
| 291 |
If there are multiple roles with the same user type, the first role (sorted in alphabetical order) will be assigned to the user. |
| 292 |
type: str |
| 293 |
required: true |
| 294 |
user_groups: |
| 295 |
type: list |
| 296 |
elements: str |
| 297 |
description: |
| 298 |
- Array of Zabbix user group names. |
| 299 |
- Note that if multiple provisioning groups mappings are matched, Zabbix user groups of all matched mappings will be assigned to the user. |
| 300 |
required: true |
| 301 |
state: |
| 302 |
description: |
| 303 |
- State of the user directory. |
| 304 |
- On C(present), it will create if user directory does not exist or update it if the associated data is different. |
| 305 |
- On C(absent) will remove the user directory if it exists. |
| 306 |
choices: ["present", "absent"] |
| 307 |
default: "present" |
| 308 |
type: str |
| 309 |
|
| 310 |
extends_documentation_fragment: |
| 311 |
- community.zabbix.zabbix |
| 312 |
|
| 313 |
""" |
| 314 |
|
| 315 |
EXAMPLES = r""" |
| 316 |
--- |
| 317 |
# If you want to use Username and Password to be authenticated by Zabbix Server |
| 318 |
- name: Set credentials to access Zabbix Server API |
| 319 |
ansible.builtin.set_fact: |
| 320 |
ansible_user: Admin |
| 321 |
ansible_httpapi_pass: zabbix |
| 322 |
|
| 323 |
# If you want to use API token to be authenticated by Zabbix Server |
| 324 |
# https://www.zabbix.com/documentation/current/en/manual/web_interface/frontend_sections/administration/general#api-tokens |
| 325 |
- name: Set API token |
| 326 |
ansible.builtin.set_fact: |
| 327 |
ansible_zabbix_auth_key: 8ec0d52432c15c91fcafe9888500cf9a607f44091ab554dbee860f6b44fac895 |
| 328 |
|
| 329 |
- name: Create new user directory or update existing info (Zabbix <= 6.2) |
| 330 |
# set task level variables as we change ansible_connection plugin here |
| 331 |
vars: |
| 332 |
ansible_network_os: community.zabbix.zabbix |
| 333 |
ansible_connection: httpapi |
| 334 |
ansible_httpapi_port: 443 |
| 335 |
ansible_httpapi_use_ssl: true |
| 336 |
ansible_httpapi_validate_certs: false |
| 337 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 338 |
ansible_host: zabbix-example-fqdn.org |
| 339 |
community.zabbix.zabbix_user_directory: |
| 340 |
state: present |
| 341 |
name: TestUserDirectory |
| 342 |
host: "test.com" |
| 343 |
port: 389 |
| 344 |
base_dn: "ou=Users,dc=example,dc=org" |
| 345 |
search_attribute: "uid" |
| 346 |
bind_dn: "cn=ldap_search,dc=example,dc=org" |
| 347 |
description: "Test user directory" |
| 348 |
search_filter: "(%{attr}=test_user)" |
| 349 |
start_tls: 0 |
| 350 |
|
| 351 |
- name: Create new user directory with LDAP IDP or update existing info (Zabbix >= 6.4) |
| 352 |
# set task level variables as we change ansible_connection plugin here |
| 353 |
vars: |
| 354 |
ansible_network_os: community.zabbix.zabbix |
| 355 |
ansible_connection: httpapi |
| 356 |
ansible_httpapi_port: 443 |
| 357 |
ansible_httpapi_use_ssl: true |
| 358 |
ansible_httpapi_validate_certs: false |
| 359 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 360 |
ansible_host: zabbix-example-fqdn.org |
| 361 |
community.zabbix.zabbix_user_directory: |
| 362 |
state: present |
| 363 |
name: TestUserDirectory |
| 364 |
idp_type: ldap |
| 365 |
host: "test.ca" |
| 366 |
port: 389 |
| 367 |
base_dn: "ou=Users,dc=example,dc=org" |
| 368 |
search_attribute: "uid" |
| 369 |
provision_status: true |
| 370 |
group_name: cn |
| 371 |
group_basedn: ou=Group,dc=example,dc=org |
| 372 |
group_member: member |
| 373 |
user_ref_attr: uid |
| 374 |
group_filter: "(member=uid=%{ref},ou=Users,dc=example,dc=com)" |
| 375 |
user_username: first_name |
| 376 |
user_lastname: last_name |
| 377 |
provision_media: |
| 378 |
- name: Media1 |
| 379 |
mediatype: Email |
| 380 |
attribute: email1 |
| 381 |
provision_groups: |
| 382 |
- name: idpname1 |
| 383 |
role: Guest role |
| 384 |
user_groups: |
| 385 |
- Guests |
| 386 |
|
| 387 |
- name: Create new user directory with SAML IDP or update existing info (Zabbix >= 6.4) |
| 388 |
# set task level variables as we change ansible_connection plugin here |
| 389 |
vars: |
| 390 |
ansible_network_os: community.zabbix.zabbix |
| 391 |
ansible_connection: httpapi |
| 392 |
ansible_httpapi_port: 443 |
| 393 |
ansible_httpapi_use_ssl: true |
| 394 |
ansible_httpapi_validate_certs: false |
| 395 |
ansible_zabbix_url_path: "zabbixeu" # If Zabbix WebUI runs on non-default (zabbix) path ,e.g. http://<FQDN>/zabbixeu |
| 396 |
ansible_host: zabbix-example-fqdn.org |
| 397 |
community.zabbix.zabbix_user_directory: |
| 398 |
state: present |
| 399 |
name: TestUserDirectory |
| 400 |
idp_type: saml |
| 401 |
idp_entityid: http://okta.com/xxxxx |
| 402 |
sp_entityid: zabbix |
| 403 |
sso_url: http://xxxx.okta.com/app/xxxxxx_123dhu8o3 |
| 404 |
username_attribute: usrEmail |
| 405 |
provision_status: true |
| 406 |
group_name: cn |
| 407 |
user_username: first_name |
| 408 |
user_lastname: last_name |
| 409 |
provision_media: |
| 410 |
- name: Media1 |
| 411 |
mediatype: Email |
| 412 |
attribute: email1 |
| 413 |
provision_groups: |
| 414 |
- name: idpname1 |
| 415 |
role: Guest role |
| 416 |
user_groups: |
| 417 |
- Guests |
| 418 |
""" |
| 419 |
|
| 420 |
|
| 421 |
from ansible.module_utils.basic import AnsibleModule |
| 422 |
|
| 423 |
from ansible_collections.community.zabbix.plugins.module_utils.base import ZabbixBase |
| 424 |
from ansible.module_utils.compat.version import LooseVersion |
| 425 |
import ansible_collections.community.zabbix.plugins.module_utils.helpers as zabbix_utils |
| 426 |
|
| 427 |
|
| 428 |
def main(): |
| 429 |
argument_spec = zabbix_utils.zabbix_common_argument_spec() |
| 430 |
argument_spec.update( |
| 431 |
dict( |
| 432 |
name=dict(type="str", required=True), |
| 433 |
idp_type=dict(type="str", required=False, choices=["ldap", "saml"]), |
| 434 |
host=dict(type="str", required=False), |
| 435 |
port=dict(type="int", required=False), |
| 436 |
base_dn=dict(type="str", required=False), |
| 437 |
search_attribute=dict(type="str", required=False), |
| 438 |
bind_dn=dict(type="str", required=False, default=""), |
| 439 |
bind_password=dict(type="str", required=False, no_log=True), |
| 440 |
description=dict(type="str", required=False, default=""), |
| 441 |
search_filter=dict(type="str", default="(%{attr}=%{user})", required=False), |
| 442 |
start_tls=dict(type="int", required=False, choices=[0, 1], default=0), |
| 443 |
idp_entityid=dict(type="str", required=False), |
| 444 |
sp_entityid=dict(type="str", required=False), |
| 445 |
sso_url=dict(type="str", required=False), |
| 446 |
slo_url=dict(type="str", required=False), |
| 447 |
username_attribute=dict(type="str", required=False), |
| 448 |
nameid_format=dict(type="str", required=False), |
| 449 |
scim_status=dict(type="bool", required=False, default=False), |
| 450 |
encrypt_nameid=dict(type="bool", required=False, default=False), |
| 451 |
encrypt_assertions=dict(type="bool", required=False, default=False), |
| 452 |
sign_messages=dict(type="bool", required=False, default=False), |
| 453 |
sign_assertions=dict(type="bool", required=False, default=False), |
| 454 |
sign_authn_requests=dict(type="bool", required=False, default=False), |
| 455 |
sign_logout_requests=dict(type="bool", required=False, default=False), |
| 456 |
sign_logout_responses=dict(type="bool", required=False, default=False), |
| 457 |
provision_status=dict(type="bool", required=False, default=False), |
| 458 |
group_basedn=dict(type="str", required=False), |
| 459 |
group_filter=dict(type="str", required=False), |
| 460 |
group_member=dict(type="str", required=False), |
| 461 |
group_membership=dict(type="str", required=False), |
| 462 |
group_name=dict(type="str", required=False), |
| 463 |
user_lastname=dict(type="str", required=False), |
| 464 |
user_ref_attr=dict(type="str", required=False), |
| 465 |
user_username=dict(type="str", required=False), |
| 466 |
provision_media=dict( |
| 467 |
type="list", |
| 468 |
required=False, |
| 469 |
elements="dict", |
| 470 |
options=dict( |
| 471 |
name=dict(type="str", required=True), |
| 472 |
mediatype=dict(type="str", required=True), |
| 473 |
attribute=dict(type="str", required=True), |
| 474 |
), |
| 475 |
), |
| 476 |
provision_groups=dict( |
| 477 |
type="list", |
| 478 |
required=False, |
| 479 |
elements="dict", |
| 480 |
options=dict( |
| 481 |
name=dict(type="str", required=True), |
| 482 |
role=dict(type="str", required=True), |
| 483 |
user_groups=dict(type="list", elements="str", required=True), |
| 484 |
), |
| 485 |
), |
| 486 |
state=dict(type="str", default="present", choices=["present", "absent"]), |
| 487 |
) |
| 488 |
) |
| 489 |
|
| 490 |
module = AnsibleModule(argument_spec=argument_spec, supports_check_mode=True) |
| 491 |
""" For future when < 7.0 disappears we should use this, now we cannot do this as at this point Zabbix version is unknown |
| 492 |
module = AnsibleModule( |
| 493 |
argument_spec=argument_spec, |
| 494 |
supports_check_mode=True, |
| 495 |
required_if=[ |
| 496 |
("state", "present", ("idp_type",)), |
| 497 |
("idp_type", "ldap", ("host", "port", "base_dn", "search_attribute"), False), |
| 498 |
("idp_type", "saml", ("idp_entityid", "sp_entityid", "sso_url", "username_attribute"), False), |
| 499 |
("provision_status", "true", ("provision_groups")) |
| 500 |
] |
| 501 |
) |
| 502 |
""" |
| 503 |
|
| 504 |
parameters = {"name": module.params["name"]} |
| 505 |
for p in [ |
| 506 |
"host", |
| 507 |
"port", |
| 508 |
"base_dn", |
| 509 |
"search_attribute", |
| 510 |
"bind_dn", |
| 511 |
"bind_password", |
| 512 |
"description", |
| 513 |
"start_tls", |
| 514 |
]: |
| 515 |
if module.params[p]: |
| 516 |
if p in ["port", "start_tls"]: |
| 517 |
parameters[p] = str(module.params[p]) |
| 518 |
else: |
| 519 |
parameters[p] = module.params[p] |
| 520 |
|
| 521 |
state = module.params["state"] |
| 522 |
|
| 523 |
user_directory = ZabbixBase(module) |
| 524 |
|
| 525 |
if LooseVersion(user_directory._zbx_api_version) < LooseVersion("7.0"): |
| 526 |
module.fail_json(msg="Zabbix < 7.0 does not support user directories.") |
| 527 |
|
| 528 |
if LooseVersion(user_directory._zbx_api_version) < LooseVersion("7.0"): |
| 529 |
parameters["search_filter"] = module.params["search_filter"] |
| 530 |
directory = user_directory._zapi.userdirectory.get( |
| 531 |
{"filter": {"name": parameters["name"]}} |
| 532 |
) |
| 533 |
else: |
| 534 |
# Zabbix >= 6.4 |
| 535 |
# Mandatory parameters check |
| 536 |
if state == "present" and not module.params["idp_type"]: |
| 537 |
module.fail_json( |
| 538 |
"'idp_type' parameter must be provided when state is 'present'" |
| 539 |
) |
| 540 |
if module.params["idp_type"]: |
| 541 |
if module.params["idp_type"] == "ldap" and ( |
| 542 |
not module.params["host"] |
| 543 |
or not module.params["port"] |
| 544 |
or not module.params["base_dn"] |
| 545 |
or not module.params["search_attribute"] |
| 546 |
): |
| 547 |
module.fail_json( |
| 548 |
"'host', 'port', 'base_dn', 'search_attribute' must be provided when idp_type is 'ldap'" |
| 549 |
) |
| 550 |
if module.params["idp_type"] == "saml" and ( |
| 551 |
not module.params["idp_entityid"] |
| 552 |
or not module.params["sp_entityid"] |
| 553 |
or not module.params["sso_url"] |
| 554 |
or not module.params["username_attribute"] |
| 555 |
): |
| 556 |
module.fail_json( |
| 557 |
"'idp_entityid', 'sp_entityid', 'sso_url', 'username_attribute' must be provided when idp_type is 'ldap'" |
| 558 |
) |
| 559 |
|
| 560 |
directory = user_directory._zapi.userdirectory.get( |
| 561 |
{ |
| 562 |
"search": {"name": parameters["name"]}, |
| 563 |
"selectProvisionMedia": "extend", |
| 564 |
"selectProvisionGroups": "extend", |
| 565 |
} |
| 566 |
) |
| 567 |
parameters["idp_type"] = str( |
| 568 |
zabbix_utils.helper_to_numeric_value( |
| 569 |
["", "ldap", "saml"], module.params["idp_type"] |
| 570 |
) |
| 571 |
) |
| 572 |
if parameters["idp_type"] == "1": |
| 573 |
# idp_type is ldap |
| 574 |
parameters["search_filter"] = module.params["search_filter"] |
| 575 |
elif parameters["idp_type"] == "2": |
| 576 |
# idp_type is saml |
| 577 |
for p in [ |
| 578 |
"idp_entityid", |
| 579 |
"sso_url", |
| 580 |
"username_attribute", |
| 581 |
"sp_entityid", |
| 582 |
"slo_url", |
| 583 |
"nameid_format", |
| 584 |
]: |
| 585 |
# str parameters |
| 586 |
if module.params[p]: |
| 587 |
parameters[p] = module.params[p] |
| 588 |
for p in [ |
| 589 |
"scim_status", |
| 590 |
"encrypt_nameid", |
| 591 |
"encrypt_assertions", |
| 592 |
"sign_messages", |
| 593 |
"sign_assertions", |
| 594 |
"sign_authn_requests", |
| 595 |
"sign_logout_requests", |
| 596 |
"sign_logout_responses", |
| 597 |
]: |
| 598 |
# boolean parameters |
| 599 |
if module.params[p]: |
| 600 |
parameters[p] = str(int(module.params[p])) |
| 601 |
|
| 602 |
if module.params["provision_status"]: |
| 603 |
parameters["provision_status"] = int(module.params["provision_status"]) |
| 604 |
|
| 605 |
if module.params["provision_media"]: |
| 606 |
if ( |
| 607 |
"provision_status" not in parameters |
| 608 |
or not parameters["provision_status"] |
| 609 |
): |
| 610 |
module.fail_json( |
| 611 |
"'provision_status' must be True to define 'provision_media'" |
| 612 |
) |
| 613 |
parameters["provision_media"] = [] |
| 614 |
for media in module.params["provision_media"]: |
| 615 |
media_type_name = media["mediatype"] |
| 616 |
media_type_ids = user_directory._zapi.mediatype.get( |
| 617 |
{"filter": {"name": media_type_name}} |
| 618 |
) |
| 619 |
if not media_type_ids: |
| 620 |
module.fail_json("Mediatype '%s' cannot be found" % media_type_name) |
| 621 |
parameters["provision_media"].append( |
| 622 |
{ |
| 623 |
"name": media["name"], |
| 624 |
"mediatypeid": media_type_ids[0]["mediatypeid"], |
| 625 |
"attribute": media["attribute"], |
| 626 |
} |
| 627 |
) |
| 628 |
|
| 629 |
if module.params["provision_groups"]: |
| 630 |
if ( |
| 631 |
"provision_status" not in parameters |
| 632 |
or not parameters["provision_status"] |
| 633 |
): |
| 634 |
module.fail_json( |
| 635 |
"'provision_status' must be True to define 'provision_groups'" |
| 636 |
) |
| 637 |
parameters["provision_groups"] = [] |
| 638 |
for group in module.params["provision_groups"]: |
| 639 |
role_name = group["role"] |
| 640 |
role_ids = user_directory._zapi.role.get( |
| 641 |
{"filter": {"name": role_name}} |
| 642 |
) |
| 643 |
if not role_ids: |
| 644 |
module.fail_json("Role '%s' cannot be found" % role_name) |
| 645 |
user_groups = [] |
| 646 |
for user_group in group["user_groups"]: |
| 647 |
ug_ids = user_directory._zapi.usergroup.get( |
| 648 |
{"filter": {"name": user_group}} |
| 649 |
) |
| 650 |
if not ug_ids: |
| 651 |
module.fail_json("User group '%s' cannot be found" % user_group) |
| 652 |
user_groups.append({"usrgrpid": ug_ids[0]["usrgrpid"]}) |
| 653 |
parameters["provision_groups"].append( |
| 654 |
{ |
| 655 |
"name": group["name"], |
| 656 |
"roleid": role_ids[0]["roleid"], |
| 657 |
"user_groups": user_groups, |
| 658 |
} |
| 659 |
) |
| 660 |
for p in [ |
| 661 |
"group_basedn", |
| 662 |
"group_filter", |
| 663 |
"group_member", |
| 664 |
"group_membership", |
| 665 |
"group_name", |
| 666 |
"group_name", |
| 667 |
"user_lastname", |
| 668 |
"user_ref_attr", |
| 669 |
"user_username", |
| 670 |
]: |
| 671 |
if module.params[p]: |
| 672 |
parameters[p] = module.params[p] |
| 673 |
|
| 674 |
if not directory: |
| 675 |
# No User Directory found with given name |
| 676 |
if state == "absent": |
| 677 |
module.exit_json( |
| 678 |
changed=False, |
| 679 |
msg="User directory not found. Not changed: %s" % parameters["name"], |
| 680 |
) |
| 681 |
|
| 682 |
elif state == "present": |
| 683 |
if module.check_mode: |
| 684 |
module.exit_json(changed=True) |
| 685 |
else: |
| 686 |
user_directory._zapi.userdirectory.create(parameters) |
| 687 |
module.exit_json( |
| 688 |
changed=True, |
| 689 |
result="Successfully added user directory %s" % parameters["name"], |
| 690 |
) |
| 691 |
else: |
| 692 |
# User Directory with given name exists |
| 693 |
if state == "absent": |
| 694 |
if module.check_mode: |
| 695 |
module.exit_json(changed=True) |
| 696 |
user_directory._zapi.userdirectory.delete([directory[0]["userdirectoryid"]]) |
| 697 |
module.exit_json( |
| 698 |
changed=True, |
| 699 |
result="Successfully deleted user directory %s" % parameters["name"], |
| 700 |
) |
| 701 |
elif state == "present": |
| 702 |
diff_dict = {} |
| 703 |
if "provision_status" in directory[0]: |
| 704 |
# Zabbix API returns provision_status as str we need it as int to correctly compare |
| 705 |
directory[0]["provision_status"] = int(directory[0]["provision_status"]) |
| 706 |
if zabbix_utils.helper_compare_dictionaries( |
| 707 |
parameters, directory[0], diff_dict |
| 708 |
): |
| 709 |
parameters["userdirectoryid"] = directory[0]["userdirectoryid"] |
| 710 |
if module.check_mode: |
| 711 |
module.exit_json(changed=True) |
| 712 |
user_directory._zapi.userdirectory.update(parameters) |
| 713 |
module.exit_json( |
| 714 |
changed=True, |
| 715 |
result="Successfully updated user directory %s" |
| 716 |
% parameters["name"], |
| 717 |
) |
| 718 |
else: |
| 719 |
module.exit_json( |
| 720 |
changed=False, |
| 721 |
result="User directory %s is up-to date" % parameters["name"], |
| 722 |
) |
| 723 |
|
| 724 |
module.exit_json( |
| 725 |
changed=False, |
| 726 |
result="User directory %s is up-to date" % parameters["name"], |
| 727 |
) |
| 728 |
|
| 729 |
|
| 730 |
if __name__ == "__main__": |
| 731 |
main() |