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 setting a token directly #4978

Merged
merged 13 commits into from
Aug 13, 2024
9 changes: 9 additions & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,8 @@ pub struct GcsConfig {
pub disable_vm_metadata: bool,
/// Disable loading configuration from the environment.
pub disable_config_load: bool,
/// A Google Cloud OAuth2 token.
pub token: String,
jdockerty marked this conversation as resolved.
Show resolved Hide resolved
}

impl Debug for GcsConfig {
Expand Down Expand Up @@ -214,6 +216,12 @@ impl GcsBuilder {
self
}

/// Provide the OAuth2 token to use.
pub fn token(mut self, token: String) -> Self {
self.config.token = token;
self
}

/// Disable attempting to load credentials from the GCE metadata server.
pub fn disable_vm_metadata(mut self) -> Self {
self.config.disable_vm_metadata = true;
Expand Down Expand Up @@ -354,6 +362,7 @@ impl Builder for GcsBuilder {
client,
signer,
token_loader,
token: self.config.token,
credential_loader: cred_loader,
predefined_acl: self.config.predefined_acl.clone(),
default_storage_class: self.config.default_storage_class.clone(),
Expand Down
14 changes: 14 additions & 0 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,7 @@ use std::time::Duration;
use backon::ExponentialBuilder;
use backon::Retryable;
use bytes::Bytes;
use http::header;
use http::header::CONTENT_LENGTH;
use http::header::CONTENT_TYPE;
use http::header::HOST;
Expand Down Expand Up @@ -53,6 +54,7 @@ pub struct GcsCore {
pub client: HttpClient,
pub signer: GoogleSigner,
pub token_loader: GoogleTokenLoader,
pub token: String,
pub credential_loader: GoogleCredentialLoader,

pub predefined_acl: Option<String>,
Expand Down Expand Up @@ -117,6 +119,12 @@ impl GcsCore {
}
let cred = self.load_token().await?;

if !self.token.is_empty() {
Xuanwo marked this conversation as resolved.
Show resolved Hide resolved
let header_value = format!("Bearer {}", self.token);
req.headers_mut()
.insert(header::AUTHORIZATION, header_value.parse().unwrap());
}

self.signer
.sign(req, &cred)
.map_err(new_request_sign_error)?;
Expand All @@ -141,6 +149,12 @@ impl GcsCore {
return Ok(());
}

if !self.token.is_empty() {
let header_value = format!("Bearer {}", self.token);
req.headers_mut()
.insert(header::AUTHORIZATION, header_value.parse().unwrap());
}

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