Skip to content

Commit

Permalink
Add setter to auth Config (#293)
Browse files Browse the repository at this point in the history
  • Loading branch information
yoshidan authored Aug 4, 2024
1 parent 0a3fbbd commit 5fc08f8
Show file tree
Hide file tree
Showing 11 changed files with 52 additions and 75 deletions.
7 changes: 1 addition & 6 deletions artifact-registry/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -45,12 +45,7 @@ impl ClientConfig {
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: None,
scopes: Some(&SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default().with_scopes(&SCOPES)
}
}

Expand Down
16 changes: 4 additions & 12 deletions bigquery/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -128,21 +128,13 @@ impl ClientConfig {
}

fn bigquery_http_auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: None,
scopes: Some(&crate::http::bigquery_client::SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default().with_scopes(&http::bigquery_client::SCOPES)
}

fn bigquery_grpc_auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: Some(crate::grpc::apiv1::conn_pool::AUDIENCE),
scopes: Some(&crate::grpc::apiv1::conn_pool::SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default()
.with_audience(crate::grpc::apiv1::conn_pool::AUDIENCE)
.with_scopes(&crate::grpc::apiv1::conn_pool::SCOPES)
}
}

Expand Down
11 changes: 3 additions & 8 deletions bigquery/src/http/bigquery_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -131,14 +131,9 @@ pub(crate) mod test {
}

pub async fn create_client() -> (BigqueryClient, String) {
let tsp = DefaultTokenSourceProvider::new(Config {
audience: None,
scopes: Some(&SCOPES),
sub: None,
..Default::default()
})
.await
.unwrap();
let tsp = DefaultTokenSourceProvider::new(Config::default().with_scopes(&SCOPES))
.await
.unwrap();
let cred = tsp.source_credentials.clone();
let ts = tsp.token_source();
let client = BigqueryClient::new(
Expand Down
30 changes: 25 additions & 5 deletions foundation/auth/src/project.rs
Original file line number Diff line number Diff line change
Expand Up @@ -19,19 +19,39 @@ const EXTERNAL_ACCOUNT_KEY: &str = "external_account";

#[derive(Debug, Clone, Default)]
pub struct Config<'a> {
pub audience: Option<&'a str>,
pub scopes: Option<&'a [&'a str]>,
pub sub: Option<&'a str>,
pub use_id_token: bool,
audience: Option<&'a str>,
scopes: Option<&'a [&'a str]>,
sub: Option<&'a str>,
use_id_token: bool,
}

impl Config<'_> {
impl<'a> Config<'a> {
pub fn scopes_to_string(&self, sep: &str) -> String {
match self.scopes {
Some(s) => s.iter().map(|x| x.to_string()).collect::<Vec<_>>().join(sep),
None => EMPTY.to_string(),
}
}

pub fn with_audience(mut self, value: &'a str) -> Self {
self.audience = Some(value);
self
}

pub fn with_scopes(mut self, value: &'a [&'a str]) -> Self {
self.scopes = Some(value);
self
}

pub fn with_sub(mut self, value: &'a str) -> Self {
self.sub = Some(value);
self
}

pub fn with_use_id_token(mut self, value: bool) -> Self {
self.use_id_token = value;
self
}
}

#[derive(Clone)]
Expand Down
7 changes: 1 addition & 6 deletions kms/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -43,12 +43,7 @@ impl ClientConfig {
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: None,
scopes: Some(&SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default().with_scopes(&SCOPES)
}
}

Expand Down
9 changes: 3 additions & 6 deletions pubsub/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -77,12 +77,9 @@ impl ClientConfig {
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: Some(crate::apiv1::conn_pool::AUDIENCE),
scopes: Some(&crate::apiv1::conn_pool::SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default()
.with_audience(crate::apiv1::conn_pool::AUDIENCE)
.with_scopes(&crate::apiv1::conn_pool::SCOPES)
}
}

Expand Down
9 changes: 3 additions & 6 deletions spanner/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -124,12 +124,9 @@ impl ClientConfig {
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: Some(crate::apiv1::conn_pool::AUDIENCE),
scopes: Some(&crate::apiv1::conn_pool::SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default()
.with_audience(crate::apiv1::conn_pool::AUDIENCE)
.with_scopes(&crate::apiv1::conn_pool::SCOPES)
}
}

Expand Down
11 changes: 5 additions & 6 deletions spanner/tests/change_stream_test.rs
Original file line number Diff line number Diff line change
Expand Up @@ -110,12 +110,11 @@ impl TryFromStruct for DataChangeRecord {
}

async fn create_environment() -> Environment {
let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new(google_cloud_auth::project::Config {
audience: Some(google_cloud_spanner::apiv1::conn_pool::AUDIENCE),
scopes: Some(&google_cloud_spanner::apiv1::conn_pool::SCOPES),
sub: None,
..Default::default()
})
let ts = google_cloud_auth::token::DefaultTokenSourceProvider::new(
google_cloud_auth::project::Config::default()
.with_audience(google_cloud_spanner::apiv1::conn_pool::AUDIENCE)
.with_scopes(&google_cloud_spanner::apiv1::conn_pool::SCOPES),
)
.await
.unwrap();
GoogleCloud(Box::new(ts))
Expand Down
7 changes: 1 addition & 6 deletions storage/src/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -114,12 +114,7 @@ impl ClientConfig {
}

fn auth_config() -> google_cloud_auth::project::Config<'static> {
google_cloud_auth::project::Config {
audience: None,
scopes: Some(&crate::http::storage_client::SCOPES),
sub: None,
..Default::default()
}
google_cloud_auth::project::Config::default().with_scopes(&crate::http::storage_client::SCOPES)
}
}

Expand Down
9 changes: 3 additions & 6 deletions storage/src/http/service_account_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -73,12 +73,9 @@ mod test {
use crate::http::service_account_client::ServiceAccountClient;

async fn client() -> (ServiceAccountClient, String) {
let tsp = DefaultTokenSourceProvider::new(Config {
audience: None,
scopes: Some(&["https://www.googleapis.com/auth/cloud-platform"]),
sub: None,
..Default::default()
})
let tsp = DefaultTokenSourceProvider::new(
Config::default().with_scopes(&["https://www.googleapis.com/auth/cloud-platform"]),
)
.await
.unwrap();
let email = tsp.source_credentials.clone().unwrap().client_email.unwrap();
Expand Down
11 changes: 3 additions & 8 deletions storage/src/http/storage_client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -1447,14 +1447,9 @@ pub(crate) mod test {
}

async fn client() -> (StorageClient, String, String) {
let tsp = DefaultTokenSourceProvider::new(Config {
audience: None,
scopes: Some(&SCOPES),
sub: None,
..Default::default()
})
.await
.unwrap();
let tsp = DefaultTokenSourceProvider::new(Config::default().with_scopes(&SCOPES))
.await
.unwrap();
let cred = tsp.source_credentials.clone();
let ts = tsp.token_source();
let client = StorageClient::new(
Expand Down

0 comments on commit 5fc08f8

Please sign in to comment.