Skip to content

Commit

Permalink
feat: Adds structures and functions to parse policy files.
Browse files Browse the repository at this point in the history
  • Loading branch information
mchernicoff authored and alilleybrinker committed Sep 4, 2024
1 parent 86864de commit db4a465
Show file tree
Hide file tree
Showing 10 changed files with 911 additions and 13 deletions.
Original file line number Diff line number Diff line change
@@ -1,3 +1,7 @@
// SPDX-License-Identifier: Apache-2.0

//! General shared types and functions for KDL files
use kdl::KdlNode;

// Helper trait to make it easier to parse KdlNodes into our own types
Expand Down Expand Up @@ -44,6 +48,7 @@ where
macro_rules! string_newtype_parse_kdl_node {
($type:ty, $identifier:expr) => {
impl $type {
#[allow(dead_code)]
pub fn new(value: String) -> Self {
Self(value)
}
Expand Down
2 changes: 2 additions & 0 deletions hipcheck/src/main.rs
Original file line number Diff line number Diff line change
Expand Up @@ -13,9 +13,11 @@ mod data;
mod engine;
mod error;
mod init;
mod kdl_helper;
mod metric;
#[allow(unused)]
mod plugin;
mod policy;
mod policy_exprs;
mod report;
mod session;
Expand Down
4 changes: 2 additions & 2 deletions hipcheck/src/plugin/download_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,9 +1,9 @@
use super::{extract_data, PluginName, PluginPublisher, PluginVersion};
use super::{PluginName, PluginPublisher, PluginVersion};
use crate::cache::plugin_cache::HcPluginCache;
use crate::context::Context;
use crate::kdl_helper::{extract_data, ParseKdlNode};
use crate::plugin::retrieval::{download_plugin, extract_plugin};
use crate::plugin::supported_arch::SupportedArch;
use crate::plugin::ParseKdlNode;
use crate::string_newtype_parse_kdl_node;
use crate::util::http::agent::agent;
use crate::{error::Error, hc_error};
Expand Down
4 changes: 2 additions & 2 deletions hipcheck/src/plugin/mod.rs
Original file line number Diff line number Diff line change
@@ -1,11 +1,12 @@
mod download_manifest;
mod kdl_parsing;
mod manager;
mod plugin_manifest;
mod retrieval;
mod supported_arch;
mod types;

use crate::context::Context;
use crate::kdl_helper::{extract_data, ParseKdlNode};
pub use crate::plugin::manager::*;
pub use crate::plugin::types::*;
use crate::policy_exprs::Expr;
Expand All @@ -14,7 +15,6 @@ pub use download_manifest::{
ArchiveFormat, DownloadManifest, DownloadManifestEntry, HashAlgorithm, HashWithDigest,
};
use futures::future::join_all;
pub use kdl_parsing::{extract_data, ParseKdlNode};
pub use plugin_manifest::{PluginManifest, PluginName, PluginPublisher, PluginVersion};
use serde_json::Value;
use std::collections::HashMap;
Expand Down
3 changes: 1 addition & 2 deletions hipcheck/src/plugin/plugin_manifest.rs
Original file line number Diff line number Diff line change
@@ -1,6 +1,5 @@
use super::extract_data;
use crate::kdl_helper::{extract_data, ParseKdlNode};
use crate::plugin::supported_arch::SupportedArch;
use crate::plugin::ParseKdlNode;
use crate::string_newtype_parse_kdl_node;
use crate::{error::Error, hc_error};
use core::panic;
Expand Down
52 changes: 52 additions & 0 deletions hipcheck/src/policy/mod.rs
Original file line number Diff line number Diff line change
@@ -0,0 +1,52 @@
// SPDX-License-Identifier: Apache-2.0

//! Data types and functions for parsing policy KDL files
mod policy_file;
mod tests;

use crate::kdl_helper::extract_data;
use crate::policy::policy_file::{PolicyAnalyze, PolicyPluginList};
use crate::util::fs as file;
use crate::{error::Result, hc_error};
use kdl::KdlDocument;
use std::path::Path;
use std::str::FromStr;

#[derive(Clone, Debug, PartialEq, Eq)]
pub struct PolicyFile {
pub plugins: PolicyPluginList,
pub analyze: PolicyAnalyze,
}

impl FromStr for PolicyFile {
type Err = crate::Error;

fn from_str(s: &str) -> Result<Self> {
let document =
KdlDocument::from_str(s).map_err(|e| hc_error!("Error parsing policy file: {}", e))?;
let nodes = document.nodes();

let plugins: PolicyPluginList =
extract_data(nodes).ok_or_else(|| hc_error!("Could not parse 'plugins'"))?;
let analyze: PolicyAnalyze =
extract_data(nodes).ok_or_else(|| hc_error!("Could not parse 'analyze'"))?;

Ok(Self { plugins, analyze })
}
}

impl PolicyFile {
/// Load policy from the given file.
pub fn load_from(policy_path: &Path) -> Result<PolicyFile> {
if policy_path.is_dir() {
return Err(hc_error!(
"Hipcheck policy path must be a file, not a directory."
));
}
file::exists(policy_path)?;
let policy = PolicyFile::from_str(&file::read_string(policy_path)?)?;

Ok(policy)
}
}
Loading

0 comments on commit db4a465

Please sign in to comment.