Skip to content

Commit 0e03dfd

Browse files
committed
Shorten timeout for public key download
While the public key download can fail and have the library fall back on the cached copy, this fallback can take such a long time that the application code (such as the HTCondor-CE) invoking the library times out its current operation and still failing. So, without this, the fallback succeeds but everything else fails due to timeouts. For now, we have arbitrarily set the timeout to 30s for the case where the pubkey has expired and 4s for the update check.
1 parent f85f271 commit 0e03dfd

File tree

2 files changed

+18
-8
lines changed

2 files changed

+18
-8
lines changed

src/scitokens_internal.cpp

Lines changed: 17 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -35,12 +35,16 @@ CurlRaii myCurl;
3535
class SimpleCurlGet {
3636

3737
int m_maxbytes;
38+
unsigned m_timeout;
3839
std::vector<char> m_data;
3940
size_t m_len{0};
4041

4142
public:
42-
SimpleCurlGet(int maxbytes=1024*1024)
43-
: m_maxbytes(maxbytes)
43+
static const unsigned default_timeout = 4;
44+
static const unsigned extended_timeout = 30;
45+
46+
SimpleCurlGet(int maxbytes=1024*1024, unsigned timeout=4)
47+
: m_maxbytes(maxbytes), m_timeout(timeout)
4448
{}
4549

4650
int perform(const std::string &url) {
@@ -58,6 +62,8 @@ class SimpleCurlGet {
5862
}
5963
}
6064

65+
long timeout = m_timeout > 120 ? 120 : m_timeout;
66+
6167
CURLcode rv = curl_easy_setopt(curl, CURLOPT_URL, url.c_str());
6268
if (rv != CURLE_OK) {
6369
throw CurlException("Failed to set CURLOPT_URL.");
@@ -70,6 +76,10 @@ class SimpleCurlGet {
7076
if (rv != CURLE_OK) {
7177
throw CurlException("Failed to set CURLOPT_WRITEDATA.");
7278
}
79+
rv = curl_easy_setopt(curl, CURLOPT_TIMEOUT, timeout);
80+
if (rv != CURLE_OK) {
81+
throw CurlException("Failed to set CURLOPT_TIMEOUT.");
82+
}
7383

7484
auto res = curl_easy_perform(curl);
7585
if (res != CURLE_OK) {
@@ -467,12 +477,12 @@ SciToken::deserialize(const std::string &data, const std::vector<std::string> al
467477

468478

469479
void
470-
Validator::get_public_keys_from_web(const std::string &issuer, picojson::value &keys, int64_t &next_update, int64_t &expires)
480+
Validator::get_public_keys_from_web(const std::string &issuer, unsigned timeout, picojson::value &keys, int64_t &next_update, int64_t &expires)
471481
{
472482
std::string openid_metadata, oauth_metadata;
473483
get_metadata_endpoint(issuer, openid_metadata, oauth_metadata);
474484

475-
SimpleCurlGet cget;
485+
SimpleCurlGet cget(1024*1024, timeout);
476486
auto status_code = cget.perform(openid_metadata);
477487

478488
if (status_code != 200) {
@@ -542,7 +552,7 @@ Validator::refresh_jwks(const std::string &issuer)
542552
{
543553
int64_t next_update, expires;
544554
picojson::value keys;
545-
get_public_keys_from_web(issuer, keys, next_update, expires);
555+
get_public_keys_from_web(issuer, SimpleCurlGet::default_timeout, keys, next_update, expires);
546556
return store_public_keys(issuer, keys, next_update, expires);
547557
}
548558

@@ -571,14 +581,14 @@ Validator::get_public_key_pem(const std::string &issuer, const std::string &kid,
571581
if (get_public_keys_from_db(issuer, now, keys, next_update)) {
572582
if (now > next_update) {
573583
try {
574-
get_public_keys_from_web(issuer, keys, next_update, expires);
584+
get_public_keys_from_web(issuer, SimpleCurlGet::default_timeout, keys, next_update, expires);
575585
store_public_keys(issuer, keys, next_update, expires);
576586
} catch (std::runtime_error &) {
577587
// ignore the exception: we have a valid set of keys already/
578588
}
579589
}
580590
} else {
581-
get_public_keys_from_web(issuer, keys, next_update, expires);
591+
get_public_keys_from_web(issuer, SimpleCurlGet::extended_timeout, keys, next_update, expires);
582592
store_public_keys(issuer, keys, next_update, expires);
583593
}
584594

src/scitokens_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -548,7 +548,7 @@ class Validator {
548548

549549
private:
550550
void get_public_key_pem(const std::string &issuer, const std::string &kid, std::string &public_pem, std::string &algorithm);
551-
static void get_public_keys_from_web(const std::string &issuer, picojson::value &keys, int64_t &next_update, int64_t &expires);
551+
static void get_public_keys_from_web(const std::string &issuer, unsigned timeout, picojson::value &keys, int64_t &next_update, int64_t &expires);
552552
static bool get_public_keys_from_db(const std::string issuer, int64_t now, picojson::value &keys, int64_t &next_update);
553553
static bool store_public_keys(const std::string &issuer, const picojson::value &keys, int64_t next_update, int64_t expires);
554554

0 commit comments

Comments
 (0)