Skip to content

Commit 71caebf

Browse files
committed
fix naming
Signed-off-by: Flavio Castelli <fcastelli@suse.com>
1 parent e0f165e commit 71caebf

8 files changed

+115
-112
lines changed

src/api/service.rs

+9-9
Original file line numberDiff line numberDiff line change
@@ -208,16 +208,16 @@ mod tests {
208208
policy_mode: PolicyMode,
209209
) -> EvaluationEnvironment {
210210
let mut mock_evaluation_environment = EvaluationEnvironment::default();
211-
212-
mock_evaluation_environment.expect_validate()
213-
urning(|_policy_id, request| {
214-
dmissionResponse {
215-
uid: request.uid().to_owned(),
216-
allowed: true,
217-
..Default::default()
218-
})
211+
mock_evaluation_environment
212+
.expect_validate()
213+
.returning(|_policy_id, request| {
214+
Ok(AdmissionResponse {
215+
uid: request.uid().to_owned(),
216+
allowed: true,
217+
..Default::default()
218+
})
219219
});
220-
220+
221221
mock_evaluation_environment
222222
.expect_get_policy_mode()
223223
.returning(move |_policy_id| Ok(policy_mode.clone()));

src/config.rs

+27-27
Original file line numberDiff line numberDiff line change
@@ -31,7 +31,7 @@ lazy_static! {
3131
pub struct Config {
3232
pub addr: SocketAddr,
3333
pub sources: Option<Sources>,
34-
pub policies: HashMap<String, Policy>,
34+
pub policies: HashMap<String, PolicyOrPolicyGroup>,
3535
pub policies_download_dir: PathBuf,
3636
pub ignore_kubernetes_connection_failure: bool,
3737
pub always_accept_admission_reviews_on_namespace: Option<String>,
@@ -193,7 +193,7 @@ fn tls_files(matches: &clap::ArgMatches) -> Result<(String, String)> {
193193
}
194194
}
195195

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

277277
#[derive(Debug, Clone)]
278-
pub enum PolicySettings {
279-
IndividualPolicy(SettingsJSON),
280-
GroupPolicy {
278+
pub enum PolicyOrPolicyGroupSettings {
279+
Policy(SettingsJSON),
280+
PolicyGroup {
281281
expression: String,
282282
message: String,
283-
sub_policies: Vec<String>,
283+
members: Vec<String>,
284284
},
285285
}
286286

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

300-
impl GroupPolicyMember {
301-
pub fn settings(&self) -> Result<PolicySettings> {
300+
impl PolicyGroupMember {
301+
pub fn settings(&self) -> Result<PolicyOrPolicyGroupSettings> {
302302
let settings = SettingsJSON::try_from(self.settings.clone().unwrap_or_default())?;
303-
Ok(PolicySettings::IndividualPolicy(settings))
303+
Ok(PolicyOrPolicyGroupSettings::Policy(settings))
304304
}
305305
}
306306

307307
/// Describes a policy that can be either an individual policy or a group policy.
308308
#[derive(Deserialize, Debug, Clone)]
309309
#[serde(untagged, rename_all = "camelCase")]
310-
pub enum Policy {
310+
pub enum PolicyOrPolicyGroup {
311311
/// An individual policy
312-
Individual {
312+
Policy {
313313
/// The URL where the policy is located
314314
url: String,
315315
#[serde(default)]
@@ -324,36 +324,36 @@ pub enum Policy {
324324
context_aware_resources: BTreeSet<ContextAwareResource>,
325325
},
326326
/// A group of policies that are evaluated together using a given expression
327-
Group {
327+
PolicyGroup {
328328
/// The mode of the policy
329329
#[serde(default)]
330330
policy_mode: PolicyMode,
331331
/// The policies that make up for this group
332332
/// Key is a unique identifier
333-
policies: HashMap<String, GroupPolicyMember>,
333+
members: HashMap<String, PolicyGroupMember>,
334334
/// The expression that is used to evaluate the group of policies
335335
expression: String,
336336
/// The message that is returned when the group of policies evaluates to false
337337
message: String,
338338
},
339339
}
340340

341-
impl Policy {
342-
pub fn settings(&self) -> Result<PolicySettings> {
341+
impl PolicyOrPolicyGroup {
342+
pub fn settings(&self) -> Result<PolicyOrPolicyGroupSettings> {
343343
match self {
344-
Policy::Individual { settings, .. } => {
344+
PolicyOrPolicyGroup::Policy { settings, .. } => {
345345
let settings = SettingsJSON::try_from(settings.clone().unwrap_or_default())?;
346-
Ok(PolicySettings::IndividualPolicy(settings))
346+
Ok(PolicyOrPolicyGroupSettings::Policy(settings))
347347
}
348-
Policy::Group {
348+
PolicyOrPolicyGroup::PolicyGroup {
349349
expression,
350350
message,
351-
policies,
351+
members: policies,
352352
..
353-
} => Ok(PolicySettings::GroupPolicy {
353+
} => Ok(PolicyOrPolicyGroupSettings::PolicyGroup {
354354
expression: expression.clone(),
355355
message: message.clone(),
356-
sub_policies: policies.keys().cloned().collect(),
356+
members: policies.keys().cloned().collect(),
357357
}),
358358
}
359359
}
@@ -391,9 +391,9 @@ fn convert_yaml_map_to_json(
391391
/// and Policy as values. The key is the name of the policy as provided by the user
392392
/// inside of the configuration file. This name is used to build the API path
393393
/// exposing the policy.
394-
fn read_policies_file(path: &Path) -> Result<HashMap<String, Policy>> {
394+
fn read_policies_file(path: &Path) -> Result<HashMap<String, PolicyOrPolicyGroup>> {
395395
let settings_file = File::open(path)?;
396-
let ps: HashMap<String, Policy> = serde_yaml::from_reader(&settings_file)?;
396+
let ps: HashMap<String, PolicyOrPolicyGroup> = serde_yaml::from_reader(&settings_file)?;
397397
Ok(ps)
398398
}
399399

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

448448
let policy = policies.get("example").unwrap();
449449
let settings = policy.settings().unwrap();
450450
match settings {
451-
PolicySettings::IndividualPolicy(settings) => {
451+
PolicyOrPolicyGroupSettings::Policy(settings) => {
452452
assert_eq!(serde_json::Value::Object(settings.0), expected);
453453
}
454454
_ => panic!("Expected an Individual policy"),

src/evaluation/errors.rs

+2-2
Original file line numberDiff line numberDiff line change
@@ -16,6 +16,6 @@ pub enum EvaluationError {
1616
#[error("WebAssembly failure: {0}")]
1717
WebAssemblyError(String),
1818

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

0 commit comments

Comments
 (0)