-
Notifications
You must be signed in to change notification settings - Fork 19
Expand file tree
/
Copy pathsupported-configurations.cpp
More file actions
648 lines (585 loc) · 19.3 KB
/
supported-configurations.cpp
File metadata and controls
648 lines (585 loc) · 19.3 KB
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
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
483
484
485
486
487
488
489
490
491
492
493
494
495
496
497
498
499
500
501
502
503
504
505
506
507
508
509
510
511
512
513
514
515
516
517
518
519
520
521
522
523
524
525
526
527
528
529
530
531
532
533
534
535
536
537
538
539
540
541
542
543
544
545
546
547
548
549
550
551
552
553
554
555
556
557
558
559
560
561
562
563
564
565
566
567
568
569
570
571
572
573
574
575
576
577
578
579
580
581
582
583
584
585
586
587
588
589
590
591
592
593
594
595
596
597
598
599
600
601
602
603
604
605
606
607
608
609
610
611
612
613
614
615
616
617
618
619
620
621
622
623
624
625
626
627
628
629
630
631
632
633
634
635
636
637
638
639
640
641
642
643
644
645
646
647
648
#include <algorithm>
#include <array>
#include <cctype>
#include <filesystem>
#include <fstream>
#include <iostream>
#include <map>
#include <optional>
#include <regex>
#include <set>
#include <stdexcept>
#include <string>
#include <vector>
#include "json.hpp"
namespace fs = std::filesystem;
using nlohmann::json;
namespace {
const std::vector<std::string> kScanDirs = {"include", "src", "test", "examples",
"fuzz"};
const std::set<std::string> kScanSuffixes = {".h", ".hh", ".hpp", ".c",
".cc", ".cpp", ".cxx"};
const std::vector<std::string> kDynamicScanDirs = {"include", "src"};
const std::set<std::string> kAllowedDynamicGetenvPaths = {
"include/datadog/environment.h",
"src/datadog/environment.cpp",
};
const std::regex kEnvNameRe("^(?:DD|OTEL)_[A-Z0-9_]+$");
const std::regex kEnvEnumRe("\\b(?:environment|env)::((?:DD|OTEL)_[A-Z0-9_]+)\\b");
const std::regex kGetenvRe(
"\\b(?:std::)?getenv\\s*\\(\\s*\"((?:DD|OTEL)_[A-Z0-9_]+)\"\\s*\\)");
const std::regex kGetenvCallRe("\\b(?:std::)?getenv\\s*\\(\\s*([^)]+?)\\s*\\)");
const std::regex kSetenvRe(
"\\b(?:setenv|setenv_s|_putenv_s?)\\s*\\(\\s*\"((?:DD|OTEL)_[A-Z0-9_]+)");
const std::regex kEnvGuardRe(
"\\bEnvGuard\\b[\\w\\s:<>&,*]*[\\(\\{]\\s*\"((?:DD|OTEL)_[A-Z0-9_]+)\"");
const std::regex kStringEnvRe("\"((?:DD|OTEL)_[A-Z0-9_]+)\"");
const std::regex kDefaultResolvedInCodeRe(
"^ENV_DEFAULT_RESOLVED_IN_CODE\\s*\\((.+)\\)$");
const std::regex kNumericRe("-?(?:[0-9]+(?:\\.[0-9]+)?|\\.[0-9]+)");
struct EnvVarDefinition {
std::string name;
std::string type_token;
std::string default_token;
};
struct Context {
fs::path repo_root;
fs::path registry_header;
fs::path supported_config_path;
};
std::string trim(std::string input) {
const auto not_space = [](unsigned char c) { return !std::isspace(c); };
auto begin = std::find_if(input.begin(), input.end(), not_space);
auto end = std::find_if(input.rbegin(), input.rend(), not_space).base();
if (begin >= end) {
return "";
}
return std::string(begin, end);
}
void replace_all(std::string &text, const std::string &from,
const std::string &to) {
if (from.empty()) {
return;
}
std::size_t start = 0;
while ((start = text.find(from, start)) != std::string::npos) {
text.replace(start, from.size(), to);
start += to.size();
}
}
std::string path_to_posix(const fs::path &path) { return path.generic_string(); }
bool is_quoted_string_literal(const std::string &text) {
return text.size() >= 2 && text.front() == '"' && text.back() == '"';
}
std::string relative_posix_path(const Context &context, const fs::path &path) {
return path_to_posix(fs::relative(path, context.repo_root));
}
std::string read_text(const fs::path &path) {
std::ifstream file(path, std::ios::binary);
if (!file) {
throw std::runtime_error("Unable to read file: " + path_to_posix(path));
}
return std::string(std::istreambuf_iterator<char>(file),
std::istreambuf_iterator<char>());
}
std::vector<std::string> find_regex_matches(const std::string &content,
const std::regex &pattern) {
std::vector<std::string> matches;
for (auto it = std::sregex_iterator(content.begin(), content.end(), pattern);
it != std::sregex_iterator(); ++it) {
matches.push_back((*it)[1].str());
}
return matches;
}
std::optional<std::array<std::string, 4>> parse_macro_arguments(
const std::string &source, std::size_t open_paren, std::size_t &close_paren) {
std::array<std::string, 4> arguments;
std::size_t argument_index = 0;
std::size_t argument_start = open_paren + 1;
int paren_depth = 0;
int brace_depth = 0;
int bracket_depth = 0;
bool in_single = false;
bool in_double = false;
bool in_line_comment = false;
bool in_block_comment = false;
bool escape = false;
for (std::size_t index = open_paren; index < source.size(); ++index) {
const char ch = source[index];
const char next = (index + 1 < source.size()) ? source[index + 1] : '\0';
if (in_line_comment) {
if (ch == '\n') in_line_comment = false;
continue;
}
if (in_block_comment) {
if (ch == '*' && next == '/') in_block_comment = false;
continue;
}
if (in_single) {
if (escape) {
escape = false;
} else if (ch == '\\') {
escape = true;
} else if (ch == '\'') {
in_single = false;
}
continue;
}
if (in_double) {
if (escape) {
escape = false;
} else if (ch == '\\') {
escape = true;
} else if (ch == '"') {
in_double = false;
}
continue;
}
if (ch == '/' && next == '/') {
in_line_comment = true;
continue;
}
if (ch == '/' && next == '*') {
in_block_comment = true;
continue;
}
if (ch == '\'') {
in_single = true;
continue;
}
if (ch == '"') {
in_double = true;
continue;
}
if (ch == '(') {
++paren_depth;
continue;
}
if (ch == ')') {
--paren_depth;
if (paren_depth == 0) {
close_paren = index;
if (argument_index < arguments.size()) {
arguments[argument_index++] =
trim(source.substr(argument_start, index - argument_start));
}
break;
}
continue;
}
if (ch == '{') {
++brace_depth;
continue;
}
if (ch == '}') {
--brace_depth;
continue;
}
if (ch == '[') {
++bracket_depth;
continue;
}
if (ch == ']') {
--bracket_depth;
continue;
}
if (ch == ',' && paren_depth == 1 && brace_depth == 0 && bracket_depth == 0) {
if (argument_index >= arguments.size()) return std::nullopt;
arguments[argument_index++] =
trim(source.substr(argument_start, index - argument_start));
argument_start = index + 1;
}
}
if (argument_index != arguments.size()) return std::nullopt;
return arguments;
}
std::string decode_cpp_single_string_literal(std::string text) {
std::string body = trim(std::move(text));
if (!is_quoted_string_literal(body)) {
throw std::runtime_error("Expected quoted C++ string literal");
}
body = body.substr(1, body.size() - 2);
replace_all(body, "\\\\", "\\");
replace_all(body, "\\\"", "\"");
replace_all(body, "\\n", "\n");
replace_all(body, "\\t", "\t");
return body;
}
std::string decode_cpp_string_literal_sequence(const std::string &text) {
const std::string input = trim(text);
std::string decoded;
std::size_t index = 0;
while (index < input.size()) {
while (index < input.size() &&
std::isspace(static_cast<unsigned char>(input[index]))) {
++index;
}
if (index == input.size()) {
break;
}
if (input[index] != '"') {
throw std::runtime_error(
"Expected one or more quoted C++ string literals");
}
std::size_t end = index + 1;
bool escaped = false;
for (; end < input.size(); ++end) {
const char ch = input[end];
if (escaped) {
escaped = false;
continue;
}
if (ch == '\\') {
escaped = true;
continue;
}
if (ch == '"') {
break;
}
}
if (end >= input.size()) {
throw std::runtime_error("Unterminated C++ string literal");
}
decoded += decode_cpp_single_string_literal(
input.substr(index, end - index + 1));
index = end + 1;
}
return decoded;
}
std::vector<EnvVarDefinition> parse_registry_definitions(const Context &context) {
std::string source = read_text(context.registry_header);
replace_all(source, "\\\n", " ");
const std::string token = "MACRO(";
std::size_t search_index = 0;
std::vector<EnvVarDefinition> definitions;
std::set<std::string> seen;
while (true) {
const std::size_t match_index = source.find(token, search_index);
if (match_index == std::string::npos) {
break;
}
const std::size_t open_paren = match_index + std::string("MACRO").size();
std::size_t close_paren = open_paren;
const auto arguments = parse_macro_arguments(source, open_paren, close_paren);
search_index = close_paren + 1;
if (!arguments) {
continue;
}
const std::string name = trim((*arguments)[1]);
const std::string type_token = trim((*arguments)[2]);
const std::string default_token = trim((*arguments)[3]);
if (!std::regex_match(name, kEnvNameRe)) {
continue;
}
if (seen.count(name)) {
throw std::runtime_error("Duplicate environment variable entry: " + name);
}
seen.insert(name);
definitions.push_back(EnvVarDefinition{name, type_token, default_token});
}
if (definitions.empty()) {
throw std::runtime_error("No environment variable definitions found in " +
path_to_posix(context.registry_header));
}
return definitions;
}
std::string normalize_default_token(const std::string &token,
const Context &context) {
const std::string value = trim(token);
std::smatch marker;
if (std::regex_match(value, marker, kDefaultResolvedInCodeRe)) {
std::string message = trim(marker[1].str());
if (message.empty() || message.front() != '"') {
throw std::runtime_error(
"ENV_DEFAULT_RESOLVED_IN_CODE(...) must contain a quoted string in " +
path_to_posix(context.registry_header));
}
return decode_cpp_string_literal_sequence(message);
}
if (is_quoted_string_literal(value)) {
return decode_cpp_string_literal_sequence(value);
}
if (value == "true" || value == "false") {
return value;
}
if (value == "[]") {
return value;
}
if (std::regex_match(value, kNumericRe)) {
return value;
}
throw std::runtime_error("Unsupported default token '" + value + "' in " +
path_to_posix(context.registry_header));
}
std::vector<fs::path> iter_source_files(const Context &context,
const std::vector<std::string> &dirs) {
std::vector<fs::path> paths;
for (const auto &directory : dirs) {
const fs::path root = context.repo_root / directory;
if (!fs::exists(root)) {
continue;
}
for (auto it = fs::recursive_directory_iterator(root);
it != fs::recursive_directory_iterator(); ++it) {
if (!it->is_regular_file()) {
continue;
}
if (kScanSuffixes.count(it->path().extension().string()) == 0) {
continue;
}
paths.push_back(it->path());
}
}
std::sort(paths.begin(), paths.end());
return paths;
}
using UsedVars = std::map<std::string, std::set<std::string>>;
UsedVars discover_used_env_variables(const Context &context) {
UsedVars used;
for (const auto &path : iter_source_files(context, kScanDirs)) {
const std::string content = read_text(path);
const std::string relative = relative_posix_path(context, path);
for (const auto &match : find_regex_matches(content, kEnvEnumRe)) {
used[match].insert(relative);
}
for (const auto &match : find_regex_matches(content, kGetenvRe)) {
used[match].insert(relative);
}
for (const auto &match : find_regex_matches(content, kSetenvRe)) {
used[match].insert(relative);
}
for (const auto &match : find_regex_matches(content, kEnvGuardRe)) {
used[match].insert(relative);
}
if (content.find("EnvGuard") != std::string::npos) {
for (const auto &match : find_regex_matches(content, kStringEnvRe)) {
used[match].insert(relative);
}
}
}
return used;
}
using DynamicCalls = std::map<std::string, std::set<std::string>>;
DynamicCalls discover_disallowed_dynamic_getenv_calls(const Context &context) {
DynamicCalls calls;
for (const auto &path : iter_source_files(context, kDynamicScanDirs)) {
const std::string relative = relative_posix_path(context, path);
if (kAllowedDynamicGetenvPaths.count(relative) > 0) {
continue;
}
const std::string content = read_text(path);
for (const auto &argument : find_regex_matches(content, kGetenvCallRe)) {
const auto trimmed = trim(argument);
if (is_quoted_string_literal(trimmed)) {
continue;
}
calls[relative].insert(trimmed);
}
}
return calls;
}
struct Verification {
bool ok;
std::vector<std::string> errors;
};
struct PreflightResult {
Verification allowlist;
Verification dynamic_getenv;
};
Verification verify_used_variables_are_allowlisted(
const std::set<std::string> &allowlisted, const UsedVars &used) {
std::vector<std::string> missing;
for (const auto &[var, _] : used) {
if (!allowlisted.count(var)) {
missing.push_back(var);
}
}
if (missing.empty()) {
return {true, {}};
}
std::vector<std::string> lines;
lines.push_back(
"Found DD_/OTEL_ environment variables used in code that are not declared in "
"include/datadog/environment_registry.h:");
for (const auto &variable : missing) {
std::string locations;
bool first = true;
for (const auto &path : used.at(variable)) {
if (!first) {
locations += ", ";
}
first = false;
locations += path;
}
lines.push_back(" - " + variable + " (" + locations + ")");
}
lines.push_back(
"Add missing variables to include/datadog/environment_registry.h (which "
"drives include/datadog/environment.h).");
return {false, lines};
}
Verification verify_no_disallowed_dynamic_getenv_calls(
const DynamicCalls &dynamic_calls) {
if (dynamic_calls.empty()) {
return {true, {}};
}
std::vector<std::string> lines;
lines.push_back(
"Found dynamic getenv(...) access in include/src. Dynamic environment "
"variable access is prohibited for tracer configuration paths.");
for (const auto &[path, arguments] : dynamic_calls) {
std::string joined;
bool first = true;
for (const auto &arg : arguments) {
if (!first) {
joined += ", ";
}
first = false;
joined += arg;
}
lines.push_back(" - " + path + " (arguments: " + joined + ")");
}
lines.push_back(
"Use include/datadog/environment_registry.h plus environment::lookup<...>() "
"instead.");
return {false, lines};
}
PreflightResult run_preflight(const Context &context,
const std::vector<EnvVarDefinition> &definitions) {
std::set<std::string> allowlisted;
for (const auto &definition : definitions) {
allowlisted.insert(definition.name);
}
return {
verify_used_variables_are_allowlisted(allowlisted,
discover_used_env_variables(context)),
verify_no_disallowed_dynamic_getenv_calls(
discover_disallowed_dynamic_getenv_calls(context)),
};
}
void print_verification_errors(const Verification &verification) {
for (const auto &line : verification.errors) {
std::cout << line << '\n';
}
}
json build_supported_configurations(const std::vector<EnvVarDefinition> &definitions,
const Context &context) {
const std::map<std::string, std::string> type_map = {
{"STRING", "string"}, {"BOOLEAN", "boolean"}, {"INT", "int"},
{"DECIMAL", "decimal"}, {"ARRAY", "array"}, {"MAP", "map"}};
std::vector<EnvVarDefinition> sorted = definitions;
std::sort(sorted.begin(), sorted.end(),
[](const EnvVarDefinition &a, const EnvVarDefinition &b) {
return a.name < b.name;
});
json supported = json::object();
for (const auto &definition : sorted) {
const auto type_it = type_map.find(definition.type_token);
if (type_it == type_map.end()) {
throw std::runtime_error("Unsupported type token '" + definition.type_token +
"' for " + definition.name);
}
supported[definition.name] = json::array(
{json{{"implementation", "A"},
{"type", type_it->second},
{"default", normalize_default_token(definition.default_token, context)}}});
}
if (supported.size() != definitions.size()) {
throw std::runtime_error(
"Internal error: every registry definition must map to exactly one "
"supported configuration entry");
}
return supported;
}
json load_existing_deprecations(const Context &context) {
if (!fs::exists(context.supported_config_path)) {
return json::object();
}
try {
const auto parsed = json::parse(read_text(context.supported_config_path));
if (parsed.contains("deprecations") && parsed["deprecations"].is_object()) {
return parsed["deprecations"];
}
return json::object();
} catch (...) {
return json::object();
}
}
void write_supported_configurations_json(const Context &context,
const json &supported_configurations) {
json output = json::object();
output["version"] = "2";
output["supportedConfigurations"] = supported_configurations;
output["deprecations"] = load_existing_deprecations(context);
fs::create_directories(context.supported_config_path.parent_path());
std::ofstream file(context.supported_config_path, std::ios::binary);
if (!file) {
throw std::runtime_error("Unable to write " +
path_to_posix(context.supported_config_path));
}
file << output.dump(2) << '\n';
}
int run_check(const Context &context) {
const auto definitions = parse_registry_definitions(context);
const auto preflight = run_preflight(context, definitions);
if (preflight.allowlist.ok && preflight.dynamic_getenv.ok) {
std::cout << "Environment variable allowlist check passed.\n";
return 0;
}
print_verification_errors(preflight.allowlist);
print_verification_errors(preflight.dynamic_getenv);
return 1;
}
int run_generate(const Context &context) {
const auto definitions = parse_registry_definitions(context);
const auto preflight = run_preflight(context, definitions);
if (!preflight.allowlist.ok || !preflight.dynamic_getenv.ok) {
print_verification_errors(preflight.allowlist);
print_verification_errors(preflight.dynamic_getenv);
return 1;
}
const auto supported = build_supported_configurations(definitions, context);
if (supported.empty()) {
std::cout << "Error: no supported configurations were generated.\n";
return 1;
}
write_supported_configurations_json(context, supported);
std::cout << "Wrote "
<< path_to_posix(fs::relative(context.supported_config_path,
context.repo_root))
<< '\n';
return 0;
}
Context make_context(const char *argv0) {
fs::path executable = fs::absolute(argv0);
fs::path script_dir = executable.parent_path();
fs::path repo_root = script_dir.parent_path();
return Context{
repo_root,
repo_root / "include/datadog/environment_registry.h",
repo_root / "metadata/supported-configurations.json",
};
}
} // namespace
int main(int argc, char **argv) {
try {
constexpr const char kUsage[] = "Usage: supported-configurations <check|generate>\n";
if (argc != 2) {
std::cerr << kUsage;
return 1;
}
const std::string command = argv[1];
const Context context = make_context(argv[0]);
if (command == "check") {
return run_check(context);
}
if (command == "generate") {
return run_generate(context);
}
std::cerr << kUsage;
return 1;
} catch (const std::exception &error) {
std::cerr << error.what() << '\n';
return 1;
}
}