-
Notifications
You must be signed in to change notification settings - Fork 845
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
Object Store (AWS): Support region configured via named profile #4161
Merged
Merged
Changes from 6 commits
Commits
Show all changes
10 commits
Select commit
Hold shift + click to select a range
a067ee6
feat(aws_profile): use profile region as fallback
mr-brobot d99f12c
fix(aws_profile): clippy & RAT errors
mr-brobot 7d414a5
fix(aws_profile): make RegionProvider async
mr-brobot ac27f32
test(aws_profile): use fake config for testing
mr-brobot 20236b5
refactor(aws_profile): remove unnecessary module
mr-brobot 075e0ab
refactor(aws_profile): tests w/ profile files
mr-brobot 61f881c
Merge remote-tracking branch 'upstream/master' into aws_profile_region
mr-brobot 599c5d6
fix(object_store): rat + clippy warnings
mr-brobot 5239543
Merge remote-tracking branch 'upstream/master' into aws_profile_region
tustvold b071014
Don't spawn thread
tustvold 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
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,111 @@ | ||
#![cfg(feature = "aws_profile")] | ||
|
||
use aws_config::meta::region::ProvideRegion; | ||
use aws_config::profile::profile_file::ProfileFiles; | ||
use aws_config::profile::ProfileFileCredentialsProvider; | ||
use aws_config::profile::ProfileFileRegionProvider; | ||
use aws_config::provider_config::ProviderConfig; | ||
use aws_credential_types::provider::ProvideCredentials; | ||
use aws_types::region::Region; | ||
use futures::future::BoxFuture; | ||
use std::sync::Arc; | ||
use std::time::Instant; | ||
use std::time::SystemTime; | ||
|
||
use crate::aws::credential::CredentialProvider; | ||
use crate::aws::AwsCredential; | ||
use crate::client::token::{TemporaryToken, TokenCache}; | ||
use crate::Result; | ||
|
||
#[cfg(test)] | ||
pub static TEST_PROFILE_NAME: &str = "object_store:fake_profile"; | ||
|
||
#[cfg(test)] | ||
pub static TEST_PROFILE_REGION: &str = "object_store:fake_region_from_profile"; | ||
|
||
#[derive(Debug)] | ||
pub struct ProfileProvider { | ||
name: String, | ||
region: Option<String>, | ||
cache: TokenCache<Arc<AwsCredential>>, | ||
} | ||
|
||
impl ProfileProvider { | ||
pub fn new(name: String, region: Option<String>) -> Self { | ||
Self { | ||
name, | ||
region, | ||
cache: Default::default(), | ||
} | ||
} | ||
|
||
#[cfg(test)] | ||
fn profile_files(&self) -> ProfileFiles { | ||
use aws_config::profile::profile_file::ProfileFileKind; | ||
|
||
let config = format!( | ||
"[profile {}]\nregion = {}", | ||
TEST_PROFILE_NAME, TEST_PROFILE_REGION | ||
); | ||
|
||
ProfileFiles::builder() | ||
.with_contents(ProfileFileKind::Config, config) | ||
.build() | ||
} | ||
|
||
#[cfg(not(test))] | ||
fn profile_files(&self) -> ProfileFiles { | ||
ProfileFiles::default() | ||
} | ||
|
||
pub async fn get_region(&self) -> Option<String> { | ||
if let Some(region) = self.region.clone() { | ||
return Some(region); | ||
} | ||
|
||
let provider = ProfileFileRegionProvider::builder() | ||
.profile_files(self.profile_files()) | ||
.profile_name(&self.name) | ||
.build(); | ||
|
||
let region = provider.region().await; | ||
|
||
region.map(|r| r.as_ref().to_owned()) | ||
} | ||
} | ||
|
||
impl CredentialProvider for ProfileProvider { | ||
fn get_credential(&self) -> BoxFuture<'_, Result<Arc<AwsCredential>>> { | ||
Box::pin(self.cache.get_or_insert_with(move || async move { | ||
let region = self.region.clone().map(Region::new); | ||
|
||
let config = ProviderConfig::default().with_region(region); | ||
|
||
let credentials = ProfileFileCredentialsProvider::builder() | ||
.configure(&config) | ||
.profile_name(&self.name) | ||
.build(); | ||
|
||
let c = credentials.provide_credentials().await.map_err(|source| { | ||
crate::Error::Generic { | ||
store: "S3", | ||
source: Box::new(source), | ||
} | ||
})?; | ||
let t_now = SystemTime::now(); | ||
let expiry = c | ||
.expiry() | ||
.and_then(|e| e.duration_since(t_now).ok()) | ||
.map(|ttl| Instant::now() + ttl); | ||
|
||
Ok(TemporaryToken { | ||
token: Arc::new(AwsCredential { | ||
key_id: c.access_key_id().to_string(), | ||
secret_key: c.secret_access_key().to_string(), | ||
token: c.session_token().map(ToString::to_string), | ||
}), | ||
expiry, | ||
}) | ||
})) | ||
} | ||
} |
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.
Both are equally "problematic" in that they pin a tokio worker, but this at least avoids spawning an additional thread