forked from seedwing-io/seedwing-policy
-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathmod.rs
264 lines (235 loc) · 6.47 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
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
use crate::lang::lir::Bindings;
use crate::runtime::{
rationale::Rationale, EvaluationResult, ExecutionContext, Output, Pattern, PatternName,
RuntimeError, World,
};
use crate::value::RuntimeValue;
use std::fmt::Debug;
use std::future::Future;
use std::pin::Pin;
use crate::lang::{PatternMeta, Severity};
use std::sync::Arc;
pub mod base64;
pub mod config;
pub mod csaf;
pub mod cyclonedx;
pub mod data;
#[cfg(feature = "debug")]
pub mod debug;
#[cfg(not(target_arch = "wasm32"))]
pub mod external;
#[cfg(not(target_arch = "wasm32"))]
pub mod guac;
#[cfg(feature = "intoto")]
pub mod intoto;
pub mod iso;
pub mod jsf;
pub mod json;
pub mod kafka;
pub mod lang;
pub mod list;
pub mod maven;
pub mod net;
#[cfg(not(target_arch = "wasm32"))]
pub mod openvex;
#[cfg(not(target_arch = "wasm32"))]
pub mod osv;
pub mod pem;
#[cfg(not(target_arch = "wasm32"))]
pub mod rhsa;
pub mod semver;
#[cfg(feature = "showcase")]
pub mod showcase;
#[cfg(feature = "sigstore")]
pub mod sigstore;
pub mod slsa;
pub mod spdx;
pub mod string;
pub mod timestamp;
pub mod uri;
pub mod x509;
#[derive(Debug)]
pub struct FunctionEvaluationResult {
/// Severity reported by the function
pub severity: Severity,
/// The output of the function
pub output: Output,
/// The rationale, including if we succeeded or not
pub rationale: Option<Arc<Rationale>>,
/// Supporting information
pub supporting: Arc<Vec<EvaluationResult>>,
}
impl From<Output> for FunctionEvaluationResult {
fn from(output: Output) -> Self {
Self {
severity: Severity::None,
output,
rationale: None,
supporting: Arc::new(vec![]),
}
}
}
impl From<Severity> for FunctionEvaluationResult {
fn from(severity: Severity) -> Self {
Self {
severity,
output: Output::Identity,
rationale: None,
supporting: Arc::new(vec![]),
}
}
}
impl From<(Severity, Arc<Vec<EvaluationResult>>)> for FunctionEvaluationResult {
fn from((severity, supporting): (Severity, Arc<Vec<EvaluationResult>>)) -> Self {
Self {
severity,
output: Output::Identity,
rationale: None,
supporting,
}
}
}
impl From<(Severity, Vec<EvaluationResult>)> for FunctionEvaluationResult {
fn from((severity, supporting): (Severity, Vec<EvaluationResult>)) -> Self {
Self {
severity,
output: Output::Identity,
rationale: None,
supporting: Arc::new(supporting),
}
}
}
impl From<(Severity, Rationale)> for FunctionEvaluationResult {
fn from((severity, rationale): (Severity, Rationale)) -> Self {
Self {
severity,
output: Output::Identity,
rationale: Some(Arc::new(rationale)),
supporting: Arc::new(vec![]),
}
}
}
impl From<(Severity, Arc<Rationale>)> for FunctionEvaluationResult {
fn from((severity, rationale): (Severity, Arc<Rationale>)) -> Self {
Self {
severity,
output: Output::Identity,
rationale: Some(rationale),
supporting: Arc::new(vec![]),
}
}
}
#[derive(Debug)]
pub enum FunctionInput {
Anything,
String,
Boolean,
Integer,
Decimal,
Pattern(FunctionInputPattern),
}
#[derive(Debug)]
pub enum FunctionInputPattern {
Just(PatternName),
And(Vec<FunctionInput>),
Or(Vec<FunctionInput>),
}
#[derive(Clone, Debug, PartialEq, Eq, serde::Serialize, serde::Deserialize)]
pub struct Example {
/// A distinct identifier
pub name: String,
/// An optional summary override, should override the use of the name when presenting the user.
pub summary: Option<String>,
/// An optional detailed description
pub description: Option<String>,
/// The value of the example
pub value: serde_json::Value,
}
pub trait Function: Sync + Send + Debug {
fn input(&self, _bindings: &[Arc<Pattern>]) -> FunctionInput {
FunctionInput::Anything
}
/// A number between 0 and u8::MAX indicating the evaluation order.
///
/// 0 means the function is likely to be fast, 255 means likely to be slow.
/// Guidance:
/// 0 - 11 - Fast non-async lookup/conversion code
/// 12 - 40 - More complex non-async code
/// 40 - 120 - Async code
/// 120 - 255 - Code that uses network or disk.
fn order(&self) -> u8 {
7
}
fn metadata(&self) -> PatternMeta {
Default::default()
}
fn examples(&self) -> Vec<Example> {
vec![]
}
fn parameters(&self) -> Vec<String> {
Default::default()
}
fn call<'v>(
&'v self,
input: Arc<RuntimeValue>,
ctx: ExecutionContext<'v>,
bindings: &'v Bindings,
world: &'v World,
) -> Pin<Box<dyn Future<Output = Result<FunctionEvaluationResult, RuntimeError>> + 'v>>;
}
/// A blocking version of [`Function`].
pub trait BlockingFunction: Sync + Send + Debug {
/// A number between 0 and u8::MAX indicating the evaluation order.
///
/// 0 means the function is likely to be fast, 255 means likely to be slow.
/// Guidance:
/// 0 - 11 - Fast non-async lookup/conversion code
/// 12 - 40 - More complex non-async code
/// 40 - 120 - Async code
/// 120 - 255 - Code that uses network or disk.
fn order(&self) -> u8 {
7
}
fn metadata(&self) -> PatternMeta {
Default::default()
}
fn examples(&self) -> Vec<Example> {
vec![]
}
fn parameters(&self) -> Vec<String> {
Default::default()
}
fn call(
&self,
input: Arc<RuntimeValue>,
ctx: ExecutionContext<'_>,
bindings: &Bindings,
world: &World,
) -> Result<FunctionEvaluationResult, RuntimeError>;
}
impl<F> Function for F
where
F: BlockingFunction,
{
fn order(&self) -> u8 {
BlockingFunction::order(self)
}
fn metadata(&self) -> PatternMeta {
BlockingFunction::metadata(self)
}
fn examples(&self) -> Vec<Example> {
BlockingFunction::examples(self)
}
fn parameters(&self) -> Vec<String> {
BlockingFunction::parameters(self)
}
fn call<'v>(
&'v self,
input: Arc<RuntimeValue>,
ctx: ExecutionContext<'v>,
bindings: &'v Bindings,
world: &'v World,
) -> Pin<Box<dyn Future<Output = Result<FunctionEvaluationResult, RuntimeError>> + 'v>> {
Box::pin(async { BlockingFunction::call(self, input, ctx, bindings, world) })
}
}