-
Notifications
You must be signed in to change notification settings - Fork 592
/
validators.cc
286 lines (250 loc) · 9.03 KB
/
validators.cc
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
/*
* Copyright 2022 Redpanda Data, Inc.
*
* Use of this software is governed by the Business Source License
* included in the file licenses/BSL.md
*
* As of the Change Date specified in that file, in accordance with
* the Business Source License, use of this software will be governed
* by the Apache License, Version 2.0
*/
#include "config/validators.h"
#include "config/client_group_byte_rate_quota.h"
#include "config/configuration.h"
#include "model/namespace.h"
#include "model/validation.h"
#include "serde/rw/chrono.h"
#include "ssx/sformat.h"
#include "utils/inet_address_wrapper.h"
#include <absl/algorithm/container.h>
#include <absl/container/flat_hash_set.h>
#include <absl/container/node_hash_set.h>
#include <fmt/format.h>
#include <array>
#include <optional>
#include <unordered_map>
namespace config {
std::optional<std::pair<ss::sstring, int64_t>>
split_string_int_by_colon(const ss::sstring& raw_option) {
auto del_pos = raw_option.find(":");
if (del_pos == ss::sstring::npos || del_pos == raw_option.size() - 1) {
return std::nullopt;
}
auto str_val = raw_option.substr(0, del_pos);
auto int_val = raw_option.substr(del_pos + 1);
std::pair<ss::sstring, int64_t> ans;
ans.first = str_val;
try {
ans.second = std::stoll(int_val);
} catch (...) {
return std::nullopt;
}
return ans;
}
std::optional<std::pair<ss::sstring, int64_t>>
parse_connection_rate_override(const ss::sstring& raw_option) {
return split_string_int_by_colon(raw_option);
}
std::optional<ss::sstring>
validate_connection_rate(const std::vector<ss::sstring>& ips_with_limit) {
absl::node_hash_set<net::inet_address_wrapper> ip_set;
for (const auto& ip_and_limit : ips_with_limit) {
auto parsing_setting = parse_connection_rate_override(ip_and_limit);
if (!parsing_setting) {
return fmt::format(
"Can not parse connection_rate override {}", ip_and_limit);
}
ss::net::inet_address addr;
try {
addr = ss::net::inet_address(parsing_setting->first);
} catch (...) {
return fmt::format(
"Looks like {} is not ip", parsing_setting->first);
}
if (!ip_set.insert(addr).second) {
return fmt::format(
"Duplicate setting for ip: {}", parsing_setting->first);
}
}
return std::nullopt;
}
std::optional<ss::sstring> validate_client_groups_byte_rate_quota(
const std::unordered_map<ss::sstring, config::client_group_quota>&
groups_with_limit) {
for (const auto& gal : groups_with_limit) {
if (gal.second.quota <= 0) {
return fmt::format(
"Quota must be a non zero positive number, got: {}",
gal.second.quota);
}
for (const auto& another_group : groups_with_limit) {
if (another_group.first == gal.first) {
continue;
}
if (std::string_view(gal.second.clients_prefix)
.starts_with(
std::string_view(another_group.second.clients_prefix))) {
return fmt::format(
"Group client prefix can not be prefix for another group "
"name. "
"Violation: {}, {}",
gal.second.clients_prefix,
another_group.second.clients_prefix);
}
}
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_sasl_mechanisms(const std::vector<ss::sstring>& mechanisms) {
constexpr auto supported = std::to_array<std::string_view>(
{"GSSAPI", "SCRAM", "OAUTHBEARER"});
// Validate results
for (const auto& m : mechanisms) {
if (absl::c_none_of(
supported, [&m](const auto& s) { return s == m; })) {
return ssx::sformat("'{}' is not a supported SASL mechanism", m);
}
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_http_authn_mechanisms(const std::vector<ss::sstring>& mechanisms) {
constexpr auto supported = std::to_array<std::string_view>(
{"BASIC", "OIDC"});
// Validate results
for (const auto& m : mechanisms) {
if (absl::c_none_of(
supported, [&m](const auto& s) { return s == m; })) {
return ssx::sformat(
"'{}' is not a supported HTTP authentication mechanism", m);
}
}
return std::nullopt;
}
bool oidc_is_enabled_http() {
return absl::c_any_of(
config::shard_local_cfg().http_authentication(),
[](const auto& m) { return m == "OIDC"; });
}
bool oidc_is_enabled_kafka() {
return absl::c_any_of(
config::shard_local_cfg().sasl_mechanisms(),
[](const auto& m) { return m == "OAUTHBEARER"; });
}
std::optional<ss::sstring> validate_0_to_1_ratio(const double d) {
if (d < 0 || d > 1) {
return fmt::format("Ratio must be in the [0,1] range, got: {}", d);
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_non_empty_string_vec(const std::vector<ss::sstring>& vs) {
for (const auto& s : vs) {
if (s.empty()) {
return "Empty strings are not valid in this collection";
}
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_non_empty_string_opt(const std::optional<ss::sstring>& os) {
if (os.has_value() && os.value().empty()) {
return "Empty string is not valid";
} else {
return std::nullopt;
}
}
std::optional<ss::sstring>
validate_audit_event_types(const std::vector<ss::sstring>& vs) {
/// TODO: Should match stringified enums in kafka/types.h
static const absl::flat_hash_set<ss::sstring> audit_event_types{
"management",
"produce",
"consume",
"describe",
"heartbeat",
"authenticate",
"admin",
"schema_registry"};
for (const auto& e : vs) {
if (!audit_event_types.contains(e)) {
return ss::format("Unsupported audit event type passed: {}", e);
}
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_audit_excluded_topics(const std::vector<ss::sstring>& vs) {
bool is_kafka_audit_topic = false;
std::optional<ss::sstring> is_invalid_topic_name = std::nullopt;
if (std::any_of(
vs.begin(),
vs.end(),
[&is_kafka_audit_topic,
&is_invalid_topic_name](const ss::sstring& topic_name) {
auto t = model::topic{topic_name};
if (t == model::kafka_audit_logging_topic) {
is_kafka_audit_topic = true;
} else if (model::validate_kafka_topic_name(t)) {
is_invalid_topic_name = topic_name;
}
return is_kafka_audit_topic || is_invalid_topic_name.has_value();
})) {
if (is_kafka_audit_topic) {
return ss::format(
"Unable to exclude audit log '{}' from auditing",
model::kafka_audit_logging_topic);
} else if (is_invalid_topic_name.has_value()) {
return ss::format(
"{} is an invalid topic name", *is_invalid_topic_name);
}
}
return std::nullopt;
}
std::optional<ss::sstring>
validate_api_endpoint(const std::optional<ss::sstring>& os) {
if (auto non_empty_string_opt = validate_non_empty_string_opt(os);
non_empty_string_opt.has_value()) {
return non_empty_string_opt;
}
if (
os.has_value()
&& (os.value().starts_with("http://") || os.value().starts_with("https://"))) {
return "String starting with URL protocol is not valid";
}
return std::nullopt;
}
std::optional<ss::sstring> validate_tombstone_retention_ms(
const std::optional<std::chrono::milliseconds>& ms) {
if (ms.has_value()) {
// For simplicity's sake, cloud storage enable/read/write permissions
// cannot be enabled at the same time as tombstone_retention_ms at the
// cluster level, to avoid the case in which redpanda refuses to create
// new, misconfigured topics due to cluster defaults
const auto& cloud_storage_enabled
= config::shard_local_cfg().cloud_storage_enabled;
const auto& cloud_storage_remote_write
= config::shard_local_cfg().cloud_storage_enable_remote_write;
const auto& cloud_storage_remote_read
= config::shard_local_cfg().cloud_storage_enable_remote_read;
if (
cloud_storage_enabled() || cloud_storage_remote_write()
|| cloud_storage_remote_read()) {
return fmt::format(
"cannot set {} if any of ({}, {}, {}) are enabled at the cluster "
"level",
config::shard_local_cfg().tombstone_retention_ms.name(),
cloud_storage_enabled.name(),
cloud_storage_remote_write.name(),
cloud_storage_remote_read.name());
}
if (ms.value() < 1ms || ms.value() > serde::max_serializable_ms) {
return fmt::format(
"tombstone_retention_ms should be in range: [1, {}]",
serde::max_serializable_ms);
}
}
return std::nullopt;
}
}; // namespace config