Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(gcs): allow unauthenticated requests #4965

Merged
merged 11 commits into from
Aug 7, 2024
13 changes: 13 additions & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -66,6 +66,10 @@ pub struct GcsConfig {
pub predefined_acl: Option<String>,
/// The default storage class used by gcs.
pub default_storage_class: Option<String>,
/// Explicitly disable authentication
///
/// Used for testing purposes against storage emulators.
jdockerty marked this conversation as resolved.
Show resolved Hide resolved
pub no_authentication: bool,
jdockerty marked this conversation as resolved.
Show resolved Hide resolved
}

impl Debug for GcsConfig {
Expand Down Expand Up @@ -234,6 +238,14 @@ impl GcsBuilder {
};
self
}

/// Explicitly disable authentication.
///
/// This is typically only done for testing purposes against storage emulators.
pub fn no_authentication(mut self) -> Self {
self.config.no_authentication = true;
self
}
}

impl Builder for GcsBuilder {
Expand Down Expand Up @@ -317,6 +329,7 @@ impl Builder for GcsBuilder {
credential_loader: cred_loader,
predefined_acl: self.config.predefined_acl.clone(),
default_storage_class: self.config.default_storage_class.clone(),
no_authentication: self.config.no_authentication,
}),
};

Expand Down
25 changes: 18 additions & 7 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -57,6 +57,8 @@ pub struct GcsCore {

pub predefined_acl: Option<String>,
pub default_storage_class: Option<String>,

pub no_authentication: bool,
}

impl Debug for GcsCore {
Expand Down Expand Up @@ -89,14 +91,18 @@ impl GcsCore {
}
}

fn load_credential(&self) -> Result<GoogleCredential> {
fn load_credential(&self) -> Result<Option<GoogleCredential>> {
jdockerty marked this conversation as resolved.
Show resolved Hide resolved
if self.no_authentication {
return Ok(None);
}

let cred = self
.credential_loader
.load()
.map_err(new_request_credential_error)?;

if let Some(cred) = cred {
Ok(cred)
Ok(Some(cred))
} else {
Err(Error::new(
ErrorKind::ConfigInvalid,
Expand All @@ -106,6 +112,9 @@ impl GcsCore {
}

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

self.signer
Expand All @@ -124,11 +133,13 @@ impl GcsCore {
}

pub async fn sign_query<T>(&self, req: &mut Request<T>, duration: Duration) -> Result<()> {
let cred = self.load_credential()?;

self.signer
.sign_query(req, duration, &cred)
.map_err(new_request_sign_error)?;
if let Some(cred) = self.load_credential()? {
self.signer
.sign_query(req, duration, &cred)
.map_err(new_request_sign_error)?;
} else {
return Ok(());
}

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