Skip to content
This repository was archived by the owner on Jul 4, 2025. It is now read-only.
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion engine/cli/command_line_parser.cc
Original file line number Diff line number Diff line change
Expand Up @@ -437,7 +437,7 @@ void CommandLineParser::SetupConfigsCommands() {

auto is_empty = true;
for (const auto& [key, value] : config_update_opts_) {
if (!value.empty()) {
if (!value.empty() || key == "api_keys") {
is_empty = false;
break;
}
Expand Down
34 changes: 26 additions & 8 deletions engine/cli/commands/config_upd_cmd.cc
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@
#include "commands/server_start_cmd.h"
#include "common/api_server_configuration.h"
#include "utils/curl_utils.h"
#include "utils/file_manager_utils.h"
#include "utils/logging_utils.h"
#include "utils/string_utils.h"
#include "utils/url_parser.h"
Expand Down Expand Up @@ -46,22 +47,39 @@ inline Json::Value NormalizeJson(
void commands::ConfigUpdCmd::Exec(
const std::string& host, int port,
const std::unordered_map<std::string, std::string>& options) {
if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

auto non_null_opts = std::unordered_map<std::string, std::string>();
for (const auto& [key, value] : options) {
if (value.empty()) {
if (value.empty() && key != "api_keys") {
continue;
}
non_null_opts[key] = value;
}

if (non_null_opts.size() == 1) {
for (const auto& [key, value] : non_null_opts) {
if (key == "api_keys") {
auto config = file_manager_utils::GetCortexConfig();
config.apiKeys = string_utils::SplitBy(value, ",");
auto result = file_manager_utils::UpdateCortexConfig(config);
if (result.has_error()) {
CLI_LOG_ERROR(result.error());
} else {
CLI_LOG("Configuration updated successfully!");
}
return;
}
}
}

if (!commands::IsServerAlive(host, port)) {
CLI_LOG("Starting server ...");
commands::ServerStartCmd ssc;
if (!ssc.Exec(host, port)) {
return;
}
}

auto url = url_parser::Url{
.protocol = "http",
.host = host + ":" + std::to_string(port),
Expand Down
9 changes: 9 additions & 0 deletions engine/common/api_server_configuration.h
Original file line number Diff line number Diff line change
Expand Up @@ -97,6 +97,15 @@ static const std::unordered_map<std::string, ApiConfigurationMetadata>
.accept_value = "string",
.default_value = "",
.allow_empty = true}},
{"api_keys",
ApiConfigurationMetadata{
.name = "api_keys",
.desc = "API header key to get access to server APIs",
.group = "Token",
.accept_value = "comma separated",
.default_value = "",
.allow_empty = true}},

};

class ApiServerConfiguration {
Expand Down
6 changes: 6 additions & 0 deletions engine/main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -255,6 +255,12 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
static const std::unordered_set<std::string> public_endpoints = {
"/openapi.json", "/healthz", "/processManager/destroy"};

if (req->getHeader("Authorization").empty() &&
req->path() == "/v1/configs") {
CTL_WRN("Require API key to access /v1/configs");
return false;
}

// If API key is not set, skip validation
if (api_keys.empty()) {
return true;
Expand Down
Loading