| 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 itertools |
| 12 |
import ldap |
| 13 |
from ldap.controls import RequestControl |
| 14 |
from ldap.controls.readentry import PostReadControl |
| 15 |
from lib389.idm.user import UserAccounts, UserAccount |
| 16 |
from test389.topologies import topology_st |
| 17 |
from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD |
| 18 |
|
| 19 |
pytestmark = pytest.mark.tier1 |
| 20 |
|
| 21 |
log = logging.getLogger(__name__) |
| 22 |
|
| 23 |
MANAGE_DSAIT_OID = "2.16.840.1.113730.3.4.2" |
| 24 |
TESTED_CTRLS = [ MANAGE_DSAIT_OID, "test", "test1", "test2" ] |
| 25 |
SUPPORTED_CTRLS = [ MANAGE_DSAIT_OID, ] |
| 26 |
MAX_CONTROLS_PER_OP_ATTR = "nsslapd-maxcontrolsperop" |
| 27 |
|
| 28 |
def test_postread_ctrl_modify(topology_st): |
| 29 |
"""Test PostReadControl with LDAP modify operations. |
| 30 |
|
| 31 |
:id: 47920dc1-7a9b-4e8d-9f3a-6c5d4e3f2a1b |
| 32 |
:setup: Standalone instance |
| 33 |
:steps: |
| 34 |
1. Create test user entry with initial description |
| 35 |
2. Verify initial description value |
| 36 |
3. Modify description with PostReadControl requesting 'cn' and 'description' |
| 37 |
4. Verify PostReadControl response contains both requested attributes |
| 38 |
5. Verify the entry was actually modified in the database |
| 39 |
:expectedresults: |
| 40 |
1. User entry created successfully |
| 41 |
2. Initial description matches expected value |
| 42 |
3. Modify operation with control succeeds |
| 43 |
4. Control response contains both 'cn' and 'description' attributes |
| 44 |
5. Database entry reflects the modification |
| 45 |
""" |
| 46 |
inst = topology_st.standalone |
| 47 |
INITIAL_DESC = "initial description" |
| 48 |
FINAL_DESC = "final description" |
| 49 |
|
| 50 |
log.info("Create test user with initial description") |
| 51 |
users = UserAccounts(inst, DEFAULT_SUFFIX) |
| 52 |
user = users.create(properties={ |
| 53 |
'uid': 'postread_user', |
| 54 |
'cn': 'postread_user', |
| 55 |
'sn': 'user', |
| 56 |
'uidNumber': '1000', |
| 57 |
'gidNumber': '2000', |
| 58 |
'homeDirectory': '/home/postread_user', |
| 59 |
'description': INITIAL_DESC |
| 60 |
}) |
| 61 |
|
| 62 |
log.info("Verify initial description value") |
| 63 |
assert user.present('description', INITIAL_DESC) |
| 64 |
|
| 65 |
log.info("Modify description with PostReadControl") |
| 66 |
pr_ctrl = PostReadControl(criticality=True, attrList=['cn', 'description']) |
| 67 |
msg_id = inst.modify_ext( |
| 68 |
user.dn, |
| 69 |
[(ldap.MOD_REPLACE, 'description', FINAL_DESC.encode('utf-8'))], |
| 70 |
serverctrls=[pr_ctrl] |
| 71 |
) |
| 72 |
|
| 73 |
log.info("Get result with response controls") |
| 74 |
_, _, _, resp_ctrls = inst.result3(msg_id) |
| 75 |
|
| 76 |
log.info("Verify PostReadControl response is properly encoded") |
| 77 |
assert resp_ctrls, "Server should return PostReadControl" |
| 78 |
assert resp_ctrls[0].dn == user.dn, "Control should return correct DN" |
| 79 |
assert 'description' in resp_ctrls[0].entry, "Control should return description attribute" |
| 80 |
assert 'cn' in resp_ctrls[0].entry, "Control should return cn attribute" |
| 81 |
|
| 82 |
log.info("Verify entry was modified with correct value") |
| 83 |
user = UserAccount(inst, user.dn) |
| 84 |
assert user.get_attr_val_utf8('description') == FINAL_DESC |
| 85 |
user.delete() |
| 86 |
|
| 87 |
|
| 88 |
def all_combinations(alist): |
| 89 |
for listlen in range(len(alist)+1): |
| 90 |
for val in itertools.combinations(alist, listlen): |
| 91 |
yield val |
| 92 |
|
| 93 |
|
| 94 |
def try_search(inst, ignored_ctrls, sent_ctrls): |
| 95 |
ctrlreqs = [ RequestControl(controlType=oid, criticality=True) \ |
| 96 |
for oid in sent_ctrls ] |
| 97 |
expected = False; |
| 98 |
for ctrl in sent_ctrls: |
| 99 |
if ctrl not in SUPPORTED_CTRLS and ctrl not in ignored_ctrls: |
| 100 |
expected = True |
| 101 |
log.info(f"Ignoring: {ignored_ctrls} - Sending: {sent_ctrls} - Expected: {expected}") |
| 102 |
if expected: |
| 103 |
with pytest.raises(ldap.UNAVAILABLE_CRITICAL_EXTENSION): |
| 104 |
srch_res = inst.search_ext_s(DEFAULT_SUFFIX, |
| 105 |
ldap.SCOPE_BASE, serverctrls=ctrlreqs) |
| 106 |
else: |
| 107 |
srch_res = inst.search_ext_s(DEFAULT_SUFFIX, |
| 108 |
ldap.SCOPE_BASE, serverctrls=ctrlreqs) |
| 109 |
|
| 110 |
|
| 111 |
def set_ignored_criticality(inst, oids): |
| 112 |
oids = list(oids) |
| 113 |
log.info(f'set_ignored_criticality({oids})') |
| 114 |
inst.config.set("ds-ignored-control-criticality", oids) |
| 115 |
|
| 116 |
|
| 117 |
def test_ignored_criticality(topology_st): |
| 118 |
"""Test ignored criticality feature |
| 119 |
|
| 120 |
:id: e2dc765a-329f-11f1-9d0f-c85309d5c3e3 |
| 121 |
:setup: Standalone instance |
| 122 |
:steps: |
| 123 |
1. Define tested control set containing both supported and not supported controls |
| 124 |
2. Generate all subsets of tested controls |
| 125 |
3. For ignored_ctrls in subsets and every send_ctrls in subsets |
| 126 |
4. Set the ignored criticality config parameter to ignored_ctrls |
| 127 |
5. Perform a search with send_ctrls controls with critical flag |
| 128 |
6. Check if UNAVAILABLE_CRITICAL_EXTENSION is returned as expected |
| 129 |
7. Reset ignored criticality config parameter |
| 130 |
:expectedresults: |
| 131 |
1. Succes |
| 132 |
2. Succes |
| 133 |
3. Succes |
| 134 |
4. Succes |
| 135 |
5. Succes |
| 136 |
6. Expect UNAVAILABLE_CRITICAL_EXTENSION if any sent control is not supported or not ignored |
| 137 |
7. Succes |
| 138 |
""" |
| 139 |
inst = topology_st.standalone |
| 140 |
ctrls = { oid: ldap.controls.RequestControl(controlType=oid, criticality=True) \ |
| 141 |
for oid in TESTED_CTRLS } |
| 142 |
|
| 143 |
all_sets = list(all_combinations(TESTED_CTRLS)) |
| 144 |
for ignored_ctrls in all_sets: |
| 145 |
for send_ctrls in all_sets: |
| 146 |
set_ignored_criticality(inst, ignored_ctrls) |
| 147 |
try_search(inst, ignored_ctrls, send_ctrls) |
| 148 |
set_ignored_criticality(inst, []) |
| 149 |
|
| 150 |
|
| 151 |
def _make_request_controls(count): |
| 152 |
return [ |
| 153 |
RequestControl(controlType=MANAGE_DSAIT_OID, criticality=False) |
| 154 |
for _ in range(count) |
| 155 |
] |
| 156 |
|
| 157 |
|
| 158 |
def test_bind_excessive_controls(topology_st): |
| 159 |
"""Bind request control count is limited by nsslapd-maxcontrolsperop |
| 160 |
|
| 161 |
:id: c3888f02-2107-4682-a50a-2189d1436233 |
| 162 |
:setup: Standalone instance |
| 163 |
:steps: |
| 164 |
1. Read nsslapd-maxcontrolsperop from cn=config (default 10) |
| 165 |
2. Bind with one fewer control than the limit |
| 166 |
3. Bind with one more control than the limit |
| 167 |
4. Set nsslapd-maxcontrolsperop to 5 |
| 168 |
5. Bind with 4 controls (new limit minus one) |
| 169 |
6. Bind with 6 controls (over new limit) |
| 170 |
7. Restore nsslapd-maxcontrolsperop and re-bind as Directory Manager |
| 171 |
:expectedresults: |
| 172 |
1. Config value is 10 |
| 173 |
2. Bind succeeds |
| 174 |
3. Bind fails with ldap.UNWILLING_TO_PERFORM |
| 175 |
4. Success |
| 176 |
5. Bind succeeds |
| 177 |
6. Bind fails with ldap.UNWILLING_TO_PERFORM |
| 178 |
7. Success |
| 179 |
""" |
| 180 |
inst = topology_st.standalone |
| 181 |
original_max = inst.config.get_attr_val_utf8(MAX_CONTROLS_PER_OP_ATTR) |
| 182 |
|
| 183 |
try: |
| 184 |
max_controls = int(inst.config.get_attr_val_utf8(MAX_CONTROLS_PER_OP_ATTR)) |
| 185 |
assert max_controls == 10 |
| 186 |
|
| 187 |
log.info("Bind with %d controls (limit %d, limit minus one)", |
| 188 |
max_controls - 1, max_controls) |
| 189 |
inst.simple_bind_s(DN_DM, PASSWORD, |
| 190 |
serverctrls=_make_request_controls(max_controls - 1)) |
| 191 |
|
| 192 |
log.info("Bind with %d controls (limit %d plus one)", |
| 193 |
max_controls + 1, max_controls) |
| 194 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 195 |
inst.simple_bind_s(DN_DM, PASSWORD, |
| 196 |
serverctrls=_make_request_controls(max_controls + 1)) |
| 197 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 198 |
|
| 199 |
lowered_max = 5 |
| 200 |
log.info("Set %s to %d", MAX_CONTROLS_PER_OP_ATTR, lowered_max) |
| 201 |
inst.config.set(MAX_CONTROLS_PER_OP_ATTR, str(lowered_max)) |
| 202 |
assert int(inst.config.get_attr_val_utf8(MAX_CONTROLS_PER_OP_ATTR)) == lowered_max |
| 203 |
|
| 204 |
log.info("Bind with %d controls (lowered limit %d, limit minus one)", |
| 205 |
lowered_max - 1, lowered_max) |
| 206 |
inst.simple_bind_s(DN_DM, PASSWORD, |
| 207 |
serverctrls=_make_request_controls(lowered_max - 1)) |
| 208 |
|
| 209 |
log.info("Bind with %d controls (lowered limit %d plus one)", |
| 210 |
lowered_max + 1, lowered_max) |
| 211 |
with pytest.raises(ldap.UNWILLING_TO_PERFORM): |
| 212 |
inst.simple_bind_s(DN_DM, PASSWORD, |
| 213 |
serverctrls=_make_request_controls(lowered_max + 1)) |
| 214 |
finally: |
| 215 |
log.info("Restore %s to %s", MAX_CONTROLS_PER_OP_ATTR, original_max) |
| 216 |
inst.config.set(MAX_CONTROLS_PER_OP_ATTR, original_max) |
| 217 |
inst.simple_bind_s(DN_DM, PASSWORD) |
| 218 |
|
| 219 |
|
| 220 |
if __name__ == '__main__': |
| 221 |
CURRENT_FILE = __file__ |
| 222 |
pytest.main(["-s", "-v", CURRENT_FILE]) |
| 223 |
|