Debian Codegraph @

dirsrvtests/tests/suites/replication/acl_replication_test.py

raw
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 os
10 import logging
11 import pytest
12 import ldap
13 from lib389.idm.user import UserAccounts
14 from lib389.idm.domain import Domain
15 from lib389.idm.directorymanager import DirectoryManager
16 from lib389.idm.account import Anonymous
17 from test389.topologies import topology_m2
18 from lib389._constants import DEFAULT_SUFFIX, PASSWORD
19 from lib389.replica import ReplicationManager
20
21 pytestmark = pytest.mark.tier2
22
23 DEBUGGING = os.getenv("DEBUGGING", default=False)
24 if DEBUGGING:
25 logging.getLogger(__name__).setLevel(logging.DEBUG)
26 else:
27 logging.getLogger(__name__).setLevel(logging.INFO)
28 log = logging.getLogger(__name__)
29
30
31 def test_nscpentrywsi_access_control(topology_m2):
32 """Test that nscpentrywsi attribute access is properly controlled by ACLs in replication.
33
34 This test verifies nscpentrywsi attribute should only be visible to Directory Manager
35 and not to regular users or anonymous binds in a replication environment.
36
37 :id: b47869a1-8c2d-4f5e-9e6a-1b2c3d4e5f67
38 :setup: Two supplier replication topology
39 :steps:
40 1. Enable ACL error logging on both suppliers
41 2. Create a bind user on supplier1
42 3. Wait for replication to supplier2
43 4. Set anonymous ACI on both suppliers
44 5. Create test entries on supplier1
45 6. Wait for replication to supplier2
46 7. Test Directory Manager can see nscpentrywsi attribute
47 8. Test regular user cannot see nscpentrywsi attribute
48 9. Test anonymous user cannot see nscpentrywsi attribute
49 10. Verify results consistent across both suppliers
50 :expectedresults:
51 1. ACL logging enabled successfully
52 2. Bind user created successfully
53 3. Replication completed successfully
54 4. Anonymous ACI set successfully
55 5. Test entries created successfully
56 6. Replication completed successfully
57 7. Directory Manager can see nscpentrywsi attribute
58 8. Regular user cannot see nscpentrywsi attribute
59 9. Anonymous user cannot see nscpentrywsi attribute
60 10. Results consistent across both suppliers
61 """
62 supplier1 = topology_m2.ms["supplier1"]
63 supplier2 = topology_m2.ms["supplier2"]
64 repl = ReplicationManager(DEFAULT_SUFFIX)
65
66 log.info("Enable ACL error logging on both suppliers")
67 supplier1.config.set('nsslapd-errorlog-level', '8192')
68 supplier2.config.set('nsslapd-errorlog-level', '8192')
69
70 log.info("Create bind user on supplier1")
71 users = UserAccounts(supplier1, DEFAULT_SUFFIX)
72 bind_user = users.create_test_user(uid=2000)
73 bind_user.set('userPassword', PASSWORD)
74
75 log.info("Wait for replication to supplier2")
76 repl.test_replication(supplier1, supplier2)
77
78 users2 = UserAccounts(supplier2, DEFAULT_SUFFIX)
79 bind_user2 = users2.get('test_user_2000')
80 assert bind_user2.exists()
81
82 log.info("Set anonymous ACI on both suppliers")
83 anonymous_aci = '(targetattr!="userPassword")(version 3.0; acl "Enable anonymous access"; allow (read, search, compare) userdn="ldap:///anyone";)'
84
85 domain1 = Domain(supplier1, DEFAULT_SUFFIX)
86 domain1.set('aci', anonymous_aci)
87
88 domain2 = Domain(supplier2, DEFAULT_SUFFIX)
89 domain2.set('aci', anonymous_aci)
90
91 log.info("Create test entries on supplier1")
92 test_users = []
93 for i in range(3000, 3010):
94 test_user = users.create_test_user(uid=i)
95 test_users.append(test_user)
96
97 log.info("Wait for replication to supplier2")
98 repl.test_replication(supplier1, supplier2)
99
100 log.info("Test Directory Manager access on supplier1")
101 dm1 = DirectoryManager(supplier1)
102 dm_conn1 = dm1.bind(PASSWORD)
103 msgid = dm_conn1.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
104 rtype, entries, rmsgid = dm_conn1.result2(msgid)
105 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
106 log.info(f"Directory Manager found {nscpentrywsi_count} entries with nscpentrywsi attribute")
107 assert nscpentrywsi_count > 0, "Directory Manager should see nscpentrywsi attribute"
108 dm_conn1.close()
109
110 log.info("Test Directory Manager access on supplier2")
111 dm2 = DirectoryManager(supplier2)
112 dm_conn2 = dm2.bind(PASSWORD)
113 msgid = dm_conn2.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
114 rtype, entries, rmsgid = dm_conn2.result2(msgid)
115 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
116 log.info(f"Directory Manager found {nscpentrywsi_count} entries with nscpentrywsi attribute")
117 assert nscpentrywsi_count > 0, "Directory Manager should see nscpentrywsi attribute"
118 dm_conn2.close()
119
120 log.info("Test regular user access on supplier1")
121 user_conn1 = bind_user.bind(PASSWORD)
122 msgid = user_conn1.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
123 rtype, entries, rmsgid = user_conn1.result2(msgid)
124 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
125 log.info(f"Regular user found {nscpentrywsi_count} entries with nscpentrywsi attribute")
126 assert nscpentrywsi_count == 0, "Regular user should not see nscpentrywsi attribute"
127 user_conn1.close()
128
129 log.info("Test regular user access on supplier2")
130 user_conn2 = bind_user2.bind(PASSWORD)
131 msgid = user_conn2.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
132 rtype, entries, rmsgid = user_conn2.result2(msgid)
133 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
134 log.info(f"Regular user found {nscpentrywsi_count} entries with nscpentrywsi attribute")
135 assert nscpentrywsi_count == 0, "Regular user should not see nscpentrywsi attribute"
136 user_conn2.close()
137
138 log.info("Test anonymous access on supplier1")
139 anon1 = Anonymous(supplier1)
140 anon_conn1 = anon1.bind()
141 msgid = anon_conn1.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
142 rtype, entries, rmsgid = anon_conn1.result2(msgid)
143 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
144 log.info(f"Anonymous user found {nscpentrywsi_count} entries with nscpentrywsi attribute")
145 assert nscpentrywsi_count == 0, "Anonymous user should not see nscpentrywsi attribute"
146 anon_conn1.close()
147
148 log.info("Test anonymous access on supplier2")
149 anon2 = Anonymous(supplier2)
150 anon_conn2 = anon2.bind()
151 msgid = anon_conn2.search_ext(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=*', ['nscpentrywsi'])
152 rtype, entries, rmsgid = anon_conn2.result2(msgid)
153 nscpentrywsi_count = sum(1 for dn, attrs in entries if 'nscpentrywsi' in attrs)
154 log.info(f"Anonymous user found {nscpentrywsi_count} entries with nscpentrywsi attribute")
155 assert nscpentrywsi_count == 0, "Anonymous user should not see nscpentrywsi attribute"
156 anon_conn2.close()
157
158 log.info("nscpentrywsi access control test completed successfully")
159
160
161 if __name__ == '__main__':
162 # Run isolated
163 # -s for DEBUG mode
164 CURRENT_FILE = os.path.realpath(__file__)
165 pytest.main(["-s", CURRENT_FILE])