Debian Codegraph @

dirsrvtests/tests/suites/clu/dsconf_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2021 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 """Test dsconf CLI with LDAPS"""
10
11 import logging
12 import os
13 import subprocess
14 import tempfile
15 import pytest
16 import ldap
17 from lib389.cli_base import LogCapture
18 from lib389._constants import DEFAULT_SUFFIX, DN_DM, PASSWORD, ReplicaRole
19 from lib389.pwpolicy import PwPolicyEntry
20 from test389.topologies import create_topology
21
22
23 pytestmark = pytest.mark.tier1
24
25 log = logging.getLogger(__name__)
26
27
28 @pytest.fixture(scope="function")
29 def enable_config(request, topology_st, config_type):
30 if config_type == 'ldapfile':
31 with open('/tmp/ldap_temp.conf', 'w') as f:
32 f.write("TLS_CACERT /etc/dirsrv/slapd-standalone1/ca.crt\n")
33 f.close()
34
35 else:
36 data = ['[localhost]\n', 'tls_cacertdir = /etc/dirsrv/slapd-standalone1\n',
37 f'uri = {topology_st.standalone.get_ldaps_uri()}\n']
38 fd = open(f'{os.environ.get("HOME")}/.dsrc', 'w')
39 for line in data:
40 fd.write(line)
41 fd.close()
42
43 def fin():
44 if config_type == 'ldapfile':
45 os.remove('/tmp/ldap_temp.conf')
46 else:
47 os.remove(f'{os.environ.get("HOME")}/.dsrc')
48
49 request.addfinalizer(fin)
50
51
52 @pytest.fixture(scope="function")
53 def topology_st(request):
54 """Create DS standalone instance"""
55
56 topology = create_topology({ReplicaRole.STANDALONE: 1})
57
58 topology.logcap = LogCapture()
59 return topology
60
61 def test_backend_referral(topology_st):
62 """Test setting and deleting referral in backend
63
64 :id: e65fa6c3-da7c-49f8-be0c-738be46a1180
65 :setup: Standalone Instance
66 :steps:
67 1. Set referral using dsconf command
68 2. Verify that referral is set correctly
69 3. Set nsslapd-state to referral and verify
70 4. Test a referral error
71 5. Restart the server
72 6. Cleanup - delete referral and set nsslapd-state to 'backend'
73 :expectedresults:
74 1. Success
75 2. Success
76 3. Success
77 4. Success
78 5. Success
79 6. Success
80 """
81 dsconf_cmd= ['/usr/sbin/dsconf', topology_st.standalone.serverid, '-D', DN_DM, '-w', 'password']
82 # Set referral
83 log.info("Use dsconf to set referral")
84 cmdline = dsconf_cmd + ['backend', 'suffix', 'set', '--add-referral', 'ldap://localhost.localdomain:389/o%3dnetscaperoot', 'userRoot']
85 log.info(f'Command used: %{cmdline}')
86 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
87 msg = proc.communicate()
88 log.info('output message : %s' % msg[0])
89 assert proc.returncode == 0
90
91 # Check referral
92 log.info("Verify referral is set correctly")
93 cmdline = dsconf_cmd + ['backend', 'suffix', 'get', 'userRoot']
94 log.info(f'Command used: %{cmdline}')
95 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
96 out, _ = proc.communicate()
97 log.info('output message : %s' % out[0])
98 assert 'referral: ldap://localhost.localdomain:389/o%3dnetscaperoot' in out.decode('utf-8')
99
100 # Set nsslapd-state to referral
101 log.info("Use dsconf to set nsslapd-state to referral")
102 cmdline = dsconf_cmd + ['backend', 'suffix', 'set', '--state', 'referral', 'userRoot']
103 log.info(f'Command used: %{cmdline}')
104 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
105 msg = proc.communicate()
106 log.info('output message : %s' % msg[0])
107 assert proc.returncode == 0
108
109 # Verify nsslapd-state
110 log.info("Verify nsslapd-state is set to referral")
111 cmdline = dsconf_cmd + ['backend', 'suffix', 'get', 'userRoot']
112 log.info(f'Command used: %{cmdline}')
113 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
114 out, _ = proc.communicate()
115 log.info('output message : %s' % out[0])
116 assert 'state: referral' in out.decode('utf-8')
117
118 # Test a referral error
119 topology_st.standalone.set_option(ldap.OPT_REFERRALS, 0) # Do not follow referral
120 with pytest.raises(ldap.REFERRAL):
121 topology_st.standalone.search_s(DEFAULT_SUFFIX, ldap.SCOPE_SUBTREE, 'objectclass=top')
122
123 # Restart the server
124 log.info('Restarting the server...')
125 topology_st.standalone.restart(timeout=10)
126
127 # Cleanup
128 log.info('Cleaning up...')
129 cmdline = dsconf_cmd + ['backend', 'suffix', 'set', '--state', 'backend', 'userRoot']
130 log.info(f'Command used: %{cmdline}')
131 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
132 out, _ = proc.communicate()
133 log.info('output message : %s' % out[0])
134 assert proc.returncode == 0
135
136 cmdline = dsconf_cmd + ['backend', 'suffix', 'set', '--del-referral', 'ldap://localhost.localdomain:389/o%3dnetscaperoot', 'userRoot']
137 log.info(f'Command used: %{cmdline}')
138 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
139 out, _ = proc.communicate()
140 log.info('output message : %s' % out[0])
141 assert proc.returncode == 0
142 topology_st.standalone.set_option(ldap.OPT_REFERRALS, 1)
143
144
145 @pytest.mark.parametrize('config_type', ('ldapfile','dsrfile'))
146 def test_dsconf_with_ldaps(topology_st, enable_config, config_type):
147 """Test dsconf CLI with LDAPS
148
149 :id: 5288a288-60f0-4e81-a44b-d2ee2611ca86
150 :parametrized: yes
151 :customerscenario: True
152 :setup: Standalone Instance
153 :steps:
154 1. Enable TLS
155 2. Set only ldap.conf
156 3. Verify dsconf command is working correctly on ldaps
157 4. Set only ~/.dsrc file
158 5. Verify dsconf command is working correctly on ldaps
159 :expectedresults:
160 1. Success
161 2. Success
162 3. Success
163 4. Success
164 5. Success
165 """
166 log.info("Enable TLS")
167 topology_st.standalone.enable_tls()
168 if config_type == 'ldapfile':
169 log.info("Use dsconf to list certificate")
170 cmdline=['/usr/sbin/dsconf', topology_st.standalone.get_ldaps_uri(), '-D',
171 DN_DM, '-w', 'password', 'security', 'certificate', 'list']
172 log.info(f'Command used : %{cmdline}')
173 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE, env={'LDAPCONF': '/tmp/ldap_temp.conf'})
174 else:
175 log.info("Use dsconf to list certificate")
176 cmdline=['/usr/sbin/dsconf','standalone1', '-D', DN_DM, '-w', 'password',
177 'security', 'certificate', 'list']
178 log.info(f'Command used : %{cmdline}')
179 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
180
181 msg = proc.communicate()
182 log.info(f'output message : {msg[0]}')
183 assert proc.returncode == 0
184
185
186 @pytest.mark.parametrize('instance_role', ('consumer', 'hub'))
187 def test_check_replica_id_rejected_hub_consumer(instance_role):
188 """Test dsconf CLI does not accept replica-id parameter for comsumer and hubs
189
190 :id: 274b47f8-111a-11ee-8321-98fa9ba19b65
191 :parametrized: yes
192 :customerscenario: True
193 :setup: Create DS instance
194 :steps:
195 1. Create ldap instance
196 2. Use dsconf cli to create replica and specify replica id for a consumer
197 3. Verify dsconf command rejects replica_id for consumers
198 4. Repeat for a hub use dsconf to create a replica w replica id
199 5. Verify dsconf command rejects replica_id for hubs
200 :expectedresults:
201 1. Success
202 2. Success
203 3. Setting the "replica-id" manually for consumers not allowed.
204 4. Success
205 5. Setting the "replica-id" manually for hubs is not allowed.
206 """
207 print("DN_DM {}".format(DN_DM))
208 cmdline = ['/usr/sbin/dsconf', 'standalone1', '-D', DN_DM, '-w', 'password', 'replication', 'enable', '--suffix', DEFAULT_SUFFIX, '--role', instance_role, '--replica-id=1']
209 log.info(f'Command used : {cmdline}')
210 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
211
212 msg = proc.communicate()
213 msg = msg[0].decode('utf-8')
214 log.info(f'output message : {msg}')
215 assert "Replication successfully enabled for" not in msg, f"Test Failed: --replica-id option is accepted....It shouldn't for {instance_role}"
216 log.info(f"Test PASSED: --replica-id option is NOT accepted for {instance_role}.")
217
218
219 @pytest.mark.parametrize('instance_role, replica_id',
220 [('consumer', None), ('hub', None), ('consumer', "65535"), ('hub', "65535")])
221 def test_check_replica_id_accepted_hub_consumer(topology_st, instance_role, replica_id):
222 """Test dsconf CLI accepts 65535 replica-id parameter for comsumer and hubs
223
224 :id: e0a1a1e6-11c1-40e6-92fe-cb550fb2170d
225 :parametrized: yes
226 :customerscenario: True
227 :setup: Create DS instance
228 :steps:
229 1. Create ldap instance
230 2. Use dsconf cli to create replica and don't specify replica id for a consumer or hub
231 3. Use dsconf cli to create replica and specify replica id for a consumer or hub
232 :expectedresults:
233 1. Success
234 2. Success
235 3. Success
236 """
237 print("DN_DM {}".format(DN_DM))
238 cmdline = ['/usr/sbin/dsconf', 'standalone1', '-D', DN_DM, '-w', 'password', 'replication', 'enable', '--suffix', DEFAULT_SUFFIX, '--role', instance_role]
239 if replica_id is not None:
240 cmdline.append(f'--replica-id={replica_id}')
241 log.info(f'Command used : {cmdline}')
242 proc = subprocess.Popen(cmdline, stdout=subprocess.PIPE)
243
244 msg = proc.communicate()
245 msg = msg[0].decode('utf-8')
246 log.info(f'output message : {msg}')
247 assert "Replication successfully enabled for" in msg
248 log.info(f"Test PASSED: --replica-id option is accepted for {instance_role}.")
249
250
251 def test_dsconf_localpwp_list_nonstandard_policy(topology_st):
252 """Verify a non-standard policy is listed by 'dsconf localpwp list' without
253 resulting in an error.
254
255 :id: a7f2c91e-4b5d-4e8a-9c3f-1d6e8a2b4c70
256 :setup: Standalone instance
257 :steps:
258 1. Enable local password policy (nsslapd-pwpolicy-local on).
259 2. Create a password policy at cn=passwordpolicy,<suffix> using PwPolicyEntry.create().
260 3. Run 'dsconf localpwp list' and verify exit code 0 and that the policy DN is listed.
261 :expectedresults:
262 1. Success
263 2. Success
264 3. Success
265 """
266 inst = topology_st.standalone
267
268 inst.simple_bind_s(DN_DM, PASSWORD)
269 inst.config.set("nsslapd-pwpolicy-local", "on")
270
271 # Create a non-standard password policy
272 policy_dn = f"cn=passwordpolicy,{DEFAULT_SUFFIX}"
273 policy = PwPolicyEntry(inst, dn=policy_dn)
274 policy.create(properties={'cn': 'passwordpolicy','passwordChange': 'on'})
275
276 # Verify dsconf localpwp list succeeds and lists the policy
277 dsconf_cmd = ["/usr/sbin/dsconf", inst.serverid, "localpwp", "list"]
278 proc = subprocess.run(
279 dsconf_cmd,
280 capture_output=True,
281 text=True,
282 timeout=10,
283 )
284 assert proc.returncode == 0, (
285 f"dsconf localpwp list failed: rc={proc.returncode} stderr={proc.stderr!r}"
286 )
287 assert policy_dn in proc.stdout, (
288 f"Policy not found in list output: {proc.stdout!r}"
289 )
290 log.info(f"dsconf localpwp list succeeded and listed policy {policy_dn}")