Skip to content

Commit bf68e0b

Browse files
committed
fix(providers): serialize refresh configuration to reserve keys atomically
Refresh configuration validated key availability and persisted the reservation as separate steps. Two concurrent configures of providers attached to the same sandbox could each pass validation before either persisted, then both reserve the same primary or additional output key, leaving conflicting unusable refreshes (CWE-362). Hold the process-wide sandbox sync guard across the reserve-and-persist sequence — the same guard sandbox create/attach and profile changes take — so configurations and sandbox mutations that share the credential-key namespace are serialized. Add a concurrent-configure regression test asserting exactly one configuration reserves the key. Signed-off-by: Russell Bryant <[email protected]>
1 parent f462775 commit bf68e0b

1 file changed

Lines changed: 99 additions & 0 deletions

File tree

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

Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -2158,6 +2158,14 @@ pub(super) async fn handle_configure_provider_refresh(
21582158
));
21592159
}
21602160

2161+
// Serialize the reserve-then-persist sequence against other configurations
2162+
// and sandbox mutations. The collision validation below and the refresh-state
2163+
// persist further down are otherwise separate steps: two concurrent
2164+
// configures of providers attached to the same sandbox could each pass
2165+
// validation before either persisted and both reserve the same key (CWE-362).
2166+
// This is the same guard sandbox create/attach and profile changes take.
2167+
let _sandbox_sync_guard = state.compute.sandbox_sync_guard().await;
2168+
21612169
let provider = state
21622170
.store
21632171
.get_message_by_name::<Provider>(provider_name)
@@ -7085,6 +7093,97 @@ mod tests {
70857093
assert!(err.message().contains("AWS_SECRET_ACCESS_KEY"));
70867094
}
70877095

7096+
#[tokio::test(flavor = "multi_thread", worker_threads = 4)]
7097+
async fn concurrent_configure_aws_sts_reserves_key_exactly_once() {
7098+
use crate::grpc::policy::set_global_bool_setting_for_test;
7099+
7100+
let state = test_server_state().await;
7101+
set_global_bool_setting_for_test(
7102+
state.store.as_ref(),
7103+
openshell_core::settings::PROVIDERS_V2_ENABLED_KEY,
7104+
true,
7105+
)
7106+
.await
7107+
.unwrap();
7108+
7109+
for name in ["aws-a", "aws-b"] {
7110+
create_provider_record(
7111+
state.store.as_ref(),
7112+
Provider {
7113+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
7114+
id: String::new(),
7115+
name: name.to_string(),
7116+
created_at_ms: 0,
7117+
labels: HashMap::new(),
7118+
resource_version: 0,
7119+
}),
7120+
r#type: "aws".to_string(),
7121+
credentials: HashMap::new(),
7122+
config: HashMap::new(),
7123+
credential_expires_at_ms: HashMap::new(),
7124+
},
7125+
)
7126+
.await
7127+
.unwrap();
7128+
}
7129+
7130+
// Both providers are attached to the same sandbox, so their minted AWS
7131+
// credential env keys share a namespace and cannot both be reserved.
7132+
state
7133+
.store
7134+
.put_message(&Sandbox {
7135+
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {
7136+
id: "sandbox-concurrent-configure".to_string(),
7137+
name: "concurrent-configure".to_string(),
7138+
created_at_ms: 1,
7139+
labels: HashMap::new(),
7140+
resource_version: 0,
7141+
}),
7142+
spec: Some(SandboxSpec {
7143+
providers: vec!["aws-a".to_string(), "aws-b".to_string()],
7144+
..SandboxSpec::default()
7145+
}),
7146+
..Default::default()
7147+
})
7148+
.await
7149+
.unwrap();
7150+
7151+
let configure = |provider: &str| {
7152+
Request::new(ConfigureProviderRefreshRequest {
7153+
provider: provider.to_string(),
7154+
credential_key: "AWS_ACCESS_KEY_ID".to_string(),
7155+
strategy: ProviderCredentialRefreshStrategy::AwsStsAssumeRole as i32,
7156+
material: HashMap::from([(
7157+
"role_arn".to_string(),
7158+
"arn:aws:iam::123456789012:role/Test".to_string(),
7159+
)]),
7160+
secret_material_keys: Vec::new(),
7161+
expires_at_ms: None,
7162+
})
7163+
};
7164+
7165+
let (result_a, result_b) = tokio::join!(
7166+
handle_configure_provider_refresh(&state, configure("aws-a")),
7167+
handle_configure_provider_refresh(&state, configure("aws-b")),
7168+
);
7169+
7170+
// Exactly one configuration wins; the other is rejected as a collision
7171+
// rather than both reserving the same key.
7172+
let ok_count = [&result_a, &result_b].iter().filter(|r| r.is_ok()).count();
7173+
assert_eq!(
7174+
ok_count,
7175+
1,
7176+
"exactly one concurrent configure should reserve the key; got a={:?} b={:?}",
7177+
result_a.as_ref().map(|_| ()),
7178+
result_b.as_ref().map(|_| ())
7179+
);
7180+
let err = [result_a, result_b]
7181+
.into_iter()
7182+
.find_map(Result::err)
7183+
.expect("one configure must fail");
7184+
assert_eq!(err.code(), Code::FailedPrecondition);
7185+
}
7186+
70887187
fn google_cloud_provider(config: HashMap<String, String>) -> Provider {
70897188
Provider {
70907189
metadata: Some(openshell_core::proto::datamodel::v1::ObjectMeta {

0 commit comments

Comments
 (0)