mirror of
https://github.com/postgresml/pgcat.git
synced 2026-03-23 09:26:30 +00:00
* Fixed all clippy warnings. * Added `clippy` to CI. * Reverted an unwanted change + Applied `cargo fmt`. * Fixed the idiom version. * Revert "Fixed the idiom version." This reverts commit6f78be0d42. * Fixed clippy issues on CI. * Revert "Fixed clippy issues on CI." This reverts commita9fa6ba189. * Revert "Reverted an unwanted change + Applied `cargo fmt`." This reverts commit6bd37b6479. * Revert "Fixed all clippy warnings." This reverts commitd1f3b847e3. * Removed Clippy * Removed Lint * `admin.rs` clippy fixes. * Applied more clippy changes. * Even more clippy changes. * `client.rs` clippy fixes. * `server.rs` clippy fixes. * Revert "Removed Lint" This reverts commitcb5042b144. * Revert "Removed Clippy" This reverts commit6dec8bffb1. * Applied lint. * Revert "Revert "Fixed clippy issues on CI."" This reverts commit49164a733c.
60 lines
1.5 KiB
Rust
60 lines
1.5 KiB
Rust
//! This query router plugin will check if the user can access a particular
|
|
//! table as part of their query. If they can't, the query will not be routed.
|
|
|
|
use async_trait::async_trait;
|
|
use sqlparser::ast::{visit_relations, Statement};
|
|
|
|
use crate::{
|
|
errors::Error,
|
|
plugins::{Plugin, PluginOutput},
|
|
query_router::QueryRouter,
|
|
};
|
|
|
|
use log::debug;
|
|
|
|
use core::ops::ControlFlow;
|
|
|
|
pub struct TableAccess<'a> {
|
|
pub enabled: bool,
|
|
pub tables: &'a Vec<String>,
|
|
}
|
|
|
|
#[async_trait]
|
|
impl<'a> Plugin for TableAccess<'a> {
|
|
async fn run(
|
|
&mut self,
|
|
_query_router: &QueryRouter,
|
|
ast: &Vec<Statement>,
|
|
) -> Result<PluginOutput, Error> {
|
|
if !self.enabled {
|
|
return Ok(PluginOutput::Allow);
|
|
}
|
|
|
|
let mut found = None;
|
|
|
|
visit_relations(ast, |relation| {
|
|
let relation = relation.to_string();
|
|
let parts = relation.split('.').collect::<Vec<&str>>();
|
|
let table_name = parts.last().unwrap();
|
|
|
|
if self.tables.contains(&table_name.to_string()) {
|
|
found = Some(table_name.to_string());
|
|
ControlFlow::<()>::Break(())
|
|
} else {
|
|
ControlFlow::<()>::Continue(())
|
|
}
|
|
});
|
|
|
|
if let Some(found) = found {
|
|
debug!("Blocking access to table \"{}\"", found);
|
|
|
|
Ok(PluginOutput::Deny(format!(
|
|
"permission for table \"{}\" denied",
|
|
found
|
|
)))
|
|
} else {
|
|
Ok(PluginOutput::Allow)
|
|
}
|
|
}
|
|
}
|