Skip to content
Open
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
6 changes: 6 additions & 0 deletions THREAT_MODEL.md
Original file line number Diff line number Diff line change
Expand Up @@ -280,6 +280,12 @@ select a different identity. This policy belongs to the AWS default provider;
caller-constructed `ProvideCredentialChain` values retain their own error
policy. Disabling the SSO slot prevents it from participating.

A selected AWS role profile resolves only its `source_profile` graph and the
static or SSO credentials configured for that graph. Missing source profiles,
cycles, conflicting `credential_source`, and an SSO source whose slot was
removed fail without selecting a later provider. The STS requests use only the
regional endpoint implied by the selected signing region.

AWS shared configuration and region discovery are separate from credential
acquisition. Loading `SharedConfig` does not execute credential processes or
contact the network. IMDSv2 region fallback requires explicit opt-in and applies
Expand Down
14 changes: 14 additions & 0 deletions services/aws-core/src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -159,6 +159,15 @@ impl SharedConfig {
})
}

/// Read another profile with the same explicit and environment file selection.
///
/// This keeps the region and endpoint overrides of the caller. It does not
/// execute role chains or credential sources.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) async fn load_profile(&self, ctx: &Context, name: &str) -> Result<Profile> {
self.clone().with_profile(name).load(ctx).await
}

/// Resolve region independently of credentials, optionally falling back to IMDSv2.
///
/// Explicit/environment regions skip file reads. A profile region skips metadata.
Expand Down Expand Up @@ -214,6 +223,11 @@ impl Profile {
pub fn endpoint_url(&self) -> Option<&str> {
self.endpoint_url.as_deref()
}
/// Return whether the selected profile section exists in either shared file.
#[cfg(not(target_arch = "wasm32"))]
pub(crate) fn is_empty(&self) -> bool {
self.properties.is_empty()
}
}

pub(crate) fn config_section(profile: &str) -> String {
Expand Down
56 changes: 43 additions & 13 deletions services/aws-core/src/provide_credential/default.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,31 +16,39 @@
// under the License.

use crate::Credential;
#[cfg(not(target_arch = "wasm32"))]
use crate::provide_credential::{
AssumeRoleChainCredentialProvider, ProcessCredentialProvider, SSOCredentialProvider,
};
use crate::provide_credential::{
AssumeRoleWithWebIdentityCredentialProvider, ECSCredentialProvider, EnvCredentialProvider,
IMDSv2CredentialProvider, ProfileCredentialProvider,
};
#[cfg(not(target_arch = "wasm32"))]
use crate::provide_credential::{ProcessCredentialProvider, SSOCredentialProvider};
use reqsign_core::{Context, ProvideCredential, ProvideCredentialChain, Result};

/// DefaultCredentialProvider is a loader that will try to load credential via default chains.
///
/// Resolution order:
///
/// 1. Environment variables
/// 2. Shared config (`~/.aws/config`, `~/.aws/credentials`)
/// 2. Shared config (`~/.aws/config`, `~/.aws/credentials`), including
/// assume-role profile chains backed by static credentials or SSO
/// 3. SSO credentials
/// 4. Web Identity Tokens
/// 5. Process credentials
/// 6. ECS (IAM Roles for Tasks) & Container credentials
/// 7. EC2 IMDSv2
///
/// Once SSO configuration is selected, SSO errors stop resolution to avoid
/// silently switching identities. An absent SSO configuration continues the chain.
/// A selected profile with `role_arn` is resolved through `source_profile`
/// until a supported source is reached, then each role is assumed in order.
/// Role-chain and SSO errors stop resolution to avoid silently switching
/// identities. An absent SSO configuration continues the chain.
#[derive(Debug)]
pub struct DefaultCredentialProvider {
chain: ProvideCredentialChain<Credential>,
env_chain: ProvideCredentialChain<Credential>,
profile_chain: ProvideCredentialChain<Credential>,
#[cfg(not(target_arch = "wasm32"))]
role_chain: Option<AssumeRoleChainCredentialProvider>,
#[cfg(not(target_arch = "wasm32"))]
sso: Option<SSOCredentialProvider>,
fallback: ProvideCredentialChain<Credential>,
Expand All @@ -66,7 +74,10 @@ impl DefaultCredentialProvider {
/// Create with a custom credential chain.
pub fn with_chain(chain: ProvideCredentialChain<Credential>) -> Self {
Self {
chain,
env_chain: chain,
profile_chain: ProvideCredentialChain::new(),
#[cfg(not(target_arch = "wasm32"))]
role_chain: None,
#[cfg(not(target_arch = "wasm32"))]
sso: None,
fallback: ProvideCredentialChain::new(),
Expand All @@ -90,7 +101,7 @@ impl DefaultCredentialProvider {
mut self,
provider: impl ProvideCredential<Credential = Credential> + 'static,
) -> Self {
self.chain = self.chain.push_front(provider);
self.env_chain = self.env_chain.push_front(provider);
self
}
}
Expand Down Expand Up @@ -251,14 +262,19 @@ impl DefaultCredentialProviderBuilder {

/// Build the `DefaultCredentialProvider` with the configured options.
pub fn build(self) -> DefaultCredentialProvider {
let mut chain = ProvideCredentialChain::new();
#[cfg(not(target_arch = "wasm32"))]
let role_chain = self.profile.as_ref().map(|profile| {
AssumeRoleChainCredentialProvider::new(profile.clone(), self.sso.clone())
});

let mut env_chain = ProvideCredentialChain::new();
if let Some(p) = self.env {
chain = chain.push(p);
env_chain = env_chain.push(p);
}

let mut profile_chain = ProvideCredentialChain::new();
if let Some(p) = self.profile {
chain = chain.push(p);
profile_chain = profile_chain.push(p);
}

let mut fallback = ProvideCredentialChain::new();
Expand All @@ -283,7 +299,10 @@ impl DefaultCredentialProviderBuilder {
}

DefaultCredentialProvider {
chain,
env_chain,
profile_chain,
#[cfg(not(target_arch = "wasm32"))]
role_chain,
#[cfg(not(target_arch = "wasm32"))]
sso: self.sso,
fallback,
Expand All @@ -294,7 +313,18 @@ impl ProvideCredential for DefaultCredentialProvider {
type Credential = Credential;

async fn provide_credential(&self, ctx: &Context) -> Result<Option<Self::Credential>> {
if let Some(credential) = self.chain.provide_credential(ctx).await? {
if let Some(credential) = self.env_chain.provide_credential(ctx).await? {
return Ok(Some(credential));
}
// A selected role profile owns its identity. Resolve its graph before
// static profile or independent SSO providers can select another identity.
#[cfg(not(target_arch = "wasm32"))]
if let Some(role_chain) = &self.role_chain {
if let Some(credential) = role_chain.provide_credential(ctx).await? {
return Ok(Some(credential));
}
}
if let Some(credential) = self.profile_chain.provide_credential(ctx).await? {
return Ok(Some(credential));
}
// SSO selects an identity. Its failure must not switch to a later source.
Expand Down
5 changes: 5 additions & 0 deletions services/aws-core/src/provide_credential/mod.rs
Original file line number Diff line number Diff line change
Expand Up @@ -44,6 +44,11 @@ pub use process::ProcessCredentialProvider;
mod profile;
pub use profile::ProfileCredentialProvider;

#[cfg(not(target_arch = "wasm32"))]
mod role_chain;
#[cfg(not(target_arch = "wasm32"))]
pub(crate) use role_chain::AssumeRoleChainCredentialProvider;

#[cfg(not(target_arch = "wasm32"))]
mod sso;
#[cfg(not(target_arch = "wasm32"))]
Expand Down
2 changes: 1 addition & 1 deletion services/aws-core/src/provide_credential/profile.rs
Original file line number Diff line number Diff line change
Expand Up @@ -76,7 +76,7 @@ impl ProfileCredentialProvider {
}

#[cfg(not(target_arch = "wasm32"))]
fn shared_config(&self) -> crate::SharedConfig {
pub(crate) fn shared_config(&self) -> crate::SharedConfig {
let mut config = crate::SharedConfig::new();
if let Some(v) = &self.profile {
config = config.with_profile(v);
Expand Down
Loading
Loading