-
-
Notifications
You must be signed in to change notification settings - Fork 41
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
Merge pull request #31 from thrykol/master
Add option to use gcloud for getting user auth
- Loading branch information
Showing
5 changed files
with
89 additions
and
14 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -0,0 +1,51 @@ | ||
use crate::authentication_manager::ServiceAccount; | ||
use crate::error::Error; | ||
use crate::error::Error::{ | ||
GCloudError, GCloudNotFound, GCloudParseError, NoProjectId, ParsingError, | ||
}; | ||
use crate::types::HyperClient; | ||
use crate::Token; | ||
use async_trait::async_trait; | ||
use serde_json::json; | ||
use std::path::PathBuf; | ||
use std::process::Command; | ||
use which::which; | ||
|
||
#[derive(Debug)] | ||
pub(crate) struct GCloudAuthorizedUser { | ||
gcloud: PathBuf, | ||
} | ||
|
||
impl GCloudAuthorizedUser { | ||
pub(crate) async fn new() -> Result<Self, Error> { | ||
which("gcloud") | ||
.map_err(|_| GCloudNotFound) | ||
.map(|path| Self { gcloud: path }) | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl ServiceAccount for GCloudAuthorizedUser { | ||
async fn project_id(&self, _: &HyperClient) -> Result<String, Error> { | ||
Err(NoProjectId) | ||
} | ||
|
||
fn get_token(&self, _scopes: &[&str]) -> Option<Token> { | ||
None | ||
} | ||
|
||
async fn refresh_token(&self, _client: &HyperClient, _scopes: &[&str]) -> Result<Token, Error> { | ||
let mut command = Command::new(&self.gcloud); | ||
command.args(&["auth", "print-access-token", "--quiet"]); | ||
|
||
match command.output() { | ||
Ok(output) if output.status.success() => String::from_utf8(output.stdout) | ||
.map_err(|_| GCloudParseError) | ||
.and_then(|access_token| { | ||
serde_json::from_value::<Token>(json!({ "access_token": access_token.trim() })) | ||
.map_err(ParsingError) | ||
}), | ||
_ => Err(GCloudError), | ||
} | ||
} | ||
} |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters