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

Commit 9b0e34b

Browse files
feat: allow to configure api_keys by cli (#2154)
* feat: allow to configure api_keys by cli * fix: typo * chore: add comment --------- Co-authored-by: sangjanai <sang@jan.ai>
1 parent 8e4809c commit 9b0e34b

File tree

4 files changed

+43
-9
lines changed

4 files changed

+43
-9
lines changed

engine/cli/command_line_parser.cc

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -437,7 +437,7 @@ void CommandLineParser::SetupConfigsCommands() {
437437

438438
auto is_empty = true;
439439
for (const auto& [key, value] : config_update_opts_) {
440-
if (!value.empty()) {
440+
if (!value.empty() || key == "api_keys") {
441441
is_empty = false;
442442
break;
443443
}

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()) {
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: 9 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -97,6 +97,15 @@ static const std::unordered_map<std::string, ApiConfigurationMetadata>
9797
.accept_value = "string",
9898
.default_value = "",
9999
.allow_empty = true}},
100+
{"api_keys",
101+
ApiConfigurationMetadata{
102+
.name = "api_keys",
103+
.desc = "API header key to get access to server APIs",
104+
.group = "Token",
105+
.accept_value = "comma separated",
106+
.default_value = "",
107+
.allow_empty = true}},
108+
100109
};
101110

102111
class ApiServerConfiguration {

engine/main.cc

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -255,6 +255,12 @@ void RunServer(std::optional<std::string> host, std::optional<int> port,
255255
static const std::unordered_set<std::string> public_endpoints = {
256256
"/openapi.json", "/healthz", "/processManager/destroy"};
257257

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+
}
263+
258264
// If API key is not set, skip validation
259265
if (api_keys.empty()) {
260266
return true;

0 commit comments

Comments
 (0)