forked from seedwing-io/seedwing-policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
115 lines (105 loc) · 3.95 KB
/
mod.rs
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
//! Data sources for the policy engine.
//!
//! A data source is a way to provide mostly static data available to the engine to use during evaluation.
use crate::runtime::RuntimeError;
use crate::value::RuntimeValue;
use std::collections::HashMap;
use std::fmt::Debug;
use std::fs::File;
use std::io::Read;
use std::path::PathBuf;
/// A source of data can be used when evaluating policies.
pub trait DataSource: Send + Sync + Debug {
/// Retrieve the data at the provided path, if found.
fn get(&self, path: &str) -> Result<Option<RuntimeValue>, RuntimeError>;
}
#[derive(Debug)]
pub enum MemDataSourceType {
String(String),
Bytes(Vec<u8>),
}
/// A source of data read from a strings.
///
#[derive(Debug)]
pub struct MemDataSource {
map: HashMap<String, MemDataSourceType>,
}
impl MemDataSource {
pub fn new(map: HashMap<String, MemDataSourceType>) -> Self {
Self { map }
}
}
impl DataSource for MemDataSource {
fn get(&self, path: &str) -> Result<Option<RuntimeValue>, RuntimeError> {
match self.map.get(path) {
Some(dst) => match dst {
MemDataSourceType::String(string) => Ok(Some(string.as_str().into())),
MemDataSourceType::Bytes(bytes) => Ok(Some(RuntimeValue::Octets(bytes.clone()))),
},
None => Err(RuntimeError::NoSuchPath(path.to_string())),
}
}
}
/// A source of data read from a directory.
///
/// The path parameter is used to locate the source file within the root directory.
#[derive(Debug)]
pub struct DirectoryDataSource {
root: PathBuf,
}
impl DirectoryDataSource {
/// Create a directory data source based on the root directory parameter.
pub fn new(root: PathBuf) -> Self {
Self { root }
}
}
impl DataSource for DirectoryDataSource {
fn get(&self, path: &str) -> Result<Option<RuntimeValue>, RuntimeError> {
let target = self.root.join(path);
if target.exists() {
if target.is_dir() {
Err(RuntimeError::FileUnreadable(target))
} else if let Some(name) = target.file_name() {
log::info!("read from file: {:?}", name);
if name.to_string_lossy().ends_with(".json") {
// parse as JSON
if let Ok(file) = File::open(target.clone()) {
let json: Result<serde_json::Value, _> = serde_json::from_reader(file);
match json {
Ok(json) => Ok(Some(json.into())),
Err(e) => Err(RuntimeError::JsonError(target, e)),
}
} else {
Err(RuntimeError::FileUnreadable(target))
}
} else if name.to_string_lossy().ends_with(".yaml")
|| name.to_string_lossy().ends_with(".yml")
{
// parse as YAML
if let Ok(file) = File::open(target.clone()) {
let yaml: Result<serde_json::Value, _> = serde_yaml::from_reader(file);
match yaml {
Ok(yaml) => Ok(Some(yaml.into())),
Err(e) => Err(RuntimeError::YamlError(target, e)),
}
} else {
Err(RuntimeError::FileUnreadable(target))
}
} else if let Ok(mut file) = File::open(target.clone()) {
// just octets
let mut octets = Vec::new();
file.read_to_end(&mut octets)
.map_err(|_| RuntimeError::FileUnreadable(target))?;
Ok(Some(RuntimeValue::Octets(octets)))
} else {
Err(RuntimeError::FileUnreadable(target))
}
} else {
Ok(None)
}
} else {
log::error!("{:?} not found", target);
Ok(None)
}
}
}