Skip to content
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
2 changes: 2 additions & 0 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 2 additions & 0 deletions Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,8 @@ aws-sdk-s3 = { version = "1.134", default-features = false, features = ["default
aws-sdk-sts = { version = "1.105", default-features = false, features = ["default-https-client", "rt-tokio"] }
aws-types = "1.5"
aws-config = { version = "=1.13.0", default-features = false, features = ["behavior-version-latest", "default-https-client", "rt-tokio", "credentials-process", "sso"] }
aws-smithy-http-client = { version = "1.5", default-features = false, features = ["default-client", "rustls-aws-lc", "rt-tokio"] }
aws-smithy-runtime-api = "1"
tokio = { version = "1", features = [
"rt-multi-thread",
"sync",
Expand Down
3 changes: 2 additions & 1 deletion docs/examples/client/client_proxy.py
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
from pydynox import DynamoDBClient

# Use an HTTP/HTTPS proxy
# Use an HTTP/HTTPS proxy (configured on this client's HTTP transport only —
# it does not mutate process-wide HTTPS_PROXY / HTTP_PROXY).
client = DynamoDBClient(
proxy_url="http://proxy.example.com:8080",
)
Expand Down
2 changes: 1 addition & 1 deletion docs/guides/client.md
Original file line number Diff line number Diff line change
Expand Up @@ -168,7 +168,7 @@ See [rate limiting](rate-limiting.md) for details.
| `connect_timeout` | float | Connection timeout (seconds) |
| `read_timeout` | float | Read timeout (seconds) |
| `max_retries` | int | Max retry attempts |
| `proxy_url` | str | HTTP/HTTPS proxy URL |
| `proxy_url` | str | HTTP/HTTPS proxy URL (per-client transport; does **not** set process `HTTPS_PROXY`) |
| `rate_limit` | FixedRate/AdaptiveRate | Rate limiter |

## Methods
Expand Down
6 changes: 0 additions & 6 deletions src/client/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -173,12 +173,6 @@ impl DynamoDBClient {
max_retries: Option<u32>,
proxy_url: Option<String>,
) -> PyResult<Self> {
// Set proxy env var if provided (AWS SDK reads from env)
if let Some(ref proxy) = proxy_url {
// SAFETY: called during client construction, before any async work starts
unsafe { std::env::set_var("HTTPS_PROXY", proxy) };
}

let config = AwsConfig {
region: region.clone(),
access_key,
Expand Down
55 changes: 43 additions & 12 deletions src/client_internal/auth.rs
Original file line number Diff line number Diff line change
Expand Up @@ -22,9 +22,11 @@
//! Just use `DynamoDBClient()` without any parameters.

use aws_config::profile::ProfileFileCredentialsProvider;
use aws_config::provider_config::ProviderConfig;
use aws_config::sts::AssumeRoleProvider;
use aws_config::{BehaviorVersion, SdkConfig};
use aws_config::{BehaviorVersion, Region, SdkConfig};
use aws_sdk_dynamodb::config::Credentials;
use aws_smithy_runtime_api::client::http::SharedHttpClient;

use super::config::AwsConfig;

Expand All @@ -48,7 +50,11 @@ pub enum CredentialProvider {
/// credentials are used to call STS.
/// 3. AWS profile (supports SSO profiles)
/// 4. Default chain (env vars, instance profile, container, EKS IRSA, GitHub OIDC, etc.)
pub async fn build_credential_provider(config: &AwsConfig) -> CredentialProvider {
pub async fn build_credential_provider(
config: &AwsConfig,
http_client: Option<SharedHttpClient>,
region: Option<Region>,
) -> CredentialProvider {
// 1. Static credentials
if let (Some(ak), Some(sk)) = (&config.access_key, &config.secret_key) {
let creds = Credentials::new(
Expand All @@ -63,7 +69,8 @@ pub async fn build_credential_provider(config: &AwsConfig) -> CredentialProvider

// 2. AssumeRole (cross-account)
if let Some(role) = &config.role_arn {
let base_config = assume_role_source_config(config.profile.as_deref()).await;
let base_config =
assume_role_source_config(config.profile.as_deref(), http_client, region).await;
let session_name = config
.role_session_name
.as_deref()
Expand All @@ -82,9 +89,7 @@ pub async fn build_credential_provider(config: &AwsConfig) -> CredentialProvider

// 3. Profile-based credentials (supports SSO profiles)
if let Some(profile_name) = &config.profile {
let provider = ProfileFileCredentialsProvider::builder()
.profile_name(profile_name)
.build();
let provider = profile_credentials_provider(profile_name, http_client, region);
return CredentialProvider::Profile(provider);
}

Expand All @@ -96,14 +101,40 @@ pub async fn build_credential_provider(config: &AwsConfig) -> CredentialProvider

/// Config used to call STS for AssumeRole. A named profile, when given, is the
/// source identity instead of the default chain.
pub(crate) async fn assume_role_source_config(profile: Option<&str>) -> SdkConfig {
pub(crate) async fn assume_role_source_config(
profile: Option<&str>,
http_client: Option<SharedHttpClient>,
region: Option<Region>,
) -> SdkConfig {
let mut loader = aws_config::defaults(BehaviorVersion::latest());
if let Some(ref http_client) = http_client {
loader = loader
.http_client(http_client.clone())
.region(region.clone());
}
if let Some(profile_name) = profile {
loader = loader.credentials_provider(
ProfileFileCredentialsProvider::builder()
.profile_name(profile_name)
.build(),
);
loader = loader.credentials_provider(profile_credentials_provider(
profile_name,
http_client,
region,
));
}
loader.load().await
}

/// Keep profile STS and SSO requests on the client's HTTP transport.
fn profile_credentials_provider(
profile_name: &str,
http_client: Option<SharedHttpClient>,
region: Option<Region>,
) -> ProfileFileCredentialsProvider {
let mut builder = ProfileFileCredentialsProvider::builder().profile_name(profile_name);
if let Some(http_client) = http_client {
builder = builder.configure(
&ProviderConfig::default()
.with_http_client(http_client)
.with_region(region),
);
}
builder.build()
}
55 changes: 48 additions & 7 deletions src/client_internal/builder.rs
Original file line number Diff line number Diff line change
Expand Up @@ -11,17 +11,47 @@ use aws_config::timeout::TimeoutConfig;
use aws_sdk_dynamodb::Client as DynamoDbClient;
use aws_sdk_kms::Client as KmsClient;
use aws_sdk_s3::Client as S3Client;
use aws_smithy_http_client::proxy::ProxyConfig;
use aws_smithy_http_client::{Builder as HttpClientBuilder, Connector, tls};
use aws_smithy_runtime_api::client::http::SharedHttpClient;

use super::auth::{CredentialProvider, build_credential_provider};
use super::config::AwsConfig;

/// Build a per-client HTTP stack that routes through `proxy_url` without
/// mutating process-wide `HTTPS_PROXY` / `HTTP_PROXY`.
fn shared_http_client_for_proxy(proxy_url: &str) -> Result<SharedHttpClient, String> {
// AWS APIs are HTTPS; prefer HTTPS-proxy config, then fall back to HTTP/all.
let proxy_config = ProxyConfig::https(proxy_url)
.or_else(|_| ProxyConfig::http(proxy_url))
.or_else(|_| ProxyConfig::all(proxy_url))
.map_err(|err| format!("invalid proxy_url '{proxy_url}': {err}"))?;

Ok(HttpClientBuilder::new().build_with_connector_fn({
let proxy_config = proxy_config.clone();
move |settings, components| {
let mut builder = Connector::builder();
builder.set_connector_settings(settings.cloned());
if let Some(components) = components {
builder.set_sleep_impl(components.sleep_impl());
}
builder
.proxy_config(proxy_config.clone())
.tls_provider(tls::Provider::Rustls(
tls::rustls_provider::CryptoMode::AwsLc,
))
.build()
}
}))
}

/// Build the base AWS SDK config from AwsConfig.
///
/// This is shared by all client builders to avoid duplication.
async fn build_sdk_config(
config: &AwsConfig,
region_override: Option<String>,
) -> aws_config::SdkConfig {
) -> Result<aws_config::SdkConfig, String> {
// Use override region if provided, otherwise use config region
let effective_region = region_override.or_else(|| config.region.clone());

Expand All @@ -30,7 +60,8 @@ async fn build_sdk_config(
.or_default_provider()
.or_else("us-east-1");

let mut config_loader = aws_config::defaults(BehaviorVersion::latest()).region(region_provider);
let region = region_provider.region().await;
let mut config_loader = aws_config::defaults(BehaviorVersion::latest()).region(region.clone());

// Configure timeouts
if config.connect_timeout.is_some() || config.read_timeout.is_some() {
Expand All @@ -50,8 +81,18 @@ async fn build_sdk_config(
config_loader = config_loader.retry_config(retry_config);
}

// Per-client proxy on the HTTP transport (do not touch process env).
let http_client = config
.proxy_url
.as_deref()
.map(shared_http_client_for_proxy)
.transpose()?;
if let Some(ref http_client) = http_client {
config_loader = config_loader.http_client(http_client.clone());
}

// Configure credentials
let cred_provider = build_credential_provider(config).await;
let cred_provider = build_credential_provider(config, http_client, region).await;
match cred_provider {
CredentialProvider::Static(creds) => {
config_loader = config_loader.credentials_provider(creds);
Expand All @@ -67,12 +108,12 @@ async fn build_sdk_config(
}
}

config_loader.load().await
Ok(config_loader.load().await)
}

/// Build DynamoDB client from shared config.
pub async fn build_dynamodb_client(config: &AwsConfig) -> Result<DynamoDbClient, String> {
let sdk_config = build_sdk_config(config, None).await;
let sdk_config = build_sdk_config(config, None).await?;
let mut dynamo_config = aws_sdk_dynamodb::config::Builder::from(&sdk_config);

if let Some(url) = config.dynamodb_endpoint() {
Expand All @@ -89,7 +130,7 @@ pub async fn build_s3_client(
config: &AwsConfig,
region_override: Option<String>,
) -> Result<S3Client, String> {
let sdk_config = build_sdk_config(config, region_override).await;
let sdk_config = build_sdk_config(config, region_override).await?;
let mut s3_config = aws_sdk_s3::config::Builder::from(&sdk_config);

if let Some(url) = config.s3_endpoint() {
Expand All @@ -106,7 +147,7 @@ pub async fn build_kms_client(
config: &AwsConfig,
region_override: Option<String>,
) -> Result<KmsClient, String> {
let sdk_config = build_sdk_config(config, region_override).await;
let sdk_config = build_sdk_config(config, region_override).await?;
let mut kms_config = aws_sdk_kms::config::Builder::from(&sdk_config);

if let Some(url) = config.kms_endpoint() {
Expand Down
4 changes: 2 additions & 2 deletions src/client_internal/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -35,8 +35,8 @@ pub struct AwsConfig {
// Retries
pub max_retries: Option<u32>,

// Proxy (sets HTTPS_PROXY env var, stored for reference)
#[allow(dead_code)]
// Per-client HTTP proxy (applied to the SDK HTTP transport; never mutates
// process-wide HTTPS_PROXY / HTTP_PROXY environment variables).
pub proxy_url: Option<String>,
}

Expand Down
6 changes: 0 additions & 6 deletions src/kms/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -70,12 +70,6 @@ impl KmsEncryptor {
max_retries: Option<u32>,
proxy_url: Option<String>,
) -> PyResult<Self> {
// Set proxy env var if provided
if let Some(ref proxy) = proxy_url {
// SAFETY: called during client construction, before any async work starts
unsafe { std::env::set_var("HTTPS_PROXY", proxy) };
}

let config = AwsConfig {
region,
access_key,
Expand Down
6 changes: 0 additions & 6 deletions src/s3/client.rs
Original file line number Diff line number Diff line change
Expand Up @@ -63,12 +63,6 @@ impl S3Client {
max_retries: Option<u32>,
proxy_url: Option<String>,
) -> PyResult<Self> {
// Set proxy env var if provided
if let Some(ref proxy) = proxy_url {
// SAFETY: called during client construction, before any async work starts
unsafe { std::env::set_var("HTTPS_PROXY", proxy) };
}

let config = AwsConfig {
region,
access_key,
Expand Down
Loading
Loading