-
Notifications
You must be signed in to change notification settings - Fork 108
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
auth: Support URL type external account source #239
Merged
Merged
Changes from all commits
Commits
Show all changes
5 commits
Select commit
Hold shift + click to select a range
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -4,40 +4,40 @@ version = "0.13.1" | |
authors = ["yoshidan <[email protected]>"] | ||
edition = "2021" | ||
repository = "https://github.com/yoshidan/google-cloud-rust/tree/main/foundation/auth" | ||
keywords = ["gcp","auth","googleapis","google-cloud-rust"] | ||
keywords = ["gcp", "auth", "googleapis", "google-cloud-rust"] | ||
license = "MIT" | ||
readme = "README.md" | ||
description = "Google Cloud Platform server application authentication library." | ||
|
||
[dependencies] | ||
tracing = "0.1" | ||
reqwest = { version="0.11", features = ["json"], default-features = false } | ||
reqwest = { version = "0.11", features = ["json"], default-features = false } | ||
serde = { version = "1.0", features = ["derive"] } | ||
serde_json = { version = "1.0" } | ||
serde_json = { version = "1.0" } | ||
jsonwebtoken = { version = "9.2.0" } | ||
thiserror = "1.0" | ||
async-trait = "0.1" | ||
home = "0.5" | ||
urlencoding = "2.1" | ||
tokio = { version = "1.32", features = ["fs"]} | ||
tokio = { version = "1.32", features = ["fs"] } | ||
google-cloud-metadata = { version = "0.4.0", path = "../metadata" } | ||
google-cloud-token = { version = "0.1.1", path = "../token" } | ||
base64 = "0.21" | ||
time = "0.3" | ||
|
||
url = { version="2.4", optional = true } | ||
path-clean = { version="1.0", optional = true } | ||
sha2 = {version = "0.10", optional = true} | ||
percent-encoding = { version="2.3", optional = true } | ||
url = { version = "2.4", optional = true } | ||
path-clean = { version = "1.0", optional = true } | ||
sha2 = { version = "0.10", optional = true } | ||
percent-encoding = { version = "2.3", optional = true } | ||
hmac = { version = "0.12", optional = true } | ||
hex = { version = "0.4", optional = true } | ||
|
||
[dev-dependencies] | ||
tokio = { version = "1.32", features = ["test-util", "rt-multi-thread", "macros"]} | ||
tracing-subscriber = {version="0.3", features=["env-filter","std"]} | ||
tokio = { version = "1.32", features = ["test-util", "rt-multi-thread", "macros"] } | ||
tracing-subscriber = { version = "0.3", features = ["env-filter", "std"] } | ||
ctor = "0.1" | ||
tempfile = "3.8.0" | ||
temp-env = {version="0.3.6", features = ["async_closure",]} | ||
temp-env = { version = "0.3.6", features = ["async_closure"] } | ||
|
||
[features] | ||
default = ["default-tls"] | ||
|
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
65 changes: 65 additions & 0 deletions
65
foundation/auth/src/token_source/external_account_source/url_subject_token_source.rs
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,65 @@ | ||
use async_trait::async_trait; | ||
use serde_json::Value; | ||
use std::collections::HashMap; | ||
use url::Url; | ||
|
||
use crate::credentials::{CredentialSource, Format}; | ||
use crate::token_source::default_http_client; | ||
use crate::token_source::external_account_source::error::Error; | ||
use crate::token_source::external_account_source::subject_token_source::SubjectTokenSource; | ||
|
||
pub struct UrlSubjectTokenSource { | ||
url: Url, | ||
headers: HashMap<String, String>, | ||
format: Format, | ||
} | ||
|
||
impl UrlSubjectTokenSource { | ||
pub async fn new(value: CredentialSource) -> Result<Self, Error> { | ||
let url = value.url.ok_or(Error::MissingTokenURL)?; | ||
let url = Url::parse(&url).map_err(Error::URLError)?; | ||
let headers = value.headers.ok_or(Error::MissingHeaders)?; | ||
let format = value.format.ok_or(Error::MissingFormat)?; | ||
|
||
Ok(Self { url, headers, format }) | ||
} | ||
|
||
async fn create_subject_token(&self) -> Result<String, Error> { | ||
let client = default_http_client(); | ||
let mut request = client.get(self.url.clone()); | ||
|
||
for (key, val) in &self.headers { | ||
request = request.header(key, val); | ||
} | ||
|
||
let response = request.send().await.map_err(Error::HttpError)?; | ||
|
||
if !response.status().is_success() { | ||
return Err(Error::UnexpectedStatusOnGetSessionToken(response.status().as_u16())); | ||
} | ||
|
||
let body = response.text_with_charset("utf-8").await?; | ||
let body = body.chars().take(1 << 20).collect::<String>(); // Limiting the response body to 1MB | ||
|
||
let format_type = self.format.tp.as_str(); | ||
match format_type { | ||
"json" => { | ||
let data: Value = serde_json::from_str(&body).map_err(Error::JsonError)?; | ||
if let Some(token) = data[&self.format.subject_token_field_name].as_str() { | ||
Ok(token.to_string()) | ||
} else { | ||
Err(Error::MissingSubjectTokenFieldName) | ||
} | ||
} | ||
"text" | "" => Ok(body), | ||
_ => Err(Error::UnsupportedFormatType), | ||
} | ||
} | ||
} | ||
|
||
#[async_trait] | ||
impl SubjectTokenSource for UrlSubjectTokenSource { | ||
async fn subject_token(&self) -> Result<String, Error> { | ||
self.create_subject_token().await | ||
} | ||
} |
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Is there any particular reason for changing from
impl SubjectTokenSource
toBox<dyn SubjectTokenSource>
?There seems to be no need for the change since the function return is limited to
AWSSubjectTokenSource and UrlSubjectTokenSource
.(I was looking at this PR and noticed that I can remove the
async_trait
of theSubjectTokenSource
in rust 1.75. Thank you very much.)There was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
@yoshidan
impl SubjectTokenSource
can return eitherAWSSubjectTokenSource
orUrlSubjectTokenSource
. However, if the type returned changes depending on the CredentialSource within the same function, it becomes impossible to specify the type at compile time, so dynamic dispatch may be required(I might be misunderstanding something 🙏 )
Actually, the following error occurs when I specify
impl SubjectTokenSource
at the return positionThere was a problem hiding this comment.
Choose a reason for hiding this comment
The reason will be displayed to describe this comment to others. Learn more.
Thanks. you are right.