Skip to content

Commit

Permalink
main, config: Move options parsing helpers
Browse files Browse the repository at this point in the history
The get_or_default and is_true are two aux bits that are used
to parse the config options. The former is duplicated in the
alternator code as well.

Put both in utils namespace for future.

Signed-off-by: Pavel Emelyanov <[email protected]>
  • Loading branch information
xemul committed Aug 20, 2021
1 parent f98cb96 commit aa88527
Show file tree
Hide file tree
Showing 3 changed files with 31 additions and 37 deletions.
15 changes: 3 additions & 12 deletions alternator/controller.cc
Original file line number Diff line number Diff line change
Expand Up @@ -32,15 +32,6 @@ using namespace seastar;

namespace alternator {

template<typename K, typename V, typename... Args, typename K2, typename V2 = V>
V get_or_default(const std::unordered_map<K, V, Args...>& ss, const K2& key, const V2& def = V()) {
const auto iter = ss.find(key);
if (iter != ss.end()) {
return iter->second;
}
return def;
}

static logging::logger logger("alternator_controller");

controller::controller(sharded<service::storage_proxy>& proxy,
Expand Down Expand Up @@ -109,10 +100,10 @@ future<> controller::start() {
}
}
creds->set_dh_level(tls::dh_params::level::MEDIUM);
auto cert = get_or_default(opts, "certificate", db::config::get_conf_sub("scylla.crt").string());
auto key = get_or_default(opts, "keyfile", db::config::get_conf_sub("scylla.key").string());
auto cert = utils::get_or_default(opts, "certificate", db::config::get_conf_sub("scylla.crt").string());
auto key = utils::get_or_default(opts, "keyfile", db::config::get_conf_sub("scylla.key").string());
creds->set_x509_key_file(cert, key, tls::x509_crt_format::PEM).get();
auto prio = get_or_default(opts, "priority_string", sstring());
auto prio = utils::get_or_default(opts, "priority_string", sstring());
creds->set_priority_string(db::config::default_tls_priority);
if (!prio.empty()) {
creds->set_priority_string(prio);
Expand Down
18 changes: 18 additions & 0 deletions db/config.hh
Original file line number Diff line number Diff line change
Expand Up @@ -393,3 +393,21 @@ private:
};

}

namespace utils {

template<typename K, typename V, typename... Args, typename K2, typename V2 = V>
V get_or_default(const std::unordered_map<K, V, Args...>& ss, const K2& key, const V2& def = V()) {
const auto iter = ss.find(key);
if (iter != ss.end()) {
return iter->second;
}
return def;
}

inline bool is_true(sstring val) {
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
return val == "true" || val == "1";
}

}
35 changes: 10 additions & 25 deletions main.cc
Original file line number Diff line number Diff line change
Expand Up @@ -143,15 +143,6 @@ class stop_signal {
sharded<abort_source>& as_sharded_abort_source() { return _abort_sources; }
};

template<typename K, typename V, typename... Args, typename K2, typename V2 = V>
V get_or_default(const std::unordered_map<K, V, Args...>& ss, const K2& key, const V2& def = V()) {
const auto iter = ss.find(key);
if (iter != ss.end()) {
return iter->second;
}
return def;
}

static future<>
read_config(bpo::variables_map& opts, db::config& cfg) {
sstring file;
Expand Down Expand Up @@ -696,22 +687,16 @@ int main(int ac, char** av) {
utils::fb_utilities::set_broadcast_rpc_address(gms::inet_address::lookup(rpc_address, family, preferred).get0());
}

// TODO: lib.
auto is_true = [](sstring val) {
std::transform(val.begin(), val.end(), val.begin(), ::tolower);
return val == "true" || val == "1";
};

// The start_native_transport method is invoked by API as well, and uses the config object
// (through db) directly. Lets fixup default valued right here instead then, so it in turn can be
// kept simple
// TODO: make intrinsic part of config defaults instead
auto ceo = cfg->client_encryption_options();
if (is_true(get_or_default(ceo, "enabled", "false"))) {
if (utils::is_true(utils::get_or_default(ceo, "enabled", "false"))) {
ceo["enabled"] = "true";
ceo["certificate"] = get_or_default(ceo, "certificate", db::config::get_conf_sub("scylla.crt").string());
ceo["keyfile"] = get_or_default(ceo, "keyfile", db::config::get_conf_sub("scylla.key").string());
ceo["require_client_auth"] = is_true(get_or_default(ceo, "require_client_auth", "false")) ? "true" : "false";
ceo["certificate"] = utils::get_or_default(ceo, "certificate", db::config::get_conf_sub("scylla.crt").string());
ceo["keyfile"] = utils::get_or_default(ceo, "keyfile", db::config::get_conf_sub("scylla.key").string());
ceo["require_client_auth"] = utils::is_true(utils::get_or_default(ceo, "require_client_auth", "false")) ? "true" : "false";
} else {
ceo["enabled"] = "false";
}
Expand Down Expand Up @@ -798,12 +783,12 @@ int main(int ac, char** av) {
dbcfg.available_memory = memory::stats().total_memory();

const auto& ssl_opts = cfg->server_encryption_options();
auto encrypt_what = get_or_default(ssl_opts, "internode_encryption", "none");
auto trust_store = get_or_default(ssl_opts, "truststore");
auto cert = get_or_default(ssl_opts, "certificate", db::config::get_conf_sub("scylla.crt").string());
auto key = get_or_default(ssl_opts, "keyfile", db::config::get_conf_sub("scylla.key").string());
auto prio = get_or_default(ssl_opts, "priority_string", sstring());
auto clauth = is_true(get_or_default(ssl_opts, "require_client_auth", "false"));
auto encrypt_what = utils::get_or_default(ssl_opts, "internode_encryption", "none");
auto trust_store = utils::get_or_default(ssl_opts, "truststore");
auto cert = utils::get_or_default(ssl_opts, "certificate", db::config::get_conf_sub("scylla.crt").string());
auto key = utils::get_or_default(ssl_opts, "keyfile", db::config::get_conf_sub("scylla.key").string());
auto prio = utils::get_or_default(ssl_opts, "priority_string", sstring());
auto clauth = utils::is_true(utils::get_or_default(ssl_opts, "require_client_auth", "false"));

netw::messaging_service::config mscfg;

Expand Down

0 comments on commit aa88527

Please sign in to comment.