Skip to main content

rustfs_policy/policy/
principal.rs

1// Copyright 2024 RustFS Team
2//
3// Licensed under the Apache License, Version 2.0 (the "License");
4// you may not use this file except in compliance with the License.
5// You may obtain a copy of the License at
6//
7//     http://www.apache.org/licenses/LICENSE-2.0
8//
9// Unless required by applicable law or agreed to in writing, software
10// distributed under the License is distributed on an "AS IS" BASIS,
11// WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
12// See the License for the specific language governing permissions and
13// limitations under the License.
14
15use super::{Validator, utils::wildcard};
16use crate::error::{Error, Result};
17use serde::{Deserialize, Serialize};
18use std::collections::HashSet;
19
20#[derive(Debug, Clone, Deserialize, Serialize, Default, PartialEq, Eq)]
21#[serde(rename_all = "PascalCase", default)]
22pub struct Principal {
23    #[serde(rename = "AWS")]
24    aws: HashSet<String>,
25}
26
27impl Principal {
28    pub fn is_match(&self, parincipal: &str) -> bool {
29        for pattern in self.aws.iter() {
30            if wildcard::is_simple_match(pattern, parincipal) {
31                return true;
32            }
33        }
34        false
35    }
36}
37
38impl Validator for Principal {
39    type Error = Error;
40    fn is_valid(&self) -> Result<()> {
41        if self.aws.is_empty() {
42            return Err(Error::other("Principal is empty"));
43        }
44        Ok(())
45    }
46}