Skip to content

Commit 29970bb

Browse files
committed
fix(providers): require AWS STS source credentials as a complete pair
A refresh that supplied only aws_access_key_id or only aws_secret_access_key silently ignored the lone key and fell back to the gateway's ambient AWS identity, minting against an unintended principal (CWE-20). Require both keys present or both absent, rejecting a partial pair at the configure boundary and again in the mint path. Signed-off-by: Russell Bryant <[email protected]>
1 parent 916e9d2 commit 29970bb

2 files changed

Lines changed: 153 additions & 9 deletions

File tree

‎crates/openshell-server/src/grpc/provider.rs‎

Lines changed: 81 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2133,6 +2133,22 @@ pub(super) async fn handle_configure_provider_refresh(
21332133
"sts_endpoint_url material is not permitted",
21342134
));
21352135
}
2136+
// Explicit AWS source credentials are all-or-nothing. Reject a partial pair
2137+
// early so a lone key can't later fall through to the gateway's ambient
2138+
// identity at mint time (CWE-20).
2139+
let has_source_access_key = request
2140+
.material
2141+
.get("aws_access_key_id")
2142+
.is_some_and(|value| !value.trim().is_empty());
2143+
let has_source_secret_key = request
2144+
.material
2145+
.get("aws_secret_access_key")
2146+
.is_some_and(|value| !value.trim().is_empty());
2147+
if has_source_access_key != has_source_secret_key {
2148+
return Err(Status::invalid_argument(
2149+
"aws_access_key_id and aws_secret_access_key must both be set or both omitted",
2150+
));
2151+
}
21362152
if request
21372153
.expires_at_ms
21382154
.is_some_and(|expires_at_ms| expires_at_ms < 0)
@@ -6347,6 +6363,71 @@ mod tests {
63476363
);
63486364
}
63496365

6366+
#[tokio::test]
6367+
async fn configure_aws_sts_rejects_partial_source_credentials() {
6368+
use crate::grpc::StoredSettingValue;
6369+
use crate::grpc::StoredSettings;
6370+
use crate::grpc::policy::save_global_settings;
6371+
6372+
let state = test_server_state().await;
6373+
let global_settings = StoredSettings {
6374+
revision: 1,
6375+
settings: std::iter::once((
6376+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY.to_string(),
6377+
StoredSettingValue::Bool(true),
6378+
))
6379+
.collect(),
6380+
..Default::default()
6381+
};
6382+
save_global_settings(state.store.as_ref(), &global_settings)
6383+
.await
6384+
.unwrap();
6385+
6386+
create_provider_record(
6387+
state.store.as_ref(),
6388+
Provider {
6389+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
6390+
id: String::new(),
6391+
name: "aws-partial-source".to_string(),
6392+
created_at_ms: 0,
6393+
labels: HashMap::new(),
6394+
resource_version: 0,
6395+
}),
6396+
r#type: "aws".to_string(),
6397+
credentials: HashMap::new(),
6398+
config: HashMap::new(),
6399+
credential_expires_at_ms: HashMap::new(),
6400+
},
6401+
)
6402+
.await
6403+
.unwrap();
6404+
6405+
// aws_access_key_id without its secret half must be rejected rather than
6406+
// silently falling back to the gateway's ambient identity.
6407+
let err = handle_configure_provider_refresh(
6408+
&state,
6409+
Request::new(ConfigureProviderRefreshRequest {
6410+
provider: "aws-partial-source".to_string(),
6411+
credential_key: "AWS_ACCESS_KEY_ID".to_string(),
6412+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole as i32,
6413+
material: HashMap::from([
6414+
(
6415+
"role_arn".to_string(),
6416+
"arn:aws:iam::123456789012:role/Test".to_string(),
6417+
),
6418+
("aws_access_key_id".to_string(), "AKIATESTKEY".to_string()),
6419+
]),
6420+
secret_material_keys: Vec::new(),
6421+
expires_at_ms: None,
6422+
}),
6423+
)
6424+
.await
6425+
.unwrap_err();
6426+
6427+
assert_eq!(err.code(), Code::InvalidArgument);
6428+
assert!(err.message().contains("both be set or both omitted"));
6429+
}
6430+
63506431
#[tokio::test]
63516432
async fn configure_aws_sts_persists_resolved_additional_output_keys() {
63526433
use crate::grpc::StoredSettingValue;

‎crates/openshell-server/src/provider_refresh.rs‎

Lines changed: 72 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -583,18 +583,29 @@ async fn mint_aws_sts_assume_role(
583583
let mut config_loader =
584584
aws_config::defaults(aws_config::BehaviorVersion::latest()).region(region_provider);
585585

586-
if let (Some(access_key), Some(secret_key)) = (
586+
// Explicit source credentials are all-or-nothing. A lone key must not
587+
// silently fall through to the gateway's ambient identity (CWE-20): the
588+
// caller asked for a specific principal, so an incomplete pair is an error.
589+
match (
587590
material_value(&state.material, &["aws_access_key_id"]),
588591
material_value(&state.material, &["aws_secret_access_key"]),
589592
) {
590-
let creds = aws_sdk_sts::config::Credentials::new(
591-
access_key,
592-
secret_key,
593-
None,
594-
None,
595-
"openshell-provider-refresh",
596-
);
597-
config_loader = config_loader.credentials_provider(creds);
593+
(Some(access_key), Some(secret_key)) => {
594+
let creds = aws_sdk_sts::config::Credentials::new(
595+
access_key,
596+
secret_key,
597+
None,
598+
None,
599+
"openshell-provider-refresh",
600+
);
601+
config_loader = config_loader.credentials_provider(creds);
602+
}
603+
(None, None) => {}
604+
_ => {
605+
return Err(Status::invalid_argument(
606+
"aws_access_key_id and aws_secret_access_key must both be set or both omitted",
607+
));
608+
}
598609
}
599610

600611
let sdk_config = config_loader.load().await;
@@ -1505,6 +1516,58 @@ mod tests {
15051516
assert!(!stored.credentials.contains_key("AWS_SECRET_ACCESS_KEY"));
15061517
}
15071518

1519+
#[tokio::test]
1520+
async fn aws_sts_mint_rejects_partial_source_credentials() {
1521+
let store = test_store().await;
1522+
let prov = provider("aws-sts-partial", "aws");
1523+
store.put_message(&prov).await.unwrap();
1524+
1525+
// Only the access key half of the explicit source pair is present. The
1526+
// mint must fail rather than fall back to the gateway's ambient identity.
1527+
let state = new_refresh_state(
1528+
&prov,
1529+
"AWS_ACCESS_KEY_ID",
1530+
NewRefreshStateConfig {
1531+
additional_output_keys: HashMap::from([
1532+
(
1533+
"secret_access_key".to_string(),
1534+
"AWS_SECRET_ACCESS_KEY".to_string(),
1535+
),
1536+
("session_token".to_string(), "AWS_SESSION_TOKEN".to_string()),
1537+
]),
1538+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole,
1539+
material: HashMap::from([
1540+
(
1541+
"role_arn".to_string(),
1542+
"arn:aws:iam::123456789012:role/TestRole".to_string(),
1543+
),
1544+
("aws_access_key_id".to_string(), "AKIATESTKEY".to_string()),
1545+
]),
1546+
secret_material_keys: Vec::new(),
1547+
expires_at_ms: 0,
1548+
token_url: String::new(),
1549+
scopes: Vec::new(),
1550+
refresh_before_seconds: 300,
1551+
max_lifetime_seconds: 3600,
1552+
},
1553+
)
1554+
.unwrap();
1555+
put_refresh_state(&store, &state).await.unwrap();
1556+
1557+
let err = refresh_provider_credential(&store, "aws-sts-partial", "AWS_ACCESS_KEY_ID")
1558+
.await
1559+
.unwrap_err();
1560+
assert_eq!(err.code(), tonic::Code::InvalidArgument);
1561+
assert!(err.message().contains("both be set or both omitted"));
1562+
1563+
let stored = store
1564+
.get_message_by_name::<Provider>("aws-sts-partial")
1565+
.await
1566+
.unwrap()
1567+
.unwrap();
1568+
assert!(!stored.credentials.contains_key("AWS_ACCESS_KEY_ID"));
1569+
}
1570+
15081571
#[tokio::test]
15091572
async fn apply_minted_credential_writes_additional_keys() {
15101573
use super::apply_minted_credential;

0 commit comments

Comments
 (0)