Debian Codegraph @

dirsrvtests/tests/suites/plugins/addn_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 import logging
10 import os
11
12 import ldap
13 import pytest
14
15 from lib389._entry import Entry
16 from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD
17 from test389.topologies import topology_st
18 from lib389.utils import ds_is_older, ensure_bytes
19 from lib389.idm.user import UserAccount, UserAccounts, DEFAULT_BASEDN_RDN
20
21 log = logging.getLogger(__name__)
22
23 DEBUGGING = os.getenv('DEBUGGING', False)
24 if DEBUGGING:
25 log.setLevel(logging.DEBUG)
26 else:
27 log.setLevel(logging.INFO)
28
29 pytestmark = [pytest.mark.tier1,
30 pytest.mark.skipif(ds_is_older('1.3.6'), reason="addn not implemented")]
31
32 ADDN_PLUGIN_DN = 'cn=addn,cn=plugins,cn=config'
33 ADDN_DOMAIN_DN = f'cn=example.com,{ADDN_PLUGIN_DN}'
34
35 USER1 = 'user1'
36 USER1_DOMAIN = '[email protected]'
37 ADDN_FILTER = '(&(objectClass=account)(uid=%s))'
38
39
40 def _addn_bind_succeeds(inst, name, cred):
41 """Return True if simple_bind_s succeeds, False on INVALID_CREDENTIALS; other errors propagate."""
42 if DEBUGGING:
43 log.debug(f'addn test BINDING AS {name}')
44 try:
45 inst.simple_bind_s(name, cred)
46 return True
47 except ldap.INVALID_CREDENTIALS:
48 return False
49
50
51 @pytest.fixture
52 def addn_plugin_setup(topology_st, request):
53 """
54 Create the addn plugin entry and cn=example.com child (addn_base / addn_filter), then
55 restart.
56 """
57 inst = topology_st.standalone
58 inst.add_s(Entry((
59 ADDN_PLUGIN_DN,
60 {
61 'objectClass': 'top nsSlapdPlugin extensibleObject'.split(),
62 'cn': 'addn',
63 'nsslapd-pluginPath': 'libaddn-plugin',
64 'nsslapd-pluginInitfunc': 'addn_init',
65 'nsslapd-pluginType': 'preoperation',
66 'nsslapd-pluginEnabled': 'on',
67 'nsslapd-pluginId': 'addn',
68 'nsslapd-pluginVendor': '389 Project',
69 'nsslapd-pluginVersion': '1.3.6.0',
70 'nsslapd-pluginDescription': 'Allow AD DN style bind names to LDAP',
71 'addn_default_domain': 'example.com',
72 }
73 )))
74
75 inst.add_s(Entry((
76 ADDN_DOMAIN_DN,
77 {
78 'objectClass': 'top extensibleObject'.split(),
79 'cn': 'example.com',
80 'addn_base': f'{DEFAULT_BASEDN_RDN},{DEFAULT_SUFFIX}',
81 'addn_filter': ADDN_FILTER,
82 }
83 )))
84
85 inst.restart(timeout=60)
86
87 def _cleanup():
88 people_user_dn = f'uid={USER1},{DEFAULT_BASEDN_RDN},{DEFAULT_SUFFIX}'
89 suffix_user_dn = f'uid={USER1},{DEFAULT_SUFFIX}'
90
91 try:
92 inst.simple_bind_s(DN_DM, PASSWORD)
93 except ldap.LDAPError as e:
94 log.warning(f'addn_plugin_setup finalizer: could not bind as DM: {e}')
95 return
96
97 for dn in (suffix_user_dn, people_user_dn, ADDN_DOMAIN_DN, ADDN_PLUGIN_DN):
98 try:
99 inst.delete_s(dn)
100 except ldap.NO_SUCH_OBJECT:
101 pass
102 except ldap.LDAPError as e:
103 log.warning(f'addn_plugin_setup finalizer: could not delete {dn}: {e}')
104
105 try:
106 inst.restart(timeout=60)
107 except Exception as e:
108 log.warning(f'addn_plugin_setup finalizer: restart failed: {e}')
109
110 request.addfinalizer(_cleanup)
111 return inst
112
113
114 def test_addn_plugin_bind_names(addn_plugin_setup):
115 """Addn (AD-style) bind: short name, UPN, and ambiguous addn search
116
117 :id: 173663ab-afcf-439c-9437-5429f0df0321
118 :setup: Standalone instance with addn_plugin_setup
119 :steps:
120 1. Add user1 under the default people subtree
121 2. Check bind with full user DN, with short name user1, and with [email protected] (unambiguous addn)
122 3. Add a second user1 at the suffix root; set addn_base to the full default suffix, restart
123 4. Check that simple binds with both full user DNs still succeed
124 5. In a loop, attempt bind for user1 and [email protected]
125 :expectedresults:
126 1. User1 is created successfully
127 2. The three unambiguous bind forms all succeed
128 3. Second user1 created successfully; addn_base covers the whole default suffix
129 4. Both DN-based binds still succeed
130 5. Short name and UPN each raise ldap.OPERATIONS_ERROR; DN-based binds remain valid
131 """
132 inst = addn_plugin_setup
133
134 # Create user1 in the default suffix
135 users = UserAccounts(inst, DEFAULT_SUFFIX)
136 user1 = users.create(
137 f'uid={USER1}',
138 {
139 'uid': USER1,
140 'cn': USER1,
141 'sn': USER1,
142 'uidNumber': '1000',
143 'gidNumber': '2000',
144 'homeDirectory': f'/home/{USER1}',
145 'userPassword': PASSWORD,
146 },
147 )
148
149 # Check bind
150 assert _addn_bind_succeeds(inst, user1.dn, PASSWORD)
151 for _ in range(10):
152 assert _addn_bind_succeeds(inst, USER1, PASSWORD)
153 assert _addn_bind_succeeds(inst, USER1_DOMAIN, PASSWORD)
154
155 # Create second user1
156 inst.simple_bind_s(DN_DM, PASSWORD)
157 user2 = UserAccount(inst, dn=None)
158 user2.create(
159 f'uid={USER1}',
160 {
161 'uid': USER1,
162 'cn': USER1,
163 'sn': USER1,
164 'uidNumber': '1001',
165 'gidNumber': '2000',
166 'homeDirectory': f'/home/{USER1}2',
167 'userPassword': PASSWORD,
168 },
169 DEFAULT_SUFFIX,
170 )
171
172 # Modify addn_base to cover the whole default suffix
173 inst.modify_s(
174 ADDN_DOMAIN_DN,
175 [(ldap.MOD_REPLACE, 'addn_base', ensure_bytes(DEFAULT_SUFFIX))],
176 )
177 inst.restart(timeout=60)
178
179 # Check bind
180 assert _addn_bind_succeeds(inst, user1.dn, PASSWORD)
181 assert _addn_bind_succeeds(inst, user2.dn, PASSWORD)
182 for _ in range(10):
183 with pytest.raises(ldap.OPERATIONS_ERROR):
184 _addn_bind_succeeds(inst, USER1, PASSWORD)
185 with pytest.raises(ldap.OPERATIONS_ERROR):
186 _addn_bind_succeeds(inst, USER1_DOMAIN, PASSWORD)
187
188
189 if __name__ == '__main__':
190 pytest.main([os.path.realpath(__file__), '-s'])