Preprocess YAML policy data: parse, normalize, validate, expand access presets, return JSON.
(yaml_str: &str)
| 721 | |
| 722 | /// Preprocess YAML policy data: parse, normalize, validate, expand access presets, return JSON. |
| 723 | fn preprocess_yaml_data(yaml_str: &str) -> Result<String> { |
| 724 | let mut data: serde_json::Value = serde_yml::from_str(yaml_str) |
| 725 | .map_err(|e| miette::miette!("failed to parse YAML data: {e}"))?; |
| 726 | |
| 727 | // Normalize port → ports for all endpoints so Rego always sees "ports" array. |
| 728 | normalize_endpoint_ports(&mut data); |
| 729 | let config_errors = normalize_l7_config_aliases(&mut data); |
| 730 | if !config_errors.is_empty() { |
| 731 | return Err(miette::miette!( |
| 732 | "L7 policy validation failed:\n{}", |
| 733 | config_errors.join("\n") |
| 734 | )); |
| 735 | } |
| 736 | |
| 737 | // Validate BEFORE expanding presets (catches user errors like rules+access) |
| 738 | let (errors, warnings) = crate::l7::validate_l7_policies(&data); |
| 739 | for w in &warnings { |
| 740 | openshell_ocsf::ocsf_emit!( |
| 741 | openshell_ocsf::ConfigStateChangeBuilder::new(openshell_ocsf::ctx::ctx()) |
| 742 | .severity(openshell_ocsf::SeverityId::Medium) |
| 743 | .status(openshell_ocsf::StatusId::Success) |
| 744 | .state(openshell_ocsf::StateId::Enabled, "validated") |
| 745 | .unmapped("warning", serde_json::json!(w.clone())) |
| 746 | .message(format!("L7 policy validation warning: {w}")) |
| 747 | .build() |
| 748 | ); |
| 749 | } |
| 750 | if !errors.is_empty() { |
| 751 | return Err(miette::miette!( |
| 752 | "L7 policy validation failed:\n{}", |
| 753 | errors.join("\n") |
| 754 | )); |
| 755 | } |
| 756 | |
| 757 | normalize_l7_policy_rule_aliases(&mut data); |
| 758 | |
| 759 | // Expand access presets to explicit rules after validation |
| 760 | crate::l7::expand_access_presets(&mut data); |
| 761 | |
| 762 | serde_json::to_string(&data).map_err(|e| miette::miette!("failed to serialize data: {e}")) |
| 763 | } |
| 764 | |
| 765 | /// Normalize endpoint port/ports in JSON data. |
| 766 | /// |
no test coverage detected