-
Notifications
You must be signed in to change notification settings - Fork 8
Commit
This commit does not belong to any branch on this repository, and may belong to a fork outside of the repository.
feat: Adds structures and functions to parse policy files.
- Loading branch information
1 parent
86864de
commit db4a465
Showing
10 changed files
with
911 additions
and
13 deletions.
There are no files selected for viewing
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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) | ||
} | ||
} |
Oops, something went wrong.