use aws_config::BehaviorVersion;
use crate::config::RuntimeConfig;
use crate::config::{Builder, Config, S3ClientConfig};
use crate::config::{ConcurrencyMode, MemoryConfig, PartSize, ReadAhead};
#[derive(Default, Debug)]
pub struct ConfigLoader {
builder: Builder,
s3_config: Option<S3ClientConfig>,
}
impl ConfigLoader {
pub fn multipart_threshold(mut self, threshold: PartSize) -> Self {
self.builder = self.builder.multipart_threshold(threshold);
self
}
pub fn part_size(mut self, part_size: PartSize) -> Self {
self.builder = self.builder.part_size(part_size);
self
}
pub fn concurrency(mut self, mode: ConcurrencyMode) -> Self {
self.builder = self.builder.concurrency(mode);
self
}
pub fn runtime_config(mut self, config: RuntimeConfig) -> Self {
self.builder = self.builder.runtime_config(config);
self
}
pub fn read_ahead(mut self, mode: ReadAhead) -> Self {
self.builder = self.builder.read_ahead(mode);
self
}
pub fn memory(mut self, memory: MemoryConfig) -> Self {
self.builder = self.builder.memory(memory);
self
}
pub fn s3_config(mut self, config: impl Into<S3ClientConfig>) -> Self {
self.s3_config = Some(config.into());
self
}
pub async fn load(self) -> Config {
let s3_config = match self.s3_config {
Some(config) => config,
None => {
S3ClientConfig::new(&aws_config::defaults(BehaviorVersion::latest()).load().await)
}
};
let profile = detect_machine_profile().await;
self.builder
.machine_profile(Some(profile))
.s3_config(s3_config)
.build()
.expect("the loader always sets s3_config, the only required field")
}
}
async fn detect_machine_profile() -> crate::runtime::platform::MachineProfile {
use crate::runtime::platform::{self, DmiDetection};
let instance_type = match platform::detect_instance_type_dmi() {
DmiDetection::Instance(ty) => Some(ty),
DmiDetection::NotEc2 => None,
DmiDetection::Unknown => imds_instance_type().await,
};
platform::MachineProfile {
instance_type,
vcpus: platform::local_vcpus(),
ram_bytes: platform::available_ram(),
}
}
fn imds_disabled() -> bool {
imds_disabled_value(std::env::var("AWS_EC2_METADATA_DISABLED").ok().as_deref())
}
fn imds_disabled_value(raw: Option<&str>) -> bool {
matches!(
raw.map(|v| v.trim().to_ascii_lowercase()).as_deref(),
Some("true" | "1" | "yes" | "on")
)
}
async fn imds_instance_type() -> Option<String> {
if imds_disabled() {
tracing::debug!(
target: crate::telemetry::TARGET_CONCURRENCY,
"IMDS disabled via AWS_EC2_METADATA_DISABLED; using vCPU-scaled fallback",
);
return None;
}
let client = aws_config::imds::Client::builder()
.max_attempts(1)
.connect_timeout(std::time::Duration::from_millis(500))
.read_timeout(std::time::Duration::from_millis(500))
.build();
match client.get("/latest/meta-data/instance-type").await {
Ok(ty) => {
let ty = ty.as_ref().trim().to_string();
(!ty.is_empty()).then_some(ty)
}
Err(err) => {
tracing::debug!(
target: crate::telemetry::TARGET_CONCURRENCY,
error = %err,
"IMDS instance-type lookup failed; using vCPU-scaled fallback",
);
None
}
}
}
#[cfg(test)]
mod tests {
use crate::config::PartSize;
use crate::types::ByteSize;
use aws_config::{BehaviorVersion, Region};
use aws_sdk_s3::config::{Credentials, Intercept, SharedCredentialsProvider};
use aws_smithy_http_client::test_util::capture_request;
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn load_with_interceptor() {
let config = crate::from_env()
.part_size(PartSize::Target(ByteSize::new(8)))
.load()
.await;
let tm_client = crate::Client::new(config);
let sdk_s3_config = tm_client.handle.s3_client.config();
let tm_interceptor_exists = sdk_s3_config
.interceptors()
.any(|item| item.name() == "S3TransferManager");
assert!(tm_interceptor_exists);
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn load_populates_machine_profile() {
let config = crate::from_env().load().await;
let profile = config
.machine_profile()
.expect("loader populates a machine profile");
assert!(profile.vcpus >= 1);
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn load_uses_supplied_s3_config() {
let (http_client, captured_request) = capture_request(None);
let sdk_config = aws_types::SdkConfig::builder()
.http_client(http_client)
.region(Region::from_static("ap-southeast-4"))
.credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
.behavior_version(BehaviorVersion::latest())
.build();
let config = crate::from_env()
.s3_config(&sdk_config)
.runtime_config(crate::config::RuntimeConfig::multi_thread_tokio())
.load()
.await;
assert!(
config.machine_profile().is_some(),
"machine detection runs with a supplied configuration"
);
let tm = crate::Client::new(config);
let mut handle = tm.download().bucket("foo").key("bar").initiate().unwrap();
let _ = handle.body_mut().next().await;
let uri = captured_request.expect_request().uri().to_string();
assert!(
uri.contains("ap-southeast-4"),
"request did not use the supplied region: {uri}"
);
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn supplied_s3_config_selects_managed_transport_unless_it_has_http() {
use aws_sdk_s3::config::IntoShared;
let sdk_config = |http_client: Option<_>| {
let mut builder = aws_types::SdkConfig::builder()
.region(Region::from_static("us-west-2"))
.credentials_provider(SharedCredentialsProvider::new(Credentials::for_tests()))
.behavior_version(BehaviorVersion::latest());
builder.set_http_client(http_client);
builder.build()
};
let config = crate::from_env().s3_config(&sdk_config(None)).load().await;
assert!(
config.runtime_http(64).is_some(),
"no HTTP client supplied: the managed transport is installed"
);
let (http_client, _captured) = capture_request(None);
let config = crate::from_env()
.s3_config(&sdk_config(Some(http_client.into_shared())))
.load()
.await;
assert!(
config.runtime_http(64).is_none(),
"an HTTP client is supplied: the managed transport is not installed"
);
}
#[test]
fn imds_disabled_parses_flag() {
use super::imds_disabled_value;
for truthy in ["true", "TRUE", " true ", "1", "yes", "YES", "on"] {
assert!(imds_disabled_value(Some(truthy)), "{truthy:?} disables");
}
for falsy in ["false", "0", "no", "off", "", " ", "enabled", "2"] {
assert!(!imds_disabled_value(Some(falsy)), "{falsy:?} stays enabled");
}
assert!(!imds_disabled_value(None), "unset defaults to enabled");
}
#[cfg_attr(miri, ignore)]
#[tokio::test]
async fn load_path_emits_attribution() {
let (http_client, captured_request) = capture_request(None);
let config = crate::from_env()
.part_size(PartSize::Target(ByteSize::new(8)))
.load()
.await;
let tm_client = crate::Client::new(config);
let sdk_s3_config = tm_client
.handle
.s3_client
.config()
.to_builder()
.http_client(http_client)
.region(Region::from_static("us-west-2"))
.with_test_defaults()
.build();
let capture_request_config = crate::Config::builder()
.sdk_client(aws_sdk_s3::Client::from_conf(sdk_s3_config))
.part_size(PartSize::Target(ByteSize::new(8)))
.build()
.expect("valid config");
let transfer_manager = crate::Client::new(capture_request_config);
let mut handle = transfer_manager
.download()
.bucket("foo")
.key("bar")
.initiate()
.unwrap();
let _ = handle.body_mut().next().await;
let expected_req = captured_request.expect_request();
let user_agent = expected_req.headers().get("x-amz-user-agent").unwrap();
let ours = concat!("md/rust-tm#", env!("CARGO_PKG_VERSION"));
assert!(
user_agent.contains(ours),
"loader path carries no attribution: {user_agent:?}"
);
}
}