Debian Codegraph @

dirsrvtests/tests/suites/acl/selfdn_permissions_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2020 Red Hat, Inc.
3 # All rights reserved.
4 #
5 # License: GPL (version 3 or any later version).
6 # See LICENSE for details.
7 # --- END COPYRIGHT BLOCK ---
8 #
9 import logging
10
11 import ldap
12 import pytest
13 from lib389 import Entry
14 from lib389._constants import *
15 from test389.topologies import topology_st
16
17 log = logging.getLogger(__name__)
18
19 from lib389.utils import *
20
21 # Skip on older versions
22 pytestmark = [pytest.mark.tier1,
23 pytest.mark.skipif(ds_is_older('1.3.2'), reason="Not implemented")]
24 OC_NAME = 'OCticket47653'
25 MUST = "(postalAddress $ postalCode)"
26 MAY = "(member $ street)"
27
28 OTHER_NAME = 'other_entry'
29 MAX_OTHERS = 10
30
31 BIND_NAME = 'bind_entry'
32 BIND_DN = 'cn=%s, %s' % (BIND_NAME, SUFFIX)
33 BIND_PW = 'password'
34
35 ENTRY_NAME = 'test_entry'
36 ENTRY_DN = 'cn=%s, %s' % (ENTRY_NAME, SUFFIX)
37 ENTRY_OC = "top person %s" % OC_NAME
38
39
40 def _oc_definition(oid_ext, name, must=None, may=None):
41 oid = "1.2.3.4.5.6.7.8.9.10.%d" % oid_ext
42 desc = 'To test ticket 47490'
43 sup = 'person'
44 if not must:
45 must = MUST
46 if not may:
47 may = MAY
48
49 new_oc = "( %s NAME '%s' DESC '%s' SUP %s AUXILIARY MUST %s MAY %s )" % (oid, name, desc, sup, must, may)
50 return ensure_bytes(new_oc)
51
52
53 @pytest.fixture(scope="module")
54 def allow_user_init(topology_st):
55 """Initialize the test environment
56
57 """
58 topology_st.standalone.log.info("Add %s that allows 'member' attribute" % OC_NAME)
59 new_oc = _oc_definition(2, OC_NAME, must=MUST, may=MAY)
60 topology_st.standalone.schema.add_schema('objectClasses', new_oc)
61
62 # entry used to bind with
63 topology_st.standalone.log.info("Add %s" % BIND_DN)
64 topology_st.standalone.add_s(Entry((BIND_DN, {
65 'objectclass': "top person".split(),
66 'sn': BIND_NAME,
67 'cn': BIND_NAME,
68 'userpassword': BIND_PW})))
69
70 # enable acl error logging
71 mod = [(ldap.MOD_REPLACE, 'nsslapd-errorlog-level', b'128')]
72 topology_st.standalone.modify_s(DN_CONFIG, mod)
73
74 # Remove aci's to start with a clean slate
75 mod = [(ldap.MOD_DELETE, 'aci', None)]
76 topology_st.standalone.modify_s(SUFFIX, mod)
77
78 # add dummy entries
79 for cpt in range(MAX_OTHERS):
80 name = "%s%d" % (OTHER_NAME, cpt)
81 topology_st.standalone.add_s(Entry(("cn=%s,%s" % (name, SUFFIX), {
82 'objectclass': "top person".split(),
83 'sn': name,
84 'cn': name})))
85
86
87 def test_selfdn_permission_add(topology_st, allow_user_init):
88 """Check add entry operation with and without SelfDN aci
89
90 :id: e837a9ef-be92-48da-ad8b-ebf42b0fede1
91 :setup: Standalone instance, add a entry which is used to bind,
92 enable acl error logging by setting 'nsslapd-errorlog-level' to '128',
93 remove aci's to start with a clean slate, and add dummy entries
94 :steps:
95 1. Check we can not ADD an entry without the proper SELFDN aci
96 2. Check with the proper ACI we can not ADD with 'member' attribute
97 3. Check entry to add with memberS and with the ACI
98 4. Check with the proper ACI and 'member' it succeeds to ADD
99 :expectedresults:
100 1. Operation should be successful
101 2. Operation should be successful
102 3. Operation should fail with Insufficient Access
103 4. Operation should be successful
104 """
105 topology_st.standalone.log.info("\n\n######################### ADD ######################\n")
106
107 # bind as bind_entry
108 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
109 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
110
111 # Prepare the entry with multivalued members
112 entry_with_members = Entry(ENTRY_DN)
113 entry_with_members.setValues('objectclass', 'top', 'person', 'OCticket47653')
114 entry_with_members.setValues('sn', ENTRY_NAME)
115 entry_with_members.setValues('cn', ENTRY_NAME)
116 entry_with_members.setValues('postalAddress', 'here')
117 entry_with_members.setValues('postalCode', '1234')
118 members = []
119 for cpt in range(MAX_OTHERS):
120 name = "%s%d" % (OTHER_NAME, cpt)
121 members.append("cn=%s,%s" % (name, SUFFIX))
122 members.append(BIND_DN)
123 entry_with_members.setValues('member', members)
124
125 # Prepare the entry with one member
126 entry_with_member = Entry(ENTRY_DN)
127 entry_with_member.setValues('objectclass', 'top', 'person', 'OCticket47653')
128 entry_with_member.setValues('sn', ENTRY_NAME)
129 entry_with_member.setValues('cn', ENTRY_NAME)
130 entry_with_member.setValues('postalAddress', 'here')
131 entry_with_member.setValues('postalCode', '1234')
132 member = []
133 member.append(BIND_DN)
134 entry_with_member.setValues('member', member)
135
136 # entry to add WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
137 try:
138 topology_st.standalone.log.info("Try to add Add %s (aci is missing): %r" % (ENTRY_DN, entry_with_member))
139
140 topology_st.standalone.add_s(entry_with_member)
141 except Exception as e:
142 topology_st.standalone.log.info("Exception (expected): %s" % type(e).__name__)
143 assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
144
145 # Ok Now add the proper ACI
146 topology_st.standalone.log.info("Bind as %s and add the ADD SELFDN aci" % DN_DM)
147 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
148
149 ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
150 ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
151 ACI_ALLOW = "(version 3.0; acl \"SelfDN add\"; allow (add)"
152 ACI_SUBJECT = " userattr = \"member#selfDN\";)"
153 ACI_BODY = ACI_TARGET + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
154 mod = [(ldap.MOD_ADD, 'aci', ensure_bytes(ACI_BODY))]
155 topology_st.standalone.modify_s(SUFFIX, mod)
156
157 # bind as bind_entry
158 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
159 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
160
161 # entry to add WITHOUT member and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
162 try:
163 topology_st.standalone.log.info("Try to add Add %s (member is missing)" % ENTRY_DN)
164 topology_st.standalone.add_s(Entry((ENTRY_DN, {
165 'objectclass': ENTRY_OC.split(),
166 'sn': ENTRY_NAME,
167 'cn': ENTRY_NAME,
168 'postalAddress': 'here',
169 'postalCode': '1234'})))
170 except Exception as e:
171 topology_st.standalone.log.info("Exception (expected): %s" % type(e).__name__)
172 assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
173
174 # entry to add WITH memberS and WITH the ACI -> ldap.INSUFFICIENT_ACCESS
175 # member should contain only one value
176 try:
177 topology_st.standalone.log.info("Try to add Add %s (with several member values)" % ENTRY_DN)
178 topology_st.standalone.add_s(entry_with_members)
179 except Exception as e:
180 topology_st.standalone.log.info("Exception (expected): %s" % type(e).__name__)
181 assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
182
183 topology_st.standalone.log.info("Try to add Add %s should be successful" % ENTRY_DN)
184 topology_st.standalone.add_s(entry_with_member)
185
186
187 def test_selfdn_permission_search(topology_st, allow_user_init):
188 """Check search operation with and without SelfDN aci
189
190 :id: 06d51ef9-c675-4583-99b2-4852dbda190e
191 :setup: Standalone instance, add a entry which is used to bind,
192 enable acl error logging by setting 'nsslapd-errorlog-level' to '128',
193 remove aci's to start with a clean slate, and add dummy entries
194 :steps:
195 1. Check we can not search an entry without the proper SELFDN aci
196 2. Add proper ACI
197 3. Check we can search with the proper ACI
198 :expectedresults:
199 1. Operation should be successful
200 2. Operation should be successful
201 3. Operation should be successful
202 """
203 topology_st.standalone.log.info("\n\n######################### SEARCH ######################\n")
204 # bind as bind_entry
205 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
206 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
207
208 # entry to search WITH member being BIND_DN but WITHOUT the ACI -> no entry returned
209 topology_st.standalone.log.info("Try to search %s (aci is missing)" % ENTRY_DN)
210 ents = topology_st.standalone.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
211 assert len(ents) == 0
212
213 # Ok Now add the proper ACI
214 topology_st.standalone.log.info("Bind as %s and add the READ/SEARCH SELFDN aci" % DN_DM)
215 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
216
217 ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
218 ACI_TARGETATTR = '(targetattr="*")'
219 ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
220 ACI_ALLOW = "(version 3.0; acl \"SelfDN search-read\"; allow (read, search, compare)"
221 ACI_SUBJECT = " userattr = \"member#selfDN\";)"
222 ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
223 mod = [(ldap.MOD_ADD, 'aci', ensure_bytes(ACI_BODY))]
224 topology_st.standalone.modify_s(SUFFIX, mod)
225
226 # bind as bind_entry
227 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
228 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
229
230 # entry to search with the proper aci
231 topology_st.standalone.log.info("Try to search %s should be successful" % ENTRY_DN)
232 ents = topology_st.standalone.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
233 assert len(ents) == 1
234
235
236 def test_selfdn_permission_modify(topology_st, allow_user_init):
237 """Check modify operation with and without SelfDN aci
238
239 :id: 97a58844-095f-44b0-9029-dd29a7d83d68
240 :setup: Standalone instance, add a entry which is used to bind,
241 enable acl error logging by setting 'nsslapd-errorlog-level' to '128',
242 remove aci's to start with a clean slate, and add dummy entries
243 :steps:
244 1. Check we can not modify an entry without the proper SELFDN aci
245 2. Add proper ACI
246 3. Modify the entry and check the modified value
247 :expectedresults:
248 1. Operation should be successful
249 2. Operation should be successful
250 3. Operation should be successful
251 """
252 # bind as bind_entry
253 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
254 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
255
256 topology_st.standalone.log.info("\n\n######################### MODIFY ######################\n")
257
258 # entry to modify WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
259 try:
260 topology_st.standalone.log.info("Try to modify %s (aci is missing)" % ENTRY_DN)
261 mod = [(ldap.MOD_REPLACE, 'postalCode', b'9876')]
262 topology_st.standalone.modify_s(ENTRY_DN, mod)
263 except Exception as e:
264 topology_st.standalone.log.info("Exception (expected): %s" % type(e).__name__)
265 assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
266
267 # Ok Now add the proper ACI
268 topology_st.standalone.log.info("Bind as %s and add the WRITE SELFDN aci" % DN_DM)
269 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
270
271 ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
272 ACI_TARGETATTR = '(targetattr="*")'
273 ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
274 ACI_ALLOW = "(version 3.0; acl \"SelfDN write\"; allow (write)"
275 ACI_SUBJECT = " userattr = \"member#selfDN\";)"
276 ACI_BODY = ACI_TARGET + ACI_TARGETATTR + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
277 mod = [(ldap.MOD_ADD, 'aci', ensure_bytes(ACI_BODY))]
278 topology_st.standalone.modify_s(SUFFIX, mod)
279
280 # bind as bind_entry
281 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
282 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
283
284 # modify the entry and checks the value
285 topology_st.standalone.log.info("Try to modify %s. It should succeeds" % ENTRY_DN)
286 mod = [(ldap.MOD_REPLACE, 'postalCode', b'1928')]
287 topology_st.standalone.modify_s(ENTRY_DN, mod)
288
289 ents = topology_st.standalone.search_s(ENTRY_DN, ldap.SCOPE_BASE, 'objectclass=*')
290 assert len(ents) == 1
291 assert ensure_str(ents[0].postalCode) == '1928'
292
293
294 def test_selfdn_permission_delete(topology_st, allow_user_init):
295 """Check delete operation with and without SelfDN aci
296
297 :id: 0ec4c0ec-e7b0-4ef1-8373-ab25aae34516
298 :setup: Standalone instance, add a entry which is used to bind,
299 enable acl error logging by setting 'nsslapd-errorlog-level' to '128',
300 remove aci's to start with a clean slate, and add dummy entries
301 :steps:
302 1. Check we can not delete an entry without the proper SELFDN aci
303 2. Add proper ACI
304 3. Check we can perform delete operation with proper ACI
305 :expectedresults:
306 1. Operation should be successful
307 2. Operation should be successful
308 3. Operation should be successful
309 """
310 topology_st.standalone.log.info("\n\n######################### DELETE ######################\n")
311
312 # bind as bind_entry
313 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
314 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
315
316 # entry to delete WITH member being BIND_DN but WITHOUT the ACI -> ldap.INSUFFICIENT_ACCESS
317 try:
318 topology_st.standalone.log.info("Try to delete %s (aci is missing)" % ENTRY_DN)
319 topology_st.standalone.delete_s(ENTRY_DN)
320 except Exception as e:
321 topology_st.standalone.log.info("Exception (expected): %s" % type(e).__name__)
322 assert isinstance(e, ldap.INSUFFICIENT_ACCESS)
323
324 # Ok Now add the proper ACI
325 topology_st.standalone.log.info("Bind as %s and add the READ/SEARCH SELFDN aci" % DN_DM)
326 topology_st.standalone.simple_bind_s(DN_DM, PASSWORD)
327
328 ACI_TARGET = "(target = \"ldap:///cn=*,%s\")" % SUFFIX
329 ACI_TARGETFILTER = "(targetfilter =\"(objectClass=%s)\")" % OC_NAME
330 ACI_ALLOW = "(version 3.0; acl \"SelfDN delete\"; allow (delete)"
331 ACI_SUBJECT = " userattr = \"member#selfDN\";)"
332 ACI_BODY = ACI_TARGET + ACI_TARGETFILTER + ACI_ALLOW + ACI_SUBJECT
333 mod = [(ldap.MOD_ADD, 'aci', ensure_bytes(ACI_BODY))]
334 topology_st.standalone.modify_s(SUFFIX, mod)
335
336 # bind as bind_entry
337 topology_st.standalone.log.info("Bind as %s" % BIND_DN)
338 topology_st.standalone.simple_bind_s(BIND_DN, BIND_PW)
339
340 # entry to delete with the proper aci
341 topology_st.standalone.log.info("Try to delete %s should be successful" % ENTRY_DN)
342 topology_st.standalone.delete_s(ENTRY_DN)
343
344
345 if __name__ == '__main__':
346 # Run isolated
347 # -s for DEBUG mode
348 CURRENT_FILE = os.path.realpath(__file__)
349 pytest.main("-s %s" % CURRENT_FILE)