Skip to content

Commit 81ff0ba

Browse files
salman1993claude
andauthored
fix(search): do not return expired AWS credentials after a failed reload (#10042)
## What was wrong `CachedCredentials::credentials` decided whether the cached AWS credentials were still valid *before* awaiting the reload, which can take a few seconds. If the reload then failed, it fell back to the cached set using that earlier check, so it could return credentials that had expired while the reload was running. The request signed with them then failed at OpenSearch with a 403, not a clear credentials error. ## Fix After a failed reload, check the cached set's expiry again against the current time. Return it only if it is still valid; otherwise return the reload error. The cache is otherwise unchanged. ## Testing - New unit test `credentials_that_expire_during_a_failed_reload_are_not_returned`. Its source hands out credentials that expire 200 ms after loading, then fails the reload only after they have expired. The call must return an error. - Confirmed the test fails with the fix reverted and passes with it. - `cargo test -p buzz-search --lib aws_credentials`: 6 passed. 🤖 Generated with [Claude Code](https://claude.com/claude-code) Signed-off-by: Salman Mohammed <smohammed@squareup.com> Co-authored-by: Claude Opus 5.5 <noreply@anthropic.com>
1 parent a1ef23c commit 81ff0ba

1 file changed

Lines changed: 47 additions & 1 deletion

File tree

‎crates/buzz-search/src/aws_credentials.rs‎

Lines changed: 47 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -176,7 +176,12 @@ impl<P: ProvideCredentials> CachedCredentials<P> {
176176
None => MIN_LOAD_INTERVAL,
177177
};
178178
state.next_load = Some(Instant::now() + wait);
179-
valid.ok_or(error)
179+
// The reload can take seconds: the old set may have expired while it
180+
// ran, and an expired set would only fail at the cluster.
181+
let now = SystemTime::now();
182+
valid
183+
.filter(|credentials| credentials.expiry().is_none_or(|expiry| now < expiry))
184+
.ok_or(error)
180185
}
181186
}
182187

@@ -284,6 +289,47 @@ mod tests {
284289
assert_eq!(cached.source.loads.load(Ordering::SeqCst), 1);
285290
}
286291

292+
/// Loads credentials that expire shortly after, then fails every reload
293+
/// only once those have expired, as a slow failing reload would.
294+
#[derive(Debug, Default)]
295+
struct FailsAfterExpiry {
296+
expiry: std::sync::Mutex<Option<SystemTime>>,
297+
}
298+
299+
impl ProvideCredentials for FailsAfterExpiry {
300+
fn provide_credentials<'a>(&'a self) -> future::ProvideCredentials<'a>
301+
where
302+
Self: 'a,
303+
{
304+
future::ProvideCredentials::new(async move {
305+
let loaded = *self.expiry.lock().expect("expiry lock");
306+
let Some(expiry) = loaded else {
307+
let expiry = SystemTime::now() + Duration::from_millis(200);
308+
*self.expiry.lock().expect("expiry lock") = Some(expiry);
309+
return Ok(Credentials::new(
310+
"access",
311+
"secret",
312+
None,
313+
Some(expiry),
314+
"test",
315+
));
316+
};
317+
while SystemTime::now() < expiry {
318+
tokio::time::sleep(Duration::from_millis(10)).await;
319+
}
320+
Err(CredentialsError::not_loaded("source is down"))
321+
})
322+
}
323+
}
324+
325+
#[tokio::test]
326+
async fn credentials_that_expire_during_a_failed_reload_are_not_returned() {
327+
let cached = CachedCredentials::new(FailsAfterExpiry::default());
328+
assert_eq!(access_key(&cached).await.as_deref(), Some("access"));
329+
reload_is_due(&cached).await;
330+
assert_eq!(access_key(&cached).await, None);
331+
}
332+
287333
/// Never answers, counting the loads.
288334
#[derive(Debug, Default)]
289335
struct Stalled {

0 commit comments

Comments
 (0)