From ea83430dbd7702bfa967a69e7eb279162b9b0dba Mon Sep 17 00:00:00 2001 From: overtrue <1472352+overtrue@users.noreply.github.com> Date: Wed, 2 Dec 2026 07:10:00 +0800 Subject: [PATCH] fix(replication): replay user credentials separately from policy grants --- rustfs/src/admin/handlers/site_replication.rs | 34 ++++++++++++++++++- rustfs/src/site_replication/hooks.rs | 3 +- rustfs/src/site_replication/tests.rs | 22 +++++++++++- 3 files changed, 56 insertions(+), 3 deletions(-) diff --git a/rustfs/src/admin/handlers/site_replication.rs b/rustfs/src/admin/handlers/site_replication.rs index 76270a3fd8..14e82913c7 100644 --- a/rustfs/src/admin/handlers/site_replication.rs +++ b/rustfs/src/admin/handlers/site_replication.rs @@ -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 diff --git a/rustfs/src/site_replication/hooks.rs b/rustfs/src/site_replication/hooks.rs index 579a9e5279..a4043219a2 100644 --- a/rustfs/src/site_replication/hooks.rs +++ b/rustfs/src/site_replication/hooks.rs @@ -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()), diff --git a/rustfs/src/site_replication/tests.rs b/rustfs/src/site_replication/tests.rs index 518e6dc307..ecee9ce77d 100644 --- a/rustfs/src/site_replication/tests.rs +++ b/rustfs/src/site_replication/tests.rs @@ -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 {