Skip to content
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
3 changes: 1 addition & 2 deletions Cargo.toml
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
[package]
name = "gcp_auth"
version = "0.12.0"
version = "0.12.1"
edition = "2021"
rust-version = "1.70"
repository = "https://github.com/hrvolapeter/gcp_auth"
Expand Down Expand Up @@ -35,7 +35,6 @@ tokio = { version = "1.1", features = ["fs", "sync"] }
tracing = "0.1.29"
tracing-futures = "0.2.5"
url = "2"
which = "6.0"

[dev-dependencies]
tokio = { version = "1.1", features = ["macros", "parking_lot", "rt-multi-thread"] }
Expand Down
21 changes: 8 additions & 13 deletions src/gcloud_authorized_user.rs
Original file line number Diff line number Diff line change
@@ -1,20 +1,17 @@
use std::path::{Path, PathBuf};
use std::process::Command;
use std::sync::Arc;
use std::time::Duration;

use async_trait::async_trait;
use tokio::sync::RwLock;
use tracing::{debug, instrument};
use which::which;

use crate::types::Token;
use crate::{Error, TokenProvider};

/// A token provider that queries the `gcloud` CLI for access tokens
#[derive(Debug)]
pub struct GCloudAuthorizedUser {
gcloud: PathBuf,
project_id: Option<Arc<str>>,
token: RwLock<Arc<Token>>,
}
Expand All @@ -23,20 +20,18 @@ impl GCloudAuthorizedUser {
/// Check if `gcloud` is installed and logged in
pub async fn new() -> Result<Self, Error> {
debug!("try to print access token via `gcloud`");
let gcloud = which("gcloud").map_err(|_| Error::Str("`gcloud` binary not found"))?;
let project_id = run(&gcloud, &["config", "get-value", "project"]).ok();
let token = RwLock::new(Self::fetch_token(&gcloud)?);
let token = RwLock::new(Self::fetch_token()?);
let project_id = run(&["config", "get-value", "project"]).ok();
Ok(Self {
gcloud,
project_id: project_id.map(Arc::from),
token,
})
}

#[instrument(level = tracing::Level::DEBUG, skip(gcloud))]
fn fetch_token(gcloud: &Path) -> Result<Arc<Token>, Error> {
#[instrument(level = tracing::Level::DEBUG)]
fn fetch_token() -> Result<Arc<Token>, Error> {
Ok(Arc::new(Token::from_string(
run(gcloud, &["auth", "print-access-token", "--quiet"])?,
run(&["auth", "print-access-token", "--quiet"])?,
DEFAULT_TOKEN_DURATION,
)))
}
Expand All @@ -51,7 +46,7 @@ impl TokenProvider for GCloudAuthorizedUser {
}

let mut locked = self.token.write().await;
let token = Self::fetch_token(&self.gcloud)?;
let token = Self::fetch_token()?;
*locked = token.clone();
Ok(token)
}
Expand All @@ -63,8 +58,8 @@ impl TokenProvider for GCloudAuthorizedUser {
}
}

fn run(gcloud: &Path, cmd: &[&str]) -> Result<String, Error> {
let mut command = Command::new(gcloud);
fn run(cmd: &[&str]) -> Result<String, Error> {
let mut command = Command::new("gcloud");
command.args(cmd);

let mut stdout = match command.output() {
Expand Down