From ed4200ea5cf57a00084a1a4eecd4660a20403e16 Mon Sep 17 00:00:00 2001 From: Fuyang Liu Date: Sun, 9 Oct 2022 12:36:40 +0200 Subject: [PATCH 1/2] feat: cache token for GCloudAuthorizedUser --- src/authentication_manager.rs | 2 +- src/gcloud_authorized_user.rs | 29 +++++++++++++++++++---------- 2 files changed, 20 insertions(+), 11 deletions(-) diff --git a/src/authentication_manager.rs b/src/authentication_manager.rs index 647d55d..b3e72b3 100644 --- a/src/authentication_manager.rs +++ b/src/authentication_manager.rs @@ -47,7 +47,7 @@ impl AuthenticationManager { } let client = types::client(); - let gcloud_error = match GCloudAuthorizedUser::new() { + let gcloud_error = match GCloudAuthorizedUser::new().await { Ok(service_account) => { tracing::debug!("Using GCloudAuthorizedUser"); return Ok(Self::build(client, service_account)); diff --git a/src/gcloud_authorized_user.rs b/src/gcloud_authorized_user.rs index a0a541b..5e9a910 100644 --- a/src/gcloud_authorized_user.rs +++ b/src/gcloud_authorized_user.rs @@ -1,5 +1,6 @@ use std::path::{Path, PathBuf}; use std::process::Command; +use std::sync::RwLock; use async_trait::async_trait; use which::which; @@ -14,18 +15,24 @@ use crate::Token; pub(crate) struct GCloudAuthorizedUser { gcloud: PathBuf, project_id: Option, + token: RwLock, } impl GCloudAuthorizedUser { - pub(crate) fn new() -> Result { + pub(crate) async fn new() -> Result { let gcloud = which("gcloud").map_err(|_| GCloudNotFound)?; let project_id = run(&gcloud, &["config", "get-value", "project"]).ok(); - Ok(Self { gcloud, project_id }) + let token = RwLock::new(Self::get_token_via_gcloud_cmd(&gcloud)?); + Ok(Self { + gcloud, + project_id, + token, + }) } - fn token(&self) -> Result { + fn get_token_via_gcloud_cmd(gcloud: &Path) -> Result { Ok(Token::from_string(run( - &self.gcloud, + gcloud, &["auth", "print-access-token", "--quiet"], )?)) } @@ -38,11 +45,13 @@ impl ServiceAccount for GCloudAuthorizedUser { } fn get_token(&self, _scopes: &[&str]) -> Option { - None + Some(self.token.read().unwrap().clone()) } async fn refresh_token(&self, _client: &HyperClient, _scopes: &[&str]) -> Result { - self.token() + let token = Self::get_token_via_gcloud_cmd(&self.gcloud)?; + *self.token.write().unwrap() = token.clone(); + Ok(token) } } @@ -66,11 +75,11 @@ fn run(gcloud: &Path, cmd: &[&str]) -> Result { mod tests { use super::*; - #[test] + #[tokio::test] #[ignore] - fn gcloud() { - let gcloud = GCloudAuthorizedUser::new().unwrap(); + async fn gcloud() { + let gcloud = GCloudAuthorizedUser::new().await.unwrap(); println!("{:?}", gcloud.project_id); - println!("{:?}", gcloud.token()); + println!("{:?}", gcloud.get_token(&[""])); } } From a00dcf3af8b0a6ee3f20bed529c88d6aafa290f0 Mon Sep 17 00:00:00 2001 From: Peter Hrvola Date: Mon, 10 Oct 2022 18:43:34 +0000 Subject: [PATCH 2/2] rename function back to token --- src/gcloud_authorized_user.rs | 6 +++--- 1 file changed, 3 insertions(+), 3 deletions(-) diff --git a/src/gcloud_authorized_user.rs b/src/gcloud_authorized_user.rs index 5e9a910..7446d64 100644 --- a/src/gcloud_authorized_user.rs +++ b/src/gcloud_authorized_user.rs @@ -22,7 +22,7 @@ impl GCloudAuthorizedUser { pub(crate) async fn new() -> Result { let gcloud = which("gcloud").map_err(|_| GCloudNotFound)?; let project_id = run(&gcloud, &["config", "get-value", "project"]).ok(); - let token = RwLock::new(Self::get_token_via_gcloud_cmd(&gcloud)?); + let token = RwLock::new(Self::token(&gcloud)?); Ok(Self { gcloud, project_id, @@ -30,7 +30,7 @@ impl GCloudAuthorizedUser { }) } - fn get_token_via_gcloud_cmd(gcloud: &Path) -> Result { + fn token(gcloud: &Path) -> Result { Ok(Token::from_string(run( gcloud, &["auth", "print-access-token", "--quiet"], @@ -49,7 +49,7 @@ impl ServiceAccount for GCloudAuthorizedUser { } async fn refresh_token(&self, _client: &HyperClient, _scopes: &[&str]) -> Result { - let token = Self::get_token_via_gcloud_cmd(&self.gcloud)?; + let token = Self::token(&self.gcloud)?; *self.token.write().unwrap() = token.clone(); Ok(token) }