pub mod config;
pub mod execute;
pub mod introspect;
pub mod pool;
pub mod routes;
use std::collections::HashMap;
use std::sync::Arc;
use tokio::sync::RwLock;
use introspect::Schema;
use pool::ConnectionPool;
pub struct SqlConnection {
pub engine: config::Engine,
pub pool: ConnectionPool,
}
#[derive(Clone)]
pub struct SqlGateway {
inner: Arc<SqlGatewayInner>,
}
struct SqlGatewayInner {
connections: HashMap<String, SqlConnection>,
#[allow(dead_code)]
schema_cache: RwLock<HashMap<String, Schema>>,
auth_token: String,
}
impl SqlGateway {
#[must_use]
pub fn new(connections: HashMap<String, SqlConnection>, auth_token: String) -> Self {
if auth_token.is_empty() {
tracing::warn!(
"sql gateway has an empty auth token: all /sql requests will be rejected"
);
}
Self {
inner: Arc::new(SqlGatewayInner {
connections,
schema_cache: RwLock::new(HashMap::new()),
auth_token,
}),
}
}
#[must_use]
pub fn connection(&self, name: &str) -> Option<&SqlConnection> {
self.inner.connections.get(name)
}
#[must_use]
pub fn check_token(&self, presented: &str) -> bool {
if self.inner.auth_token.is_empty() {
return false;
}
use sha2::{Digest, Sha256};
use subtle::ConstantTimeEq;
let presented = Sha256::digest(presented.as_bytes());
let expected = Sha256::digest(self.inner.auth_token.as_bytes());
presented.ct_eq(&expected).into()
}
#[allow(dead_code)]
pub(crate) fn cache(&self) -> &RwLock<HashMap<String, Schema>> {
&self.inner.schema_cache
}
}
#[cfg(test)]
mod tests {
use super::*;
#[test]
fn empty_token_rejects_everything() {
let gw = SqlGateway::new(HashMap::new(), String::new());
assert!(!gw.check_token(""));
assert!(!gw.check_token("anything"));
}
#[test]
fn configured_token_is_checked() {
let gw = SqlGateway::new(HashMap::new(), "tok".to_string());
assert!(gw.check_token("tok"));
assert!(!gw.check_token("tok2"));
assert!(!gw.check_token(""));
}
}