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 252 253 254 255 256 257 258 259 260 261 262 263 264 265 266 267 268 269 270 271 272 273 274 275 276 277 278 279 280 281 282 283 284 285 286 287 288 289 290 291 292 293
|
/* Copyright (c) 2016, 2025, Oracle and/or its affiliates.
This program is free software; you can redistribute it and/or modify
it under the terms of the GNU General Public License, version 2.0,
as published by the Free Software Foundation.
This program is designed to work with certain software (including
but not limited to OpenSSL) that is licensed under separate terms,
as designated in a particular file or component or in included license
documentation. The authors of MySQL hereby grant you an additional
permission to link the program and your derivative works with the
separately licensed software that they have either included with
the program or referenced in the documentation.
This program is distributed in the hope that it will be useful,
but WITHOUT ANY WARRANTY; without even the implied warranty of
MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
GNU General Public License, version 2.0, for more details.
You should have received a copy of the GNU General Public License
along with this program; if not, write to the Free Software
Foundation, Inc., 51 Franklin St, Fifth Floor, Boston, MA 02110-1301 USA */
#include <string>
#include <vector>
#include "gcs_base_test.h"
using std::vector;
namespace gcs_allowlist_unittest {
class GcsAllowlist : public GcsBaseTest {
protected:
GcsAllowlist() = default;
};
TEST_F(GcsAllowlist, ValidIPs) {
Gcs_ip_allowlist wl;
ASSERT_TRUE(wl.is_valid("192.168.1.1"));
ASSERT_TRUE(wl.is_valid("192.168.1.2"));
ASSERT_TRUE(wl.is_valid("192.168.1.254"));
ASSERT_TRUE(wl.is_valid("::1"));
ASSERT_TRUE(wl.is_valid("2606:b400:8b0:40:4308:1306:ad4a:6e51"));
ASSERT_TRUE(wl.is_valid("::1/2"));
ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24"));
ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24,192.168.1.1"));
ASSERT_TRUE(wl.is_valid("::1/64,192.168.1.2/24,192.168.1.1,10.1.1.1"));
}
TEST_F(GcsAllowlist, InvalidConfiguration) {
Gcs_ip_allowlist wl;
ASSERT_FALSE(wl.is_valid("192.168.1"));
ASSERT_FALSE(wl.is_valid("192.168.1/24"));
ASSERT_FALSE(wl.is_valid("192.168.1.0/33"));
ASSERT_FALSE(wl.is_valid("192.168.1.0/24,192.168.2.0/33"));
}
TEST_F(GcsAllowlist, ValidListIPv6) {
Gcs_ip_allowlist wl;
std::string list =
"::1/128,192.168.1.1/24,fe80::2ab2:bdff:fe16:8d07/67, "
"2606:b400:8b0:40:4308:1306:ad4a:6e51";
wl.configure(list);
ASSERT_FALSE(wl.shall_block("::1"));
ASSERT_FALSE(wl.shall_block("fe80::2ab2:bdff:fe16:8d07"));
ASSERT_FALSE(wl.shall_block("2606:b400:8b0:40:4308:1306:ad4a:6e51"));
ASSERT_FALSE(wl.shall_block("::ffff:192.168.1.10"));
ASSERT_FALSE(wl.shall_block("192.168.1.10"));
}
TEST_F(GcsAllowlist, ValidListIPv4) {
Gcs_ip_allowlist wl;
wl.configure("192.168.1.0/31,localhost/32");
ASSERT_FALSE(wl.shall_block("192.168.1.1"));
ASSERT_TRUE(wl.shall_block("192.168.2.1"));
ASSERT_TRUE(wl.shall_block("192.168.1.2"));
wl.configure("192.168.1.0/32");
ASSERT_TRUE(wl.shall_block("192.168.1.1"));
wl.configure("192.168.1.1/32");
ASSERT_FALSE(wl.shall_block("192.168.1.1"));
// never block localhost
ASSERT_FALSE(wl.shall_block("127.0.0.1"));
wl.configure("192.168.1.0/24,192.168.2.0/24");
ASSERT_FALSE(wl.shall_block("127.0.0.1"));
ASSERT_FALSE(wl.shall_block("192.168.1.2"));
ASSERT_FALSE(wl.shall_block("192.168.1.254"));
ASSERT_FALSE(wl.shall_block("192.168.2.2"));
ASSERT_FALSE(wl.shall_block("192.168.2.254"));
}
TEST_F(GcsAllowlist, DefaultList) {
Gcs_ip_allowlist wl;
wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
ASSERT_FALSE(wl.shall_block("127.0.0.1"));
ASSERT_FALSE(wl.shall_block("::1"));
ASSERT_FALSE(wl.shall_block("fe80::da2:aab6:88aa:5061"));
ASSERT_FALSE(wl.shall_block("192.168.1.2"));
ASSERT_FALSE(wl.shall_block("192.168.2.2"));
ASSERT_FALSE(wl.shall_block("10.0.0.1"));
ASSERT_TRUE(wl.shall_block("172.15.0.1"));
ASSERT_FALSE(wl.shall_block("172.16.0.1"));
ASSERT_FALSE(wl.shall_block("172.24.0.1"));
ASSERT_FALSE(wl.shall_block("172.31.0.1"));
ASSERT_TRUE(wl.shall_block("172.38.0.1"));
}
TEST_F(GcsAllowlist, ListAsText) {
Gcs_ip_allowlist wl;
wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
ASSERT_STRCASEEQ(Gcs_ip_allowlist::DEFAULT_ALLOWLIST.c_str(),
wl.get_configured_ip_allowlist().c_str());
}
TEST_F(GcsAllowlist, AbsentList) {
Gcs_interface_parameters params;
params.add_parameter("group_name", "ola");
params.add_parameter("peer_nodes", "127.0.0.1:24844");
params.add_parameter("local_node", "127.0.0.1:24844");
params.add_parameter("bootstrap_group", "true");
params.add_parameter("poll_spin_loops", "100");
Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
enum_gcs_error err = gcs->initialize(params);
ASSERT_EQ(err, GCS_OK);
// verify that a allowlist was provided by default
Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
<< xcs->get_ip_allowlist().to_string());
ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
// this finalizes the m_logger, so be careful to not add a call to
// MYSQL_GCS_LOG after this line
err = gcs->finalize();
// claim interface memory back
xcs->cleanup();
// initialization failed, and thus so will finalization
ASSERT_EQ(err, GCS_OK);
}
TEST_F(GcsAllowlist, ListWithHostname) {
Gcs_interface_parameters params;
params.add_parameter("group_name", "ola");
params.add_parameter("peer_nodes", "127.0.0.1:24844");
params.add_parameter("local_node", "127.0.0.1:24844");
params.add_parameter("bootstrap_group", "true");
params.add_parameter("poll_spin_loops", "100");
vector<char> machine_hostname;
machine_hostname.resize(MAXHOSTNAMELEN);
gethostname(machine_hostname.data(), MAXHOSTNAMELEN);
std::ostringstream assembled_allowlist;
assembled_allowlist << machine_hostname.data();
assembled_allowlist << "/16,";
assembled_allowlist << "localhost/32";
params.add_parameter("ip_allowlist", assembled_allowlist.str().c_str());
Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
enum_gcs_error err = gcs->initialize(params);
ASSERT_EQ(err, GCS_OK);
// verify that a allowlist was provided by default
Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
<< xcs->get_ip_allowlist().to_string());
ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
std::vector<std::pair<sa_family_t, std::string>> ips;
resolve_all_ip_addr_from_hostname("localhost", ips);
auto has_v4_addresses_it =
std::find_if(ips.begin(), ips.end(),
[](std::pair<sa_family_t, std::string> const &ip_entry) {
return ip_entry.first == AF_INET;
});
bool has_v4_addresses = has_v4_addresses_it != ips.end();
// This should not block to whatever address localhost resolves
for (auto &ip : ips) {
if (has_v4_addresses && ip.first == AF_INET6)
EXPECT_TRUE(xcs->get_ip_allowlist().shall_block(ip.second));
else
EXPECT_FALSE(xcs->get_ip_allowlist().shall_block(ip.second));
}
// this finalizes the m_logger, so be careful to not add a call to
// MYSQL_GCS_LOG after this line
err = gcs->finalize();
// claim interface memory back
xcs->cleanup();
ASSERT_EQ(err, GCS_OK);
}
TEST_F(GcsAllowlist, ListWithUnresolvableHostname) {
Gcs_interface_parameters params;
params.add_parameter("group_name", "ola");
params.add_parameter("peer_nodes", "127.0.0.1:24844");
params.add_parameter("local_node", "127.0.0.1:24844");
params.add_parameter("bootstrap_group", "true");
params.add_parameter("poll_spin_loops", "100");
vector<char> machine_hostname;
machine_hostname.resize(MAXHOSTNAMELEN);
gethostname(machine_hostname.data(), MAXHOSTNAMELEN);
std::ostringstream assembled_allowlist;
assembled_allowlist << machine_hostname.data();
assembled_allowlist << "/16,";
assembled_allowlist << "unresolvablehostname/32,";
assembled_allowlist << "localhost/32";
params.add_parameter("ip_allowlist", assembled_allowlist.str().c_str());
Gcs_interface *gcs = Gcs_xcom_interface::get_interface();
enum_gcs_error err = gcs->initialize(params);
ASSERT_EQ(err, GCS_OK);
// verify that a allowlist was provided by default
Gcs_xcom_interface *xcs = static_cast<Gcs_xcom_interface *>(gcs);
MYSQL_GCS_LOG_INFO("Allowlist as string with collected IP addresses: "
<< xcs->get_ip_allowlist().to_string());
ASSERT_FALSE(xcs->get_ip_allowlist().get_configured_ip_allowlist().empty());
ASSERT_FALSE(xcs->get_ip_allowlist().to_string().empty());
// This will force a allowlist validation and a failure on name resolution
// code
ASSERT_TRUE(xcs->get_ip_allowlist().shall_block("192.12.13.14"));
// this finalizes the m_logger, so be careful to not add a call to
// MYSQL_GCS_LOG after this line
err = gcs->finalize();
// claim interface memory back
xcs->cleanup();
ASSERT_EQ(err, GCS_OK);
}
TEST_F(GcsAllowlist, XComMembers) {
Gcs_ip_allowlist wl;
char const *members[] = {"8.8.8.8:12435", "8.8.4.4:1234", "localhost:12346"};
const char **xcom_addrs{members};
node_address *xcom_names = new_node_address(3, xcom_addrs);
site_def *xcom_config = new_site_def();
init_site_def(3, xcom_names, xcom_config);
wl.configure(Gcs_ip_allowlist::DEFAULT_ALLOWLIST);
ASSERT_FALSE(wl.shall_block("127.0.0.1", xcom_config));
ASSERT_FALSE(wl.shall_block("::1", xcom_config));
ASSERT_FALSE(wl.shall_block("192.168.1.2", xcom_config));
ASSERT_FALSE(wl.shall_block("192.168.2.2", xcom_config));
ASSERT_FALSE(wl.shall_block("10.0.0.1", xcom_config));
ASSERT_TRUE(wl.shall_block("172.15.0.1", xcom_config));
ASSERT_FALSE(wl.shall_block("172.16.0.1", xcom_config));
ASSERT_FALSE(wl.shall_block("172.24.0.1", xcom_config));
ASSERT_FALSE(wl.shall_block("172.31.0.1", xcom_config));
ASSERT_TRUE(wl.shall_block("172.38.0.1", xcom_config));
ASSERT_FALSE(wl.shall_block("8.8.8.8", xcom_config));
ASSERT_FALSE(wl.shall_block("8.8.4.4", xcom_config));
ASSERT_TRUE(wl.shall_block("8.8.8.9", xcom_config));
ASSERT_TRUE(wl.shall_block("8.8.9.8", xcom_config));
ASSERT_TRUE(wl.shall_block("8.9.8.8", xcom_config));
ASSERT_TRUE(wl.shall_block("9.8.8.8", xcom_config));
delete_node_address(3, xcom_names);
free_site_def(xcom_config);
}
TEST_F(GcsAllowlist, ComparisonBetweenIPv4AndIPv6) {
Gcs_ip_allowlist wl;
wl.configure("ac0f:0001::/32");
ASSERT_TRUE(wl.shall_block("172.15.0.1"));
}
} // namespace gcs_allowlist_unittest
|