From 850d36232b5bc579b575328260087f8819217b03 Mon Sep 17 00:00:00 2001 From: Fabrizio Sestito Date: Wed, 25 Oct 2023 14:46:29 +0200 Subject: [PATCH 1/2] feat: add policy type to metadata Signed-off-by: Fabrizio Sestito --- src/policy_artifacthub.rs | 2 ++ src/policy_metadata.rs | 14 ++++++++++++++ 2 files changed, 16 insertions(+) diff --git a/src/policy_artifacthub.rs b/src/policy_artifacthub.rs index 1a92b044..35b309f1 100644 --- a/src/policy_artifacthub.rs +++ b/src/policy_artifacthub.rs @@ -509,6 +509,7 @@ mod tests { background_audit: true, context_aware_resources: HashSet::new(), execution_mode: Default::default(), + policy_type: Default::default(), minimum_kubewarden_version: None, } } @@ -574,6 +575,7 @@ mod tests { context_aware_resources, execution_mode: Default::default(), minimum_kubewarden_version: None, + policy_type: Default::default(), } } diff --git a/src/policy_metadata.rs b/src/policy_metadata.rs index a0950110..21e8b5e7 100644 --- a/src/policy_metadata.rs +++ b/src/policy_metadata.rs @@ -128,6 +128,15 @@ pub struct ContextAwareResource { pub kind: String, } +#[derive(Deserialize, Serialize, Debug, Clone, Default)] +pub enum PolicyType { + #[default] + #[serde(rename = "kubernetes")] + Kubernetes, + #[serde(rename = "raw")] + Raw, +} + #[derive(Deserialize, Serialize, Debug, Clone, Validate)] #[serde(rename_all = "camelCase")] #[validate(schema(function = "validate_metadata", skip_on_field_errors = false))] @@ -144,6 +153,8 @@ pub struct Metadata { #[serde(default)] pub execution_mode: PolicyExecutionMode, #[serde(default)] + pub policy_type: PolicyType, + #[serde(default)] #[validate] pub context_aware_resources: HashSet, #[serde(skip_serializing_if = "Option::is_none")] @@ -163,6 +174,7 @@ impl Default for Metadata { mutating: false, background_audit: true, execution_mode: PolicyExecutionMode::KubewardenWapc, + policy_type: PolicyType::Kubernetes, context_aware_resources: HashSet::new(), minimum_kubewarden_version: None, } @@ -345,6 +357,7 @@ mod tests { "backgroundAudit": true, "contextAwareResources": [ ], "executionMode": "kubewarden-wapc", + "policyType": "kubernetes" }); let actual = serde_json::to_value(&metadata).unwrap(); @@ -417,6 +430,7 @@ mod tests { "backgroundAudit": true, "contextAwareResources": [ ], "executionMode": "kubewarden-wapc", + "policyType": "kubernetes" }); let actual = serde_json::to_value(&metadata).unwrap(); From 57f8365a4ca9909f9a3c3fcf844260ee2de46cc3 Mon Sep 17 00:00:00 2001 From: Fabrizio Sestito Date: Wed, 25 Oct 2023 16:39:20 +0200 Subject: [PATCH 2/2] test: be explicit about policy type expectation in tests Signed-off-by: Fabrizio Sestito --- src/policy_artifacthub.rs | 4 ++-- src/policy_metadata.rs | 1 + 2 files changed, 3 insertions(+), 2 deletions(-) diff --git a/src/policy_artifacthub.rs b/src/policy_artifacthub.rs index 35b309f1..055b9c6f 100644 --- a/src/policy_artifacthub.rs +++ b/src/policy_artifacthub.rs @@ -474,7 +474,7 @@ fn parse_annotations( #[cfg(test)] mod tests { use super::*; - use crate::policy_metadata::ContextAwareResource; + use crate::policy_metadata::{ContextAwareResource, PolicyType}; use assert_json_diff::assert_json_eq; use serde_json::json; use std::collections::{HashMap, HashSet}; @@ -509,7 +509,7 @@ mod tests { background_audit: true, context_aware_resources: HashSet::new(), execution_mode: Default::default(), - policy_type: Default::default(), + policy_type: PolicyType::Kubernetes, minimum_kubewarden_version: None, } } diff --git a/src/policy_metadata.rs b/src/policy_metadata.rs index 21e8b5e7..f95f6229 100644 --- a/src/policy_metadata.rs +++ b/src/policy_metadata.rs @@ -382,6 +382,7 @@ mod tests { annotations: None, background_audit: true, context_aware_resources: HashSet::new(), + policy_type: PolicyType::Kubernetes, ..Default::default() };