mirror of
https://github.com/rustfs/rustfs.git
synced 2026-12-01 13:44:49 +00:00
perf(policy): defer variable resolver cloning and fast-path static resource matching (#9726)
This commit is contained in:
committed by
GitHub
parent
2de04eeaf9
commit
82e595aad6
@@ -180,6 +180,38 @@ enum ResourceToken {
|
||||
Values(Vec<String>),
|
||||
}
|
||||
|
||||
fn resource_bytes_match(pattern: &[u8], name: &str, wildcard: bool) -> bool {
|
||||
if !wildcard {
|
||||
return pattern == name.as_bytes();
|
||||
}
|
||||
let name = name.as_bytes();
|
||||
let (mut p, mut n) = (0, 0);
|
||||
let mut star = None;
|
||||
while n < name.len() {
|
||||
match pattern.get(p).copied() {
|
||||
Some(b'*') => {
|
||||
star = Some((p, n));
|
||||
p += 1;
|
||||
}
|
||||
Some(b'?') => {
|
||||
p += 1;
|
||||
n += 1;
|
||||
}
|
||||
Some(byte) if byte == name[n] => {
|
||||
p += 1;
|
||||
n += 1;
|
||||
}
|
||||
_ => {
|
||||
let Some((star_pattern, star_name)) = star else { return false };
|
||||
n = star_name + 1;
|
||||
p = star_pattern + 1;
|
||||
star = Some((star_pattern, n));
|
||||
}
|
||||
}
|
||||
}
|
||||
pattern[p..].iter().all(|&byte| byte == b'*') || p == pattern.len()
|
||||
}
|
||||
|
||||
fn resource_tokens_match(pattern: &[ResourceToken], name: &str, wildcard: bool) -> bool {
|
||||
let name = name.as_bytes();
|
||||
if pattern.iter().all(|token| !matches!(token, ResourceToken::Values(_))) {
|
||||
@@ -366,6 +398,14 @@ impl Resource {
|
||||
matches!(self, Resource::Kms(_))
|
||||
}
|
||||
|
||||
#[inline]
|
||||
pub(crate) fn contains_variables(&self) -> bool {
|
||||
match self {
|
||||
Resource::All => false,
|
||||
Resource::S3(s) | Resource::Kms(s) => s.contains("${"),
|
||||
}
|
||||
}
|
||||
|
||||
pub async fn is_match(&self, resource: &str, conditions: &HashMap<String, Vec<String>>) -> bool {
|
||||
self.is_match_with_resolver(resource, conditions, None).await
|
||||
}
|
||||
@@ -388,11 +428,29 @@ impl Resource {
|
||||
) -> bool {
|
||||
let pattern = match self {
|
||||
Resource::All => return true,
|
||||
Resource::S3(s) => s.to_owned(),
|
||||
Resource::Kms(s) => s.to_owned(),
|
||||
Resource::S3(s) => s.as_str(),
|
||||
Resource::Kms(s) => s.as_str(),
|
||||
};
|
||||
|
||||
if let Some(pattern) = resolve_resource_tokens(&pattern, conditions, resolver, 0).await {
|
||||
if !pattern.contains("${") {
|
||||
let cp = path::clean(resource);
|
||||
if cp == "." {
|
||||
return false;
|
||||
}
|
||||
let pat_bytes = pattern.as_bytes();
|
||||
if resource_bytes_match(pat_bytes, &cp, false) {
|
||||
return true;
|
||||
}
|
||||
if matches!(self, Resource::S3(_)) && resource.ends_with('/') && cp.contains('/') {
|
||||
let mut wildcard_resource = String::with_capacity(cp.len() + 1);
|
||||
wildcard_resource.push_str(&cp);
|
||||
wildcard_resource.push('/');
|
||||
return resource_bytes_match(pat_bytes, &wildcard_resource, true);
|
||||
}
|
||||
return resource_bytes_match(pat_bytes, &cp, true);
|
||||
}
|
||||
|
||||
if let Some(pattern) = resolve_resource_tokens(pattern, conditions, resolver, 0).await {
|
||||
let cp = path::clean(resource);
|
||||
if cp != "." && resource_tokens_match(&pattern, &cp, false) {
|
||||
return true;
|
||||
@@ -400,12 +458,14 @@ impl Resource {
|
||||
|
||||
// Keep directory objects distinct from their bare key for prefix
|
||||
// matching, while retaining path cleaning and exact-match compatibility.
|
||||
let wildcard_resource = if matches!(self, Resource::S3(_)) && resource.ends_with('/') && cp.contains('/') {
|
||||
format!("{cp}/")
|
||||
} else {
|
||||
cp.clone()
|
||||
};
|
||||
if cp != "." && resource_tokens_match(&pattern, &wildcard_resource, true) {
|
||||
if matches!(self, Resource::S3(_)) && resource.ends_with('/') && cp.contains('/') {
|
||||
let mut wildcard_resource = String::with_capacity(cp.len() + 1);
|
||||
wildcard_resource.push_str(&cp);
|
||||
wildcard_resource.push('/');
|
||||
if cp != "." && resource_tokens_match(&pattern, &wildcard_resource, true) {
|
||||
return true;
|
||||
}
|
||||
} else if cp != "." && resource_tokens_match(&pattern, &cp, true) {
|
||||
return true;
|
||||
}
|
||||
} else {
|
||||
|
||||
@@ -67,13 +67,17 @@ fn build_resource(action: &Action, bucket: &str, object: &str, bucket_resource_o
|
||||
)
|
||||
) && bucket_resource_only;
|
||||
|
||||
let mut resource = String::from(bucket);
|
||||
if bucket_resource_only || object.is_empty() {
|
||||
let mut resource = String::with_capacity(bucket.len() + 1);
|
||||
resource.push_str(bucket);
|
||||
resource.push('/');
|
||||
return resource;
|
||||
}
|
||||
|
||||
if !object.starts_with('/') {
|
||||
let needs_sep = !object.starts_with('/');
|
||||
let mut resource = String::with_capacity(bucket.len() + usize::from(needs_sep) + object.len());
|
||||
resource.push_str(bucket);
|
||||
if needs_sep {
|
||||
resource.push('/');
|
||||
}
|
||||
resource.push_str(object);
|
||||
@@ -118,9 +122,9 @@ impl<'a> RequestedKmsKey<'a> {
|
||||
}
|
||||
|
||||
pub fn parse(identifier: &'a str) -> Self {
|
||||
let fields: Vec<&str> = identifier.splitn(6, ':').collect();
|
||||
let resource = match fields.as_slice() {
|
||||
["arn", _partition, "kms", _region, _account, resource] => *resource,
|
||||
let mut fields = identifier.splitn(6, ':');
|
||||
let resource = match (fields.next(), fields.next(), fields.next(), fields.next(), fields.next(), fields.next()) {
|
||||
(Some("arn"), Some(_partition), Some("kms"), Some(_region), Some(_account), Some(resource)) => resource,
|
||||
_ if identifier.starts_with(Resource::KMS_ALIAS_SEGMENT) => return Self::Alias,
|
||||
_ => return Self::Key(identifier),
|
||||
};
|
||||
@@ -219,6 +223,13 @@ impl Statement {
|
||||
Some(ActionFamily::Mixed)
|
||||
}
|
||||
|
||||
fn resources_contain_variables(&self) -> bool {
|
||||
self.resources
|
||||
.iter()
|
||||
.chain(self.not_resources.iter())
|
||||
.any(Resource::contains_variables)
|
||||
}
|
||||
|
||||
/// Resource scope check for KMS statements, which match `arn:aws:kms:::key/<key_id>`
|
||||
/// patterns instead of the bucket/object path grammar used by S3 statements.
|
||||
///
|
||||
@@ -227,7 +238,7 @@ impl Statement {
|
||||
/// left empty; [`RequestedKmsKey`] reduces it to the key id the patterns are written
|
||||
/// against. An empty `args.object` means the caller did not scope the request to a
|
||||
/// key, which preserves the legacy match-every-key behaviour.
|
||||
async fn kms_key_scope_matches(&self, args: &Args<'_>, resolver: &VariableResolver) -> bool {
|
||||
async fn kms_key_scope_matches(&self, args: &Args<'_>, resolver: Option<&VariableResolver>) -> bool {
|
||||
if self.not_resources.iter().any(|resource| matches!(resource, Resource::All)) {
|
||||
return false;
|
||||
}
|
||||
@@ -290,12 +301,13 @@ impl Statement {
|
||||
}
|
||||
};
|
||||
let requested = format!("{}{}", Resource::KMS_KEY_SEGMENT, key_id);
|
||||
let dyn_resolver = resolver.map(|r| r as &dyn super::variables::PolicyVariableResolver);
|
||||
|
||||
if !kms_resources.is_empty() {
|
||||
let mut matched = false;
|
||||
for resource in kms_resources {
|
||||
if resource
|
||||
.is_match_with_unresolved(&requested, args.conditions, Some(resolver), matches!(self.effect, Effect::Deny))
|
||||
.is_match_with_unresolved(&requested, args.conditions, dyn_resolver, matches!(self.effect, Effect::Deny))
|
||||
.await
|
||||
{
|
||||
matched = true;
|
||||
@@ -309,7 +321,7 @@ impl Statement {
|
||||
|
||||
for resource in kms_not_resources {
|
||||
if resource
|
||||
.is_match_with_unresolved(&requested, args.conditions, Some(resolver), matches!(self.effect, Effect::Allow))
|
||||
.is_match_with_unresolved(&requested, args.conditions, dyn_resolver, matches!(self.effect, Effect::Allow))
|
||||
.await
|
||||
{
|
||||
return false;
|
||||
@@ -322,6 +334,10 @@ impl Statement {
|
||||
/// Returns true when this statement would reach `conditions.evaluate_with_resolver` in
|
||||
/// [`Statement::is_allowed`] (including the KMS resource path). Does not evaluate conditions.
|
||||
pub(crate) async fn request_reaches_condition_eval(&self, args: &Args<'_>, resolver: &VariableResolver) -> bool {
|
||||
self.request_reaches_condition_eval_optional(args, Some(resolver)).await
|
||||
}
|
||||
|
||||
async fn request_reaches_condition_eval_optional(&self, args: &Args<'_>, resolver: Option<&VariableResolver>) -> bool {
|
||||
let deny = matches!(self.effect, Effect::Deny);
|
||||
if !self.actions.statement_covers(&self.not_actions, &args.action, deny) {
|
||||
return false;
|
||||
@@ -331,21 +347,22 @@ impl Statement {
|
||||
return self.kms_key_scope_matches(args, resolver).await;
|
||||
}
|
||||
|
||||
if self.resources.is_empty() && self.not_resources.is_empty() && !self.is_admin() && !self.is_sts() {
|
||||
return false;
|
||||
}
|
||||
|
||||
let resource = build_resource(
|
||||
&args.action,
|
||||
args.bucket,
|
||||
args.object,
|
||||
self.conditions.references_key_name(&KeyName::S3(S3KeyName::S3Prefix)),
|
||||
);
|
||||
|
||||
if self.resources.is_empty() && self.not_resources.is_empty() && !self.is_admin() && !self.is_sts() {
|
||||
return false;
|
||||
}
|
||||
let dyn_resolver = resolver.map(|r| r as &dyn super::variables::PolicyVariableResolver);
|
||||
|
||||
if !self.resources.is_empty()
|
||||
&& !self
|
||||
.resources
|
||||
.is_match_with_unresolved(&resource, args.conditions, Some(resolver), deny)
|
||||
.is_match_with_unresolved(&resource, args.conditions, dyn_resolver, deny)
|
||||
.await
|
||||
&& !self.skips_resource_match_for_args(args)
|
||||
{
|
||||
@@ -355,7 +372,7 @@ impl Statement {
|
||||
if !self.not_resources.is_empty()
|
||||
&& self
|
||||
.not_resources
|
||||
.is_match_with_unresolved(&resource, args.conditions, Some(resolver), !deny)
|
||||
.is_match_with_unresolved(&resource, args.conditions, dyn_resolver, !deny)
|
||||
.await
|
||||
&& !self.skips_resource_match_for_args(args)
|
||||
{
|
||||
@@ -366,10 +383,20 @@ impl Statement {
|
||||
}
|
||||
|
||||
pub async fn is_allowed(&self, args: &Args<'_>) -> bool {
|
||||
let deny = matches!(self.effect, Effect::Deny);
|
||||
if !self.actions.statement_covers(&self.not_actions, &args.action, deny) {
|
||||
return self.effect.is_allowed(false);
|
||||
}
|
||||
|
||||
if !self.resources_contain_variables() && self.conditions.is_empty() {
|
||||
let check = self.request_reaches_condition_eval_optional(args, None).await;
|
||||
return self.effect.is_allowed(check);
|
||||
}
|
||||
|
||||
let resolver = variable_resolver_for_policy_args(args);
|
||||
|
||||
let check = 'c: {
|
||||
if !self.request_reaches_condition_eval(args, &resolver).await {
|
||||
if !self.request_reaches_condition_eval_optional(args, Some(&resolver)).await {
|
||||
break 'c false;
|
||||
}
|
||||
|
||||
|
||||
Reference in New Issue
Block a user