Skip to content

Commit f82b704

Browse files
authored
object-store: fix handling of AWS profile credentials without expiry (#3766)
* fix aws profile * fix unused import * support None as expiry * fix clippy * fix fmt * revert fmt whitespace fix
1 parent 034c43f commit f82b704

File tree

4 files changed

+32
-30
lines changed

4 files changed

+32
-30
lines changed

object_store/src/aws/credential.rs

Lines changed: 6 additions & 12 deletions
Original file line numberDiff line numberDiff line change
@@ -438,7 +438,7 @@ async fn instance_creds(
438438
let ttl = (creds.expiration - now).to_std().unwrap_or_default();
439439
Ok(TemporaryToken {
440440
token: Arc::new(creds.into()),
441-
expiry: Instant::now() + ttl,
441+
expiry: Some(Instant::now() + ttl),
442442
})
443443
}
444444

@@ -509,7 +509,7 @@ async fn web_identity(
509509

510510
Ok(TemporaryToken {
511511
token: Arc::new(creds.into()),
512-
expiry: Instant::now() + ttl,
512+
expiry: Some(Instant::now() + ttl),
513513
})
514514
}
515515

@@ -553,17 +553,11 @@ mod profile {
553553
store: "S3",
554554
source: Box::new(source),
555555
})?;
556-
557556
let t_now = SystemTime::now();
558-
let expiry = match c.expiry().and_then(|e| e.duration_since(t_now).ok()) {
559-
Some(ttl) => Instant::now() + ttl,
560-
None => {
561-
return Err(crate::Error::Generic {
562-
store: "S3",
563-
source: "Invalid expiry".into(),
564-
})
565-
}
566-
};
557+
let expiry = c
558+
.expiry()
559+
.and_then(|e| e.duration_since(t_now).ok())
560+
.map(|ttl| Instant::now() + ttl);
567561

568562
Ok(TemporaryToken {
569563
token: Arc::new(AwsCredential {

object_store/src/azure/credential.rs

Lines changed: 9 additions & 7 deletions
Original file line numberDiff line numberDiff line change
@@ -360,7 +360,7 @@ impl TokenCredential for ClientSecretOAuthProvider {
360360

361361
let token = TemporaryToken {
362362
token: response.access_token,
363-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
363+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
364364
};
365365

366366
Ok(token)
@@ -467,7 +467,7 @@ impl TokenCredential for ImdsManagedIdentityOAuthProvider {
467467

468468
let token = TemporaryToken {
469469
token: response.access_token,
470-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
470+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
471471
};
472472

473473
Ok(token)
@@ -541,7 +541,7 @@ impl TokenCredential for WorkloadIdentityOAuthProvider {
541541

542542
let token = TemporaryToken {
543543
token: response.access_token,
544-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
544+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
545545
};
546546

547547
Ok(token)
@@ -640,10 +640,12 @@ impl TokenCredential for AzureCliCredential {
640640
- chrono::Local::now().naive_local();
641641
Ok(TemporaryToken {
642642
token: token_response.access_token,
643-
expiry: Instant::now()
644-
+ duration.to_std().map_err(|_| Error::AzureCli {
645-
message: "az returned invalid lifetime".to_string(),
646-
})?,
643+
expiry: Some(
644+
Instant::now()
645+
+ duration.to_std().map_err(|_| Error::AzureCli {
646+
message: "az returned invalid lifetime".to_string(),
647+
})?,
648+
),
647649
})
648650
}
649651
Ok(az_output) => {

object_store/src/client/token.rs

Lines changed: 14 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -25,7 +25,8 @@ pub struct TemporaryToken<T> {
2525
/// The temporary credential
2626
pub token: T,
2727
/// The instant at which this credential is no longer valid
28-
pub expiry: Instant,
28+
/// None means the credential does not expire
29+
pub expiry: Option<Instant>,
2930
}
3031

3132
/// Provides [`TokenCache::get_or_insert_with`] which can be used to cache a
@@ -53,13 +54,18 @@ impl<T: Clone + Send> TokenCache<T> {
5354
let mut locked = self.cache.lock().await;
5455

5556
if let Some(cached) = locked.as_ref() {
56-
let delta = cached
57-
.expiry
58-
.checked_duration_since(now)
59-
.unwrap_or_default();
60-
61-
if delta.as_secs() > 300 {
62-
return Ok(cached.token.clone());
57+
match cached.expiry {
58+
Some(ttl)
59+
if ttl
60+
.checked_duration_since(now)
61+
.unwrap_or_default()
62+
.as_secs()
63+
> 300 =>
64+
{
65+
return Ok(cached.token.clone());
66+
}
67+
None => return Ok(cached.token.clone()),
68+
_ => (),
6369
}
6470
}
6571

object_store/src/gcp/credential.rs

Lines changed: 3 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -220,7 +220,7 @@ impl TokenProvider for OAuthProvider {
220220

221221
let token = TemporaryToken {
222222
token: response.access_token,
223-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
223+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
224224
};
225225

226226
Ok(token)
@@ -393,7 +393,7 @@ impl TokenProvider for InstanceCredentialProvider {
393393
.await?;
394394
let token = TemporaryToken {
395395
token: response.access_token,
396-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
396+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
397397
};
398398
Ok(token)
399399
}
@@ -467,7 +467,7 @@ impl TokenProvider for ApplicationDefaultCredentials {
467467
.context(TokenResponseBodySnafu)?;
468468
let token = TemporaryToken {
469469
token: response.access_token,
470-
expiry: Instant::now() + Duration::from_secs(response.expires_in),
470+
expiry: Some(Instant::now() + Duration::from_secs(response.expires_in)),
471471
};
472472
Ok(token)
473473
}

0 commit comments

Comments
 (0)