Skip to content

Commit 7ec2670

Browse files
committed
Avoid retrying for 5 minutes after failed key retrieval
Without this, a persistent failure in the key retrieval will not be remembered, meaning rapid-fire retries from the client will result in a similar number of key retrieval attempts. This causes the next_update to be set forward by 5 minutes after each failure.
1 parent aec994e commit 7ec2670

File tree

3 files changed

+9
-6
lines changed

3 files changed

+9
-6
lines changed

src/scitokens_cache.cpp

Lines changed: 2 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -121,7 +121,7 @@ remove_issuer_entry(sqlite3 *db, const std::string &issuer, bool new_transaction
121121

122122

123123
bool
124-
scitokens::Validator::get_public_keys_from_db(const std::string issuer, int64_t now, picojson::value &keys, int64_t &next_update) {
124+
scitokens::Validator::get_public_keys_from_db(const std::string issuer, int64_t now, picojson::value &keys, int64_t &next_update, int64_t &expires) {
125125
auto cache_fname = get_cache_file();
126126
if (cache_fname.size() == 0) {return false;}
127127

@@ -177,6 +177,7 @@ scitokens::Validator::get_public_keys_from_db(const std::string issuer, int64_t
177177
sqlite3_close(db);
178178
return false;
179179
}
180+
expires = expiry;
180181
sqlite3_close(db);
181182
iter = top_obj.find("next_update");
182183
if (iter == top_obj.end() || !iter->second.is<int64_t>()) {

src/scitokens_internal.cpp

Lines changed: 6 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -539,8 +539,8 @@ Validator::get_jwks(const std::string &issuer)
539539
{
540540
auto now = std::time(NULL);
541541
picojson::value jwks;
542-
int64_t next_update;
543-
if (get_public_keys_from_db(issuer, now, jwks, next_update)) {
542+
int64_t next_update, expires;
543+
if (get_public_keys_from_db(issuer, now, jwks, next_update, expires)) {
544544
return jwks.serialize();
545545
}
546546
return std::string("{\"keys\": []}");
@@ -578,13 +578,15 @@ Validator::get_public_key_pem(const std::string &issuer, const std::string &kid,
578578
picojson::value keys;
579579
int64_t next_update, expires;
580580
auto now = std::time(NULL);
581-
if (get_public_keys_from_db(issuer, now, keys, next_update)) {
581+
if (get_public_keys_from_db(issuer, now, keys, next_update, expires)) {
582582
if (now > next_update) {
583583
try {
584584
get_public_keys_from_web(issuer, SimpleCurlGet::default_timeout, keys, next_update, expires);
585585
store_public_keys(issuer, keys, next_update, expires);
586586
} catch (std::runtime_error &) {
587-
// ignore the exception: we have a valid set of keys already/
587+
// ignore the exception: we have a valid set of keys already. However, we don't want to continuously
588+
// hammer the upstream server which is not currently working ... move forward the next_update by 5 minutes.
589+
store_public_keys(issuer, keys, now + 300, expires);
588590
}
589591
}
590592
} else {

src/scitokens_internal.h

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -549,7 +549,7 @@ class Validator {
549549
private:
550550
void get_public_key_pem(const std::string &issuer, const std::string &kid, std::string &public_pem, std::string &algorithm);
551551
static void get_public_keys_from_web(const std::string &issuer, unsigned timeout, picojson::value &keys, int64_t &next_update, int64_t &expires);
552-
static bool get_public_keys_from_db(const std::string issuer, int64_t now, picojson::value &keys, int64_t &next_update);
552+
static bool get_public_keys_from_db(const std::string issuer, int64_t now, picojson::value &keys, int64_t &next_update, int64_t &expires);
553553
static bool store_public_keys(const std::string &issuer, const picojson::value &keys, int64_t next_update, int64_t expires);
554554

555555
bool m_validate_all_claims{true};

0 commit comments

Comments
 (0)