Debian Codegraph @

dirsrvtests/tests/suites/acl/acivattr_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2026 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 pytest, os, ldap, time
10 from lib389._constants import DEFAULT_SUFFIX, PW_DM
11 from lib389.idm.user import UserAccount
12 from lib389.idm.organization import Organization
13 from lib389.idm.organizationalunit import OrganizationalUnit
14 from lib389.cos import CosTemplate, CosClassicDefinition
15 from test389.topologies import topology_st as topo
16 from lib389.idm.nscontainer import nsContainer
17 from lib389.idm.domain import Domain
18 from lib389.idm.role import FilteredRoles
19
20 pytestmark = pytest.mark.tier1
21
22 DNBASE = "o=acivattr,{}".format(DEFAULT_SUFFIX)
23 ENG_USER = "cn=enguser1,ou=eng,{}".format(DNBASE)
24 SALES_UESER = "cn=salesuser1,ou=sales,{}".format(DNBASE)
25 ENG_MANAGER = "cn=engmanager1,ou=eng,{}".format(DNBASE)
26 SALES_MANAGER = "cn=salesmanager1,ou=sales,{}".format(DNBASE)
27 SALES_OU = "ou=sales,{}".format(DNBASE)
28 ENG_OU = "ou=eng,{}".format(DNBASE)
29 FILTERROLESALESROLE = "cn=FILTERROLESALESROLE,{}".format(DNBASE)
30 FILTERROLEENGROLE = "cn=FILTERROLEENGROLE,{}".format(DNBASE)
31
32
33 @pytest.fixture(scope="function")
34 def aci_of_user(request, topo):
35 aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals('aci')
36
37 def finofaci():
38 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
39 domain.set('aci', None)
40 for i in aci_list:
41 domain.add("aci", i)
42
43 request.addfinalizer(finofaci)
44
45
46 @pytest.fixture(scope="function")
47 def _add_user(request, topo):
48 org = Organization(topo.standalone).create(properties={"o": "acivattr"}, basedn=DEFAULT_SUFFIX)
49 org.add('aci', '(targetattr="*")(targetfilter="(nsrole=*)")(version 3.0; aci "tester"; '
50 'allow(all) userdn="ldap:///cn=enguser1,ou=eng,o=acivattr,{}";)'.format(DEFAULT_SUFFIX))
51
52 ou = OrganizationalUnit(topo.standalone, "ou=eng,o=acivattr,{}".format(DEFAULT_SUFFIX))
53 ou.create(properties={'ou': 'eng'})
54
55 ou = OrganizationalUnit(topo.standalone, "ou=sales,o=acivattr,{}".format(DEFAULT_SUFFIX))
56 ou.create(properties={'ou': 'sales'})
57
58 roles = FilteredRoles(topo.standalone, DNBASE)
59 roles.create(properties={'cn':'FILTERROLEENGROLE', 'nsRoleFilter':'cn=eng*'})
60 roles.create(properties={'cn': 'FILTERROLESALESROLE', 'nsRoleFilter': 'cn=sales*'})
61
62 nsContainer(topo.standalone,
63 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,{}'.format(DEFAULT_SUFFIX)).create(
64 properties={'cn': 'cosTemplates'})
65
66 properties = {'employeeType': 'EngType', 'cn':'"cn=filterRoleEngRole,o=acivattr,dc=example,dc=com",cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,dc=example,dc=com'}
67 CosTemplate(topo.standalone,'cn="cn=filterRoleEngRole,o=acivattr,dc=example,dc=com",'
68 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,{}'.format(DEFAULT_SUFFIX)).\
69 create(properties=properties)
70
71 properties = {'employeeType': 'SalesType', 'cn': '"cn=filterRoleSalesRole,o=acivattr,dc=example,dc=com",cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,dc=example,dc=com'}
72 CosTemplate(topo.standalone,
73 'cn="cn=filterRoleSalesRole,o=acivattr,dc=example,dc=com",cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,'
74 'o=acivattr,{}'.format(DEFAULT_SUFFIX)).create(properties=properties)
75
76 properties = {
77 'cosTemplateDn': 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,{}'.format(DEFAULT_SUFFIX),
78 'cosAttribute': 'employeeType', 'cosSpecifier': 'nsrole', 'cn': 'cosClassicGenerateEmployeeTypeUsingnsrole'}
79 CosClassicDefinition(topo.standalone,
80 'cn=cosClassicGenerateEmployeeTypeUsingnsrole,o=acivattr,{}'.format(DEFAULT_SUFFIX)).create(
81 properties=properties)
82
83 properties = {
84 'uid': 'salesuser1',
85 'cn': 'salesuser1',
86 'sn': 'user',
87 'uidNumber': '1000',
88 'gidNumber': '2000',
89 'homeDirectory': '/home/' + 'salesuser1',
90 'userPassword': PW_DM
91 }
92 user = UserAccount(topo.standalone, 'cn=salesuser1,ou=sales,o=acivattr,{}'.format(DEFAULT_SUFFIX))
93 user.create(properties=properties)
94
95 properties = {
96 'uid': 'salesmanager1',
97 'cn': 'salesmanager1',
98 'sn': 'user',
99 'uidNumber': '1000',
100 'gidNumber': '2000',
101 'homeDirectory': '/home/' + 'salesmanager1',
102 'userPassword': PW_DM,
103 }
104 user = UserAccount(topo.standalone, 'cn=salesmanager1,ou=sales,o=acivattr,{}'.format(DEFAULT_SUFFIX))
105 user.create(properties=properties)
106
107 properties = {
108 'uid': 'enguser1',
109 'cn': 'enguser1',
110 'sn': 'user',
111 'uidNumber': '1000',
112 'gidNumber': '2000',
113 'homeDirectory': '/home/' + 'enguser1',
114 'userPassword': PW_DM
115 }
116 user = UserAccount(topo.standalone, 'cn=enguser1,ou=eng,o=acivattr,{}'.format(DEFAULT_SUFFIX))
117 user.create(properties=properties)
118
119 properties = {
120 'uid': 'engmanager1',
121 'cn': 'engmanager1',
122 'sn': 'user',
123 'uidNumber': '1000',
124 'gidNumber': '2000',
125 'homeDirectory': '/home/' + 'engmanager1',
126 'userPassword': PW_DM
127 }
128 user = UserAccount(topo.standalone, 'cn=engmanager1,ou=eng,o=acivattr,{}'.format(DEFAULT_SUFFIX))
129 user.create(properties=properties)
130
131 def fin():
132 for DN in [ENG_USER,SALES_UESER,ENG_MANAGER,SALES_MANAGER,FILTERROLESALESROLE,FILTERROLEENGROLE,ENG_OU,SALES_OU,
133 'cn="cn=filterRoleEngRole,o=acivattr,dc=example,dc=com",'
134 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,dc=example,dc=com',
135 'cn="cn=filterRoleSalesRole,o=acivattr,dc=example,dc=com",'
136 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,{}'.format(DEFAULT_SUFFIX), 'cn=cosClassicGenerateEmployeeTypeUsingnsroleTemplates,o=acivattr,{}'.format(DEFAULT_SUFFIX),
137 'cn=cosClassicGenerateEmployeeTypeUsingnsrole,o=acivattr,{}'.format(DEFAULT_SUFFIX), DNBASE]:
138 UserAccount(topo.standalone, DN).delete()
139
140 request.addfinalizer(fin)
141
142
143 REAL_EQ_ACI = '(targetattr="*")(targetfilter="(cn=engmanager1)") (version 3.0; acl "real-eq"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
144 REAL_PRES_ACI = '(targetattr="*")(targetfilter="(cn=*)") (version 3.0; acl "real-pres"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
145 REAL_SUB_ACI = '(targetattr="*")(targetfilter="(cn=eng*)") (version 3.0; acl "real-sub"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
146 ROLE_EQ_ACI = '(targetattr="*")(targetfilter="(nsrole=cn=filterroleengrole,o=sun.com)") (version 3.0; acl "role-eq"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
147 ROLE_PRES_ACI = '(targetattr="*")(targetfilter="(nsrole=*)") (version 3.0; acl "role-pres"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
148 ROLE_SUB_ACI = '(targetattr="*")(targetfilter="(nsrole=cn=filterroleeng*)") (version 3.0; acl "role-sub"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
149 COS_EQ_ACI = '(targetattr="*")(targetfilter="(employeetype=engtype)") (version 3.0; acl "cos-eq"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
150 COS_PRES_ACI = '(targetattr="*")(targetfilter="(employeetype=*)") (version 3.0; acl "cos-pres"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
151 COS_SUB_ACI = '(targetattr="*")(targetfilter="(employeetype=eng*)") (version 3.0; acl "cos-sub"; allow (all) userdn="ldap:///{}";)'.format(ENG_USER)
152 LDAPURL_ACI = '(targetattr="*")(version 3.0; acl "url"; allow (all) userdn="ldap:///o=acivattr,dc=example,dc=com??sub?(nsrole=*eng*)";)'
153
154
155 @pytest.mark.parametrize("user,entry,aci", [
156 (ENG_USER, ENG_MANAGER, REAL_EQ_ACI),
157 (ENG_USER, ENG_MANAGER, REAL_PRES_ACI),
158 (ENG_USER, ENG_MANAGER, REAL_SUB_ACI),
159 (ENG_USER, ENG_MANAGER, ROLE_PRES_ACI),
160 (ENG_USER, ENG_MANAGER, ROLE_SUB_ACI),
161 (ENG_USER, ENG_MANAGER, COS_EQ_ACI),
162 (ENG_USER, ENG_MANAGER, COS_PRES_ACI),
163 (ENG_USER, ENG_MANAGER, COS_SUB_ACI),
164 (ENG_USER, ENG_MANAGER, LDAPURL_ACI),
165 ], ids=[
166 "(ENG_USER, ENG_MANAGER, REAL_EQ_ACI)",
167 "(ENG_USER, ENG_MANAGER, REAL_PRES_ACI)",
168 "(ENG_USER, ENG_MANAGER, REAL_SUB_ACI)",
169 "(ENG_USER, ENG_MANAGER, ROLE_PRES_ACI)",
170 '(ENG_USER, ENG_MANAGER, ROLE_SUB_ACI)',
171 '(ENG_USER, ENG_MANAGER, COS_EQ_ACI)',
172 '(ENG_USER, ENG_MANAGER, COS_PRES_ACI)',
173 '(ENG_USER, ENG_MANAGER, COS_SUB_ACI)',
174 '(ENG_USER, ENG_MANAGER, LDAPURL_ACI)',
175 ])
176 def test_positive(topo, _add_user, aci_of_user, user, entry, aci):
177 """Positive testing of ACLs
178
179 :id: ba6d5e9c-786b-11e8-860d-8c16451d917b
180 :parametrized: yes
181 :setup: server
182 :steps:
183 1. Add test entry
184 2. Add ACI
185 3. ACI role should be followed
186 :expectedresults:
187 1. Entry should be added
188 2. Operation should succeed
189 3. Operation should succeed
190 """
191 # set aci
192 Domain(topo.standalone, DNBASE).set("aci", aci)
193 time.sleep(.5)
194
195 # create connection
196 conn = UserAccount(topo.standalone, user).bind(PW_DM)
197 # according to the aci , user will be able to change description
198 UserAccount(conn, entry).replace("description", "Fred")
199 assert UserAccount(conn, entry).present('description')
200
201
202 @pytest.mark.parametrize("user,entry,aci", [
203 (ENG_USER, SALES_MANAGER, REAL_EQ_ACI),
204 (ENG_USER, SALES_OU, REAL_PRES_ACI),
205 (ENG_USER, SALES_MANAGER, REAL_SUB_ACI),
206 (ENG_USER, SALES_MANAGER, ROLE_EQ_ACI),
207 (ENG_USER, SALES_OU, ROLE_PRES_ACI),
208 (ENG_USER, SALES_MANAGER, ROLE_SUB_ACI),
209 (ENG_USER, SALES_MANAGER, COS_EQ_ACI),
210 (ENG_USER, SALES_OU, COS_PRES_ACI),
211 (ENG_USER, SALES_MANAGER, COS_SUB_ACI),
212 (SALES_UESER, SALES_MANAGER, LDAPURL_ACI),
213 (ENG_USER, ENG_MANAGER, ROLE_EQ_ACI),
214 ], ids=[
215
216 "(ENG_USER, SALES_MANAGER, REAL_EQ_ACI)",
217 "(ENG_USER, SALES_OU, REAL_PRES_ACI)",
218 "(ENG_USER, SALES_MANAGER, REAL_SUB_ACI)",
219 "(ENG_USER, SALES_MANAGER, ROLE_EQ_ACI)",
220 "(ENG_USER, SALES_MANAGER, ROLE_PRES_ACI)",
221 '(ENG_USER, SALES_MANAGER, ROLE_SUB_ACI)',
222 '(ENG_USER, SALES_MANAGER, COS_EQ_ACI)',
223 '(ENG_USER, SALES_MANAGER, COS_PRES_ACI)',
224 '(ENG_USER, SALES_MANAGER, COS_SUB_ACI)',
225 '(SALES_UESER, SALES_MANAGER, LDAPURL_ACI)',
226 '(ENG_USER, ENG_MANAGER, ROLE_EQ_ACI)'
227
228
229 ])
230 def test_negative(topo, _add_user, aci_of_user, user, entry, aci):
231 """Negative testing of ACLs
232
233 :id: c4c887c2-786b-11e8-a328-8c16451d917b
234 :parametrized: yes
235 :setup: server
236 :steps:
237 1. Add test entry
238 2. Add ACI
239 3. ACI role should be followed
240 :expectedresults:
241 1. Entry should be added
242 2. Operation should succeed
243 3. Operation should not succeed
244 """
245 # set aci
246 Domain(topo.standalone, DNBASE).set("aci", aci)
247 time.sleep(.5)
248 # create connection
249 conn = UserAccount(topo.standalone, user).bind(PW_DM)
250 # according to the aci , user will not be able to change description
251 with pytest.raises(ldap.INSUFFICIENT_ACCESS):
252 UserAccount(conn, entry).replace("description", "Fred")
253
254
255 if __name__ == "__main__":
256 CURRENT_FILE = os.path.realpath(__file__)
257 pytest.main("-s -v %s" % CURRENT_FILE)