| 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 |
""" |
| 10 |
This script will test different type of user attributes. |
| 11 |
""" |
| 12 |
|
| 13 |
import os |
| 14 |
import pytest |
| 15 |
|
| 16 |
from lib389._constants import DEFAULT_SUFFIX, PW_DM |
| 17 |
from lib389.idm.user import UserAccount, UserAccounts |
| 18 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 19 |
from lib389.idm.group import Groups |
| 20 |
from lib389.idm.role import ManagedRoles |
| 21 |
from test389.topologies import topology_st as topo |
| 22 |
|
| 23 |
import ldap |
| 24 |
|
| 25 |
pytestmark = pytest.mark.tier1 |
| 26 |
|
| 27 |
|
| 28 |
OU = f"ou=Accounting,{DEFAULT_SUFFIX}" |
| 29 |
OU_2 = f"ou=Inheritance,{DEFAULT_SUFFIX}" |
| 30 |
CAN = f"uid=Anuj Borah,{OU}" |
| 31 |
CANNOT = f"uid=Ananda Borah,{OU}" |
| 32 |
LEVEL_0 = f"uid=Grandson,{OU_2}" |
| 33 |
LEVEL_1 = f"uid=Child,{OU_2}" |
| 34 |
LEVEL_2 = f"uid=Parent,{OU_2}" |
| 35 |
LEVEL_3 = f"uid=Grandparent,{OU_2}" |
| 36 |
LEVEL_4 = f"uid=Ancestor,{OU_2}" |
| 37 |
ROLE1 = f'cn=ROLE1,{OU}' |
| 38 |
ROLE2 = f'cn=ROLE2,{OU}' |
| 39 |
NSSIMPLEGROUP = f'cn=NSSIMPLEGROUP,{OU}' |
| 40 |
NSSIMPLEGROUP1 = f'cn=NSSIMPLEGROUP1,{OU}' |
| 41 |
ROLEDNACCESS = f'uid=ROLEDNACCESS,{OU}' |
| 42 |
USERDNACCESS = f'uid=USERDNACCESS,{OU}' |
| 43 |
GROUPDNACCESS = f'uid=GROUPDNACCESS,{OU}' |
| 44 |
LDAPURLACCESS = f'uid=LDAPURLACCESS,{OU}' |
| 45 |
ATTRNAMEACCESS = f'uid=ATTRNAMEACCESS,{OU}' |
| 46 |
ANCESTORS = f'ou=ANCESTORS,{OU_2}' |
| 47 |
GRANDPARENTS = f'ou=GRANDPARENTS,{ANCESTORS}' |
| 48 |
PARENTS = f'ou=PARENTS,{GRANDPARENTS}' |
| 49 |
CHILDREN = f'ou=CHILDREN,{PARENTS}' |
| 50 |
GRANDSONS = f'ou=GRANDSONS,{CHILDREN}' |
| 51 |
|
| 52 |
|
| 53 |
@pytest.fixture(scope="module") |
| 54 |
def _add_user(topo): |
| 55 |
""" |
| 56 |
This function will create user for the test and in the end entries will be deleted . |
| 57 |
""" |
| 58 |
role_aci_body = '(targetattr="*")(version 3.0; aci "role aci"; allow(all)' |
| 59 |
# Creating OUs |
| 60 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 61 |
ou_accounting = ous.create(properties={'ou': 'Accounting'}) |
| 62 |
ou_accounting.set('aci', [f'(target="ldap:///{ROLEDNACCESS}"){role_aci_body} ' |
| 63 |
f'userattr = "Description#ROLEDN";)', |
| 64 |
f'(target="ldap:///{USERDNACCESS}"){role_aci_body} ' |
| 65 |
f'userattr = "Description#USERDN";)', |
| 66 |
f'(target="ldap:///{GROUPDNACCESS}"){role_aci_body} ' |
| 67 |
f'userattr = "Description#GROUPDN";)', |
| 68 |
f'(target="ldap:///{LDAPURLACCESS}"){role_aci_body} ' |
| 69 |
f'userattr = "Description#LDAPURL";)', |
| 70 |
f'(target="ldap:///{ATTRNAMEACCESS}"){role_aci_body} ' |
| 71 |
f'userattr = "Description#4612";)']) |
| 72 |
|
| 73 |
ou_inheritance = ous.create(properties={'ou': 'Inheritance', |
| 74 |
'street': LEVEL_4, |
| 75 |
'seeAlso': LEVEL_3, |
| 76 |
'st': LEVEL_2, |
| 77 |
'description': LEVEL_1, |
| 78 |
'businessCategory': LEVEL_0}) |
| 79 |
|
| 80 |
inheritance_aci_body = '(targetattr="*")(version 3.0; aci "Inheritance aci"; allow(all) ' |
| 81 |
ou_inheritance.set('aci', [f'{inheritance_aci_body} ' |
| 82 |
f'userattr = "parent[0].businessCategory#USERDN";)', |
| 83 |
f'{inheritance_aci_body} ' |
| 84 |
f'userattr = "parent[0,1].description#USERDN";)', |
| 85 |
f'{inheritance_aci_body} ' |
| 86 |
f'userattr = "parent[0,1,2].st#USERDN";)', |
| 87 |
f'{inheritance_aci_body} ' |
| 88 |
f'userattr = "parent[0,1,2,3].seeAlso#USERDN";)', |
| 89 |
f'{inheritance_aci_body} ' |
| 90 |
f'userattr = "parent[0,1,2,3,4].street#USERDN";)']) |
| 91 |
|
| 92 |
# Creating Users |
| 93 |
users = UserAccounts(topo.standalone, OU, rdn=None) |
| 94 |
|
| 95 |
for i in [['Anuj Borah', 'Sunnyvale', ROLE1, '4612'], |
| 96 |
['Ananda Borah', 'Santa Clara', ROLE2, 'Its Unknown']]: |
| 97 |
users.create(properties={ |
| 98 |
'uid': i[0], |
| 99 |
'cn': i[0].split()[0], |
| 100 |
'sn': 'user', |
| 101 |
'uidNumber': '1000', |
| 102 |
'gidNumber': '2000', |
| 103 |
'homeDirectory': '/home/' + i[0].split()[0], |
| 104 |
'userPassword': PW_DM, |
| 105 |
'givenname': i[0].split()[0], |
| 106 |
'l': i[1], |
| 107 |
'mail': "[email protected]", |
| 108 |
'telephonenumber': "+1 408 555 4798", |
| 109 |
'facsimiletelephonenumber': "+1 408 555 9751", |
| 110 |
'roomnumber': i[3], |
| 111 |
'Description': i[3], |
| 112 |
'nsroledn': i[2] |
| 113 |
}) |
| 114 |
|
| 115 |
for demo1 in [('ROLEDNACCESS', ROLE1), |
| 116 |
('USERDNACCESS', CAN), |
| 117 |
('GROUPDNACCESS', NSSIMPLEGROUP), |
| 118 |
('ATTRNAMEACCESS', '4612'), |
| 119 |
('LDAPURLACCESS', f"ldap:///{DEFAULT_SUFFIX}??sub?(l=Sunnyvale)")]: |
| 120 |
users.create(properties={ |
| 121 |
'uid': demo1[0], |
| 122 |
'cn': demo1[0], |
| 123 |
'sn': 'user', |
| 124 |
'uidNumber': '1000', |
| 125 |
'gidNumber': '2000', |
| 126 |
'homeDirectory': '/home/' + demo1[0], |
| 127 |
'userPassword': PW_DM, |
| 128 |
'Description': demo1[1] |
| 129 |
}) |
| 130 |
|
| 131 |
# Creating roles |
| 132 |
roles = ManagedRoles(topo.standalone, OU) |
| 133 |
for i in ['ROLE1', 'ROLE2']: |
| 134 |
roles.create(properties={"cn": i}) |
| 135 |
|
| 136 |
# Creating Groups |
| 137 |
grps = Groups(topo.standalone, OU, rdn=None) |
| 138 |
for i in [('NSSIMPLEGROUP', CAN), ('NSSIMPLEGROUP1', CANNOT)]: |
| 139 |
grps.create(properties={ |
| 140 |
'cn': i[0], |
| 141 |
'ou': 'groups', |
| 142 |
'member': i[1] |
| 143 |
}) |
| 144 |
|
| 145 |
users = UserAccounts(topo.standalone, OU_2, rdn=None) |
| 146 |
for i in ['Grandson', 'Child', 'Parent', 'Grandparent', 'Ancestor']: |
| 147 |
users.create( |
| 148 |
properties={ |
| 149 |
'uid': i, |
| 150 |
'cn': i, |
| 151 |
'sn': 'user', |
| 152 |
'uidNumber': '1000', |
| 153 |
'gidNumber': '2000', |
| 154 |
'homeDirectory': '/home/' + i, |
| 155 |
'userPassword': PW_DM |
| 156 |
}) |
| 157 |
|
| 158 |
# Creating Other OUs |
| 159 |
for dn_dn in [(OU_2, 'ANCESTORS'), |
| 160 |
(ANCESTORS, 'GRANDPARENTS'), |
| 161 |
(GRANDPARENTS, 'PARENTS'), |
| 162 |
(PARENTS, 'CHILDREN'), |
| 163 |
(CHILDREN, 'GRANDSONS')]: |
| 164 |
OrganizationalUnits(topo.standalone, dn_dn[0]).create(properties={'ou': dn_dn[1]}) |
| 165 |
|
| 166 |
|
| 167 |
@pytest.mark.parametrize("user,entry", [ |
| 168 |
(CAN, ROLEDNACCESS), |
| 169 |
(CAN, USERDNACCESS), |
| 170 |
(CAN, GROUPDNACCESS), |
| 171 |
(CAN, LDAPURLACCESS), |
| 172 |
(CAN, ATTRNAMEACCESS), |
| 173 |
(LEVEL_0, OU_2), |
| 174 |
(LEVEL_1, ANCESTORS), |
| 175 |
(LEVEL_2, GRANDPARENTS), |
| 176 |
(LEVEL_4, OU_2), |
| 177 |
(LEVEL_4, ANCESTORS), |
| 178 |
(LEVEL_4, GRANDPARENTS), |
| 179 |
(LEVEL_4, PARENTS), |
| 180 |
(LEVEL_4, CHILDREN), |
| 181 |
pytest.param(LEVEL_3, CHILDREN, marks=pytest.mark.xfail(reason="May be some bug")), |
| 182 |
], ids=[ |
| 183 |
"(CAN,ROLEDNACCESS)", |
| 184 |
"(CAN,USERDNACCESS)", |
| 185 |
"(CAN,GROUPDNACCESS)", |
| 186 |
"(CAN,LDAPURLACCESS)", |
| 187 |
"(CAN,ATTRNAMEACCESS)", |
| 188 |
"(LEVEL_0, OU_2)", |
| 189 |
"(LEVEL_1,ANCESTORS)", |
| 190 |
"(LEVEL_2,GRANDPARENTS)", |
| 191 |
"(LEVEL_4,OU_2)", |
| 192 |
"(LEVEL_4, ANCESTORS)", |
| 193 |
"(LEVEL_4,GRANDPARENTS)", |
| 194 |
"(LEVEL_4,PARENTS)", |
| 195 |
"(LEVEL_4,CHILDREN)", |
| 196 |
"(LEVEL_3, CHILDREN)" |
| 197 |
]) |
| 198 |
def test_mod_see_also_positive(topo, _add_user, user, entry): |
| 199 |
""" |
| 200 |
Try to set seeAlso on entry with binding specific user, it will success |
| 201 |
as per the ACI. |
| 202 |
|
| 203 |
:id: 65745426-7a01-11e8-8ac2-8c16451d917b |
| 204 |
:parametrized: yes |
| 205 |
:setup: Standalone Instance |
| 206 |
:steps: |
| 207 |
1. Add test entry |
| 208 |
2. Add ACI |
| 209 |
3. User should follow ACI role |
| 210 |
:expectedresults: |
| 211 |
1. Entry should be added |
| 212 |
2. Operation should succeed |
| 213 |
3. Operation should succeed |
| 214 |
""" |
| 215 |
conn = UserAccount(topo.standalone, user).bind(PW_DM) |
| 216 |
UserAccount(conn, entry).replace('seeAlso', 'cn=1') |
| 217 |
|
| 218 |
|
| 219 |
@pytest.mark.parametrize("user,entry", [ |
| 220 |
(CANNOT, ROLEDNACCESS), |
| 221 |
(CANNOT, USERDNACCESS), |
| 222 |
(CANNOT, GROUPDNACCESS), |
| 223 |
(CANNOT, LDAPURLACCESS), |
| 224 |
(CANNOT, ATTRNAMEACCESS), |
| 225 |
(LEVEL_0, ANCESTORS), |
| 226 |
(LEVEL_0, GRANDPARENTS), |
| 227 |
(LEVEL_0, PARENTS), |
| 228 |
(LEVEL_0, CHILDREN), |
| 229 |
(LEVEL_2, PARENTS), |
| 230 |
(LEVEL_4, GRANDSONS), |
| 231 |
], ids=[ |
| 232 |
"(CANNOT,ROLEDNACCESS)", |
| 233 |
"(CANNOT,USERDNACCESS)", |
| 234 |
"(CANNOT,GROUPDNACCESS)", |
| 235 |
"(CANNOT,LDAPURLACCESS)", |
| 236 |
"(CANNOT,ATTRNAMEACCESS)", |
| 237 |
"(LEVEL_0, ANCESTORS)", |
| 238 |
"(LEVEL_0,GRANDPARENTS)", |
| 239 |
"(LEVEL_0,PARENTS)", |
| 240 |
"(LEVEL_0,CHILDREN)", |
| 241 |
"(LEVEL_2,PARENTS)", |
| 242 |
"(LEVEL_4,GRANDSONS)", |
| 243 |
]) |
| 244 |
def test_mod_see_also_negative(topo, _add_user, user, entry): |
| 245 |
""" |
| 246 |
Try to set seeAlso on entry with binding specific user, it will Fail |
| 247 |
as per the ACI. |
| 248 |
|
| 249 |
:id: 9ea93252-7a01-11e8-a85b-8c16451d917b |
| 250 |
:parametrized: yes |
| 251 |
:setup: Standalone Instance |
| 252 |
:steps: |
| 253 |
1. Add test entry |
| 254 |
2. Add ACI |
| 255 |
3. User should follow ACI role |
| 256 |
:expectedresults: |
| 257 |
1. Entry should be added |
| 258 |
2. Operation should succeed |
| 259 |
3. Operation should succeed |
| 260 |
""" |
| 261 |
conn = UserAccount(topo.standalone, user).bind(PW_DM) |
| 262 |
user = UserAccount(conn, entry) |
| 263 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 264 |
user.replace('seeAlso', 'cn=1') |
| 265 |
|
| 266 |
|
| 267 |
@pytest.mark.parametrize("user,entry", [ |
| 268 |
(CANNOT, USERDNACCESS), |
| 269 |
(CANNOT, ROLEDNACCESS), |
| 270 |
(CANNOT, GROUPDNACCESS) |
| 271 |
]) |
| 272 |
def test_last_three(topo, _add_user, user, entry): |
| 273 |
""" |
| 274 |
When we use the userattr keyword to associate the entry used to bind |
| 275 |
with the target entry the ACI applies only to the target specified and |
| 276 |
not to subentries. |
| 277 |
|
| 278 |
:id: add58a0a-7a01-11e8-85f1-8c16451d917b |
| 279 |
:parametrized: yes |
| 280 |
:setup: Standalone Instance |
| 281 |
:steps: |
| 282 |
1. Add test entry |
| 283 |
2. Add ACI |
| 284 |
3. User should follow ACI role |
| 285 |
:expectedresults: |
| 286 |
1. Entry should be added |
| 287 |
2. Operation should succeed |
| 288 |
3. Operation should succeed |
| 289 |
""" |
| 290 |
conn = UserAccount(topo.standalone, user).bind(PW_DM) |
| 291 |
users = UserAccounts(conn, entry) |
| 292 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 293 |
users.create_test_user() |
| 294 |
|
| 295 |
|
| 296 |
if __name__ == "__main__": |
| 297 |
CURRENT_FILE = os.path.realpath(__file__) |
| 298 |
pytest.main("-s -v %s" % CURRENT_FILE) |