Skip to content

Commit b87a31c

Browse files
committed
Add Config::set_internal API.
1 parent 3a2abf9 commit b87a31c

File tree

5 files changed

+43
-34
lines changed

5 files changed

+43
-34
lines changed

test/src/unit-capi-config.cc

+15-7
Original file line numberDiff line numberDiff line change
@@ -5,7 +5,7 @@
55
*
66
* The MIT License
77
*
8-
* @copyright Copyright (c) 2017-2023 TileDB Inc.
8+
* @copyright Copyright (c) 2017-2024 TileDB Inc.
99
*
1010
* Permission is hereby granted, free of charge, to any person obtaining a copy
1111
* of this software and associated documentation files (the "Software"), to deal
@@ -40,6 +40,7 @@
4040
#include <map>
4141
#include <sstream>
4242
#include <thread>
43+
#include <unordered_set>
4344

4445
void remove_file(const std::string& filename) {
4546
// Remove file
@@ -98,9 +99,7 @@ void check_load_incorrect_file_cannot_open() {
9899
rc = tiledb_config_load_from_file(config, "non_existent_file", &error);
99100
CHECK(rc == TILEDB_ERR);
100101
CHECK(error != nullptr);
101-
check_error(
102-
error,
103-
"[TileDB::Config] Error: Failed to open config file 'non_existent_file'");
102+
check_error(error, "Config: Failed to open config file 'non_existent_file'");
104103
tiledb_error_free(&error);
105104
tiledb_config_free(&config);
106105
CHECK(config == nullptr);
@@ -127,7 +126,7 @@ void check_load_incorrect_file_missing_value() {
127126
CHECK(error != nullptr);
128127
check_error(
129128
error,
130-
"[TileDB::Config] Error: Failed to parse config file 'test_config.txt'; "
129+
"Config: Failed to parse config file 'test_config.txt'; "
131130
"Missing parameter value (line: 1)");
132131
tiledb_error_free(&error);
133132
CHECK(error == nullptr);
@@ -157,7 +156,7 @@ void check_load_incorrect_file_extra_word() {
157156
CHECK(error != nullptr);
158157
check_error(
159158
error,
160-
"[TileDB::Config] Error: Failed to parse config file 'test_config.txt'; "
159+
"Config: Failed to parse config file 'test_config.txt'; "
161160
"Invalid line format (line: 3)");
162161
tiledb_error_free(&error);
163162
tiledb_config_free(&config);
@@ -906,6 +905,12 @@ TEST_CASE("C API: Test config iter", "[capi][config]") {
906905
s3_param_values["config_source"] = "auto";
907906
s3_param_values["install_sigpipe_handler"] = "true";
908907

908+
// A list of "sensitive" parameters, whose key-values should not be exposed.
909+
std::unordered_set<std::string> sensitive_param_values;
910+
sensitive_param_values.emplace("rest.token");
911+
sensitive_param_values.emplace("rest.username");
912+
sensitive_param_values.emplace("rest.password");
913+
909914
// Create an iterator and iterate over all parameters
910915
tiledb_config_iter_t* config_iter = nullptr;
911916
rc = tiledb_config_iter_alloc(config, nullptr, &config_iter, &error);
@@ -924,7 +929,10 @@ TEST_CASE("C API: Test config iter", "[capi][config]") {
924929
CHECK(error == nullptr);
925930
CHECK(param != nullptr);
926931
CHECK(value != nullptr);
927-
all_iter_map[std::string(param)] = std::string(value);
932+
// Skip checks for sensitive params to avoid exposing their values.
933+
if (!sensitive_param_values.contains(std::string(param))) {
934+
all_iter_map[std::string(param)] = std::string(value);
935+
}
928936
rc = tiledb_config_iter_next(config_iter, &error);
929937
CHECK(rc == TILEDB_OK);
930938
CHECK(error == nullptr);

test/src/unit-curl.cc

+3-3
Original file line numberDiff line numberDiff line change
@@ -129,13 +129,13 @@ TEST_CASE(
129129
"RestClient: Remove trailing slash from rest_server_", "[rest-client]") {
130130
std::string rest_server =
131131
GENERATE("http://localhost:8080/", "http://localhost:8080//");
132+
SECTION("rest.server_address set in environment") {
133+
setenv_local("TILEDB_REST_SERVER_ADDRESS", rest_server.c_str());
134+
}
132135
tiledb::sm::Config cfg;
133136
SECTION("rest.server_address set in Config") {
134137
cfg.set("rest.server_address", rest_server).ok();
135138
}
136-
SECTION("rest.server_address set in environment") {
137-
setenv_local("TILEDB_REST_SERVER_ADDRESS", rest_server.c_str());
138-
}
139139
SECTION("rest.server_address set by loaded config file") {
140140
std::string cfg_file = "tiledb_config.txt";
141141
std::ofstream file(cfg_file);

tiledb/sm/config/config.cc

+15-11
Original file line numberDiff line numberDiff line change
@@ -52,7 +52,6 @@ bool ignore_default_via_env(const std::string& param) {
5252
// We should not use the default value for `vfs.s3.region` if the user
5353
// has set either AWS_REGION or AWS_DEFAULT_REGION in their environment.
5454
// We defer to the SDK to interpret these values.
55-
5655
if ((std::getenv("AWS_REGION") != nullptr) ||
5756
(std::getenv("AWS_DEFAULT_REGION") != nullptr)) {
5857
return true;
@@ -555,6 +554,7 @@ const std::set<std::string> Config::unserialized_params_ = {
555554
Config::Config() {
556555
// Set config values
557556
param_values_ = default_config_values;
557+
login();
558558
}
559559

560560
Config::~Config() = default;
@@ -573,33 +573,32 @@ void Config::login() {
573573
home_dir = std::string(std::getenv("HOME"));
574574
#endif
575575

576-
// For library versions 22 and older, simply parse the local .json file
577-
if (constants::format_version <= 22) {
576+
// For library versions 2.27.0 and older, simply parse the local .json file
577+
auto version = constants::library_version;
578+
if (version[0] <= 2 && version[1] <= 27) {
578579
// Find and parse the cloud.json file
579580
std::string file = home_dir + "/.tiledb/cloud.json";
580581
if (!std::filesystem::exists(file)) {
581-
throw ConfigException("Cannot login; cloud.json file does not exist.");
582+
return;
582583
}
583584
json data = json::parse(std::ifstream(file));
584585

585586
// Set the config values that have been saved to the file
586587
if (data.contains("api_key") &&
587588
data["api_key"].contains("X-TILEDB-REST-API-KEY")) {
588-
throw_if_not_ok(
589-
set("rest.token", data["api_key"]["X-TILEDB-REST-API-KEY"]));
589+
set_internal("rest.token", data["api_key"]["X-TILEDB-REST-API-KEY"]);
590590
}
591591
if (data.contains("host")) {
592-
throw_if_not_ok(set("rest.server_address", data["host"]));
592+
set_internal("rest.server_address", data["host"]);
593593
}
594594
if (data.contains("password")) {
595-
throw_if_not_ok(set("rest.password", data["password"]));
595+
set_internal("rest.password", data["password"]);
596596
}
597597
if (data.contains("username")) {
598-
throw_if_not_ok(set("rest.username", data["username"]));
598+
set_internal("rest.username", data["username"]);
599599
}
600600
if (data.contains("verify_ssl")) {
601-
throw_if_not_ok(
602-
set("vfs.s3.verify_ssl", data["verify_ssl"] ? "true" : "false"));
601+
set_internal("vfs.s3.verify_ssl", data["verify_ssl"] ? "true" : "false");
603602
}
604603
}
605604
}
@@ -1030,6 +1029,11 @@ optional<std::string> Config::get_internal_string(
10301029
return {nullopt};
10311030
}
10321031

1032+
void Config::set_internal(const std::string& param, const std::string& value) {
1033+
throw_if_not_ok(sanity_check(param, value));
1034+
param_values_[param] = value;
1035+
}
1036+
10331037
/*
10341038
* Explicit instantiations
10351039
*/

tiledb/sm/config/config.h

+10
Original file line numberDiff line numberDiff line change
@@ -827,6 +827,16 @@ class Config {
827827

828828
/** Returns the param -> value map. */
829829
const std::map<std::string, std::string>& param_values() const;
830+
831+
/**
832+
* Internally sets the given config parameter.
833+
*
834+
* @note For internal use only; This API does not update the user-set params.
835+
*
836+
* @param param The config parameter to set.
837+
* @param value The value of the parameter.
838+
*/
839+
void set_internal(const std::string& param, const std::string& value);
830840
};
831841

832842
/**

tiledb/sm/config/test/unit_config.cc

-13
Original file line numberDiff line numberDiff line change
@@ -111,16 +111,3 @@ TEST_CASE("Config::get<std::string> - found and matched", "[config]") {
111111
CHECK(c.set(key, expected_value).ok());
112112
TestConfig<std::string>::check_expected(expected_value, c, key);
113113
}
114-
115-
TEST_CASE("Config::login", "[config][login]") {
116-
Config c{};
117-
118-
// Upon initial Config construction, no rest token is set
119-
auto initial_token = c.get<std::string>("rest.token");
120-
CHECK(!initial_token.has_value());
121-
122-
// After login, the rest token is set from the cloud.json file
123-
c.login();
124-
auto login_token = c.get<std::string>("rest.token");
125-
CHECK(login_token.has_value());
126-
}

0 commit comments

Comments
 (0)