Parse a policy YAML string into a [`PolicyModel`].
(yaml: &str)
| 346 | |
| 347 | /// Parse a policy YAML string into a [`PolicyModel`]. |
| 348 | pub fn parse_policy_str(yaml: &str) -> Result<PolicyModel> { |
| 349 | let raw: PolicyFile = serde_yml::from_str(yaml) |
| 350 | .into_diagnostic() |
| 351 | .wrap_err("parsing policy YAML")?; |
| 352 | |
| 353 | let fs = match raw.filesystem_policy { |
| 354 | Some(fs_def) => FilesystemPolicy { |
| 355 | include_workdir: fs_def.include_workdir, |
| 356 | read_only: fs_def.read_only, |
| 357 | read_write: fs_def.read_write, |
| 358 | }, |
| 359 | None => FilesystemPolicy::default(), |
| 360 | }; |
| 361 | |
| 362 | let mut network_policies = BTreeMap::new(); |
| 363 | if let Some(np) = raw.network_policies { |
| 364 | for (key, rule_raw) in np { |
| 365 | let endpoints = rule_raw |
| 366 | .endpoints |
| 367 | .into_iter() |
| 368 | .map(|ep_raw| { |
| 369 | let rules = ep_raw |
| 370 | .rules |
| 371 | .into_iter() |
| 372 | .map(|r| L7Rule { |
| 373 | method: r.allow.method, |
| 374 | path: r.allow.path, |
| 375 | command: r.allow.command, |
| 376 | }) |
| 377 | .collect(); |
| 378 | Endpoint { |
| 379 | host: ep_raw.host, |
| 380 | port: ep_raw.port, |
| 381 | ports: ep_raw.ports, |
| 382 | protocol: ep_raw.protocol, |
| 383 | tls: ep_raw.tls, |
| 384 | enforcement: ep_raw.enforcement, |
| 385 | access: ep_raw.access, |
| 386 | rules, |
| 387 | allowed_ips: ep_raw.allowed_ips, |
| 388 | } |
| 389 | }) |
| 390 | .collect(); |
| 391 | |
| 392 | let binaries = rule_raw |
| 393 | .binaries |
| 394 | .into_iter() |
| 395 | .map(|b| Binary { path: b.path }) |
| 396 | .collect(); |
| 397 | |
| 398 | let name = rule_raw.name.unwrap_or_else(|| key.clone()); |
| 399 | network_policies.insert( |
| 400 | key, |
| 401 | NetworkPolicyRule { |
| 402 | name, |
| 403 | endpoints, |
| 404 | binaries, |
| 405 | }, |
no outgoing calls