|
1 | 1 | //! Python's argument merge, SQL rendering and policy resolution for one attachment. |
2 | 2 |
|
3 | | -use crate::audits::_helpers::parameters::{RenderStop, render_parameterized_sql}; |
| 3 | +use crate::audits::_helpers::parameters::{ |
| 4 | + RenderStop, parameter_matches, render_parameterized_sql, |
| 5 | +}; |
4 | 6 | use crate::audits::_helpers::policies::{resolve_run_scope, resolve_severity}; |
5 | 7 | use crate::audits::constants::{MODEL_RESOURCE, SEED_RESOURCE, SOURCE_RESOURCE}; |
6 | 8 | use crate::audits::models::{ArgumentValue, AuditAttachment, PolicySource}; |
@@ -94,3 +96,36 @@ pub(crate) fn merged_arguments( |
94 | 96 | } |
95 | 97 | Ok(merged) |
96 | 98 | } |
| 99 | + |
| 100 | +/// The error for the first explicit argument neither the audit SQL nor its evidence SQL reads. |
| 101 | +pub(crate) fn unused_argument(attachment: &AuditAttachment) -> Option<String> { |
| 102 | + let mut used: Vec<&str> = parameter_matches(&attachment.sql_body) |
| 103 | + .into_iter() |
| 104 | + .map(|item| item.name) |
| 105 | + .collect(); |
| 106 | + if let Some(evidence) = &attachment.evidence_sql { |
| 107 | + used.extend( |
| 108 | + parameter_matches(evidence) |
| 109 | + .into_iter() |
| 110 | + .map(|item| item.name), |
| 111 | + ); |
| 112 | + } |
| 113 | + let implicit: Vec<&str> = attachment |
| 114 | + .implicit_arguments |
| 115 | + .iter() |
| 116 | + .map(|(name, _)| name.as_str()) |
| 117 | + .collect(); |
| 118 | + let (name, _) = attachment |
| 119 | + .explicit_arguments |
| 120 | + .iter() |
| 121 | + .find(|(name, _)| !used.contains(&name.as_str()) && !implicit.contains(&name.as_str()))?; |
| 122 | + let definition: String = attachment |
| 123 | + .definition_path |
| 124 | + .as_deref() |
| 125 | + .map_or_else(|| "its built-in definition".to_owned(), str::to_owned); |
| 126 | + Some(format!( |
| 127 | + "{} audit '{}' passes argument '{name}', which the audit's SQL in {definition} never \ |
| 128 | + uses; remove '{name}' from the attachment", |
| 129 | + attachment.owner_label, attachment.definition_name |
| 130 | + )) |
| 131 | +} |
0 commit comments