Skip to content

Commit 14ee18d

Browse files
committed
Allow the scitokens library user to setup a custom CA file
1 parent fedce7d commit 14ee18d

File tree

3 files changed

+27
-2
lines changed

3 files changed

+27
-2
lines changed

src/scitokens.cpp

Lines changed: 6 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -17,6 +17,8 @@ std::atomic_int configurer::Configuration::m_expiry_delta{4 * 24 * 3600};
1717
// SciTokens cache home config
1818
std::shared_ptr<std::string> configurer::Configuration::m_cache_home =
1919
std::make_shared<std::string>("");
20+
std::shared_ptr<std::string> configurer::Configuration::m_tls_ca_file =
21+
std::make_shared<std::string>("");
2022

2123
SciTokenKey scitoken_key_create(const char *key_id, const char *alg,
2224
const char *public_contents,
@@ -1051,8 +1053,9 @@ int scitoken_config_set_str(const char *key, const char *value,
10511053
}
10521054
return -1;
10531055
}
1056+
} else if (_key == "tls.ca_file") {
1057+
configurer::Configuration::set_tls_ca_file(value ? std::string(value) : "");
10541058
}
1055-
10561059
else {
10571060
if (err_msg) {
10581061
*err_msg = strdup("Key not recognized.");
@@ -1073,6 +1076,8 @@ int scitoken_config_get_str(const char *key, char **output, char **err_msg) {
10731076
std::string _key = key;
10741077
if (_key == "keycache.cache_home") {
10751078
*output = strdup(configurer::Configuration::get_cache_home().c_str());
1079+
} else if (_key == "tls.ca_file") {
1080+
*output = strdup(configurer::Configuration::get_tls_ca_file().c_str());
10761081
}
10771082

10781083
else {

src/scitokens_internal.cpp

Lines changed: 16 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -79,6 +79,14 @@ SimpleCurlGet::GetStatus SimpleCurlGet::perform_start(const std::string &url) {
7979
throw CurlException("Failed to set CURLOPT_FOLLOWLOCATION.");
8080
}
8181

82+
auto ca_file = configurer::Configuration::get_tls_ca_file();
83+
if (!ca_file.empty()) {
84+
rv = curl_easy_setopt(m_curl.get(), CURLOPT_CAINFO, ca_file.c_str());
85+
if (rv != CURLE_OK) {
86+
throw CurlException("Failed to set CURLOPT_CAINFO.");
87+
}
88+
}
89+
8290
{
8391
auto mres = curl_multi_add_handle(m_curl_multi.get(), m_curl.get());
8492
if (mres) {
@@ -1131,10 +1139,18 @@ configurer::Configuration::set_cache_home(const std::string dir_path) {
11311139
return std::make_pair(true, "");
11321140
}
11331141

1142+
void configurer::Configuration::set_tls_ca_file(const std::string ca_file) {
1143+
m_tls_ca_file = std::make_shared<std::string>(ca_file);
1144+
}
1145+
11341146
std::string configurer::Configuration::get_cache_home() {
11351147
return *m_cache_home;
11361148
}
11371149

1150+
std::string configurer::Configuration::get_tls_ca_file() {
1151+
return *m_tls_ca_file;
1152+
}
1153+
11381154
// bool configurer::Configuration::check_dir(const std::string dir_path) {
11391155
// struct stat info;
11401156
// return stat(dir_path.c_str(), &info) == 0 && (info.st_mode & S_IFDIR);

src/scitokens_internal.h

Lines changed: 5 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -42,13 +42,17 @@ class Configuration {
4242
m_expiry_delta = _expiry_delta;
4343
}
4444
static int get_expiry_delta() { return m_expiry_delta; }
45-
static std::pair<bool, std::string> set_cache_home(const std::string cache_home);
45+
static std::pair<bool, std::string>
46+
set_cache_home(const std::string cache_home);
4647
static std::string get_cache_home();
48+
static void set_tls_ca_file(const std::string ca_file);
49+
static std::string get_tls_ca_file();
4750

4851
private:
4952
static std::atomic_int m_next_update_delta;
5053
static std::atomic_int m_expiry_delta;
5154
static std::shared_ptr<std::string> m_cache_home;
55+
static std::shared_ptr<std::string> m_tls_ca_file;
5256
// static bool check_dir(const std::string dir_path);
5357
static std::pair<bool, std::string>
5458
mkdir_and_parents_if_needed(const std::string dir_path);

0 commit comments

Comments
 (0)