Skip to content

Commit

Permalink
fix naming
Browse files Browse the repository at this point in the history
Signed-off-by: Flavio Castelli <fcastelli@suse.com>
  • Loading branch information
flavio committed Jul 9, 2024
1 parent 33a7244 commit 9d638c1
Show file tree
Hide file tree
Showing 8 changed files with 115 additions and 112 deletions.
18 changes: 9 additions & 9 deletions src/api/service.rs
Original file line number Diff line number Diff line change
Expand Up @@ -208,16 +208,16 @@ mod tests {
policy_mode: PolicyMode,
) -> EvaluationEnvironment {
let mut mock_evaluation_environment = EvaluationEnvironment::default();

mock_evaluation_environment.expect_validate()
urning(|_policy_id, request| {
dmissionResponse {
uid: request.uid().to_owned(),
allowed: true,
..Default::default()
})
mock_evaluation_environment
.expect_validate()
.returning(|_policy_id, request| {
Ok(AdmissionResponse {
uid: request.uid().to_owned(),
allowed: true,
..Default::default()
})
});

mock_evaluation_environment
.expect_get_policy_mode()
.returning(move |_policy_id| Ok(policy_mode.clone()));
Expand Down
54 changes: 27 additions & 27 deletions src/config.rs
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ lazy_static! {
pub struct Config {
pub addr: SocketAddr,
pub sources: Option<Sources>,
pub policies: HashMap<String, Policy>,
pub policies: HashMap<String, PolicyOrPolicyGroup>,
pub policies_download_dir: PathBuf,
pub ignore_kubernetes_connection_failure: bool,
pub always_accept_admission_reviews_on_namespace: Option<String>,
Expand Down Expand Up @@ -193,7 +193,7 @@ fn tls_files(matches: &clap::ArgMatches) -> Result<(String, String)> {
}
}

fn policies(matches: &clap::ArgMatches) -> Result<HashMap<String, Policy>> {
fn policies(matches: &clap::ArgMatches) -> Result<HashMap<String, PolicyOrPolicyGroup>> {
let policies_file = Path::new(matches.get_one::<String>("policies").unwrap());
read_policies_file(policies_file).map_err(|e| {
anyhow!(
Expand Down Expand Up @@ -275,19 +275,19 @@ impl TryFrom<HashMap<String, serde_yaml::Value>> for SettingsJSON {
}

#[derive(Debug, Clone)]
pub enum PolicySettings {
IndividualPolicy(SettingsJSON),
GroupPolicy {
pub enum PolicyOrPolicyGroupSettings {
Policy(SettingsJSON),
PolicyGroup {
expression: String,
message: String,
sub_policies: Vec<String>,
members: Vec<String>,
},
}

/// `GroupPolicyMember` represents a single policy that is part of a group policy.
/// `PolicyGroupMember` represents a single policy that is part of a policy group.
#[derive(Deserialize, Debug, Clone)]
#[serde(rename_all = "camelCase")]
pub struct GroupPolicyMember {
pub struct PolicyGroupMember {
/// Thge URL where the policy is located
pub url: String,
/// The settings for the policy
Expand All @@ -297,19 +297,19 @@ pub struct GroupPolicyMember {
pub context_aware_resources: BTreeSet<ContextAwareResource>,
}

impl GroupPolicyMember {
pub fn settings(&self) -> Result<PolicySettings> {
impl PolicyGroupMember {
pub fn settings(&self) -> Result<PolicyOrPolicyGroupSettings> {
let settings = SettingsJSON::try_from(self.settings.clone().unwrap_or_default())?;
Ok(PolicySettings::IndividualPolicy(settings))
Ok(PolicyOrPolicyGroupSettings::Policy(settings))
}
}

/// Describes a policy that can be either an individual policy or a group policy.
#[derive(Deserialize, Debug, Clone)]
#[serde(untagged, rename_all = "camelCase")]
pub enum Policy {
pub enum PolicyOrPolicyGroup {
/// An individual policy
Individual {
Policy {
/// The URL where the policy is located
url: String,
#[serde(default)]
Expand All @@ -324,36 +324,36 @@ pub enum Policy {
context_aware_resources: BTreeSet<ContextAwareResource>,
},
/// A group of policies that are evaluated together using a given expression
Group {
PolicyGroup {
/// The mode of the policy
#[serde(default)]
policy_mode: PolicyMode,
/// The policies that make up for this group
/// Key is a unique identifier
policies: HashMap<String, GroupPolicyMember>,
members: HashMap<String, PolicyGroupMember>,
/// The expression that is used to evaluate the group of policies
expression: String,
/// The message that is returned when the group of policies evaluates to false
message: String,
},
}

impl Policy {
pub fn settings(&self) -> Result<PolicySettings> {
impl PolicyOrPolicyGroup {
pub fn settings(&self) -> Result<PolicyOrPolicyGroupSettings> {
match self {
Policy::Individual { settings, .. } => {
PolicyOrPolicyGroup::Policy { settings, .. } => {
let settings = SettingsJSON::try_from(settings.clone().unwrap_or_default())?;
Ok(PolicySettings::IndividualPolicy(settings))
Ok(PolicyOrPolicyGroupSettings::Policy(settings))
}
Policy::Group {
PolicyOrPolicyGroup::PolicyGroup {
expression,
message,
policies,
members: policies,
..
} => Ok(PolicySettings::GroupPolicy {
} => Ok(PolicyOrPolicyGroupSettings::PolicyGroup {
expression: expression.clone(),
message: message.clone(),
sub_policies: policies.keys().cloned().collect(),
members: policies.keys().cloned().collect(),
}),
}
}
Expand Down Expand Up @@ -391,9 +391,9 @@ fn convert_yaml_map_to_json(
/// and Policy as values. The key is the name of the policy as provided by the user
/// inside of the configuration file. This name is used to build the API path
/// exposing the policy.
fn read_policies_file(path: &Path) -> Result<HashMap<String, Policy>> {
fn read_policies_file(path: &Path) -> Result<HashMap<String, PolicyOrPolicyGroup>> {
let settings_file = File::open(path)?;
let ps: HashMap<String, Policy> = serde_yaml::from_reader(&settings_file)?;
let ps: HashMap<String, PolicyOrPolicyGroup> = serde_yaml::from_reader(&settings_file)?;
Ok(ps)
}

Expand Down Expand Up @@ -442,13 +442,13 @@ example:
"#, json!({"counter": 1, "items": ["a", "b"], "nested": {"key": "value"}})
)]
fn handle_settings_conversion(#[case] input: &str, #[case] expected: serde_json::Value) {
let policies: HashMap<String, Policy> = serde_yaml::from_str(input).unwrap();
let policies: HashMap<String, PolicyOrPolicyGroup> = serde_yaml::from_str(input).unwrap();
assert!(!policies.is_empty());

let policy = policies.get("example").unwrap();
let settings = policy.settings().unwrap();
match settings {
PolicySettings::IndividualPolicy(settings) => {
PolicyOrPolicyGroupSettings::Policy(settings) => {
assert_eq!(serde_json::Value::Object(settings.0), expected);
}
_ => panic!("Expected an Individual policy"),
Expand Down
4 changes: 2 additions & 2 deletions src/evaluation/errors.rs
Original file line number Diff line number Diff line change
Expand Up @@ -16,6 +16,6 @@ pub enum EvaluationError {
#[error("WebAssembly failure: {0}")]
WebAssemblyError(String),

#[error("Group policy used as individual policy: {0}")]
GroupPolicyUsedAsIndividualPolicy(String),
#[error("Attempted to rehydrated policy group '{0}'")]
CannotRehydratePolicyGroup(String),
}
Loading

0 comments on commit 9d638c1

Please sign in to comment.