Debian Codegraph @

dirsrvtests/tests/suites/tls/tls_repl_clientauth_test.py

raw
1 # --- BEGIN COPYRIGHT BLOCK ---
2 # Copyright (C) 2024 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 pytest
11 import logging
12 from lib389.replica import Replicas, ReplicationManager
13 from lib389._constants import DEFAULT_SUFFIX
14 from lib389.config import CertmapLegacy
15 from lib389.idm.services import ServiceAccounts
16 from test389.topologies import topology_m2 as topo
17
18 pytestmark = pytest.mark.tier1
19
20 DEBUGGING = os.getenv("DEBUGGING", default=False)
21 if DEBUGGING:
22 logging.getLogger(__name__).setLevel(logging.DEBUG)
23 else:
24 logging.getLogger(__name__).setLevel(logging.INFO)
25 log = logging.getLogger(__name__)
26
27
28 @pytest.fixture(scope="module")
29 def topo_tls_ldapi(topo):
30 """Enable TLS on both suppliers and reconfigure both agreements
31 to use TLS Client auth. Also, setup ldapi and export DB
32 """
33
34 m1, m2 = topo.ms["supplier1"], topo.ms["supplier2"]
35
36 # Create and configure certmaps
37 cm_m1, cm_m2 = CertmapLegacy(m1), CertmapLegacy(m2)
38 certmaps = cm_m1.list()
39 certmaps['default'].update({'DNComps': None, 'CmapLdapAttr': 'nsCertSubjectDN'})
40 cm_m1.set(certmaps)
41 cm_m2.set(certmaps)
42
43 # Enable TLS on both instances
44 for instance in topo:
45 instance.enable_tls()
46
47 # Create replication DNs
48 services = ServiceAccounts(m1, DEFAULT_SUFFIX)
49 for instance in (m1, m2):
50 repl = services.get(f'{DEFAULT_SUFFIX}:{instance.host}:{instance.sslport}')
51 repl.set('nsCertSubjectDN', instance.get_server_tls_subject())
52
53 # Check the replication is "done".
54 repl = ReplicationManager(DEFAULT_SUFFIX)
55 repl.wait_for_replication(m1, m2)
56
57 # Now change the auth type
58 for instance, other in [(m1, m2), (m2, m1)]:
59 replica = Replicas(instance).get(DEFAULT_SUFFIX)
60 agmt = replica.get_agreements().list()[0]
61 agmt.replace_many(
62 ('nsDS5ReplicaBindMethod', 'SSLCLIENTAUTH'),
63 ('nsDS5ReplicaTransportInfo', 'SSL'),
64 ('nsDS5ReplicaPort', str(other.sslport)),
65 )
66 agmt.remove_all('nsDS5ReplicaBindDN')
67
68 # Set up LDAPI
69 for instance in topo:
70 instance.config.set('nsslapd-ldapilisten', 'on')
71 instance.config.set('nsslapd-ldapifilepath', f'/var/run/slapd-{instance.serverid}.socket')
72 instance.restart()
73
74 repl.test_replication(m1, m2)
75 repl.test_replication(m2, m1)
76
77 return topo
78
79
80 def find_ca_files():
81 ca_files = []
82 for root, dirs, files in os.walk('/tmp'):
83 if 'Self-Signed-CA.pem' in files and 'dirsrv@' in root:
84 ca_files.append(os.path.join(root, 'Self-Signed-CA.pem'))
85 return ca_files
86
87
88 def test_new_tls_context_error(topo_tls_ldapi):
89 """Test TLS context error when CA certificate is removed
90
91 :id: 88d4c841-9f91-499b-ba30-f834225effd8
92 :setup: Two supplier replication with SSLCLIENTAUTH agmts
93 :steps:
94 1. Remove tmp's Self-Signed-CA.pem dirsrv file
95 2. Reinit agreement
96 3. Check errors log and make sure the detailed error is there
97 :expectedresults:
98 1. Self-Signed-CA.pem file is removed
99 2. Replication reinitialization fails
100 3. Error log contains the expected SSL certificate verify failed message
101 """
102
103 m1 = topo_tls_ldapi.ms["supplier1"]
104
105 log.info('Find and remove Self-Signed-CA.pem dirsrv files')
106 ca_files = find_ca_files()
107 if not ca_files:
108 pytest.skip("No Self-Signed-CA.pem files found. Skipping test.")
109
110 log.info(f'Found {len(ca_files)} Self-Signed-CA.pem files')
111 log.info(f'CA files are: {", ".join(ca_files)}')
112
113 for ca_file in ca_files:
114 try:
115 os.remove(ca_file)
116 log.info(f"Removed file: {ca_file}")
117 except OSError as e:
118 log.info(f"Error removing file {ca_file}: {e}")
119
120 log.info('Reinit agreement')
121 replica_m1 = Replicas(m1).get(DEFAULT_SUFFIX)
122 agmt_m1 = replica_m1.get_agreements().list()[0]
123 agmt_m1.begin_reinit()
124 agmt_m1.wait_reinit()
125
126 log.info('Restart the server to flush logs')
127 m1.restart()
128
129 log.info('Check errors log for certificate verify failed message')
130 error_msg = "error:80000002:system library::No such file or directory"
131 assert m1.searchErrorsLog(error_msg), f"Expected error message not found: {error_msg}"
132
133 log.info('Test completed successfully')
134
135
136 if __name__ == '__main__':
137 # Run isolated
138 # -s for DEBUG mode
139 CURRENT_FILE = os.path.realpath(__file__)
140 pytest.main(["-s", CURRENT_FILE])