Skip to content

Commit 94841fe

Browse files
committed
group policy: cleanups
* Rename `PolicyId` to `PolicyID` * Config: policy group policies are now stored inside of a key named `policies` instead of `members` * `EvaluationEnvironment`: introduce builder patter, remove duplication between policies and group policies Signed-off-by: Flavio Castelli <fcastelli@suse.com>
1 parent e3d3568 commit 94841fe

9 files changed

+382
-322
lines changed

src/api/service.rs

+4-4
Original file line numberDiff line numberDiff line change
@@ -9,7 +9,7 @@ use tracing::info;
99

1010
use crate::{
1111
config::PolicyMode,
12-
evaluation::{errors::EvaluationError, EvaluationEnvironment, PolicyId},
12+
evaluation::{errors::EvaluationError, EvaluationEnvironment, PolicyID},
1313
metrics,
1414
};
1515

@@ -34,7 +34,7 @@ pub(crate) fn evaluate(
3434
request_origin: RequestOrigin,
3535
) -> Result<AdmissionResponse, EvaluationError> {
3636
let start_time = Instant::now();
37-
let policy_id: PolicyId = policy_id.parse()?;
37+
let policy_id: PolicyID = policy_id.parse()?;
3838

3939
let vanilla_validation_response = match evaluation_environment
4040
.clone()
@@ -147,7 +147,7 @@ pub(crate) fn evaluate(
147147
// - A policy might be running in "Monitor" mode, that always
148148
// accepts the request (without mutation), logging the answer
149149
fn validation_response_with_constraints(
150-
policy_id: &PolicyId,
150+
policy_id: &PolicyID,
151151
policy_mode: &PolicyMode,
152152
allowed_to_mutate: bool,
153153
validation_response: AdmissionResponse,
@@ -204,7 +204,7 @@ mod tests {
204204
use super::*;
205205

206206
lazy_static! {
207-
static ref POLICY_ID: PolicyId = PolicyId::Policy("policy-id".to_string());
207+
static ref POLICY_ID: PolicyID = PolicyID::Policy("policy-id".to_string());
208208
}
209209

210210
fn create_evaluation_environment_that_accepts_request(

src/config.rs

+10-11
Original file line numberDiff line numberDiff line change
@@ -207,23 +207,23 @@ fn policies(matches: &clap::ArgMatches) -> Result<HashMap<String, PolicyOrPolicy
207207

208208
// Validate the policies and policy groups:
209209
// - ensure policy names do not contain a '/' character
210-
// - ensure names of policy group members do not contain a '/' character
210+
// - ensure names of policy group's policies do not contain a '/' character
211211
fn validate_policies(policies: &HashMap<String, PolicyOrPolicyGroup>) -> Result<()> {
212212
for (name, policy) in policies.iter() {
213213
if name.contains('/') {
214214
return Err(anyhow!("policy name '{}' contains a '/' character", name));
215215
}
216-
if let PolicyOrPolicyGroup::PolicyGroup { members, .. } = policy {
217-
let members_with_invalid_name: Vec<String> = members
216+
if let PolicyOrPolicyGroup::PolicyGroup { policies, .. } = policy {
217+
let policies_with_invalid_name: Vec<String> = policies
218218
.iter()
219219
.filter_map(|(id, _)| if id.contains('/') { Some(id) } else { None })
220220
.cloned()
221221
.collect();
222-
if !members_with_invalid_name.is_empty() {
222+
if !policies_with_invalid_name.is_empty() {
223223
return Err(anyhow!(
224-
"policy group '{}' contains members with invalid names: {:?}",
224+
"policy group '{}' contains policies with invalid names: {:?}",
225225
name,
226-
members_with_invalid_name
226+
policies_with_invalid_name
227227
));
228228
}
229229
}
@@ -307,7 +307,7 @@ pub enum PolicyOrPolicyGroupSettings {
307307
PolicyGroup {
308308
expression: String,
309309
message: String,
310-
members: Vec<String>,
310+
policies: Vec<String>,
311311
},
312312
}
313313

@@ -357,8 +357,7 @@ pub enum PolicyOrPolicyGroup {
357357
policy_mode: PolicyMode,
358358
/// The policies that make up for this group
359359
/// Key is a unique identifier
360-
#[serde(rename = "policies")]
361-
members: HashMap<String, PolicyGroupMember>,
360+
policies: HashMap<String, PolicyGroupMember>,
362361
/// The expression that is used to evaluate the group of policies
363362
expression: String,
364363
/// The message that is returned when the group of policies evaluates to false
@@ -376,12 +375,12 @@ impl PolicyOrPolicyGroup {
376375
PolicyOrPolicyGroup::PolicyGroup {
377376
expression,
378377
message,
379-
members: policies,
378+
policies,
380379
..
381380
} => Ok(PolicyOrPolicyGroupSettings::PolicyGroup {
382381
expression: expression.clone(),
383382
message: message.clone(),
384-
members: policies.keys().cloned().collect(),
383+
policies: policies.keys().cloned().collect(),
385384
}),
386385
}
387386
}

src/evaluation.rs

+3-1
Original file line numberDiff line numberDiff line change
@@ -7,5 +7,7 @@ pub(crate) mod precompiled_policy;
77
#[mockall_double::double]
88
pub(crate) use evaluation_environment::EvaluationEnvironment;
99

10+
pub(crate) use evaluation_environment::EvaluationEnvironmentBuilder;
11+
1012
pub(crate) mod policy_id;
11-
pub(crate) use policy_id::PolicyId;
13+
pub(crate) use policy_id::PolicyID;

0 commit comments

Comments
 (0)