Skip to content

Commit

Permalink
fix(gcs): do not skip signing with allow_anonymous (#4979)
Browse files Browse the repository at this point in the history
* fix: do not skip signing with allow_anonymous

* feat: load_token handles allow_anonymous
  • Loading branch information
jdockerty authored Aug 7, 2024
1 parent 0a3e98f commit 1f4c119
Showing 1 changed file with 16 additions and 13 deletions.
29 changes: 16 additions & 13 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -75,20 +75,24 @@ static BACKOFF: Lazy<ExponentialBuilder> =
Lazy::new(|| ExponentialBuilder::default().with_jitter());

impl GcsCore {
async fn load_token(&self) -> Result<GoogleToken> {
async fn load_token(&self) -> Result<Option<GoogleToken>> {
let cred = { || self.token_loader.load() }
.retry(&*BACKOFF)
.await
.map_err(new_request_credential_error)?;

if let Some(cred) = cred {
Ok(cred)
} else {
Err(Error::new(
ErrorKind::ConfigInvalid,
"no valid credential found",
))
return Ok(Some(cred));
}

if self.allow_anonymous {
return Ok(None);
}

Err(Error::new(
ErrorKind::ConfigInvalid,
"no valid credential found",
))
}

fn load_credential(&self) -> Result<Option<GoogleCredential>> {
Expand All @@ -112,14 +116,13 @@ impl GcsCore {
}

pub async fn sign<T>(&self, req: &mut Request<T>) -> Result<()> {
if self.allow_anonymous {
if let Some(cred) = self.load_token().await? {
self.signer
.sign(req, &cred)
.map_err(new_request_sign_error)?;
} else {
return Ok(());
}
let cred = self.load_token().await?;

self.signer
.sign(req, &cred)
.map_err(new_request_sign_error)?;

// Always remove host header, let users' client to set it based on HTTP
// version.
Expand Down

0 comments on commit 1f4c119

Please sign in to comment.