Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.

Commit b0bf02b

Browse files
authored
Merge pull request #2160 from menloresearch/s/chore/sync-main
chore: sync main to dev
2 parents 262fb1f + 2af7798 commit b0bf02b

File tree

4 files changed

+61
-12
lines changed

4 files changed

+61
-12
lines changed

engine/cli/command_line_parser.cc

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -86,7 +86,7 @@ bool CommandLineParser::SetupCommand(int argc, char** argv) {
8686
#else
8787
CLI_LOG("default");
8888
#endif
89-
(void) c;
89+
(void)c;
9090
};
9191
app_.add_flag_function("-v,--version", cb, "Get Cortex version");
9292

@@ -436,7 +436,7 @@ void CommandLineParser::SetupConfigsCommands() {
436436

437437
auto is_empty = true;
438438
for (const auto& [key, value] : config_update_opts_) {
439-
if (!value.empty() || CONFIGURATIONS.at(key).allow_empty) {
439+
if (!value.empty() || key == "api_keys") {
440440
is_empty = false;
441441
break;
442442
}

engine/cli/commands/config_upd_cmd.cc

Lines changed: 27 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22
#include "commands/server_start_cmd.h"
33
#include "common/api_server_configuration.h"
44
#include "utils/curl_utils.h"
5+
#include "utils/file_manager_utils.h"
56
#include "utils/logging_utils.h"
67
#include "utils/string_utils.h"
78
#include "utils/url_parser.h"
@@ -46,22 +47,40 @@ inline Json::Value NormalizeJson(
4647
void commands::ConfigUpdCmd::Exec(
4748
const std::string& host, int port,
4849
const std::unordered_map<std::string, std::string>& options) {
49-
if (!commands::IsServerAlive(host, port)) {
50-
CLI_LOG("Starting server ...");
51-
commands::ServerStartCmd ssc;
52-
if (!ssc.Exec(host, port)) {
53-
return;
54-
}
55-
}
5650

5751
auto non_null_opts = std::unordered_map<std::string, std::string>();
5852
for (const auto& [key, value] : options) {
59-
if (value.empty() && !CONFIGURATIONS.at(key).allow_empty) {
53+
// In case of api_keys, we allow empty value
54+
if (value.empty() && key != "api_keys") {
6055
continue;
6156
}
6257
non_null_opts[key] = value;
6358
}
6459

60+
if (non_null_opts.size() == 1) {
61+
for (const auto& [key, value] : non_null_opts) {
62+
if (key == "api_keys") {
63+
auto config = file_manager_utils::GetCortexConfig();
64+
config.apiKeys = string_utils::SplitBy(value, ",");
65+
auto result = file_manager_utils::UpdateCortexConfig(config);
66+
if (result.has_error()) {
67+
CLI_LOG_ERROR(result.error());
68+
} else {
69+
CLI_LOG("Configuration updated successfully!");
70+
}
71+
return;
72+
}
73+
}
74+
}
75+
76+
if (!commands::IsServerAlive(host, port)) {
77+
CLI_LOG("Starting server ...");
78+
commands::ServerStartCmd ssc;
79+
if (!ssc.Exec(host, port)) {
80+
return;
81+
}
82+
}
83+
6584
auto url = url_parser::Url{
6685
/* .protocol = */ "http",
6786
/* .host = */ host + ":" + std::to_string(port),

engine/common/api_server_configuration.h

Lines changed: 10 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -80,6 +80,15 @@ static const std::unordered_map<std::string, ApiConfigurationMetadata>
8080
/* .accept_value = */ "string",
8181
/* .default_value = */ "",
8282
/* .allow_empty = */ true}},
83+
{"api_keys",
84+
ApiConfigurationMetadata{
85+
/* .name = */ "api_keys",
86+
/* .desc = */ "API header key to get access to server APIs",
87+
/* .group = */ "Token",
88+
/* .accept_value = */ " comma separated",
89+
/* .default_value = */ "",
90+
/* .allow_empty = */ true}},
91+
8392
};
8493

8594
class ApiServerConfiguration {
@@ -316,4 +325,4 @@ class ApiServerConfiguration {
316325
}
317326
}
318327
};
319-
};
328+
};

engine/main.cc

Lines changed: 22 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -253,7 +253,13 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
253253
auto validate_api_key = [config_service](const drogon::HttpRequestPtr& req) {
254254
auto api_keys = config_service->GetApiServerConfiguration()->api_keys;
255255
static const std::unordered_set<std::string> public_endpoints = {
256-
"/openapi.json", "/healthz", "/processManager/destroy"};
256+
"/openapi.json", "/healthz", "/processManager/destroy", "/events"};
257+
258+
if (req->getHeader("Authorization").empty() &&
259+
req->path() == "/v1/configs") {
260+
CTL_WRN("Require API key to access /v1/configs");
261+
return false;
262+
}
257263

258264
// If API key is not set, skip validation
259265
if (api_keys.empty()) {
@@ -377,6 +383,7 @@ void print_help() {
377383
"~/cortexcpp)\n";
378384
std::cout << " --host Host name (default: 127.0.0.1)\n";
379385
std::cout << " --port Port number (default: 39281)\n";
386+
std::cout << " --api_configs Keys to acess API endpoints\n";
380387
std::cout << " --ignore_cout Ignore cout output\n";
381388
std::cout << " --loglevel Set log level\n";
382389

@@ -405,6 +412,7 @@ int main(int argc, char* argv[]) {
405412

406413
std::optional<std::string> server_host;
407414
std::optional<int> server_port;
415+
std::optional<std::string> api_keys;
408416
bool ignore_cout_log = false;
409417
#if defined(_WIN32)
410418
for (int i = 0; i < argc; i++) {
@@ -421,6 +429,8 @@ int main(int argc, char* argv[]) {
421429
server_host = cortex::wc::WstringToUtf8(argv[i + 1]);
422430
} else if (command == L"--port") {
423431
server_port = std::stoi(argv[i + 1]);
432+
} else if (command == L"--api_keys") {
433+
api_keys = cortex::wc::WstringToUtf8(argv[i + 1]);
424434
} else if (command == L"--ignore_cout") {
425435
ignore_cout_log = true;
426436
} else if (command == L"--loglevel") {
@@ -441,6 +451,8 @@ int main(int argc, char* argv[]) {
441451
server_host = argv[i + 1];
442452
} else if (strcmp(argv[i], "--port") == 0) {
443453
server_port = std::stoi(argv[i + 1]);
454+
} else if (strcmp(argv[i], "--api_keys") == 0) {
455+
api_keys = argv[i + 1];
444456
} else if (strcmp(argv[i], "--ignore_cout") == 0) {
445457
ignore_cout_log = true;
446458
} else if (strcmp(argv[i], "--loglevel") == 0) {
@@ -476,6 +488,15 @@ int main(int argc, char* argv[]) {
476488
}
477489
}
478490

491+
if (api_keys) {
492+
auto config = file_manager_utils::GetCortexConfig();
493+
config.apiKeys = string_utils::SplitBy(*api_keys, ",");
494+
auto result = file_manager_utils::UpdateCortexConfig(config);
495+
if (result.has_error()) {
496+
CTL_ERR(result.error());
497+
}
498+
}
499+
479500
// check if migration is needed
480501
if (auto res = cortex::migr::MigrationManager(
481502
cortex::db::Database::GetInstance().db())

0 commit comments

Comments
 (0)