From bd071b814fa57c98c70283deb827ccc44aebfcf7 Mon Sep 17 00:00:00 2001 From: sangjanai Date: Thu, 20 Mar 2025 16:24:27 +0700 Subject: [PATCH 1/3] feat: allow to configure api_keys by cli --- engine/cli/command_line_parser.cc | 2 +- engine/cli/commands/config_upd_cmd.cc | 34 ++++++++++++++++++------ engine/common/api_server_configuration.h | 9 +++++++ engine/main.cc | 6 +++++ 4 files changed, 42 insertions(+), 9 deletions(-) diff --git a/engine/cli/command_line_parser.cc b/engine/cli/command_line_parser.cc index b423a6896..834d9775b 100644 --- a/engine/cli/command_line_parser.cc +++ b/engine/cli/command_line_parser.cc @@ -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; } diff --git a/engine/cli/commands/config_upd_cmd.cc b/engine/cli/commands/config_upd_cmd.cc index 58bedb2e5..8a7e5599d 100644 --- a/engine/cli/commands/config_upd_cmd.cc +++ b/engine/cli/commands/config_upd_cmd.cc @@ -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" @@ -46,22 +47,39 @@ inline Json::Value NormalizeJson( void commands::ConfigUpdCmd::Exec( const std::string& host, int port, const std::unordered_map& 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(); 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), diff --git a/engine/common/api_server_configuration.h b/engine/common/api_server_configuration.h index 63383301b..b3de92c65 100644 --- a/engine/common/api_server_configuration.h +++ b/engine/common/api_server_configuration.h @@ -97,6 +97,15 @@ static const std::unordered_map .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 { diff --git a/engine/main.cc b/engine/main.cc index d407726e0..c1edb828c 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -255,6 +255,12 @@ void RunServer(std::optional host, std::optional port, static const std::unordered_set public_endpoints = { "/openapi.json", "/healthz", "/processManager/destroy"}; + if (req->getHeader("Authorization").empty() && + req->path() == "/v1/configs") { + CTL_WRN("Require API key to acceess /v1/configs"); + return false; + } + // If API key is not set, skip validation if (api_keys.empty()) { return true; From da1a7448cf014967baee788ea5c6fa9ba1083e5c Mon Sep 17 00:00:00 2001 From: sangjanai Date: Thu, 20 Mar 2025 16:36:06 +0700 Subject: [PATCH 2/3] fix: typo --- engine/main.cc | 2 +- 1 file changed, 1 insertion(+), 1 deletion(-) diff --git a/engine/main.cc b/engine/main.cc index c1edb828c..623e941a1 100644 --- a/engine/main.cc +++ b/engine/main.cc @@ -257,7 +257,7 @@ void RunServer(std::optional host, std::optional port, if (req->getHeader("Authorization").empty() && req->path() == "/v1/configs") { - CTL_WRN("Require API key to acceess /v1/configs"); + CTL_WRN("Require API key to access /v1/configs"); return false; } From 299bdab620df099db33e779107bf6171b0bf8aa3 Mon Sep 17 00:00:00 2001 From: sangjanai Date: Thu, 20 Mar 2025 17:03:08 +0700 Subject: [PATCH 3/3] chore: add comment --- engine/cli/commands/config_upd_cmd.cc | 1 + 1 file changed, 1 insertion(+) diff --git a/engine/cli/commands/config_upd_cmd.cc b/engine/cli/commands/config_upd_cmd.cc index 8a7e5599d..3abdbac83 100644 --- a/engine/cli/commands/config_upd_cmd.cc +++ b/engine/cli/commands/config_upd_cmd.cc @@ -50,6 +50,7 @@ void commands::ConfigUpdCmd::Exec( auto non_null_opts = std::unordered_map(); for (const auto& [key, value] : options) { + // In case of api_keys, we allow empty value if (value.empty() && key != "api_keys") { continue; }