Skip to content

Commit

Permalink
feat(gcs): allow setting a token directly (#4978)
Browse files Browse the repository at this point in the history
* feat: add token to gcsconfig

* feat: add bearer token to header requests

* feat: add token to builder

* feat: make token optional

* feat: place token within header

* feat: pass scope to gcs core

* feat: return token directly from load_token if set

* refactor: use tuple match for token/scope handling

* fix: use default scope set for gcscore

* fix: scope must be valid

* revert: do not change sign logic

* revert: do not change sign_query logic
  • Loading branch information
jdockerty committed Aug 13, 2024
1 parent 43d5388 commit eb7d430
Show file tree
Hide file tree
Showing 2 changed files with 18 additions and 0 deletions.
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

0 comments on commit eb7d430

Please sign in to comment.