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

Add support for post-resolution policies #214

Merged
merged 8 commits into from
Jun 26, 2024
Merged
Show file tree
Hide file tree
Changes from 5 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
227 changes: 150 additions & 77 deletions Cargo.lock

Large diffs are not rendered by default.

3 changes: 2 additions & 1 deletion crates/weaver_checker/Cargo.toml
Original file line number Diff line number Diff line change
Expand Up @@ -24,7 +24,8 @@ walkdir.workspace = true
globset.workspace = true
miette.workspace = true

regorus = { version = "0.1.5", default-features = false, features = [
regorus = { version = "0.2.0", default-features = false, features = [
"std",
"arc",
"base64",
"base64url",
Expand Down
66 changes: 61 additions & 5 deletions crates/weaver_checker/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@
#![allow(rustdoc::broken_intra_doc_links)]
#![doc = include_str!("../README.md")]

use std::collections::HashSet;
use std::fmt::{Display, Formatter};
use std::path::Path;

Expand All @@ -11,8 +12,8 @@
use serde::Serialize;
use serde_json::to_value;
use walkdir::DirEntry;
use weaver_common::diagnostic::{DiagnosticMessage, DiagnosticMessages};

use weaver_common::diagnostic::{DiagnosticMessage, DiagnosticMessages};
use weaver_common::error::{format_errors, handle_errors, WeaverError};

use crate::violation::Violation;
Expand Down Expand Up @@ -146,6 +147,13 @@
pub struct Engine {
// The `regorus` policy engine.
engine: regorus::Engine,
// Flag to enable the coverage report.
coverage_enabled: bool,
// Number of policy packages added.
policy_package_count: usize,
// Policy packages loaded. This is used to check if a policy package has been imported
// before evaluating it.
policy_packages: HashSet<String>,
}

impl Engine {
Expand All @@ -155,21 +163,36 @@
Default::default()
}

/// Enables the coverage report.
pub fn enable_coverage(&mut self) {
self.engine.set_enable_coverage(true);
self.coverage_enabled = true;

Check warning on line 169 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L167-L169

Added lines #L167 - L169 were not covered by tests
}

/// Adds a policy file to the policy engine.
/// A policy file is a `rego` file that contains the policies to be evaluated.
///
/// # Arguments
///
/// * `policy_path` - The path to the policy file.
pub fn add_policy<P: AsRef<Path>>(&mut self, policy_path: P) -> Result<(), Error> {
pub fn add_policy<P: AsRef<Path>>(&mut self, policy_path: P) -> Result<String, Error> {
let policy_path_str = policy_path.as_ref().to_string_lossy().to_string();

self.engine
let policy_package = self
.engine

Check warning on line 182 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L182

Added line #L182 was not covered by tests
.add_policy_from_file(policy_path)
.map_err(|e| Error::InvalidPolicyFile {
file: policy_path_str.clone(),
error: e.to_string(),
})
.inspect(|_| {
self.policy_package_count += 1;
})?;
// Add the policy package defined in the imported policy file.
// Nothing prevent multiple policy files to import the same policy package.
// All the rules will be combined and evaluated together.
_ = self.policy_packages.insert(policy_package.clone());
Ok(policy_package)
}

/// Adds all the policy files present in the given directory that match the
Expand Down Expand Up @@ -224,9 +247,16 @@

handle_errors(errors)?;

self.policy_package_count += added_policy_count;
Ok(added_policy_count)
}

/// Returns the number of policy packages added to the policy engine.
#[must_use]
pub fn policy_package_count(&self) -> usize {
Fixed Show fixed Hide fixed
self.policy_package_count

Check warning on line 257 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L256-L257

Added lines #L256 - L257 were not covered by tests
}

/// Adds a data document to the policy engine.
///
/// Data versus Input: In essence, data is about what the policy engine
Expand Down Expand Up @@ -280,14 +310,39 @@

/// Returns a list of violations based on the policies, the data, the
/// input, and the given policy stage.
#[allow(clippy::print_stdout)] // Used to display the coverage (debugging purposes only)
pub fn check(&mut self, stage: PolicyStage) -> Result<Vec<Violation>, Error> {
// If we don't have any policy package that matches the stage,
// return an empty list of violations.
if !self.policy_packages.contains(&format!("data.{}", stage)) {
return Ok(vec![]);

Check warning on line 318 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L318

Added line #L318 was not covered by tests
}

let value = self
.engine
.eval_rule(format!("data.{}.deny", stage))
.map_err(|e| Error::ViolationEvaluationError {
error: e.to_string(),
})?;

// Print the coverage report if enabled
// This is useful for debugging purposes
if self.coverage_enabled {
let report =

Check warning on line 331 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L331

Added line #L331 was not covered by tests
self.engine
.get_coverage_report()
.map_err(|e| Error::ViolationEvaluationError {
error: e.to_string(),

Check warning on line 335 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L334-L335

Added lines #L334 - L335 were not covered by tests
})?;
let pretty_report =

Check warning on line 337 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L337

Added line #L337 was not covered by tests
report
.to_string_pretty()
.map_err(|e| Error::ViolationEvaluationError {
error: e.to_string(),

Check warning on line 341 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L340-L341

Added lines #L340 - L341 were not covered by tests
})?;
println!("{}", pretty_report);

Check warning on line 343 in crates/weaver_checker/src/lib.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_checker/src/lib.rs#L343

Added line #L343 was not covered by tests
Fixed Show fixed Hide fixed
}

// convert `regorus` value to `serde_json` value
let json_value = to_value(&value).map_err(|e| Error::ViolationEvaluationError {
error: e.to_string(),
Expand Down Expand Up @@ -317,7 +372,8 @@
#[test]
fn test_policy() -> Result<(), Box<dyn std::error::Error>> {
let mut engine = Engine::new();
engine.add_policy("data/policies/otel_policies.rego")?;
let policy_package = engine.add_policy("data/policies/otel_policies.rego")?;
assert_eq!(policy_package, "data.before_resolution");

let old_semconv = std::fs::read_to_string("data/registries/registry.network.old.yaml")?;
let old_semconv: Value = serde_yaml::from_str(&old_semconv)?;
Expand Down Expand Up @@ -378,7 +434,7 @@
#[test]
fn test_invalid_violation_object() {
let mut engine = Engine::new();
engine
_ = engine
.add_policy("data/policies/invalid_violation_object.rego")
.unwrap();

Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package after_resolution

# Example of rules that will be applied on resolved semconv files

# Detect `http.request.method` attribute and consider it invalid.
# This is just an example for testing purposes.
deny[invalid_attr_violation("invalid_http_attr", group.id, attr.name)] {
group := input.groups[_]
attr := group.attributes[_]
attr.name == "http.request.method"
}

invalid_attr_violation(violation_id, group_id, attr_id) = violation {
violation := {
"id": violation_id,
"type": "semconv_attribute",
"category": "attrigute",
"group": group_id,
"attr": attr_id,
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,21 @@
package after_resolution

# Example of rules that will be applied on resolved semconv files

# Detect `system.cpu.logical_number` attribute and consider it invalid.
# This is just an example for testing purposes.
deny[invalid_attr_violation("invalid_metric_attr", group.id, attr.name)] {
group := input.groups[_]
attr := group.attributes[_]
attr.name == "system.cpu.logical_number"
}

invalid_attr_violation(violation_id, group_id, attr_id) = violation {
violation := {
"id": violation_id,
"type": "semconv_attribute",
"category": "attrigute",
"group": group_id,
"attr": attr_id,
}
}
53 changes: 51 additions & 2 deletions crates/weaver_common/src/diagnostic.rs
Original file line number Diff line number Diff line change
Expand Up @@ -39,7 +39,7 @@
}

/// A generic and serializable representation of a diagnostic message
#[derive(Debug, serde::Serialize)]
#[derive(Debug, serde::Serialize, Clone)]
pub struct DiagnosticMessage {
/// The error
pub(crate) error: serde_json::Value,
Expand All @@ -48,7 +48,7 @@
}

/// A list of diagnostic messages
#[derive(Debug, serde::Serialize)]
#[derive(Debug, serde::Serialize, Clone)]
#[serde(transparent)]
pub struct DiagnosticMessages(Vec<DiagnosticMessage>);

Expand Down Expand Up @@ -86,6 +86,18 @@
Self(diag_msgs)
}

/// Creates an empty list of diagnostic messages
#[must_use]
pub fn empty() -> Self {
Self(Vec::new())

Check warning on line 92 in crates/weaver_common/src/diagnostic.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/diagnostic.rs#L91-L92

Added lines #L91 - L92 were not covered by tests
}

/// Extends the current `DiagnosticMessages` with the provided
/// `DiagnosticMessages`.
pub fn extend(&mut self, diag_msgs: DiagnosticMessages) {
self.0.extend(diag_msgs.0);

Check warning on line 98 in crates/weaver_common/src/diagnostic.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/diagnostic.rs#L97-L98

Added lines #L97 - L98 were not covered by tests
}

/// Logs all the diagnostic messages
pub fn log(&self, logger: impl Logger) {
self.0
Expand Down Expand Up @@ -139,6 +151,43 @@
}
}

/// An extension trait for `Result` that captures the diagnostic messages
pub trait ResultExt<T, E> {
/// Captures the diagnostic messages into the provided `DiagnosticMessages`
/// or returns the value if there are no diagnostic messages.
fn capture_diag_msgs_into(self, diags: &mut DiagnosticMessages) -> Option<T>;
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I like these additions. Will need to figure out if we have the right set of combinators here.


/// Combines the diagnostic messages with the provided `DiagnosticMessages`
/// and returns the `ok` value or the combined `DiagnosticMessages`.
fn combine_diag_msgs_with(self, diags: &DiagnosticMessages) -> Result<T, DiagnosticMessages>;
}

impl<T, E> ResultExt<T, E> for Result<T, E>
where
E: Into<DiagnosticMessages>,
{
fn capture_diag_msgs_into(self, diags: &mut DiagnosticMessages) -> Option<T> {
match self {
Ok(v) => Some(v),
Err(diag_msgs) => {
diags.extend(diag_msgs.into());
None

Check warning on line 174 in crates/weaver_common/src/diagnostic.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/diagnostic.rs#L169-L174

Added lines #L169 - L174 were not covered by tests
}
}
}

fn combine_diag_msgs_with(self, diags: &DiagnosticMessages) -> Result<T, DiagnosticMessages> {
match self {
Ok(v) => Ok(v),
Err(errs) => {
let mut diag_msgs: DiagnosticMessages = errs.into();
diag_msgs.extend(diags.clone());
Err(diag_msgs)

Check warning on line 185 in crates/weaver_common/src/diagnostic.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_common/src/diagnostic.rs#L179-L185

Added lines #L179 - L185 were not covered by tests
}
}
}
}

#[cfg(test)]
mod tests {
use super::*;
Expand Down
15 changes: 15 additions & 0 deletions crates/weaver_semconv/src/path.rs
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,8 @@

//! Semantic convention registry path.

use std::fmt::{Display, Formatter};

use serde::{Deserialize, Serialize};

/// A semantic convention registry path.
Expand All @@ -23,3 +25,16 @@
path: Option<String>,
},
}

impl Display for RegistryPath {
fn fmt(&self, f: &mut Formatter<'_>) -> std::fmt::Result {
let path = match self {
RegistryPath::Local { path_pattern } => format!("LocalRegistry:{}", path_pattern),
RegistryPath::GitUrl { git_url, path } => match path {
Some(path) => format!("GitRegistry:{}/{:?}", git_url, path),
None => format!("GitRegistry:{}", git_url),

Check warning on line 35 in crates/weaver_semconv/src/path.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv/src/path.rs#L30-L35

Added lines #L30 - L35 were not covered by tests
},
};
f.write_str(&path)

Check warning on line 38 in crates/weaver_semconv/src/path.rs

View check run for this annotation

Codecov / codecov/patch

crates/weaver_semconv/src/path.rs#L38

Added line #L38 was not covered by tests
}
}
Loading
Loading