fix(replication): replay user credentials separately from policy grants

This commit is contained in:
overtrue
2026-12-02 07:10:00 +08:00
committed by overtrue
parent e37222bccd
commit ea83430dbd
3 changed files with 56 additions and 3 deletions
+33 -1
View File
@@ -6832,7 +6832,7 @@ async fn apply_iam_user_item(
if user.is_delete_req {
iam_sys.delete_user(&user.access_key, true).await.map_err(ApiError::from)?;
} else {
let Some(user_req) = user.user_req else {
let Some(mut user_req) = user.user_req else {
return Err(s3_error!(InvalidRequest, "userReq is required"));
};
let is_status_only_update = user_req.secret_key.is_empty() && user_req.policy.is_none();
@@ -6842,6 +6842,11 @@ async fn apply_iam_user_item(
.await
.map_err(ApiError::from)?;
} else {
// Older peers included the display policy on credential snapshots.
// Only a separate policy-mapping item can change the assignment.
if !user_req.secret_key.is_empty() {
user_req.policy = None;
}
iam_sys
.create_user_at(&user.access_key, &user_req, stamp)
.await
@@ -9407,6 +9412,33 @@ mod tests {
(serde_json::to_string(&doc.policy).expect("serialize policy"), doc.update_date)
}
#[tokio::test]
#[serial]
async fn durable_iam_state_legacy_bootstrap_user_leaves_grants_to_policy_mappings() {
publish_ready_iam_context().await;
seed_two_peer_state_for_iam_apply().await;
let iam = current_iam_handle().expect("test IAM");
let user = "sr-bootstrap-mapped-user";
let user_time = OffsetDateTime::now_utc() - time::Duration::hours(2);
let mapping_time = user_time - time::Duration::minutes(5);
let user_type = rustfs_iam::store::UserType::Reg;
let mut request = user_req("bootstrap-user-secret", rustfs_madmin::AccountStatus::Enabled);
request.policy = Some("readwrite".to_string());
apply_iam_item(sr_user_item(user, Some(request), user_time))
.await
.expect("legacy user snapshot");
assert_eq!(iam.get_user(user).await.expect("user").update_at, Some(user_time));
assert!(iam.get_mapped_policy_record(user, user_type, false).await.is_none());
apply_iam_item(sr_mapping_item(user, "readonly", mapping_time))
.await
.expect("separate mapping");
let mapping = iam.get_mapped_policy_record(user, user_type, false).await.expect("mapping");
assert_eq!(mapping.policies, "readonly");
assert_eq!(mapping.update_at, mapping_time);
clear_seeded_state().await;
}
/// Review finding on rustfs#7195 (P1): two source edits T1 < T2 that both
/// predate their delivery. The record T1 writes must carry T1 — not the
/// later receive time — or T2 is judged stale against it and the newer
+2 -1
View File
@@ -504,7 +504,8 @@ pub(crate) fn site_replication_bootstrap_plan(
is_delete_req: false,
user_req: Some(AddOrUpdateUserReq {
secret_key,
policy: user.policy_name.clone(),
// Policy mappings follow the identities and carry their own timestamps.
policy: None,
status,
}),
api_version: Some(SITE_REPL_API_VERSION.to_string()),
+21 -1
View File
@@ -3809,6 +3809,17 @@ fn test_bootstrap_plan_carries_users_from_the_credential_snapshot() {
},
);
let user_updated_at = OffsetDateTime::from_unix_timestamp(1_700_000_500).expect("timestamp");
let mapping_updated_at = OffsetDateTime::from_unix_timestamp(1_700_000_600).expect("timestamp");
info.user_policies.insert(
"alice".to_string(),
SRPolicyMapping {
user_or_group: "alice".to_string(),
user_type: sr_wire_user_type(UserType::Reg, false),
policy: "readwrite".to_string(),
updated_at: Some(mapping_updated_at),
..Default::default()
},
);
let mut credentials = SiteReplicationIamCredentials::default();
credentials.users.insert(
"alice".to_string(),
@@ -3828,9 +3839,18 @@ fn test_bootstrap_plan_carries_users_from_the_credential_snapshot() {
let req = alice.user_req.as_ref().expect("user request");
assert_eq!(req.secret_key, "alice-secret");
assert_eq!(req.status, rustfs_madmin::AccountStatus::Disabled);
assert_eq!(req.policy.as_deref(), Some("readwrite"));
assert!(req.policy.is_none(), "user credentials must be replayable without attaching a policy");
// the user record's own axis, not the policy-mapping time list_users reports
assert_eq!(users[0].updated_at, Some(user_updated_at));
let item = plan
.iam_items
.iter()
.find(|item| item.r#type == "policy-mapping")
.expect("mapping");
let mapping = item.policy_mapping.as_ref().expect("policy mapping body");
assert_eq!(mapping.user_or_group, "alice");
assert_eq!(mapping.policy, "readwrite");
assert_eq!(item.updated_at, Some(mapping_updated_at));
}
fn service_account_snapshot(access_key: &str, parent: &str, status: &str) -> SiteReplicationServiceAccountSnapshot {