From 6128dcadd49e44917cbebd828c0145f6dce9e303 Mon Sep 17 00:00:00 2001 From: Ramakrishna Chilaka <49393831+RamakrishnaChilaka@users.noreply.github.com> Date: Sun, 29 Nov 2026 07:18:49 +0530 Subject: [PATCH] perf(iam): avoid redundant UserIdentity clones in auth lookup and policy merge (#9727) --- crates/iam/src/manager.rs | 17 +++++++---- crates/iam/src/sys.rs | 62 ++++++++++++++++++++++++--------------- 2 files changed, 50 insertions(+), 29 deletions(-) diff --git a/crates/iam/src/manager.rs b/crates/iam/src/manager.rs index ae190a9f31..46c21c74a5 100644 --- a/crates/iam/src/manager.rs +++ b/crates/iam/src/manager.rs @@ -673,14 +673,14 @@ where let policy_docs = Arc::clone(&cache.policy_docs); drop(cache); - for policy in MappedPolicy::new(name).to_slice() { + for policy in name.split(',') { if policy.is_empty() { continue; } - if let Some(v) = policy_docs.get(&policy).cloned() { - policies.push(policy); - to_merge.push(v.policy); + if let Some(v) = policy_docs.get(policy) { + policies.push(policy.to_string()); + to_merge.push(v.policy.clone()); } else { miss_policies.push(policy); } @@ -689,7 +689,7 @@ where if !miss_policies.is_empty() { let mut m = HashMap::new(); for policy in miss_policies { - let _ = self.api.load_policy_doc(&policy, &mut m).await; + let _ = self.api.load_policy_doc(policy, &mut m).await; } self.cache.with_write_lock(|cache| { @@ -702,7 +702,12 @@ where }); } - (policies.join(","), Policy::merge_policies(to_merge)) + let joined = if policies.len() == 1 { + policies.pop().unwrap_or_default() + } else { + policies.join(",") + }; + (joined, Policy::merge_policies(to_merge)) } pub async fn list_policy_docs(&self, bucket_name: &str) -> Result> { diff --git a/crates/iam/src/sys.rs b/crates/iam/src/sys.rs index 9a324aec8d..1f1b0dd762 100644 --- a/crates/iam/src/sys.rs +++ b/crates/iam/src/sys.rs @@ -951,24 +951,26 @@ impl IamSys { } pub async fn is_temp_user(&self, name: &str) -> Result<(bool, String)> { - let Some(u) = self.store.get_user(name).await else { + let cache = self.store.cache.snapshot(); + let Some(u) = cache.users.get(name).or_else(|| cache.sts_accounts.get(name)) else { return Err(IamError::NoSuchUser(name.to_string())); }; if u.credentials.is_temp() { - Ok((true, u.credentials.parent_user)) + Ok((true, u.credentials.parent_user.clone())) } else { - Ok((false, "".to_string())) + Ok((false, String::new())) } } pub async fn is_service_account(&self, name: &str) -> Result<(bool, String)> { - let Some(u) = self.store.get_user(name).await else { + let cache = self.store.cache.snapshot(); + let Some(u) = cache.users.get(name).or_else(|| cache.sts_accounts.get(name)) else { return Err(IamError::NoSuchUser(name.to_string())); }; if u.credentials.is_service_account() { - Ok((true, u.credentials.parent_user)) + Ok((true, u.credentials.parent_user.clone())) } else { - Ok((false, "".to_string())) + Ok((false, String::new())) } } @@ -2167,7 +2169,14 @@ impl IamSys { /// 1) know whether policy evaluation may need `s3:ExistingObjectTag`, and /// 2) evaluate with final conditions without re-merging identity policies. pub async fn prepare_auth(&self, args: &Args<'_>) -> PreparedIamAuth { - let session = self.store.cache.snapshot().sts_accounts.get(args.account).cloned(); + let session = { + let snapshot = self.store.cache.snapshot(); + if snapshot.sts_accounts.is_empty() { + None + } else { + snapshot.sts_accounts.get(args.account).cloned() + } + }; if let Some(session) = session && !self.sts_generation_is_current(&session).await.unwrap_or(false) { @@ -2254,27 +2263,34 @@ impl IamSys { } async fn prepare_builtin_auth(&self, args: &Args<'_>) -> PreparedIamAuth { - let Ok((is_svc, parent_user)) = self.is_service_account(args.account).await else { - return PreparedIamAuth { - needs_existing_object_tag: false, - mode: PreparedIamMode::Deny, - }; - }; - if is_svc { - return self.prepare_service_account_auth(args, &parent_user).await; + enum BuiltinAccountKind { + ServiceAccount(String), + Temp(String), + Regular, } - let Ok((is_temp, parent_user)) = self.is_temp_user(args.account).await else { - return PreparedIamAuth { - needs_existing_object_tag: false, - mode: PreparedIamMode::Deny, + let kind = { + let cache = self.store.cache.snapshot(); + let Some(u) = cache.users.get(args.account).or_else(|| cache.sts_accounts.get(args.account)) else { + return PreparedIamAuth { + needs_existing_object_tag: false, + mode: PreparedIamMode::Deny, + }; }; + if u.credentials.is_service_account() { + BuiltinAccountKind::ServiceAccount(u.credentials.parent_user.clone()) + } else if u.credentials.is_temp() { + BuiltinAccountKind::Temp(u.credentials.parent_user.clone()) + } else { + BuiltinAccountKind::Regular + } }; - if is_temp { - return self.prepare_sts_auth(args, &parent_user).await; - } - self.prepare_regular_auth(args).await + match kind { + BuiltinAccountKind::ServiceAccount(parent_user) => self.prepare_service_account_auth(args, &parent_user).await, + BuiltinAccountKind::Temp(parent_user) => self.prepare_sts_auth(args, &parent_user).await, + BuiltinAccountKind::Regular => self.prepare_regular_auth(args).await, + } } pub async fn eval_prepared(&self, prepared: &PreparedIamAuth, args: &Args<'_>) -> bool {