Skip to content

Commit

Permalink
Port the code to latest policy-evaluator API
Browse files Browse the repository at this point in the history
Make the code work with the changes introduced by the latest
version of policy-evaluator
  • Loading branch information
flavio committed Jul 9, 2021
1 parent c475a6b commit cb05703
Show file tree
Hide file tree
Showing 4 changed files with 25 additions and 5 deletions.
4 changes: 2 additions & 2 deletions Cargo.lock

Some generated files are not rendered by default. Learn more about how customized files appear on GitHub.

2 changes: 1 addition & 1 deletion Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,7 @@ k8s-openapi = { version = "0.11.0", default-features = false, features = ["v1_20
kube = "0.51.0"
kubewarden-policy-sdk = "0.2.3"
mdcat = "0.22"
policy-evaluator = { git = "https://github.com/kubewarden/policy-evaluator", tag = "v0.1.18" }
policy-evaluator = { git = "https://github.com/kubewarden/policy-evaluator", tag = "v0.1.19" }
policy-fetcher = { git = "https://github.com/kubewarden/policy-fetcher", tag = "v0.1.13" }
pretty-bytes = "0.2.2"
prettytable-rs = "^0.8"
Expand Down
6 changes: 5 additions & 1 deletion src/annotate.rs
Original file line number Diff line number Diff line change
Expand Up @@ -17,7 +17,11 @@ pub(crate) fn write_annotation(
}

fn protocol_detector(wasm_path: PathBuf) -> Result<ProtocolVersion> {
let policy_evaluator = PolicyEvaluator::from_file(wasm_path.as_path(), None)?;
let policy_evaluator = PolicyEvaluator::from_file(
String::from(wasm_path.to_string_lossy()),
wasm_path.as_path(),
None,
)?;
policy_evaluator
.protocol_version()
.map_err(|e| anyhow!("Cannot compute ProtocolVersion used by the policy: {:?}", e))
Expand Down
18 changes: 17 additions & 1 deletion src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@ use anyhow::{anyhow, Result};
use kube::Client;
use policy_evaluator::{
cluster_context::ClusterContext,
constants::*,
policy_evaluator::{PolicyEvaluator, ValidateRequest},
policy_metadata::Metadata,
};
Expand All @@ -28,7 +29,8 @@ pub(crate) async fn pull_and_run(
.map_err(|e| anyhow!("error pulling policy {}: {}", uri, e))?;
let policy_path = policy_path.as_path();

if let Some(metadata) = Metadata::from_path(policy_path)? {
let metadata = Metadata::from_path(policy_path)?;
if let Some(ref metadata) = metadata {
if metadata.context_aware {
println!("Fetching Kubernetes context since this policy is context-aware");

Expand All @@ -42,9 +44,11 @@ pub(crate) async fn pull_and_run(
.map_err(|e| anyhow!("could not initialize a cluster context: {}", e))?;
}
}
let policy_id = read_policy_title_from_metadata(metadata).unwrap_or_else(|| uri.clone());

let request = serde_json::from_str::<serde_json::Value>(&request)?;
let policy_evaluator = PolicyEvaluator::from_file(
policy_id,
policy_path,
settings.map_or(Ok(None), |settings| {
if settings.is_empty() {
Expand Down Expand Up @@ -90,3 +94,15 @@ pub(crate) async fn pull_and_run(

Ok(())
}

fn read_policy_title_from_metadata(metadata: Option<Metadata>) -> Option<String> {
match metadata {
Some(ref metadata) => match metadata.annotations {
Some(ref annotations) => annotations
.get(KUBEWARDEN_ANNOTATION_POLICY_TITLE)
.map(Clone::clone),
None => None,
},
None => None,
}
}

0 comments on commit cb05703

Please sign in to comment.