| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2019 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 roles. |
| 11 |
""" |
| 12 |
|
| 13 |
import os |
| 14 |
import pytest |
| 15 |
|
| 16 |
from lib389._constants import DEFAULT_SUFFIX, PW_DM |
| 17 |
from lib389.idm.user import UserAccounts, UserAccount |
| 18 |
from lib389.idm.organizationalunit import OrganizationalUnits |
| 19 |
from test389.topologies import topology_st as topo |
| 20 |
from lib389.idm.domain import Domain |
| 21 |
from lib389.idm.role import NestedRoles, ManagedRoles, FilteredRoles |
| 22 |
from lib389.idm.account import Anonymous |
| 23 |
|
| 24 |
import ldap |
| 25 |
|
| 26 |
|
| 27 |
pytestmark = pytest.mark.tier1 |
| 28 |
|
| 29 |
|
| 30 |
OU_ROLE = f"ou=roledntest,{DEFAULT_SUFFIX}" |
| 31 |
STEVE_ROLE = f"uid=STEVE_ROLE,{OU_ROLE}" |
| 32 |
HARRY_ROLE = f"uid=HARRY_ROLE,{OU_ROLE}" |
| 33 |
MARY_ROLE = f"uid=MARY_ROLE,{OU_ROLE}" |
| 34 |
ROLE1 = f"cn=ROLE1,{OU_ROLE}" |
| 35 |
ROLE2 = f"cn=ROLE2,{OU_ROLE}" |
| 36 |
ROLE3 = f"cn=ROLE3,{OU_ROLE}" |
| 37 |
ROLE21 = f"cn=ROLE21,{OU_ROLE}" |
| 38 |
ROLE31 = f"cn=ROLE31,{OU_ROLE}" |
| 39 |
FILTERROLE = f"cn=FILTERROLE,{OU_ROLE}" |
| 40 |
JOE_ROLE = f"uid=JOE_ROLE,{OU_ROLE}" |
| 41 |
NOROLEUSER = f"uid=NOROLEUSER,{OU_ROLE}" |
| 42 |
SCRACHENTRY = f"uid=SCRACHENTRY,{OU_ROLE}" |
| 43 |
ALL_ACCESS = f"uid=all access,{OU_ROLE}" |
| 44 |
NOT_RULE_ACCESS = f"uid=not rule access,{OU_ROLE}" |
| 45 |
OR_RULE_ACCESS = f"uid=or rule access,{OU_ROLE}" |
| 46 |
NESTED_ROLE_TESTER = f"uid=nested role tester,{OU_ROLE}" |
| 47 |
|
| 48 |
|
| 49 |
@pytest.fixture(scope="function") |
| 50 |
def _aci_of_user(request, topo): |
| 51 |
""" |
| 52 |
Removes and Restores ACIs after the test. |
| 53 |
""" |
| 54 |
aci_list = Domain(topo.standalone, DEFAULT_SUFFIX).get_attr_vals_utf8('aci') |
| 55 |
|
| 56 |
def finofaci(): |
| 57 |
""" |
| 58 |
Removes and Restores ACIs after the test. |
| 59 |
""" |
| 60 |
domain = Domain(topo.standalone, DEFAULT_SUFFIX) |
| 61 |
domain.remove_all('aci') |
| 62 |
for i in aci_list: |
| 63 |
domain.add("aci", i) |
| 64 |
|
| 65 |
request.addfinalizer(finofaci) |
| 66 |
|
| 67 |
|
| 68 |
@pytest.fixture(scope="module") |
| 69 |
def _add_user(request, topo): |
| 70 |
""" |
| 71 |
A Function that will create necessary users delete the created user |
| 72 |
""" |
| 73 |
ous = OrganizationalUnits(topo.standalone, DEFAULT_SUFFIX) |
| 74 |
ou_ou = ous.create(properties={'ou': 'roledntest'}) |
| 75 |
ou_ou.set('aci', [f'(target="ldap:///{NESTED_ROLE_TESTER}")(targetattr="*") ' |
| 76 |
f'(version 3.0; aci "nested role aci"; allow(all)' |
| 77 |
f'roledn = "ldap:///{ROLE2}";)', |
| 78 |
f'(target="ldap:///{OR_RULE_ACCESS}")(targetattr="*")' |
| 79 |
f'(version 3.0; aci "or role aci"; allow(all) ' |
| 80 |
f'roledn = "ldap:///{ROLE1} || ldap:///{ROLE21}";)', |
| 81 |
f'(target="ldap:///{ALL_ACCESS}")(targetattr="*")' |
| 82 |
f'(version 3.0; aci "anyone role aci"; allow(all) ' |
| 83 |
f'roledn = "ldap:///anyone";)', |
| 84 |
f'(target="ldap:///{NOT_RULE_ACCESS}")(targetattr="*")' |
| 85 |
f'(version 3.0; aci "not role aci"; allow(all)' |
| 86 |
f'roledn != "ldap:///{ROLE1} || ldap:///{ROLE21}";)']) |
| 87 |
|
| 88 |
nestedroles = NestedRoles(topo.standalone, OU_ROLE) |
| 89 |
for i in [('role2', [ROLE1, ROLE21]), ('role3', [ROLE2, ROLE31])]: |
| 90 |
nestedroles.create(properties={'cn': i[0], |
| 91 |
'nsroledn': i[1]}) |
| 92 |
|
| 93 |
managedroles = ManagedRoles(topo.standalone, OU_ROLE) |
| 94 |
for i in ['ROLE1', 'ROLE21', 'ROLE31']: |
| 95 |
managedroles.create(properties={'cn': i}) |
| 96 |
|
| 97 |
filterroles = FilteredRoles(topo.standalone, OU_ROLE) |
| 98 |
filterroles.create(properties={'cn': 'filterRole', |
| 99 |
'nsRoleFilter': 'sn=Dr Drake', |
| 100 |
'description': 'filter role tester'}) |
| 101 |
|
| 102 |
users = UserAccounts(topo.standalone, OU_ROLE, rdn=None) |
| 103 |
for i in [('STEVE_ROLE', ROLE1, 'Has roles 1, 2 and 3.'), |
| 104 |
('HARRY_ROLE', ROLE21, 'Has roles 21, 2 and 3.'), |
| 105 |
('MARY_ROLE', ROLE31, 'Has roles 31 and 3.')]: |
| 106 |
users.create(properties={ |
| 107 |
'uid': i[0], |
| 108 |
'cn': i[0], |
| 109 |
'sn': 'user', |
| 110 |
'uidNumber': '1000', |
| 111 |
'gidNumber': '2000', |
| 112 |
'homeDirectory': '/home/' + i[0], |
| 113 |
'userPassword': PW_DM, |
| 114 |
'nsroledn': i[1], |
| 115 |
'Description': i[2] |
| 116 |
}) |
| 117 |
|
| 118 |
for i in [('JOE_ROLE', 'Has filterRole.'), |
| 119 |
('NOROLEUSER', 'Has no roles.'), |
| 120 |
('SCRACHENTRY', 'Entry to test rights on.'), |
| 121 |
('all access', 'Everyone has acccess (incl anon).'), |
| 122 |
('not rule access', 'Only accessible to mary.'), |
| 123 |
('or rule access', 'Only to steve and harry but nbot mary or anon'), |
| 124 |
('nested role tester', 'Only accessible to harry and steve.')]: |
| 125 |
users.create(properties={ |
| 126 |
'uid': i[0], |
| 127 |
'cn': i[0], |
| 128 |
'sn': 'user', |
| 129 |
'uidNumber': '1000', |
| 130 |
'gidNumber': '2000', |
| 131 |
'homeDirectory': '/home/' + i[0], |
| 132 |
'userPassword': PW_DM, |
| 133 |
'Description': i[1] |
| 134 |
}) |
| 135 |
|
| 136 |
# Setting SN for user JOE |
| 137 |
UserAccount(topo.standalone, f'uid=JOE_ROLE,ou=roledntest,{DEFAULT_SUFFIX}').set('sn', 'Dr Drake') |
| 138 |
|
| 139 |
def fin(): |
| 140 |
""" |
| 141 |
It will delete the created users |
| 142 |
""" |
| 143 |
for i in users.list() + managedroles.list() + nestedroles.list(): |
| 144 |
i.delete() |
| 145 |
|
| 146 |
request.addfinalizer(fin) |
| 147 |
|
| 148 |
|
| 149 |
@pytest.mark.parametrize("user,entry", [ |
| 150 |
(STEVE_ROLE, NESTED_ROLE_TESTER), |
| 151 |
(HARRY_ROLE, NESTED_ROLE_TESTER), |
| 152 |
(MARY_ROLE, NOT_RULE_ACCESS), |
| 153 |
(STEVE_ROLE, OR_RULE_ACCESS), |
| 154 |
(HARRY_ROLE, OR_RULE_ACCESS), |
| 155 |
(STEVE_ROLE, ALL_ACCESS), |
| 156 |
(HARRY_ROLE, ALL_ACCESS), |
| 157 |
(MARY_ROLE, ALL_ACCESS), |
| 158 |
], ids=[ |
| 159 |
"(STEVE_ROLE, NESTED_ROLE_TESTER)", |
| 160 |
"(HARRY_ROLE, NESTED_ROLE_TESTER)", |
| 161 |
"(MARY_ROLE, NOT_RULE_ACCESS)", |
| 162 |
"(STEVE_ROLE, OR_RULE_ACCESS)", |
| 163 |
"(HARRY_ROLE, OR_RULE_ACCESS)", |
| 164 |
"(STEVE_ROLE, ALL_ACCESS)", |
| 165 |
"(HARRY_ROLE, ALL_ACCESS)", |
| 166 |
"(MARY_ROLE, ALL_ACCESS)", |
| 167 |
]) |
| 168 |
def test_mod_seealso_positive(topo, _add_user, _aci_of_user, user, entry): |
| 169 |
""" |
| 170 |
Testing the roledn keyword that allows access control |
| 171 |
based on the role of the bound user. |
| 172 |
|
| 173 |
:id: a33c5d6a-79f4-11e8-8551-8c16451d917b |
| 174 |
:parametrized: yes |
| 175 |
:setup: Standalone server |
| 176 |
:steps: |
| 177 |
1. Add test entry |
| 178 |
2. Add ACI |
| 179 |
3. User should follow ACI role |
| 180 |
:expectedresults: |
| 181 |
1. Entry should be added |
| 182 |
2. Operation should succeed |
| 183 |
3. Operation should succeed |
| 184 |
""" |
| 185 |
conn = UserAccount(topo.standalone, user).bind(PW_DM) |
| 186 |
UserAccount(conn, entry).replace('seeAlso', 'cn=1') |
| 187 |
|
| 188 |
|
| 189 |
@pytest.mark.parametrize( |
| 190 |
"user,entry", [ |
| 191 |
(MARY_ROLE, NESTED_ROLE_TESTER), |
| 192 |
(STEVE_ROLE, NOT_RULE_ACCESS), |
| 193 |
(HARRY_ROLE, NOT_RULE_ACCESS), |
| 194 |
(MARY_ROLE, OR_RULE_ACCESS), |
| 195 |
], ids=[ |
| 196 |
"(MARY_ROLE, NESTED_ROLE_TESTER)", |
| 197 |
"(STEVE_ROLE, NOT_RULE_ACCESS)", |
| 198 |
"(HARRY_ROLE, NOT_RULE_ACCESS)", |
| 199 |
"(MARY_ROLE , OR_RULE_ACCESS)"] |
| 200 |
) |
| 201 |
def test_mod_seealso_negative(topo, _add_user, _aci_of_user, user, entry): |
| 202 |
""" |
| 203 |
Testing the roledn keyword that do not allows access control |
| 204 |
based on the role of the bound user. |
| 205 |
|
| 206 |
:id: b2444aa2-79f4-11e8-a2c3-8c16451d917b |
| 207 |
:parametrized: yes |
| 208 |
:setup: Standalone server |
| 209 |
:steps: |
| 210 |
1. Add test entry |
| 211 |
2. Add ACI |
| 212 |
3. User should follow ACI role |
| 213 |
:expectedresults: |
| 214 |
1. Entry should be added |
| 215 |
2. Operation should succeed |
| 216 |
3. Operation should succeed |
| 217 |
""" |
| 218 |
conn = UserAccount(topo.standalone, user).bind(PW_DM) |
| 219 |
user = UserAccount(conn, entry) |
| 220 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 221 |
user.replace('seeAlso', 'cn=1') |
| 222 |
|
| 223 |
|
| 224 |
@pytest.mark.parametrize("entry", [NOT_RULE_ACCESS, ALL_ACCESS], |
| 225 |
ids=["NOT_RULE_ACCESS", "ALL_ACCESS"]) |
| 226 |
def test_mod_anonseealso_positive(topo, _add_user, _aci_of_user, entry): |
| 227 |
""" |
| 228 |
Testing the roledn keyword that allows access control |
| 229 |
based on the role of the bound user. |
| 230 |
|
| 231 |
:id: c3eb41ac-79f4-11e8-aa8b-8c16451d917b |
| 232 |
:parametrized: yes |
| 233 |
:setup: Standalone server |
| 234 |
:steps: |
| 235 |
1. Add test entry |
| 236 |
2. Add ACI |
| 237 |
3. User should follow ACI role |
| 238 |
:expectedresults: |
| 239 |
1. Entry should be added |
| 240 |
2. Operation should succeed |
| 241 |
3. Operation should succeed |
| 242 |
""" |
| 243 |
conn = Anonymous(topo.standalone).bind() |
| 244 |
UserAccount(conn, entry).replace('seeAlso', 'cn=1') |
| 245 |
|
| 246 |
|
| 247 |
@pytest.mark.parametrize("entry", [NESTED_ROLE_TESTER, OR_RULE_ACCESS], |
| 248 |
ids=["NESTED_ROLE_TESTER", "OR_RULE_ACCESS"]) |
| 249 |
def test_mod_anonseealso_negaive(topo, _add_user, _aci_of_user, entry): |
| 250 |
""" |
| 251 |
Testing the roledn keyword that do not allows access control |
| 252 |
based on the role of the bound user. |
| 253 |
|
| 254 |
:id: d385611a-79f4-11e8-adc8-8c16451d917b |
| 255 |
:parametrized: yes |
| 256 |
:setup: Standalone Instance |
| 257 |
:steps: |
| 258 |
1. Add test entry |
| 259 |
2. Add ACI |
| 260 |
3. User should follow ACI role |
| 261 |
:expectedresults: |
| 262 |
1. Entry should be added |
| 263 |
2. Operation should succeed |
| 264 |
3. Operation should succeed |
| 265 |
""" |
| 266 |
conn = Anonymous(topo.standalone).bind() |
| 267 |
user = UserAccount(conn, entry) |
| 268 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 269 |
user.replace('seeAlso', 'cn=1') |
| 270 |
|
| 271 |
|
| 272 |
if __name__ == "__main__": |
| 273 |
CURRENT_FILE = os.path.realpath(__file__) |
| 274 |
pytest.main("-s -v %s" % CURRENT_FILE) |