Debian Codegraph @

unittest/gunit/libmysqlgcs/xcom/gcs_allowlist-t.cc

raw symbols SCIP
1 /* Copyright (c) 2016, 2026, Oracle and/or its affiliates.
2
3 This program is free software; you can redistribute it and/or modify
4 it under the terms of the GNU General Public License, version 2.0,
5 as published by the Free Software Foundation.
6
7 This program is designed to work with certain software (including
8 but not limited to OpenSSL) that is licensed under separate terms,
9 as designated in a particular file or component or in included license
10 documentation. The authors of MySQL hereby grant you an additional
11 permission to link the program and your derivative works with the
12 separately licensed software that they have either included with
13 the program or referenced in the documentation.
14
15 This program is distributed in the hope that it will be useful,
16 but WITHOUT ANY WARRANTY; without even the implied warranty of
17 MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
18 GNU General Public License, version 2.0, for more details.
19
20 You should have received a copy of the GNU General Public License
21 along with this program; if not, write to the Free Software
22 Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
23
24 #include <string>
25 #include <vector>
26
27 #include "gcs_base_test.h"
28
29 using std::vector;
30
31 namespace gcs_allowlist_unittest {
32
33 class GcsAllowlist : public GcsBaseTest {
34 protected:
35 GcsAllowlist() = default;
36 };
37
38 TEST_F(GcsAllowlist, ValidIPs) {
39 Gcs_ip_allowlist wl;
40 ASSERT_TRUE(wl.is_valid("192.168.1.1"));
41 ASSERT_TRUE(wl.is_valid("192.168.1.2"));
42 ASSERT_TRUE(wl.is_valid("192.168.1.254"));
43
44 ASSERT_TRUE(wl.is_valid("::1"));
45 ASSERT_TRUE(wl.is_valid("2606:b400:8b0:40:4308:1306:ad4a:6e51"));
46 ASSERT_TRUE(wl.is_valid("::1/2"));
47 ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24"));
48 ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24,192.168.1.1"));
49 ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24,192.168.1.1,10.1.1.1"));
50 }
51
52 TEST_F(GcsAllowlist, InvalidConfiguration) {
53 Gcs_ip_allowlist wl;
54 ASSERT_FALSE(wl.is_valid("192.168.1"));
55 ASSERT_FALSE(wl.is_valid("192.168.1/24"));
56 ASSERT_FALSE(wl.is_valid("192.168.1.0/33"));
57 ASSERT_FALSE(wl.is_valid("192.168.1.0/24,192.168.2.0/33"));
58 }
59
60 TEST_F(GcsAllowlist, ValidListIPv6) {
61 Gcs_ip_allowlist wl;
62 std::string list =
63 "::1/128,192.168.1.1/24,fe80::2ab2:bdff:fe16:8d07/67, "
64 "2606:b400:8b0:40:4308:1306:ad4a:6e51";
65 wl.configure(list);
66
67 ASSERT_FALSE(wl.shall_block("::1"));
68 ASSERT_FALSE(wl.shall_block("fe80::2ab2:bdff:fe16:8d07"));
69 ASSERT_FALSE(wl.shall_block("2606:b400:8b0:40:4308:1306:ad4a:6e51"));
70 ASSERT_FALSE(wl.shall_block("::ffff:192.168.1.10"));
71 ASSERT_FALSE(wl.shall_block("192.168.1.10"));
72 }
73
74 TEST_F(GcsAllowlist, ValidListIPv4) {
75 Gcs_ip_allowlist wl;
76 wl.configure("192.168.1.0/31,localhost/32");
77
78 ASSERT_FALSE(wl.shall_block("192.168.1.1"));
79 ASSERT_TRUE(wl.shall_block("192.168.2.1"));
80 ASSERT_TRUE(wl.shall_block("192.168.1.2"));
81
82 wl.configure("192.168.1.0/32");
83 ASSERT_TRUE(wl.shall_block("192.168.1.1"));
84
85 wl.configure("192.168.1.1/32");
86 ASSERT_FALSE(wl.shall_block("192.168.1.1"));
87
88 // never block localhost
89 ASSERT_FALSE(wl.shall_block("127.0.0.1"));
90
91 wl.configure("192.168.1.0/24,192.168.2.0/24");
92 ASSERT_FALSE(wl.shall_block("127.0.0.1"));
93 ASSERT_FALSE(wl.shall_block("192.168.1.2"));
94 ASSERT_FALSE(wl.shall_block("192.168.1.254"));
95 ASSERT_FALSE(wl.shall_block("192.168.2.2"));
96 ASSERT_FALSE(wl.shall_block("192.168.2.254"));
97 }
98
99 TEST_F(GcsAllowlist, DefaultList) {
100 Gcs_ip_allowlist wl;
101
102 wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
103 ASSERT_FALSE(wl.shall_block("127.0.0.1"));
104 ASSERT_FALSE(wl.shall_block("::1"));
105 ASSERT_FALSE(wl.shall_block("fe80::da2:aab6:88aa:5061"));
106 ASSERT_FALSE(wl.shall_block("192.168.1.2"));
107 ASSERT_FALSE(wl.shall_block("192.168.2.2"));
108 ASSERT_FALSE(wl.shall_block("10.0.0.1"));
109 ASSERT_TRUE(wl.shall_block("172.15.0.1"));
110 ASSERT_FALSE(wl.shall_block("172.16.0.1"));
111 ASSERT_FALSE(wl.shall_block("172.24.0.1"));
112 ASSERT_FALSE(wl.shall_block("172.31.0.1"));
113 ASSERT_TRUE(wl.shall_block("172.38.0.1"));
114 }
115
116 TEST_F(GcsAllowlist, ListAsText) {
117 Gcs_ip_allowlist wl;
118
119 wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
120 ASSERT_STRCASEEQ(Gcs_ip_allowlist::DEFAULT_ALLOWLIST.c_str(),
121 wl.get_configured_ip_allowlist().c_str());
122 }
123
124 TEST_F(GcsAllowlist, AbsentList) {
125 Gcs_interface_parameters params;
126 params.add_parameter("group_name", "ola");
127 params.add_parameter("peer_nodes", "127.0.0.1:24844");
128 params.add_parameter("local_node", "127.0.0.1:24844");
129 params.add_parameter("bootstrap_group", "true");
130 params.add_parameter("poll_spin_loops", "100");
131
132 Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
133 enum_gcs_error err = gcs->initialize(params);
134 ASSERT_EQ(err, GCS_OK);
135
136 // verify that a allowlist was provided by default
137 Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
138 MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
139 << xcs->get_ip_allowlist().to_string());
140 ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
141 ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
142
143 // this finalizes the m_logger, so be careful to not add a call to
144 // MYSQL_GCS_LOG after this line
145 err = gcs->finalize();
146
147 // claim interface memory back
148 xcs->cleanup();
149
150 // initialization failed, and thus so will finalization
151 ASSERT_EQ(err, GCS_OK);
152 }
153
154 TEST_F(GcsAllowlist, ListWithHostname) {
155 Gcs_interface_parameters params;
156 params.add_parameter("group_name", "ola");
157 params.add_parameter("peer_nodes", "127.0.0.1:24844");
158 params.add_parameter("local_node", "127.0.0.1:24844");
159 params.add_parameter("bootstrap_group", "true");
160 params.add_parameter("poll_spin_loops", "100");
161
162 vector<char> machine_hostname;
163 machine_hostname.resize(MAXHOSTNAMELEN);
164 gethostname(machine_hostname.data(), MAXHOSTNAMELEN);
165
166 std::ostringstream assembled_allowlist;
167 assembled_allowlist << machine_hostname.data();
168 assembled_allowlist << "/16,";
169 assembled_allowlist << "localhost/32";
170 params.add_parameter("ip_allowlist", assembled_allowlist.str().c_str());
171
172 Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
173 enum_gcs_error err = gcs->initialize(params);
174 ASSERT_EQ(err, GCS_OK);
175
176 // verify that a allowlist was provided by default
177 Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
178 MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
179 << xcs->get_ip_allowlist().to_string());
180 ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
181 ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
182
183 std::vector<std::pair<sa_family_t, std::string>> ips;
184 resolve_all_ip_addr_from_hostname("localhost", ips);
185
186 auto has_v4_addresses_it =
187 std::find_if(ips.begin(), ips.end(),
188 [](std::pair<sa_family_t, std::string> const &ip_entry) {
189 return ip_entry.first == AF_INET;
190 });
191 bool has_v4_addresses = has_v4_addresses_it != ips.end();
192
193 // This should not block to whatever address localhost resolves
194 for (auto &ip : ips) {
195 if (has_v4_addresses && ip.first == AF_INET6)
196 EXPECT_TRUE(xcs->get_ip_allowlist().shall_block(ip.second));
197 else
198 EXPECT_FALSE(xcs->get_ip_allowlist().shall_block(ip.second));
199 }
200
201 // this finalizes the m_logger, so be careful to not add a call to
202 // MYSQL_GCS_LOG after this line
203 err = gcs->finalize();
204
205 // claim interface memory back
206 xcs->cleanup();
207
208 ASSERT_EQ(err, GCS_OK);
209 }
210
211 TEST_F(GcsAllowlist, ListWithUnresolvableHostname) {
212 Gcs_interface_parameters params;
213 params.add_parameter("group_name", "ola");
214 params.add_parameter("peer_nodes", "127.0.0.1:24844");
215 params.add_parameter("local_node", "127.0.0.1:24844");
216 params.add_parameter("bootstrap_group", "true");
217 params.add_parameter("poll_spin_loops", "100");
218
219 vector<char> machine_hostname;
220 machine_hostname.resize(MAXHOSTNAMELEN);
221 gethostname(machine_hostname.data(), MAXHOSTNAMELEN);
222
223 std::ostringstream assembled_allowlist;
224 assembled_allowlist << machine_hostname.data();
225 assembled_allowlist << "/16,";
226 assembled_allowlist << "unresolvablehostname/32,";
227 assembled_allowlist << "localhost/32";
228 params.add_parameter("ip_allowlist", assembled_allowlist.str().c_str());
229
230 Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
231 enum_gcs_error err = gcs->initialize(params);
232 ASSERT_EQ(err, GCS_OK);
233
234 // verify that a allowlist was provided by default
235 Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
236 MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
237 << xcs->get_ip_allowlist().to_string());
238 ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
239 ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
240
241 // This will force a allowlist validation and a failure on name resolution
242 // code
243 ASSERT_TRUE(xcs->get_ip_allowlist().shall_block("192.12.13.14"));
244
245 // this finalizes the m_logger, so be careful to not add a call to
246 // MYSQL_GCS_LOG after this line
247 err = gcs->finalize();
248
249 // claim interface memory back
250 xcs->cleanup();
251
252 ASSERT_EQ(err, GCS_OK);
253 }
254
255 TEST_F(GcsAllowlist, XComMembers) {
256 Gcs_ip_allowlist wl;
257 char const *members[] = {"8.8.8.8:12435", "8.8.4.4:1234", "localhost:12346"};
258 const char **xcom_addrs{members};
259 node_address *xcom_names = new_node_address(3, xcom_addrs);
260 site_def *xcom_config = new_site_def();
261 init_site_def(3, xcom_names, xcom_config);
262 wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
263
264 ASSERT_FALSE(wl.shall_block("127.0.0.1", xcom_config));
265 ASSERT_FALSE(wl.shall_block("::1", xcom_config));
266 ASSERT_FALSE(wl.shall_block("192.168.1.2", xcom_config));
267 ASSERT_FALSE(wl.shall_block("192.168.2.2", xcom_config));
268 ASSERT_FALSE(wl.shall_block("10.0.0.1", xcom_config));
269 ASSERT_TRUE(wl.shall_block("172.15.0.1", xcom_config));
270 ASSERT_FALSE(wl.shall_block("172.16.0.1", xcom_config));
271 ASSERT_FALSE(wl.shall_block("172.24.0.1", xcom_config));
272 ASSERT_FALSE(wl.shall_block("172.31.0.1", xcom_config));
273 ASSERT_TRUE(wl.shall_block("172.38.0.1", xcom_config));
274
275 ASSERT_FALSE(wl.shall_block("8.8.8.8", xcom_config));
276 ASSERT_FALSE(wl.shall_block("8.8.4.4", xcom_config));
277 ASSERT_TRUE(wl.shall_block("8.8.8.9", xcom_config));
278 ASSERT_TRUE(wl.shall_block("8.8.9.8", xcom_config));
279 ASSERT_TRUE(wl.shall_block("8.9.8.8", xcom_config));
280 ASSERT_TRUE(wl.shall_block("9.8.8.8", xcom_config));
281
282 delete_node_address(3, xcom_names);
283 free_site_def(xcom_config);
284 }
285
286 TEST_F(GcsAllowlist, ComparisonBetweenIPv4AndIPv6) {
287 Gcs_ip_allowlist wl;
288 wl.configure("ac0f:0001::/32");
289
290 ASSERT_TRUE(wl.shall_block("172.15.0.1"));
291 }
292
293 } // namespace gcs_allowlist_unittest