Debian Codegraph @

dirsrvtests/tests/suites/acl/anonymous_default_aci_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 """Regression tests for the default anonymous access ACI (ticket 48354)."""
10
11 import logging
12 import os
13
14 import ldap
15 import pytest
16
17 from lib389._constants import DEFAULT_SUFFIX
18 from lib389.idm.account import Anonymous
19 from lib389.idm.domain import Domain
20 from test389.topologies import topology_st as topo
21 from lib389.idm.user import UserAccounts
22
23 pytestmark = pytest.mark.tier1
24
25 DEBUGGING = os.getenv('DEBUGGING', default=False)
26 if DEBUGGING:
27 logging.getLogger(__name__).setLevel(logging.DEBUG)
28 else:
29 logging.getLogger(__name__).setLevel(logging.INFO)
30 log = logging.getLogger(__name__)
31
32 DEFAULT_ANON_ACI = (
33 '(targetattr!="userPassword || aci")(version 3.0; '
34 'acl "Enable anonymous access"; '
35 'allow (read, search, compare) userdn="ldap:///anyone";)'
36 )
37
38 TEST_PASSWORD = 'testpassword'
39
40 ENTRY_ACI = (
41 '(targetattr="cn || sn || uid")(version 3.0; '
42 'acl "test entry aci"; allow (read) userdn="ldap:///self";)'
43 )
44
45
46 def _user_properties(uid, uid_number):
47 return {
48 'uid': uid,
49 'cn': uid,
50 'sn': uid,
51 'uidNumber': str(uid_number),
52 'gidNumber': str(uid_number),
53 'homeDirectory': f'/home/{uid}',
54 }
55
56
57 @pytest.fixture(scope='function')
58 def anonymous_test_users(topo, request):
59 """Create users with userPassword, entry-level aci, or both."""
60
61 users = UserAccounts(topo.standalone, DEFAULT_SUFFIX, rdn=None)
62 created = []
63
64 # Create user with userPassword
65 user_pw = users.create(properties={
66 **_user_properties('anon_test_pw', 10001),
67 'userPassword': TEST_PASSWORD,
68 })
69 created.append(user_pw)
70
71 # Create user with entry-level aci
72 user_aci = users.create(properties=_user_properties('anon_test_aci', 10002))
73 user_aci.add('aci', ENTRY_ACI)
74 created.append(user_aci)
75
76 # Create user with both userPassword and entry-level aci
77 user_pw_aci = users.create(properties={
78 **_user_properties('anon_test_pw_aci', 10003),
79 'userPassword': TEST_PASSWORD,
80 })
81 user_pw_aci.add('aci', ENTRY_ACI)
82 created.append(user_pw_aci)
83
84 def fin():
85 for user in created:
86 try:
87 user.delete()
88 except ldap.NO_SUCH_OBJECT:
89 pass
90
91 request.addfinalizer(fin)
92
93 return {
94 'user_pw': user_pw,
95 'user_aci': user_aci,
96 'user_pw_aci': user_pw_aci,
97 }
98
99
100 def _attr_present(conn, attr_name):
101 """Return True if anonymous search finds any entry matching (attr=*)."""
102 results = UserAccounts(conn, DEFAULT_SUFFIX).filter(f'({attr_name}=*)', scope=ldap.SCOPE_SUBTREE, strict=True)
103 if DEBUGGING:
104 log.debug(f"Anonymous ({attr_name}=*) search returned: {results}")
105 return len(results) > 0
106
107
108 @pytest.fixture(scope='function')
109 def anonymous_default_aci_setup(topo, request):
110 """Install the default anonymous ACI on the suffix."""
111 domain = Domain(topo.standalone, DEFAULT_SUFFIX)
112 saved_acis = domain.get_attr_vals_utf8('aci') or []
113
114 domain.remove_all('aci')
115 domain.add('aci', DEFAULT_ANON_ACI)
116
117 def fin():
118 domain.remove_all('aci')
119 for aci in saved_acis:
120 domain.add('aci', aci)
121
122 request.addfinalizer(fin)
123
124
125 def test_anonymous_cannot_read_userpassword_or_aci(topo, anonymous_default_aci_setup, anonymous_test_users):
126 """Anonymous users cannot read userPassword or aci with the default ACI
127
128 Regression for ticket 48354: the default anonymous access ACI must exclude
129 both userPassword and aci from anonymous read access.
130
131 :id: e67a099d-72c6-4cf2-b0f8-50dfb3430e6b
132 :setup: Standalone instance with default anonymous ACI and users that
133 have userPassword and/or entry-level aci attributes
134 :steps:
135 1. Bind anonymously
136 2. Search the suffix for entries matching (userPassword=*)
137 3. Search the suffix for entries matching (aci=*)
138 :expectedresults:
139 1. Anonymous bind succeeds
140 2. No entries are returned
141 3. No entries are returned
142 """
143 anon_conn = Anonymous(topo.standalone).bind()
144
145 assert not _attr_present(anon_conn, 'userPassword'), (
146 'Anonymous user must not see entries with userPassword'
147 )
148 assert not _attr_present(anon_conn, 'aci'), (
149 'Anonymous user must not see entries with aci'
150 )
151
152
153 if __name__ == '__main__':
154 CURRENT_FILE = os.path.realpath(__file__)
155 pytest.main(['-s', CURRENT_FILE])