Simplify DB handling with sqlx::Any

This commit is contained in:
Valentin Tolmer 2021-03-10 12:06:32 +01:00
parent 285a51db6e
commit 1a947358fa
4 changed files with 21 additions and 38 deletions

View File

@ -17,7 +17,7 @@ http = "*"
ldap3_server = "*"
log = "*"
serde = "*"
sqlx = { version = "0.5", features = [ "runtime-actix-native-tls", "sqlite", "macros" ] }
sqlx = { version = "0.5", features = [ "runtime-actix-native-tls", "any", "sqlite", "mysql", "postgres", "macros" ] }
thiserror = "*"
tokio = { version = "1.2.0", features = ["full"] }
tokio-util = "0.6.3"

View File

@ -6,25 +6,19 @@ use anyhow::bail;
use anyhow::Result;
use futures_util::future::ok;
use log::*;
use std::rc::Rc;
use sqlx::any::AnyPool;
use tokio::net::tcp::WriteHalf;
use tokio_util::codec::{FramedRead, FramedWrite};
use ldap3_server::simple::*;
use ldap3_server::LdapCodec;
pub struct LdapSession<DB>
where
DB: sqlx::Database,
{
pub struct LdapSession {
dn: String,
sql_pool: Rc<sqlx::Pool<DB>>,
sql_pool: AnyPool,
}
impl<DB> LdapSession<DB>
where
DB: sqlx::Database,
{
impl LdapSession {
pub fn do_bind(&mut self, sbr: &SimpleBindRequest) -> LdapMsg {
if sbr.dn == "cn=Directory Manager" && sbr.pw == "password" {
self.dn = sbr.dn.to_string();
@ -72,10 +66,8 @@ where
pub fn do_whoami(&mut self, wr: &WhoamiRequest) -> LdapMsg {
wr.gen_success(format!("dn: {}", self.dn).as_str())
}
pub fn handle_ldap_message(&mut self, server_op: ServerOps) -> Option<Vec<LdapMsg>>
where
DB: sqlx::Database,
{
pub fn handle_ldap_message(&mut self, server_op: ServerOps) -> Option<Vec<LdapMsg>> {
let result = match server_op {
ServerOps::SimpleBind(sbr) => vec![self.do_bind(&sbr)],
ServerOps::Search(sr) => self.do_search(&sr),
@ -89,14 +81,11 @@ where
}
}
async fn handle_incoming_message<DB>(
async fn handle_incoming_message(
msg: Result<LdapMsg, std::io::Error>,
resp: &mut FramedWrite<WriteHalf<'_>, LdapCodec>,
session: &mut LdapSession<DB>,
) -> Result<bool>
where
DB: sqlx::Database,
{
session: &mut LdapSession,
) -> Result<bool> {
use futures_util::SinkExt;
use std::convert::TryFrom;
let server_op = match msg
@ -133,19 +122,16 @@ where
Ok(true)
}
pub fn build_ldap_server<DB>(
pub fn build_ldap_server(
config: &Configuration,
sql_pool: sqlx::Pool<DB>,
sql_pool: AnyPool,
server_builder: ServerBuilder,
) -> Result<ServerBuilder>
where
DB: sqlx::Database,
{
) -> Result<ServerBuilder> {
use futures_util::StreamExt;
Ok(
server_builder.bind("ldap", ("0.0.0.0", config.ldap_port), move || {
let sql_pool = std::rc::Rc::new(sql_pool.clone());
let sql_pool = sql_pool.clone();
pipeline_factory(fn_service(move |mut stream: TcpStream| {
let sql_pool = sql_pool.clone();
async move {

View File

@ -5,16 +5,14 @@ use actix_service::pipeline_factory;
use anyhow::{Context, Result};
use futures_util::future::ok;
use log::*;
use sqlx::any::AnyPool;
use std::sync::Arc;
pub fn build_tcp_server<DB>(
pub fn build_tcp_server(
config: &Configuration,
sql_pool: sqlx::Pool<DB>,
sql_pool: AnyPool,
server_builder: ServerBuilder,
) -> Result<ServerBuilder>
where
DB: sqlx::Database,
{
) -> Result<ServerBuilder> {
use std::sync::atomic::AtomicUsize;
use std::sync::atomic::Ordering;
use tokio::io::AsyncReadExt;

View File

@ -2,15 +2,14 @@ use crate::infra::configuration::Configuration;
use anyhow::Result;
use futures_util::TryFutureExt;
use log::*;
use sqlx::sqlite::{SqliteConnectOptions, SqlitePoolOptions};
use std::str::FromStr;
use sqlx::any::AnyPoolOptions;
mod infra;
async fn run_server(config: Configuration) -> Result<()> {
let sql_pool = SqlitePoolOptions::new()
let sql_pool = AnyPoolOptions::new()
.max_connections(5)
.connect_with(SqliteConnectOptions::from_str("sqlite://users.db")?.create_if_missing(true))
.connect("sqlite://users.db?mode=rwc")
.await?;
let server_builder = infra::ldap_server::build_ldap_server(
&config,