use super::{generator, Candidate, ColumnContext, Confidence, Precedence};
use crate::synthetic::model::GeneratorConfig;
use crate::synthetic::schema::SqlTypeFamily;
fn guardable_family(family: &SqlTypeFamily) -> bool {
matches!(
family,
SqlTypeFamily::Text
| SqlTypeFamily::Other
| SqlTypeFamily::Bytes
| SqlTypeFamily::Uuid
| SqlTypeFamily::Json
)
}
pub(super) fn is_high_confidence_credential(name: &str) -> bool {
classify(name).is_some()
}
pub(super) fn is_guarded(ctx: &ColumnContext<'_>) -> bool {
let column = ctx.column();
guardable_family(&column.family) && classify(&column.name).is_some()
}
pub(super) fn candidates(ctx: &ColumnContext<'_>) -> Vec<Candidate> {
let column = ctx.column();
if !guardable_family(&column.family) {
return Vec::new();
}
let Some((kind, confidence)) = classify(&column.name) else {
return Vec::new();
};
let generator = match column.family {
SqlTypeFamily::Text | SqlTypeFamily::Other => credential_generator(kind),
SqlTypeFamily::Uuid => generator("uuid"),
SqlTypeFamily::Json => generator("json_value"),
SqlTypeFamily::Bytes => generator("bytes"),
_ => return Vec::new(),
};
vec![Candidate::new(
Precedence::CredentialGuard,
confidence,
"credential_name_guard",
generator,
)]
}
fn classify(name: &str) -> Option<(&'static str, Confidence)> {
let lower = name.to_ascii_lowercase();
if contains_any(&lower, &["password", "passwd", "pwd", "password_hash"]) {
return Some(("credential.password_hash", Confidence::High));
}
if contains_any(&lower, &["private_key", "privatekey", "secret_key", "pem"]) {
return Some(("credential.placeholder", Confidence::High));
}
if contains_any(
&lower,
&["api_key", "apikey", "api_secret", "client_secret"],
) {
return Some(("credential.api_key", Confidence::High));
}
if contains_any(
&lower,
&[
"access_token",
"refresh_token",
"session_token",
"auth_token",
"bearer",
"token",
],
) {
return Some(("credential.token", Confidence::High));
}
if contains_any(&lower, &["secret", "salt"]) {
return Some(("credential.secret", Confidence::Medium));
}
None
}
fn credential_generator(kind: &'static str) -> GeneratorConfig {
generator(kind)
}
fn contains_any(haystack: &str, needles: &[&str]) -> bool {
needles.iter().any(|needle| haystack.contains(needle))
}