Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

fix: Make Rego inventories ordered and deterministic #655

Merged
merged 2 commits into from
Nov 23, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
32 changes: 13 additions & 19 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 @@ -22,7 +22,7 @@ lazy_static = "1.4.0"
pulldown-cmark-mdcat = { version = "2.1.0", default-features = false, features = [
"regex-fancy",
] }
policy-evaluator = { git = "https://github.com/kubewarden/policy-evaluator", tag = "v0.12.1" }
policy-evaluator = { git = "https://github.com/kubewarden/policy-evaluator", tag = "v0.12.2" }
prettytable-rs = "^0.10"
pulldown-cmark = { version = "0.9.3", default-features = false }
regex = "1"
Expand Down
15 changes: 8 additions & 7 deletions src/run.rs
Original file line number Diff line number Diff line change
Expand Up @@ -9,8 +9,8 @@ use policy_evaluator::{
policy_fetcher::{sources::Sources, verify::FulcioAndRekorData, PullDestination},
policy_metadata::{ContextAwareResource, Metadata, PolicyType},
};
use std::collections::BTreeSet;
use std::{
collections::HashSet,
net::{Ipv4Addr, Ipv6Addr},
path::Path,
};
Expand Down Expand Up @@ -299,15 +299,15 @@ fn has_raw_policy_type(metadata: Option<&Metadata>) -> bool {
fn compute_context_aware_resources(
metadata: Option<&Metadata>,
cfg: &PullAndRunSettings,
) -> HashSet<ContextAwareResource> {
) -> BTreeSet<ContextAwareResource> {
match metadata {
None => {
info!("Policy is not annotated, access to Kubernetes resources is not allowed");
HashSet::new()
BTreeSet::new()
}
Some(metadata) => {
if metadata.context_aware_resources.is_empty() {
return HashSet::new();
return BTreeSet::new();
}

if cfg.allow_context_aware_resources {
Expand All @@ -317,7 +317,7 @@ fn compute_context_aware_resources(
warn!("Policy requires access to Kubernetes resources at evaluation time. During this execution the access to Kubernetes resources is denied. This can cause the policy to not behave properly");
warn!("Carefully review which types of Kubernetes resources the policy needs via the `inspect` command, then run the policy using the `--allow-context-aware` flag.");

HashSet::new()
BTreeSet::new()
}
}
}
Expand Down Expand Up @@ -585,7 +585,8 @@ mod tests {

#[test]
fn prevent_access_to_kubernetes_resources_when_allow_context_aware_resources_is_disabled() {
let mut context_aware_resources = HashSet::new();
let mut context_aware_resources = BTreeSet::new();

context_aware_resources.insert(ContextAwareResource {
api_version: "v1".to_string(),
kind: "Pod".to_string(),
Expand All @@ -607,7 +608,7 @@ mod tests {

#[test]
fn allow_access_to_kubernetes_resources_when_allow_context_aware_resources_is_enabled() {
let mut context_aware_resources = HashSet::new();
let mut context_aware_resources = BTreeSet::new();
context_aware_resources.insert(ContextAwareResource {
api_version: "v1".to_string(),
kind: "Pod".to_string(),
Expand Down
19 changes: 9 additions & 10 deletions src/scaffold.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,7 +2,7 @@ use anyhow::{anyhow, Result};
use k8s_openapi::apimachinery::pkg::apis::meta::v1::ObjectMeta;
use policy_evaluator::validator::Validate;
use serde::{Deserialize, Serialize};
use std::collections::{BTreeMap, HashMap, HashSet};
use std::collections::{BTreeMap, BTreeSet, HashMap};
use std::convert::TryFrom;
use std::fs::{self, File};
use std::path::PathBuf;
Expand Down Expand Up @@ -58,14 +58,14 @@ struct ClusterAdmissionPolicySpec {
#[serde(skip_serializing_if = "is_true")]
background_audit: bool,
#[serde(skip_serializing_if = "is_empty")]
context_aware_resources: HashSet<ContextAwareResource>,
context_aware_resources: BTreeSet<ContextAwareResource>,
}

fn is_true(b: &bool) -> bool {
*b
}

fn is_empty(h: &HashSet<ContextAwareResource>) -> bool {
fn is_empty(h: &BTreeSet<ContextAwareResource>) -> bool {
h.is_empty()
}

Expand Down Expand Up @@ -196,7 +196,7 @@ fn generate_yaml_resource(
warn!("Carefully review which types of Kubernetes resources the policy needs via the `inspect` command an populate the `contextAwareResources` accordingly.");
warn!("Otherwise, invoke the `scaffold` command using the `--allow-context-aware` flag.");

scaffold_data.metadata.context_aware_resources = HashSet::new();
scaffold_data.metadata.context_aware_resources = BTreeSet::new();
}
}

Expand Down Expand Up @@ -331,7 +331,6 @@ pub(crate) fn artifacthub(
#[cfg(test)]
mod tests {
use super::*;
use std::collections::HashSet;

fn mock_metadata_with_no_annotations() -> Metadata {
Metadata {
Expand All @@ -340,7 +339,7 @@ mod tests {
annotations: None,
mutating: false,
background_audit: true,
context_aware_resources: HashSet::new(),
context_aware_resources: BTreeSet::new(),
execution_mode: Default::default(),
policy_type: Default::default(),
minimum_kubewarden_version: None,
Expand All @@ -357,7 +356,7 @@ mod tests {
)])),
mutating: false,
background_audit: true,
context_aware_resources: HashSet::new(),
context_aware_resources: BTreeSet::new(),
execution_mode: Default::default(),
policy_type: Default::default(),
minimum_kubewarden_version: None,
Expand All @@ -384,7 +383,7 @@ mod tests {
])),
mutating: false,
background_audit: true,
context_aware_resources: HashSet::new(),
context_aware_resources: BTreeSet::new(),
execution_mode: Default::default(),
policy_type: Default::default(),
minimum_kubewarden_version: None,
Expand Down Expand Up @@ -529,7 +528,7 @@ mod tests {

#[test]
fn scaffold_cluster_admission_policy_with_context_aware_enabled() {
let mut context_aware_resources: HashSet<ContextAwareResource> = HashSet::new();
let mut context_aware_resources: BTreeSet<ContextAwareResource> = BTreeSet::new();
context_aware_resources.insert(ContextAwareResource {
api_version: "v1".to_string(),
kind: "Pod".to_string(),
Expand Down Expand Up @@ -559,7 +558,7 @@ mod tests {

#[test]
fn scaffold_cluster_admission_policy_with_context_aware_disabled() {
let mut context_aware_resources: HashSet<ContextAwareResource> = HashSet::new();
let mut context_aware_resources: BTreeSet<ContextAwareResource> = BTreeSet::new();
context_aware_resources.insert(ContextAwareResource {
api_version: "v1".to_string(),
kind: "Pod".to_string(),
Expand Down
Loading