use std::sync::Arc;
use std::time::{Duration, Instant};
use futures_util::{SinkExt, StreamExt};
use moka::Expiry;
use moka::future::Cache;
use tokio::net::TcpStream;
use tokio_util::codec::Framed;
use tracing::debug;
use crate::auth::scram;
use crate::connections::server::connect_tcp;
use crate::errors;
use crate::proto;
type CacheKey = (String, String);
const MAX_CACHED_VERIFIERS: u64 = 10_000;
pub struct VerifierCache {
entries: Cache<CacheKey, CachedEntry>,
}
#[derive(Clone)]
struct CachedEntry {
verifier: scram::ScramVerifier,
ttl: Duration,
}
struct CachedEntryExpiry;
impl Expiry<CacheKey, CachedEntry> for CachedEntryExpiry {
fn expire_after_create(
&self,
_key: &CacheKey,
value: &CachedEntry,
_created_at: Instant,
) -> Option<Duration> {
Some(value.ttl)
}
}
impl Default for VerifierCache {
fn default() -> Self {
Self::new()
}
}
impl VerifierCache {
pub fn new() -> Self {
let expiry: Arc<dyn Expiry<CacheKey, CachedEntry> + Send + Sync + 'static> =
Arc::new(CachedEntryExpiry);
Self {
entries: Cache::builder()
.max_capacity(MAX_CACHED_VERIFIERS)
.expire_after(expiry)
.build(),
}
}
pub(crate) async fn get(
&self,
user: &str,
admin: &crate::auth::AdminConn<'_>,
) -> Result<scram::ScramVerifier, errors::AuthError> {
let cache_key: CacheKey = (admin.addr.to_owned(), user.to_owned());
let admin_user = admin.admin_user.to_owned();
let admin_database = admin.admin_database.to_owned();
let admin_pw = admin.password.map(str::to_owned);
let auth_query = admin.auth_query.to_owned();
let upstream_addr_owned = admin.addr.to_owned();
let user_owned = user.to_owned();
let cache_ttl = admin.cache_ttl;
let connect_timeout = admin.connect_timeout;
let entry = self
.entries
.try_get_with(cache_key, async move {
debug!(
user = %user_owned,
upstream = %upstream_addr_owned,
admin_database = %admin_database,
"fetching verifier via auth_query",
);
let verifier = fetch_verifier(
&upstream_addr_owned,
&admin_user,
&admin_database,
admin_pw.as_deref(),
&auth_query,
&user_owned,
connect_timeout,
)
.await?;
Ok::<_, errors::AuthError>(CachedEntry {
verifier,
ttl: cache_ttl,
})
})
.await
.map_err(|e: Arc<errors::AuthError>| errors::AuthError::VerifierFetch(e.to_string()))?;
Ok(entry.verifier)
}
}
#[allow(clippy::too_many_arguments)]
async fn fetch_verifier(
upstream_addr: &str,
admin_user: &str,
admin_database: &str,
admin_password: Option<&str>,
auth_query_template: &str,
target_user: &str,
connect_timeout: Duration,
) -> Result<scram::ScramVerifier, errors::AuthError> {
let stream = connect_tcp(upstream_addr, connect_timeout)
.await
.map_err(|e| {
errors::AuthError::VerifierFetch(format!("connect to {upstream_addr}: {e}"))
})?;
let mut conn = Framed::new(stream, proto::codec::BackendCodec::new());
let result = run_auth_query(
&mut conn,
admin_user,
admin_database,
admin_password,
auth_query_template,
target_user,
)
.await;
let _ = conn.send(proto::frontend::FrontendMessage::Terminate).await;
result
}
async fn run_auth_query(
conn: &mut Framed<TcpStream, proto::codec::BackendCodec>,
admin_user: &str,
admin_database: &str,
admin_password: Option<&str>,
auth_query_template: &str,
target_user: &str,
) -> Result<scram::ScramVerifier, errors::AuthError> {
conn.send(proto::frontend::FrontendMessage::Startup(
proto::frontend::Startup {
version: proto::frontend::ProtocolVersion { major: 3, minor: 0 },
parameters: vec![
("user".into(), admin_user.into()),
("database".into(), admin_database.into()),
],
},
))
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send startup: {e}")))?;
loop {
match conn
.next()
.await
.transpose()
.map_err(|e| errors::AuthError::VerifierFetch(format!("admin startup: {e}")))?
{
Some(
proto::backend::BackendMessage::AuthenticationOk
| proto::backend::BackendMessage::ParameterStatus { .. }
| proto::backend::BackendMessage::BackendKeyData { .. },
) => {}
Some(proto::backend::BackendMessage::AuthenticationSasl { mechanisms }) => {
if !mechanisms.iter().any(|m| m == "SCRAM-SHA-256") {
return Err(errors::AuthError::VerifierFetch(format!(
"admin connection requires unsupported SASL mechanism: {mechanisms:?}"
)));
}
let pw = admin_password.ok_or_else(|| {
errors::AuthError::VerifierFetch(
"admin connection requires SCRAM auth but no password in .pgpass".into(),
)
})?;
scram::client::authenticate(conn, pw).await?;
}
Some(proto::backend::BackendMessage::AuthenticationCleartextPassword) => {
let pw = admin_password.ok_or_else(|| {
errors::AuthError::VerifierFetch(
"admin connection requires password but none in .pgpass".into(),
)
})?;
conn.send(proto::frontend::FrontendMessage::PasswordMessage(
pw.as_bytes().to_vec(),
))
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send password: {e}")))?;
}
Some(proto::backend::BackendMessage::ReadyForQuery(_)) => break,
Some(proto::backend::BackendMessage::ErrorResponse(err)) => {
return Err(errors::AuthError::VerifierFetch(format!(
"admin startup error: {}",
err.message().unwrap_or("unknown")
)));
}
Some(other) => {
return Err(errors::AuthError::VerifierFetch(format!(
"unexpected during admin startup: {other:?}"
)));
}
None => {
return Err(errors::AuthError::VerifierFetch(
"upstream closed during admin startup".into(),
));
}
}
}
conn.feed(proto::frontend::FrontendMessage::Parse(
proto::frontend::Parse {
name: String::new(),
query: auth_query_template.to_owned(),
param_types: vec![],
},
))
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send parse: {e}")))?;
conn.feed(proto::frontend::FrontendMessage::Bind(
proto::frontend::Bind {
portal: String::new(),
statement: String::new(),
param_formats: vec![],
params: vec![Some(target_user.as_bytes().to_vec())],
result_formats: vec![],
},
))
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send bind: {e}")))?;
conn.feed(proto::frontend::FrontendMessage::Execute(
proto::frontend::Execute {
portal: String::new(),
max_rows: 0,
},
))
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send execute: {e}")))?;
conn.send(proto::frontend::FrontendMessage::Sync)
.await
.map_err(|e| errors::AuthError::VerifierFetch(format!("send sync: {e}")))?;
let mut verifier_str: Option<String> = None;
loop {
match conn
.next()
.await
.transpose()
.map_err(|e| errors::AuthError::VerifierFetch(format!("auth_query: {e}")))?
{
Some(proto::backend::BackendMessage::DataRow(cols)) => {
if cols.len() >= 2
&& let Some(ref data) = cols[1]
{
verifier_str = Some(String::from_utf8_lossy(data).into_owned());
}
}
Some(proto::backend::BackendMessage::ReadyForQuery(_)) => break,
Some(proto::backend::BackendMessage::ErrorResponse(err)) => {
return Err(errors::AuthError::VerifierFetch(format!(
"auth_query error: {}",
err.message().unwrap_or("unknown")
)));
}
None => {
return Err(errors::AuthError::VerifierFetch(
"upstream closed during auth_query".into(),
));
}
Some(_) => {}
}
}
let raw = verifier_str.ok_or_else(|| {
errors::AuthError::VerifierFetch(format!("no password found for user {target_user:?}"))
})?;
scram::parse_verifier(&raw)
}