Skip to content
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

Reload token from AWS_WEB_IDENTITY_TOKEN_FILE #3274

Merged
merged 3 commits into from
Dec 5, 2022
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
11 changes: 7 additions & 4 deletions object_store/src/aws/credential.rs
Original file line number Diff line number Diff line change
Expand Up @@ -341,7 +341,7 @@ impl CredentialProvider for InstanceCredentialProvider {
#[derive(Debug)]
pub struct WebIdentityProvider {
pub cache: TokenCache<Arc<AwsCredential>>,
pub token: String,
pub token_path: String,
pub role_arn: String,
pub session_name: String,
pub endpoint: String,
Expand All @@ -355,7 +355,7 @@ impl CredentialProvider for WebIdentityProvider {
web_identity(
&self.client,
&self.retry_config,
&self.token,
&self.token_path,
&self.role_arn,
&self.session_name,
&self.endpoint,
Expand Down Expand Up @@ -477,11 +477,14 @@ impl From<AssumeRoleCredentials> for AwsCredential {
async fn web_identity(
client: &Client,
retry_config: &RetryConfig,
token: &str,
token_path: &str,
role_arn: &str,
session_name: &str,
endpoint: &str,
) -> Result<TemporaryToken<Arc<AwsCredential>>, StdError> {
let token = std::fs::read_to_string(token_path)
.map_err(|e| format!("Failed to read token file '{}': {}", token_path, e))?;

let bytes = client
.request(Method::POST, endpoint)
.query(&[
Expand All @@ -490,7 +493,7 @@ async fn web_identity(
("RoleArn", role_arn),
("RoleSessionName", session_name),
("Version", "2011-06-15"),
("WebIdentityToken", token),
("WebIdentityToken", &token),
])
.send_retry(retry_config)
.await?
Expand Down
11 changes: 3 additions & 8 deletions object_store/src/aws/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -116,9 +116,6 @@ enum Error {

#[snafu(display("Received header containing non-ASCII data"))]
BadHeader { source: reqwest::header::ToStrError },

#[snafu(display("Error reading token file: {}", source))]
ReadTokenFile { source: std::io::Error },
}

impl From<Error> for super::Error {
Expand Down Expand Up @@ -588,13 +585,11 @@ impl AmazonS3Builder {
(Some(_), None, _) => return Err(Error::MissingSecretAccessKey.into()),
// TODO: Replace with `AmazonS3Builder::credentials_from_env`
_ => match (
std::env::var_os("AWS_WEB_IDENTITY_TOKEN_FILE"),
std::env::var("AWS_WEB_IDENTITY_TOKEN_FILE"),
std::env::var("AWS_ROLE_ARN"),
) {
(Some(token_file), Ok(role_arn)) => {
(Ok(token_path), Ok(role_arn)) => {
info!("Using WebIdentity credential provider");
let token = std::fs::read_to_string(token_file)
.context(ReadTokenFileSnafu)?;

let session_name = std::env::var("AWS_ROLE_SESSION_NAME")
.unwrap_or_else(|_| "WebIdentitySession".to_string());
Expand All @@ -610,7 +605,7 @@ impl AmazonS3Builder {

Box::new(WebIdentityProvider {
cache: Default::default(),
token,
token_path,
session_name,
role_arn,
endpoint,
Expand Down