mirror of https://github.com/OpenIdentityPlatform/OpenDJ.git

Valery Kharseko
4 hours ago 8672f11245231e8d7eecc2054653a3e3b133f1a3
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
#!/usr/bin/env python3
#
# The contents of this file are subject to the terms of the Common Development and
# Distribution License (the License). You may not use this file except in compliance with the
# License.
#
# You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
# specific language governing permission and limitations under the License.
#
# When distributing Covered Software, include this CDDL Header Notice in each file and include
# the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
# Header, with the fields enclosed by brackets [] replaced by your own identifying
# information: "Portions copyright [year] [name of copyright owner]".
#
# Copyright 2026 3A Systems, LLC.
 
import importlib.util
from pathlib import Path
import socket
import subprocess
import sys
import threading
import time
import unittest
 
sys.dont_write_bytecode = True
spec = importlib.util.spec_from_file_location('monitor', Path(__file__).with_name('scale-monitor.py'))
monitor = importlib.util.module_from_spec(spec)
spec.loader.exec_module(monitor)
 
 
def wrap(tag, value):
    return monitor.tlv(tag, value)
 
 
def msg(message_id, tag, value):
    return wrap(0x30, wrap(0x02, bytes([message_id])) + wrap(tag, value))
 
 
def ldap_result(code):
    return wrap(0x0a, bytes([code])) + wrap(0x04, b'') + wrap(0x04, b'')
 
 
def entry():
    attrs = []
    for name, value in [(b'domain-name', b'dc=example,dc=com'),
                        (b'replicationServer-database', b'42'),
                        (b'last-change', b'000001a1a1e836e4002a00000001')]:
        attrs.append(wrap(0x30, wrap(0x04, name) + wrap(0x31, wrap(0x04, value))))
    return wrap(0x04, b'cn=example,cn=monitor') + wrap(0x30, b''.join(attrs))
 
 
def member_entry(dn, attributes):
    attrs = [wrap(0x30, wrap(0x04, name) + wrap(0x31, b''.join(wrap(0x04, v) for v in values)))
             for name, values in attributes.items()]
    return wrap(0x04, dn) + wrap(0x30, b''.join(attrs))
 
 
class MonitorTest(unittest.TestCase):
    def exchange(self, responses, timeout=3, search=monitor.search, args=()):
        client, server = socket.socketpair()
        errors = []
        self.requests = []
        def respond():
            try:
                self.requests.append(monitor.read_frame(server, time.monotonic() + 3))
                server.sendall(responses[0])
                if len(responses) > 1:
                    self.requests.append(monitor.read_frame(server, time.monotonic() + 3))
                    for data in responses[1:]:
                        for i in range(0, len(data), 3):
                            server.sendall(data[i:i + 3])
            except (BrokenPipeError, ConnectionResetError):
                pass
            except Exception as error:
                errors.append(error)
            finally:
                server.close()
        thread = threading.Thread(target=respond)
        thread.start()
        try:
            return search(client, b'password', time.monotonic() + timeout, *args)
        finally:
            client.close()
            thread.join(4)
            if errors:
                raise errors[0]
 
    def test_search_collects_fragmented_entries_after_success(self):
        text = self.exchange([msg(1, 0x61, ldap_result(0)), msg(2, 0x64, entry()),
                              msg(2, 0x65, ldap_result(0))])
        self.assertIn('replicationServer-database:: NDI=', text)
        self.assertIn('dn:: ', text)
        self.assertTrue(text.endswith('\n\n'))
 
    def test_connected_to_reads_only_the_workload_domain(self):
        domain = 'cn=dc_example_dc_com,cn=Replication,cn=monitor'
        def route(entries, code=0):
            responses = [msg(1, 0x61, ldap_result(0))]
            responses.extend(msg(2, 0x64, member_entry(b'cn=DS,' + domain.encode(),
                             {b'connected-to': values})) for values in entries)
            responses.append(msg(2, 0x65, ldap_result(code)))
            return self.exchange(responses, search=monitor.connected_to, args=(domain,))
 
        endpoint = b'172.18.0.10:8989'
        self.assertEqual(route([[endpoint]]), endpoint.decode())
        query = list(monitor.fields(self.requests[1]))[1][1]
        fields = list(monitor.fields(query))
        self.assertEqual(fields[:2], [(0x04, domain.encode()), (0x0a, bytes([1]))])
        self.assertEqual(fields[-2:], [(0x87, b'connected-to'), (0x30, wrap(0x04, b'connected-to'))])
        for entries, code in [([], 0), ([[endpoint], [endpoint]], 0), ([[endpoint, endpoint]], 0),
                              ([[b'']], 0), ([[endpoint]], 3)]:
            with self.subTest(entries=entries, code=code), self.assertRaises(monitor.LdapError):
                route(entries, code)
 
    def test_rejects_bind_failure(self):
        with self.assertRaises(monitor.LdapError):
            self.exchange([msg(1, 0x61, ldap_result(49))])
 
    def test_rejects_incomplete_search_even_after_entries(self):
        with self.assertRaises(monitor.LdapError):
            self.exchange([msg(1, 0x61, ldap_result(0)), msg(2, 0x64, entry()),
                           msg(2, 0x65, ldap_result(3))])
 
    def test_rejects_wrong_message_id(self):
        with self.assertRaises(monitor.LdapError):
            self.exchange([msg(7, 0x61, ldap_result(0))])
 
    def test_rejects_invalid_lengths_and_truncated_messages(self):
        for value in (b'\x30\x80', b'\x30\x82\xff\xff', b'\x30\x05\x02'):
            with self.subTest(value=value):
                client, server = socket.socketpair()
                server.sendall(value); server.close()
                try:
                    with self.assertRaises((monitor.LdapError, EOFError)):
                        monitor.read_frame(client, time.monotonic() + 1)
                finally:
                    client.close()
 
    def test_refuses_oversized_snapshot_without_returning_partial_data(self):
        original = monitor.MAX_SNAPSHOT
        monitor.MAX_SNAPSHOT = 10
        try:
            with self.assertRaises(monitor.LdapError):
                self.exchange([msg(1, 0x61, ldap_result(0)), msg(2, 0x64, entry()),
                               msg(2, 0x65, ldap_result(0))])
        finally:
            monitor.MAX_SNAPSHOT = original
 
    def test_expired_load_deadline_returns_no_snapshot(self):
        result = subprocess.run([sys.executable, str(Path(__file__).with_name('scale-monitor.py')), '1', '1'],
                                input=b'password', stdout=subprocess.PIPE, stderr=subprocess.PIPE)
        self.assertEqual(result.returncode, 124)
        self.assertEqual(result.stdout, b'')
        self.assertIn(b'timeout=load', result.stderr)
 
    def test_deadline_after_final_response_does_not_return_a_snapshot(self):
        original = monitor.require_success
        calls = []
        def delayed_result(value):
            calls.append(1)
            original(value)
            if len(calls) == 2:
                time.sleep(0.4)
        monitor.require_success = delayed_result
        try:
            with self.assertRaises(TimeoutError):
                self.exchange([msg(1, 0x61, ldap_result(0)), msg(2, 0x65, ldap_result(0))], timeout=0.3)
            self.assertEqual(len(calls), 2)
        finally:
            monitor.require_success = original
 
    def test_no_bind_is_sent_after_the_deadline(self):
        client, server = socket.socketpair()
        server.settimeout(0.05)
        try:
            with self.assertRaises(TimeoutError):
                monitor.search(client, b'password', time.monotonic() - 1)
            with self.assertRaises(socket.timeout):
                server.recv(1)
        finally:
            client.close(); server.close()
 
    def test_absolute_deadline_interrupts_wait(self):
        client, server = socket.socketpair()
        try:
            with self.assertRaises(TimeoutError):
                monitor.read_frame(client, time.monotonic() + 0.05)
        finally:
            client.close(); server.close()
 
    def membership_exchange(self, registry, config, last_result=0):
        client, server = socket.socketpair()
        requests, errors = [], []
        def respond():
            try:
                for message_id, entries in [(1, None), (2, registry), (3, config)]:
                    requests.append(monitor.read_frame(server, time.monotonic() + 3))
                    if entries is None:
                        server.sendall(msg(1, 0x61, ldap_result(0)))
                    else:
                        for value in entries:
                            server.sendall(msg(message_id, 0x64, value))
                        server.sendall(msg(message_id, 0x65, ldap_result(last_result if message_id == 3 else 0)))
            except (BrokenPipeError, ConnectionResetError):
                pass
            except Exception as error:
                errors.append(error)
            finally:
                server.close()
        thread = threading.Thread(target=respond); thread.start()
        try:
            result = monitor.membership(client, b'password', time.monotonic() + 3, 'cv-r1', 'absent')
            return result, requests
        finally:
            client.close(); thread.join(4)
            if errors: raise errors[0]
 
    def test_membership_uses_one_bind_and_two_complete_searches(self):
        registry = [member_entry(b'cn=d0,cn=Servers,cn=admin data', {b'hostname': [b'cv-d0.example']})]
        config = [member_entry(b'cn=domain,cn=config', {b'ds-cfg-replication-server': [b'cv-r10.example:8989']})]
        result, requests = self.membership_exchange(registry, config)
        self.assertTrue(result)
        self.assertEqual(len(requests), 3)
        self.assertIn(b'cn=Servers,cn=admin data', requests[1])
        self.assertIn(b'cn=config', requests[2])
 
    def test_membership_rejects_a_partial_second_search(self):
        registry = [member_entry(b'cn=d0', {b'hostname': [b'cv-d0']})]
        config = [member_entry(b'cn=domain', {})]
        with self.assertRaises(monitor.LdapError):
            self.membership_exchange(registry, config, last_result=3)
 
    def test_membership_semantics_for_all_lists(self):
        good_registry = [{'hostname': ['CV-R1.example']}]
        good_config = [{'ds-cfg-replication-server': ['cv-r1.example:8989']}]
        self.assertTrue(monitor.membership_matches(good_registry, good_config, 'cv-r1', 'present'))
        self.assertFalse(monitor.membership_matches(good_registry, good_config + [{}], 'cv-r1', 'present'))
        self.assertFalse(monitor.membership_matches(good_registry, good_config, 'cv-r1', 'absent'))
        self.assertFalse(monitor.membership_matches([], good_config, 'cv-r1', 'absent'))
        self.assertFalse(monitor.membership_matches([{}], good_config, 'cv-r1', 'absent'))
        self.assertFalse(monitor.membership_matches([{'hostname': ['']}], [{}], 'cv-r1', 'absent'))
        self.assertFalse(monitor.membership_matches(good_registry, [], 'cv-r1', 'present'))
        self.assertTrue(monitor.membership_matches([{'hostname': ['cv-d0']}], [{}], 'cv-r1', 'absent'))
        self.assertFalse(monitor.membership_matches([{'hostname': ['cv-r0']}], good_config, 'cv-r1', 'present'))
        self.assertFalse(monitor.membership_matches(good_registry, [{'ds-cfg-replication-server': ['cv-r0:8989']}], 'cv-r1', 'present'))
        self.assertFalse(monitor.membership_matches([{'hostname': ['cv-d0']}], good_config, 'cv-r1', 'absent'))
 
 
if __name__ == '__main__':
    unittest.main()