Debian Codegraph @

ansible_collections/theforeman/foreman/plugins/modules/auth_source_ldap.py

raw symbols SCIP
1 #!/usr/bin/python
2 # -*- coding: utf-8 -*-
3 # (c) 2019 Christoffer Reijer (Basalt AB)
4 #
5 # This program is free software: you can redistribute it and/or modify
6 # it under the terms of the GNU General Public License as published by
7 # the Free Software Foundation, either version 3 of the License, or
8 # (at your option) any later version.
9 #
10 # This program is distributed in the hope that it will be useful,
11 # but WITHOUT ANY WARRANTY; without even the implied warranty of
12 # MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
13 # GNU General Public License for more details.
14 #
15 # You should have received a copy of the GNU General Public License
16 # along with this program. If not, see <http://www.gnu.org/licenses/>.
17
18 from __future__ import absolute_import, division, print_function
19 __metaclass__ = type
20
21
22 DOCUMENTATION = '''
23 ---
24 module: auth_source_ldap
25 version_added: 1.0.0
26 short_description: Manage LDAP Authentication Sources
27 description:
28 - Create, update, and delete LDAP authentication sources
29 author:
30 - "Christoffer Reijer (@ephracis) Basalt AB"
31 options:
32 name:
33 description: The name of the LDAP authentication source
34 required: true
35 type: str
36 host:
37 description: The hostname of the LDAP server
38 required: true
39 type: str
40 port:
41 description: The port number of the LDAP server
42 required: false
43 type: int
44 default: 389
45 account:
46 description: Account name to use when accessing the LDAP server.
47 required: false
48 type: str
49 account_password:
50 description:
51 - Account password to use when accessing the LDAP server.
52 - Required when using I(onthefly_register).
53 - When this parameter is set, the module will not be idempotent.
54 required: false
55 type: str
56 base_dn:
57 description: The base DN to use when searching.
58 required: false
59 type: str
60 attr_login:
61 description:
62 - Attribute containing login ID.
63 - Required when using I(onthefly_register).
64 required: false
65 type: str
66 attr_firstname:
67 description:
68 - Attribute containing first name.
69 - Required when using I(onthefly_register).
70 required: false
71 type: str
72 attr_lastname:
73 description:
74 - Attribute containing last name.
75 - Required when using I(onthefly_register).
76 required: false
77 type: str
78 attr_mail:
79 description:
80 - Attribute containing email address.
81 - Required when using I(onthefly_register).
82 required: false
83 type: str
84 attr_photo:
85 description: Attribute containing user photo
86 required: false
87 type: str
88 onthefly_register:
89 description: Whether or not to register users on the fly.
90 required: false
91 type: bool
92 usergroup_sync:
93 description: Whether or not to sync external user groups on login
94 required: false
95 type: bool
96 tls:
97 description: Whether or not to use TLS when contacting the LDAP server.
98 required: false
99 type: bool
100 groups_base:
101 description: Base DN where groups reside.
102 required: false
103 type: str
104 use_netgroups:
105 description:
106 - Whether to use NIS netgroups instead of posix groups, not valid for I(server_type=active_directory)
107 - "Deprecated: The I(use_netgroups) parameter is deprecated with Foreman 3.16
108 in favor of I(ldap_group_membership) and will be removed in a future release."
109 required: false
110 type: bool
111 ldap_group_membership:
112 description: Which group membership method to use, not valid for I(server_type=active_directory). Option I(rfc4519) is valid only for I(server_type=posix).
113 required: false
114 type: str
115 choices: ["posix", "nis_netgroups", "rfc4519"]
116 server_type:
117 description: Type of the LDAP server
118 required: false
119 choices: ["free_ipa", "active_directory", "posix"]
120 type: str
121 ldap_filter:
122 description: Filter to apply to LDAP searches
123 required: false
124 type: str
125 extends_documentation_fragment:
126 - theforeman.foreman.foreman
127 - theforeman.foreman.foreman.entity_state
128 - theforeman.foreman.foreman.taxonomy
129 '''
130
131 EXAMPLES = '''
132 - name: Simple FreeIPA authentication source
133 theforeman.foreman.auth_source_ldap:
134 name: "Example LDAP"
135 host: "ldap.example.org"
136 server_url: "https://foreman.example.com"
137 locations:
138 - "Uppsala"
139 organizations:
140 - "Sweden"
141 username: "admin"
142 password: "changeme"
143 state: present
144
145 - name: FreeIPA with automatic registration
146 theforeman.foreman.auth_source_ldap:
147 name: "Example LDAP"
148 host: "ldap.example.org"
149 onthefly_register: true
150 account: uid=ansible,cn=sysaccounts,cn=etc,dc=example,dc=com
151 account_password: secret
152 base_dn: dc=example,dc=com
153 groups_base: cn=groups,cn=accounts, dc=example,dc=com
154 server_type: free_ipa
155 attr_login: uid
156 attr_firstname: givenName
157 attr_lastname: sn
158 attr_mail: mail
159 attr_photo: jpegPhoto
160 server_url: "https://foreman.example.com"
161 username: "admin"
162 password: "changeme"
163 state: present
164
165 - name: Active Directory with automatic registration
166 theforeman.foreman.auth_source_ldap:
167 name: "Example AD"
168 host: "ad.example.org"
169 onthefly_register: true
170 account: EXAMPLE\\ansible
171 account_password: secret
172 base_dn: cn=Users,dc=example,dc=com
173 groups_base: cn=Users,dc=example,dc=com
174 server_type: active_directory
175 attr_login: sAMAccountName
176 attr_firstname: givenName
177 attr_lastname: sn
178 attr_mail: mail
179 ldap_filter: (memberOf=CN=Domain Users,CN=Users,DC=example,DC=com)
180 server_url: "https://foreman.example.com"
181 username: "admin"
182 password: "changeme"
183 state: present
184 '''
185
186 RETURN = '''
187 entity:
188 description: Final state of the affected entities grouped by their type.
189 returned: success
190 type: dict
191 contains:
192 auth_source_ldaps:
193 description: List of auth sources for LDAP.
194 type: list
195 elements: dict
196 '''
197
198
199 from ansible_collections.theforeman.foreman.plugins.module_utils.foreman_helper import ForemanTaxonomicEntityAnsibleModule
200
201
202 class ForemanAuthSourceLdapModule(ForemanTaxonomicEntityAnsibleModule):
203 pass
204
205
206 def main():
207 module = ForemanAuthSourceLdapModule(
208 foreman_spec=dict(
209 name=dict(required=True),
210 host=dict(required=True),
211 port=dict(type='int', default=389),
212 account=dict(),
213 account_password=dict(no_log=True),
214 base_dn=dict(),
215 attr_login=dict(),
216 attr_firstname=dict(),
217 attr_lastname=dict(),
218 attr_mail=dict(),
219 attr_photo=dict(),
220 onthefly_register=dict(type='bool'),
221 usergroup_sync=dict(type='bool'),
222 tls=dict(type='bool'),
223 groups_base=dict(),
224 server_type=dict(choices=["free_ipa", "active_directory", "posix"]),
225 ldap_filter=dict(),
226 use_netgroups=dict(type='bool'),
227 ldap_group_membership=dict(choices=["posix", "nis_netgroups", "rfc4519"]),
228 ),
229 required_if=[['onthefly_register', True, ['attr_login', 'attr_firstname', 'attr_lastname', 'attr_mail']]],
230 )
231
232 # additional parameter checks
233 use_netgroups = module.foreman_params.get('use_netgroups')
234 server_type = module.foreman_params.get('server_type', 'posix')
235
236 if server_type == 'active_directory' and ('ldap_group_membership' in module.foreman_params or 'use_netgroups' in module.foreman_params):
237 module.fail_json(msg='ldap_group_membership and use_netgroup params cannot be used when server_type=active_directory')
238
239 if 'ldap_group_membership' in module.foreman_params and module.foreman_params['ldap_group_membership'] == 'rfc4519' and server_type != 'posix':
240 module.fail_json(msg=f'ldap_group_membership=rfc4519 cannot be used when server_type={server_type}')
241
242 with module.api_connection():
243 _supported_params, unsupported_params = module.foremanapi.validate_payload('auth_source_ldaps', 'create', module.foreman_params)
244 # Priority: ldap_group_membership > use_netgroups > use_netgroups derived from ldap_group_membership
245 if 'ldap_group_membership' in unsupported_params:
246 ldap_group_membership = module.foreman_params.pop('ldap_group_membership')
247 if use_netgroups is None:
248 derived = ldap_group_membership == 'nis_netgroups'
249 module.warn(
250 f"Server does not support ldap_group_membership parameter and no use_netgroups value was provided, using derived use_netgroups={derived}"
251 )
252 module.foreman_params['use_netgroups'] = derived
253
254 module.run()
255
256
257 if __name__ == '__main__':
258 main()