Skip to content
Merged
12 changes: 12 additions & 0 deletions core/src/services/gcs/backend.rs
Original file line number Diff line number Diff line change
Expand Up @@ -74,6 +74,10 @@ pub struct GcsConfig {
pub disable_vm_metadata: bool,
/// Disable loading configuration from the environment.
pub disable_config_load: bool,
/// A Google Cloud OAuth2 token.
///
/// Takes precedence over `credential` and `credential_path`.
pub token: Option<String>,
}

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

/// Provide the OAuth2 token to use.
pub fn token(mut self, token: String) -> Self {
self.config.token = Some(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 +364,8 @@ impl Builder for GcsBuilder {
client,
signer,
token_loader,
token: self.config.token,
scope: scope.to_string(),
credential_loader: cred_loader,
predefined_acl: self.config.predefined_acl.clone(),
default_storage_class: self.config.default_storage_class.clone(),
Expand Down
6 changes: 6 additions & 0 deletions core/src/services/gcs/core.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,6 +53,8 @@ pub struct GcsCore {
pub client: HttpClient,
pub signer: GoogleSigner,
pub token_loader: GoogleTokenLoader,
pub token: Option<String>,
pub scope: String,
pub credential_loader: GoogleCredentialLoader,

pub predefined_acl: Option<String>,
Expand All @@ -76,6 +78,10 @@ static BACKOFF: Lazy<ExponentialBuilder> =

impl GcsCore {
async fn load_token(&self) -> Result<Option<GoogleToken>> {
if let Some(token) = &self.token {
return Ok(Some(GoogleToken::new(token, usize::MAX, &self.scope)));
}

let cred = { || self.token_loader.load() }
.retry(&*BACKOFF)
.await
Expand Down