perf(iam): avoid redundant UserIdentity clones in auth lookup and policy merge (#9727)

This commit is contained in:
Ramakrishna Chilaka
2026-11-29 07:18:49 +05:30
committed by GitHub
parent 486914b5cf
commit 6128dcadd4
2 changed files with 50 additions and 29 deletions
+11 -6
View File
@@ -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<HashMap<String, PolicyDoc>> {
+39 -23
View File
@@ -951,24 +951,26 @@ impl<T: Store> IamSys<T> {
}
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<T: Store> IamSys<T> {
/// 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<T: Store> IamSys<T> {
}
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 {