Evaluate a network access request against the loaded policy. Builds an OPA input document from the `NetworkInput`, evaluates the `allow_network` rule, and returns a `PolicyDecision` with the result, deny reason, and matched policy name.
(&self, input: &NetworkInput)
| 244 | /// `allow_network` rule, and returns a `PolicyDecision` with the result, |
| 245 | /// deny reason, and matched policy name. |
| 246 | pub fn evaluate_network(&self, input: &NetworkInput) -> Result<PolicyDecision> { |
| 247 | let ancestor_strs: Vec<String> = input |
| 248 | .ancestors |
| 249 | .iter() |
| 250 | .map(|p| p.to_string_lossy().into_owned()) |
| 251 | .collect(); |
| 252 | let cmdline_strs: Vec<String> = input |
| 253 | .cmdline_paths |
| 254 | .iter() |
| 255 | .map(|p| p.to_string_lossy().into_owned()) |
| 256 | .collect(); |
| 257 | let input_json = serde_json::json!({ |
| 258 | "exec": { |
| 259 | "path": input.binary_path.to_string_lossy(), |
| 260 | "ancestors": ancestor_strs, |
| 261 | "cmdline_paths": cmdline_strs, |
| 262 | }, |
| 263 | "network": { |
| 264 | "host": input.host, |
| 265 | "port": input.port, |
| 266 | } |
| 267 | }); |
| 268 | |
| 269 | let mut engine = self |
| 270 | .engine |
| 271 | .lock() |
| 272 | .map_err(|_| miette::miette!("OPA engine lock poisoned"))?; |
| 273 | |
| 274 | engine |
| 275 | .set_input_json(&input_json.to_string()) |
| 276 | .map_err(|e| miette::miette!("{e}"))?; |
| 277 | |
| 278 | let allowed = engine |
| 279 | .eval_rule("data.openshell.sandbox.allow_network".into()) |
| 280 | .map_err(|e| miette::miette!("{e}"))?; |
| 281 | let allowed = allowed == regorus::Value::from(true); |
| 282 | |
| 283 | let reason = engine |
| 284 | .eval_rule("data.openshell.sandbox.deny_reason".into()) |
| 285 | .map_err(|e| miette::miette!("{e}"))?; |
| 286 | let reason = value_to_string(&reason); |
| 287 | |
| 288 | let matched = engine |
| 289 | .eval_rule("data.openshell.sandbox.matched_network_policy".into()) |
| 290 | .map_err(|e| miette::miette!("{e}"))?; |
| 291 | let matched_policy = if matched == regorus::Value::Undefined { |
| 292 | None |
| 293 | } else { |
| 294 | Some(value_to_string(&matched)) |
| 295 | }; |
| 296 | |
| 297 | Ok(PolicyDecision { |
| 298 | allowed, |
| 299 | reason, |
| 300 | matched_policy, |
| 301 | }) |
| 302 | } |
| 303 |