use thiserror::Error;
#[derive(Clone, Debug, Error, PartialEq, Eq)]
pub enum SqlGuardDenyReason {
#[error("sql operation '{operation}' is not allowed")]
OperationNotAllowed {
operation: String,
},
#[error("table '{table}' is not in the allowlist")]
TableNotAllowed {
table: String,
},
#[error("column '{column}' on table '{table}' is not in the allowlist")]
ColumnNotAllowed {
table: String,
column: String,
},
#[error("predicate matched denylist pattern '{pattern}'")]
PredicateDenylisted {
pattern: String,
},
#[error("{operation} without WHERE clause is not allowed")]
MissingWhereClause {
operation: String,
},
#[error("sql parse error: {error}")]
ParseError {
error: String,
},
#[error("sql guard has no configured allowlists and allow_all is false")]
NoConfig,
#[error("SELECT * on table '{table}' is denied when a column allowlist is active")]
SelectStarDenied {
table: String,
},
}
impl SqlGuardDenyReason {
pub fn code(&self) -> &'static str {
match self {
Self::OperationNotAllowed { .. } => "operation_not_allowed",
Self::TableNotAllowed { .. } => "table_not_allowed",
Self::ColumnNotAllowed { .. } => "column_not_allowed",
Self::PredicateDenylisted { .. } => "predicate_denylisted",
Self::MissingWhereClause { .. } => "missing_where_clause",
Self::ParseError { .. } => "parse_error",
Self::NoConfig => "no_config",
Self::SelectStarDenied { .. } => "select_star_denied",
}
}
}