| 1 |
# --- BEGIN COPYRIGHT BLOCK --- |
| 2 |
# Copyright (C) 2025 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 |
import pytest |
| 11 |
import ldap |
| 12 |
import os |
| 13 |
|
| 14 |
from test389.topologies import topology_m2 |
| 15 |
from lib389.idm.user import UserAccounts, UserAccount |
| 16 |
from lib389.idm.group import Groups, Group |
| 17 |
from lib389.idm.domain import Domain |
| 18 |
from lib389.idm.directorymanager import DirectoryManager |
| 19 |
from ldap.schema.models import ObjectClass |
| 20 |
from lib389.replica import ReplicationManager |
| 21 |
from lib389.schema import Schema, OBJECT_MODEL_PARAMS |
| 22 |
from lib389._constants import DEFAULT_SUFFIX, PASSWORD |
| 23 |
|
| 24 |
log = logging.getLogger(__name__) |
| 25 |
pytestmark = pytest.mark.tier2 |
| 26 |
|
| 27 |
|
| 28 |
@pytest.fixture(scope="module") |
| 29 |
def setup_test_env(topology_m2, request): |
| 30 |
""" |
| 31 |
Sets up a Two supplier replication environment for testing SELFDN ACIs. |
| 32 |
|
| 33 |
This fixture prepares the environment by creating a custom object class, |
| 34 |
a bind user, and several dummy entries. It removes default ACIs for a |
| 35 |
clean slate and handles cleanup of all created objects after the test. |
| 36 |
""" |
| 37 |
supplier1 = topology_m2.ms["supplier1"] |
| 38 |
supplier2 = topology_m2.ms["supplier2"] |
| 39 |
users = UserAccounts(supplier1, DEFAULT_SUFFIX) |
| 40 |
|
| 41 |
schema = Schema(supplier1) |
| 42 |
oc_name = 'OCSelfDNTest' |
| 43 |
bind_name = 'bind_entry' |
| 44 |
|
| 45 |
log.info(f"Adding custom object class '{oc_name}'") |
| 46 |
parameters = OBJECT_MODEL_PARAMS[ObjectClass].copy() |
| 47 |
parameters.update({ |
| 48 |
'names': (oc_name,), |
| 49 |
'oid': '1.2.3.4.5.6.7.8.9.10.2', |
| 50 |
'desc': 'Test SELFDN ACLs', |
| 51 |
'sup': ('person',), |
| 52 |
'kind': 2, # 0=STRUCTURAL, 1=ABSTRACT, 2=AUXILIARY |
| 53 |
'must': ('postalAddress', 'postalCode'), |
| 54 |
'may': ('member', 'street') |
| 55 |
}) |
| 56 |
schema.add_objectclass(parameters) |
| 57 |
|
| 58 |
log.info(f"Adding bind user '{bind_name}'") |
| 59 |
bind_properties = { |
| 60 |
'cn': bind_name, |
| 61 |
'sn': bind_name, |
| 62 |
'userpassword': 'password', |
| 63 |
'uidNumber': '1000', |
| 64 |
'gidNumber': '1000', |
| 65 |
'homeDirectory': f'/home/{bind_name}' |
| 66 |
} |
| 67 |
bind_user = users.create(f"cn={bind_name}", bind_properties) |
| 68 |
|
| 69 |
log.info("Removing default ACIs from suffix") |
| 70 |
domain1 = Domain(supplier1, DEFAULT_SUFFIX) |
| 71 |
domain2 = Domain(supplier2, DEFAULT_SUFFIX) |
| 72 |
domain1.remove_all('aci') |
| 73 |
domain2.remove_all('aci') |
| 74 |
|
| 75 |
log.info("Adding dummy entries for member attribute") |
| 76 |
for i in range(10): |
| 77 |
dummy_name = f"other_entry{i}" |
| 78 |
dummy_properties = { |
| 79 |
'cn': dummy_name, |
| 80 |
'sn': dummy_name, |
| 81 |
'uidNumber': str(1001 + i), |
| 82 |
'gidNumber': '1000', |
| 83 |
'homeDirectory': f'/home/{dummy_name}' |
| 84 |
} |
| 85 |
users.create(f"cn={dummy_name}", dummy_properties) |
| 86 |
|
| 87 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 88 |
dm = DirectoryManager(supplier1) |
| 89 |
dm.rebind() |
| 90 |
repl.wait_for_replication(supplier1, supplier2) |
| 91 |
|
| 92 |
def cleanup(): |
| 93 |
log.info("Cleaning up: removing test entries and custom schema") |
| 94 |
dm = DirectoryManager(supplier1) |
| 95 |
dm.rebind() |
| 96 |
bind_user.delete() |
| 97 |
try: |
| 98 |
test_user = users.get('test_entry') |
| 99 |
test_user.delete() |
| 100 |
except ldap.NO_SUCH_OBJECT: |
| 101 |
pass |
| 102 |
try: |
| 103 |
test_user = users.get('test_without_member') |
| 104 |
test_user.delete() |
| 105 |
except ldap.NO_SUCH_OBJECT: |
| 106 |
pass |
| 107 |
try: |
| 108 |
test_user = users.get('test_multiple_members') |
| 109 |
test_user.delete() |
| 110 |
except ldap.NO_SUCH_OBJECT: |
| 111 |
pass |
| 112 |
for i in range(10): |
| 113 |
try: |
| 114 |
dummy_user = users.get(f"other_entry{i}") |
| 115 |
dummy_user.delete() |
| 116 |
except ldap.NO_SUCH_OBJECT: |
| 117 |
pass |
| 118 |
schema.remove_objectclass(oc_name) |
| 119 |
|
| 120 |
request.addfinalizer(cleanup) |
| 121 |
|
| 122 |
yield {'oc_name': oc_name, 'bind_dn': bind_user.dn, 'suffix': DEFAULT_SUFFIX} |
| 123 |
|
| 124 |
|
| 125 |
def test_selfdn_acl_operations(topology_m2, setup_test_env): |
| 126 |
""" |
| 127 |
Validates that SELFDN ACIs for ADD and MODIFY operations function |
| 128 |
correctly and are replicated across a multi-master setup. |
| 129 |
|
| 130 |
:id: 4db4a0eb-62a4-4288-91aa-c4241c144981 |
| 131 |
:setup: Two supplier replication |
| 132 |
:steps: |
| 133 |
1. Verify ADD is denied without a 'SELFDN' ACI. |
| 134 |
2. Add ACI to allow ADD if bind DN is in the 'member' attribute. |
| 135 |
3. Verify ADD is denied if the 'member' attribute is missing. |
| 136 |
4. Verify ADD is denied if multiple 'member' values are present. |
| 137 |
5. Verify ADD is allowed when a single 'member' attribute contains the bind DN. |
| 138 |
6. Verify the new entry is replicated to the replica. |
| 139 |
7. Verify MODIFY is denied without a 'SELFDN' WRITE ACI. |
| 140 |
8. Add ACI to allow WRITE if bind DN is in the 'member' attribute. |
| 141 |
9. Verify MODIFY is allowed with the correct ACI. |
| 142 |
10. Verify the modification is replicated to the replica. |
| 143 |
11. Verify a modification on the replica is replicated back to the master. |
| 144 |
:expectedresults: |
| 145 |
1. The ADD operation should fail with INSUFFICIENT_ACCESS. |
| 146 |
2. The ACI should be added successfully. |
| 147 |
3. The ADD operation should fail with INSUFFICIENT_ACCESS. |
| 148 |
4. The ADD operation should fail with INSUFFICIENT_ACCESS. |
| 149 |
5. The ADD operation should succeed. |
| 150 |
6. The new entry should be present on the replica. |
| 151 |
7. The MODIFY operation should fail with INSUFFICIENT_ACCESS. |
| 152 |
8. The ACI should be added successfully. |
| 153 |
9. The MODIFY operation should succeed. |
| 154 |
10. The modification should be present on the replica. |
| 155 |
11. The modification should be replicated back to the master. |
| 156 |
""" |
| 157 |
supplier1 = topology_m2.ms["supplier1"] |
| 158 |
supplier2 = topology_m2.ms["supplier2"] |
| 159 |
|
| 160 |
oc_name = setup_test_env['oc_name'] |
| 161 |
bind_dn = setup_test_env['bind_dn'] |
| 162 |
entry_name = 'test_entry' |
| 163 |
|
| 164 |
entry_attrs = { |
| 165 |
'objectclass': ['top', 'person', oc_name], |
| 166 |
'sn': entry_name, |
| 167 |
'cn': entry_name, |
| 168 |
'postalAddress': 'here', |
| 169 |
'postalCode': '1234', |
| 170 |
'member': bind_dn |
| 171 |
} |
| 172 |
|
| 173 |
log.info("Verifying ADD is denied without ACI") |
| 174 |
bind_user = UserAccount(supplier1, bind_dn) |
| 175 |
user_conn = bind_user.bind(PASSWORD) |
| 176 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 177 |
groups = Groups(user_conn, DEFAULT_SUFFIX) |
| 178 |
groups.create(properties=entry_attrs) |
| 179 |
user_conn.close() |
| 180 |
|
| 181 |
log.info("Adding 'SelfDN add' ACI") |
| 182 |
dm = DirectoryManager(supplier1) |
| 183 |
dm.rebind() |
| 184 |
aci_add = (f'(target = "ldap:///cn=*,ou=Groups,{DEFAULT_SUFFIX}")(targetfilter ="(objectClass={oc_name})")' |
| 185 |
f'(version 3.0; acl "SelfDN add"; allow (add) userattr = "member#selfDN";)') |
| 186 |
domain = Domain(supplier1, DEFAULT_SUFFIX) |
| 187 |
domain.add('aci', aci_add) |
| 188 |
repl = ReplicationManager(DEFAULT_SUFFIX) |
| 189 |
repl.wait_for_replication(supplier1, supplier2) |
| 190 |
|
| 191 |
log.info("Verifying ADD is denied if 'member' attribute is missing") |
| 192 |
entry_without_member_attrs = { |
| 193 |
'objectclass': ['top', 'person', oc_name], |
| 194 |
'sn': 'test_without_member', |
| 195 |
'cn': 'test_without_member', |
| 196 |
'postalAddress': 'here', |
| 197 |
'postalCode': '1234' |
| 198 |
} |
| 199 |
|
| 200 |
user_conn = bind_user.bind(PASSWORD) |
| 201 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 202 |
groups = Groups(user_conn, DEFAULT_SUFFIX) |
| 203 |
groups.create(properties=entry_without_member_attrs) |
| 204 |
user_conn.close() |
| 205 |
|
| 206 |
log.info("Verifying ADD is denied with multiple 'member' values") |
| 207 |
entry_with_multiple_members_attrs = { |
| 208 |
'objectclass': ['top', 'person', oc_name], |
| 209 |
'sn': 'test_multiple_members', |
| 210 |
'cn': 'test_multiple_members', |
| 211 |
'postalAddress': 'here', |
| 212 |
'postalCode': '1234', |
| 213 |
'member': [bind_dn, f'cn=other_entry0,ou=People,{DEFAULT_SUFFIX}'] |
| 214 |
} |
| 215 |
user_conn = bind_user.bind(PASSWORD) |
| 216 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 217 |
groups = Groups(user_conn, DEFAULT_SUFFIX) |
| 218 |
groups.create(properties=entry_with_multiple_members_attrs) |
| 219 |
user_conn.close() |
| 220 |
|
| 221 |
log.info("Verifying ADD is allowed with correct ACI and 'member' attribute") |
| 222 |
user_conn = bind_user.bind(PASSWORD) |
| 223 |
groups = Groups(user_conn, DEFAULT_SUFFIX) |
| 224 |
test_entry = groups.create(properties=entry_attrs) |
| 225 |
user_conn.close() |
| 226 |
|
| 227 |
log.info("Verifying entry was replicated") |
| 228 |
repl.wait_for_replication(supplier1, supplier2) |
| 229 |
assert Group(supplier2, test_entry.dn), f"Entry {test_entry.dn} not found on supplier2" |
| 230 |
|
| 231 |
log.info("Verifying MODIFY is denied without WRITE ACI") |
| 232 |
user_conn = bind_user.bind(PASSWORD) |
| 233 |
test_entry_user = Group(user_conn, test_entry.dn) |
| 234 |
with pytest.raises(ldap.INSUFFICIENT_ACCESS): |
| 235 |
test_entry_user.replace('postalCode', '9876') |
| 236 |
user_conn.close() |
| 237 |
|
| 238 |
log.info("Adding 'SelfDN write' ACI") |
| 239 |
aci_write = (f'(target = "ldap:///cn=*,ou=Groups,{DEFAULT_SUFFIX}")(targetattr = *)(targetfilter ="(objectClass={oc_name})")' |
| 240 |
f'(version 3.0; acl "SelfDN write"; allow (write) userattr = "member#selfDN";)') |
| 241 |
domain.add('aci', aci_write) |
| 242 |
repl.wait_for_replication(supplier1, supplier2) |
| 243 |
|
| 244 |
log.info("Verifying MODIFY is allowed with correct ACI") |
| 245 |
user_conn = bind_user.bind(PASSWORD) |
| 246 |
test_entry_user = Group(user_conn, test_entry.dn) |
| 247 |
test_entry_user.replace('postalCode', '1928') |
| 248 |
user_conn.close() |
| 249 |
|
| 250 |
log.info("Verifying modification was replicated") |
| 251 |
repl.wait_for_replication(supplier1, supplier2) |
| 252 |
|
| 253 |
entry_on_supplier2 = Group(supplier2, test_entry.dn) |
| 254 |
assert entry_on_supplier2.get_attr_val_utf8('postalCode') == '1928' |
| 255 |
|
| 256 |
log.info("Verifying MODIFY on replica is replicated back to master") |
| 257 |
bind_user_m2 = UserAccount(supplier2, bind_dn) |
| 258 |
user_conn_m2 = bind_user_m2.bind(PASSWORD) |
| 259 |
entry_m2_group = Group(user_conn_m2, test_entry.dn) |
| 260 |
entry_m2_group.replace('postalCode', '9999') |
| 261 |
user_conn_m2.close() |
| 262 |
|
| 263 |
repl.wait_for_replication(supplier2, supplier1) |
| 264 |
|
| 265 |
entry_m1_updated = Group(supplier1, test_entry.dn) |
| 266 |
assert entry_m1_updated.get_attr_val_utf8('postalCode') == '9999' |
| 267 |
|
| 268 |
|
| 269 |
if __name__ == "__main__": |
| 270 |
# Run isolated |
| 271 |
# -s for DEBUG mode |
| 272 |
CURRENT_FILE = os.path.realpath(__file__) |
| 273 |
pytest.main(["-s", CURRENT_FILE]) |