|
| 1 | +//! Python's argument merge, SQL rendering and policy resolution for one attachment. |
| 2 | +
|
| 3 | +use crate::audits::_helpers::parameters::{RenderStop, render_parameterized_sql}; |
| 4 | +use crate::audits::_helpers::policies::{resolve_run_scope, resolve_severity}; |
| 5 | +use crate::audits::models::{ArgumentValue, AuditAttachment, PolicySource}; |
| 6 | + |
| 7 | +/// Severity first, then run scope, as Python resolves them after expanding the SQL. |
| 8 | +pub(crate) fn policies( |
| 9 | + attachment: &AuditAttachment, |
| 10 | +) -> Result<(&'static str, PolicySource), String> { |
| 11 | + let severity: &'static str = if attachment.has_thresholds { |
| 12 | + if attachment.threshold_error { |
| 13 | + "error" |
| 14 | + } else { |
| 15 | + "warn" |
| 16 | + } |
| 17 | + } else { |
| 18 | + resolve_severity( |
| 19 | + attachment.instance_severity.as_deref(), |
| 20 | + attachment.default_severity.as_deref(), |
| 21 | + &format!( |
| 22 | + "{} audit '{}'", |
| 23 | + attachment.owner_label, attachment.definition_name |
| 24 | + ), |
| 25 | + )? |
| 26 | + }; |
| 27 | + let run_scope_source: PolicySource = resolve_run_scope( |
| 28 | + attachment.instance_run_scope.as_deref(), |
| 29 | + attachment.default_run_scope.as_deref(), |
| 30 | + )?; |
| 31 | + Ok((severity, run_scope_source)) |
| 32 | +} |
| 33 | + |
| 34 | +/// One SQL text rendered, Python's missing-argument error, or None to defer. |
| 35 | +pub(crate) fn rendered( |
| 36 | + attachment: &AuditAttachment, |
| 37 | + sql: &str, |
| 38 | + arguments: &[(String, ArgumentValue)], |
| 39 | +) -> Option<Result<String, String>> { |
| 40 | + match render_parameterized_sql(sql, arguments, false) { |
| 41 | + Ok(rendered) => Some(Ok(rendered)), |
| 42 | + Err(RenderStop::MissingArgument(name)) => Some(Err(format!( |
| 43 | + "{} is missing argument '{name}' for generic audit '{}'", |
| 44 | + attachment.owner_label, attachment.definition_name |
| 45 | + ))), |
| 46 | + Err(RenderStop::UnsupportedValue(name)) => Some(Err(format!( |
| 47 | + "{} generic audit '{}' argument '{name}' uses an unsupported value", |
| 48 | + attachment.owner_label, attachment.definition_name |
| 49 | + ))), |
| 50 | + Err(RenderStop::Deferred) => None, |
| 51 | + } |
| 52 | +} |
| 53 | + |
| 54 | +/// Python's `merge_audit_arguments`; None where only Python's `!=` can compare the values. |
| 55 | +pub(crate) fn merged_arguments( |
| 56 | + attachment: &AuditAttachment, |
| 57 | +) -> Option<Result<Vec<(String, ArgumentValue)>, String>> { |
| 58 | + let mut merged: Vec<(String, ArgumentValue)> = attachment.implicit_arguments.clone(); |
| 59 | + for (name, value) in &attachment.explicit_arguments { |
| 60 | + match merged.iter_mut().find(|(existing, _)| existing == name) { |
| 61 | + Some((_, existing)) => { |
| 62 | + if python_unequal(existing, value)? { |
| 63 | + return Some(Err(format!( |
| 64 | + "{} audit '{}' must not override implicit {name} from attached context", |
| 65 | + attachment.owner_label, attachment.definition_name |
| 66 | + ))); |
| 67 | + } |
| 68 | + } |
| 69 | + None => merged.push((name.clone(), value.clone())), |
| 70 | + } |
| 71 | + } |
| 72 | + Some(Ok(merged)) |
| 73 | +} |
| 74 | + |
| 75 | +/// Python's `left != right`, or None for opaque values and numbers differing only in text. |
| 76 | +fn python_unequal(left: &ArgumentValue, right: &ArgumentValue) -> Option<bool> { |
| 77 | + if matches!(left, ArgumentValue::Opaque) || matches!(right, ArgumentValue::Opaque) { |
| 78 | + return None; |
| 79 | + } |
| 80 | + if left == right { |
| 81 | + return Some(false); |
| 82 | + } |
| 83 | + match (left, right) { |
| 84 | + (ArgumentValue::List(left), ArgumentValue::List(right)) => { |
| 85 | + if left.len() != right.len() { |
| 86 | + return Some(true); |
| 87 | + } |
| 88 | + let mut undecided: bool = false; |
| 89 | + for (left, right) in left.iter().zip(right) { |
| 90 | + match python_unequal(left, right) { |
| 91 | + Some(true) => return Some(true), |
| 92 | + Some(false) => {} |
| 93 | + None => undecided = true, |
| 94 | + } |
| 95 | + } |
| 96 | + if undecided { None } else { Some(false) } |
| 97 | + } |
| 98 | + ( |
| 99 | + ArgumentValue::Number(_) | ArgumentValue::Boolean(_), |
| 100 | + ArgumentValue::Number(_) | ArgumentValue::Boolean(_), |
| 101 | + ) if !matches!( |
| 102 | + (left, right), |
| 103 | + (ArgumentValue::Boolean(_), ArgumentValue::Boolean(_)) |
| 104 | + ) => |
| 105 | + { |
| 106 | + None |
| 107 | + } |
| 108 | + _ => Some(true), |
| 109 | + } |
| 110 | +} |
0 commit comments