use crate::error::DbError;
pub const PROTECTED_COLLECTIONS: [&str; 3] = ["_env", "_admins", "_api_keys"];
pub fn is_protected_collection(name: &str) -> bool {
let bare = name.rsplit(':').next().unwrap_or(name);
PROTECTED_COLLECTIONS.contains(&bare)
}
pub fn protected_collection_error(name: &str) -> DbError {
DbError::Forbidden(format!(
"Access denied: '{}' stores credentials and is not readable or \
writable through this API; use the admin-only endpoints",
name.rsplit(':').next().unwrap_or(name)
))
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn bare_names_are_protected() {
assert!(is_protected_collection("_env"));
assert!(is_protected_collection("_admins"));
assert!(is_protected_collection("_api_keys"));
}
#[test]
fn qualified_names_are_protected() {
assert!(is_protected_collection("mydb:_env"));
assert!(is_protected_collection("_system:_admins"));
assert!(is_protected_collection("victim:_api_keys"));
}
#[test]
fn ordinary_collections_are_not() {
assert!(!is_protected_collection("users"));
assert!(!is_protected_collection("mydb:users"));
assert!(!is_protected_collection("_scripts"));
assert!(!is_protected_collection("_slow_queries"));
}
#[test]
fn near_misses_are_not_protected() {
assert!(!is_protected_collection("_environment"));
assert!(!is_protected_collection("my_env"));
assert!(!is_protected_collection("_env2"));
}
}