2022-10-18 00:47:58 +03:00
|
|
|
//! Utilities for authorization of logged in and anonymous users.
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
use super::errors::FrontendErrorResponse;
|
|
|
|
use crate::app::{UserKeyData, Web3ProxyApp};
|
2022-10-10 07:15:07 +03:00
|
|
|
use crate::jsonrpc::JsonRpcRequest;
|
2022-09-23 00:03:37 +03:00
|
|
|
use anyhow::Context;
|
2022-09-24 07:31:06 +03:00
|
|
|
use axum::headers::{authorization::Bearer, Origin, Referer, UserAgent};
|
2022-10-21 23:59:05 +03:00
|
|
|
use axum::TypedHeader;
|
2022-10-11 08:13:00 +03:00
|
|
|
use chrono::Utc;
|
2022-09-23 00:03:37 +03:00
|
|
|
use deferred_rate_limiter::DeferredRateLimitResult;
|
2022-10-20 00:34:05 +03:00
|
|
|
use entities::{user, user_keys};
|
2022-09-23 08:22:33 +03:00
|
|
|
use ipnet::IpNet;
|
2022-09-24 07:31:06 +03:00
|
|
|
use redis_rate_limiter::redis::AsyncCommands;
|
2022-09-24 06:59:21 +03:00
|
|
|
use redis_rate_limiter::RedisRateLimitResult;
|
2022-09-24 05:47:44 +03:00
|
|
|
use sea_orm::{prelude::Decimal, ColumnTrait, DatabaseConnection, EntityTrait, QueryFilter};
|
2022-09-23 00:03:37 +03:00
|
|
|
use serde::Serialize;
|
2022-09-24 08:53:45 +03:00
|
|
|
use std::fmt::Display;
|
2022-10-25 06:41:59 +03:00
|
|
|
use std::sync::atomic::{AtomicBool, AtomicU64};
|
2022-09-24 08:53:45 +03:00
|
|
|
use std::{net::IpAddr, str::FromStr, sync::Arc};
|
2022-09-28 06:35:55 +03:00
|
|
|
use tokio::sync::{OwnedSemaphorePermit, Semaphore};
|
2022-09-23 00:03:37 +03:00
|
|
|
use tokio::time::Instant;
|
2022-09-23 08:22:33 +03:00
|
|
|
use tracing::{error, trace};
|
2022-09-24 08:53:45 +03:00
|
|
|
use ulid::Ulid;
|
2022-09-23 00:03:37 +03:00
|
|
|
use uuid::Uuid;
|
|
|
|
|
2022-09-24 08:53:45 +03:00
|
|
|
/// This lets us use UUID and ULID while we transition to only ULIDs
|
2022-10-18 00:47:58 +03:00
|
|
|
/// TODO: include the key's description.
|
2022-09-24 08:53:45 +03:00
|
|
|
#[derive(Copy, Clone, Debug, Eq, PartialEq, serde::Serialize)]
|
|
|
|
pub enum UserKey {
|
|
|
|
Ulid(Ulid),
|
|
|
|
Uuid(Uuid),
|
|
|
|
}
|
|
|
|
|
2022-10-10 07:15:07 +03:00
|
|
|
#[derive(Debug)]
|
|
|
|
pub enum RateLimitResult {
|
|
|
|
/// contains the IP of the anonymous user
|
|
|
|
/// TODO: option inside or outside the arc?
|
2022-10-25 07:01:41 +03:00
|
|
|
AllowedIp(IpAddr, Option<OwnedSemaphorePermit>),
|
2022-10-10 07:15:07 +03:00
|
|
|
/// contains the user_key_id of an authenticated user
|
|
|
|
AllowedUser(UserKeyData, Option<OwnedSemaphorePermit>),
|
|
|
|
/// contains the IP and retry_at of the anonymous user
|
|
|
|
RateLimitedIp(IpAddr, Option<Instant>),
|
|
|
|
/// contains the user_key_id and retry_at of an authenticated user key
|
|
|
|
RateLimitedUser(UserKeyData, Option<Instant>),
|
|
|
|
/// This key is not in our database. Deny access!
|
|
|
|
UnknownKey,
|
|
|
|
}
|
|
|
|
|
|
|
|
#[derive(Clone, Debug, Serialize)]
|
|
|
|
pub struct AuthorizedKey {
|
|
|
|
pub ip: IpAddr,
|
|
|
|
pub origin: Option<String>,
|
2022-10-21 23:59:05 +03:00
|
|
|
pub user_id: u64,
|
2022-10-10 07:15:07 +03:00
|
|
|
pub user_key_id: u64,
|
|
|
|
// TODO: just use an f32? even an f16 is probably fine
|
|
|
|
pub log_revert_chance: Decimal,
|
|
|
|
}
|
|
|
|
|
2022-10-21 02:50:23 +03:00
|
|
|
#[derive(Debug)]
|
2022-10-10 07:15:07 +03:00
|
|
|
pub struct RequestMetadata {
|
2022-10-21 02:50:23 +03:00
|
|
|
pub start_datetime: chrono::DateTime<Utc>,
|
|
|
|
pub start_instant: tokio::time::Instant,
|
|
|
|
// TODO: better name for this
|
2022-10-11 22:58:25 +03:00
|
|
|
pub period_seconds: u64,
|
2022-10-11 20:34:25 +03:00
|
|
|
pub request_bytes: u64,
|
2022-10-11 22:58:25 +03:00
|
|
|
/// if this is 0, there was a cache_hit
|
2022-10-25 06:41:59 +03:00
|
|
|
pub backend_requests: AtomicU64,
|
|
|
|
pub no_servers: AtomicU64,
|
2022-10-11 22:58:25 +03:00
|
|
|
pub error_response: AtomicBool,
|
|
|
|
pub response_bytes: AtomicU64,
|
|
|
|
pub response_millis: AtomicU64,
|
2022-10-10 07:15:07 +03:00
|
|
|
}
|
|
|
|
|
2022-10-21 23:59:05 +03:00
|
|
|
#[derive(Clone, Debug)]
|
2022-10-10 07:15:07 +03:00
|
|
|
pub enum AuthorizedRequest {
|
|
|
|
/// Request from this app
|
|
|
|
Internal,
|
|
|
|
/// Request from an anonymous IP address
|
2022-10-21 23:59:05 +03:00
|
|
|
Ip(IpAddr, Option<Origin>),
|
2022-10-10 07:15:07 +03:00
|
|
|
/// Request from an authenticated and authorized user
|
2022-10-21 23:59:05 +03:00
|
|
|
User(Option<DatabaseConnection>, AuthorizedKey),
|
2022-10-10 07:15:07 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl RequestMetadata {
|
2022-10-11 22:58:25 +03:00
|
|
|
pub fn new(period_seconds: u64, request: &JsonRpcRequest) -> anyhow::Result<Self> {
|
|
|
|
// TODO: how can we do this without turning it into a string first. this is going to slow us down!
|
|
|
|
let request_bytes = serde_json::to_string(request)
|
|
|
|
.context("finding request size")?
|
|
|
|
.len()
|
|
|
|
.try_into()?;
|
2022-10-10 07:15:07 +03:00
|
|
|
|
2022-10-11 22:58:25 +03:00
|
|
|
let new = Self {
|
2022-10-21 02:50:23 +03:00
|
|
|
start_instant: Instant::now(),
|
|
|
|
start_datetime: Utc::now(),
|
2022-10-11 22:58:25 +03:00
|
|
|
period_seconds,
|
2022-10-11 20:34:25 +03:00
|
|
|
request_bytes,
|
2022-10-21 02:50:23 +03:00
|
|
|
backend_requests: 0.into(),
|
|
|
|
no_servers: 0.into(),
|
|
|
|
error_response: false.into(),
|
|
|
|
response_bytes: 0.into(),
|
|
|
|
response_millis: 0.into(),
|
2022-10-11 22:58:25 +03:00
|
|
|
};
|
|
|
|
|
|
|
|
Ok(new)
|
2022-10-10 07:15:07 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 08:53:45 +03:00
|
|
|
impl UserKey {
|
|
|
|
pub fn new() -> Self {
|
|
|
|
Ulid::new().into()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Display for UserKey {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
// TODO: do this without dereferencing
|
|
|
|
let ulid: Ulid = (*self).into();
|
|
|
|
|
|
|
|
ulid.fmt(f)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl Default for UserKey {
|
|
|
|
fn default() -> Self {
|
|
|
|
Self::new()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl FromStr for UserKey {
|
|
|
|
type Err = anyhow::Error;
|
|
|
|
|
|
|
|
fn from_str(s: &str) -> Result<Self, Self::Err> {
|
|
|
|
if let Ok(ulid) = s.parse::<Ulid>() {
|
|
|
|
Ok(ulid.into())
|
|
|
|
} else if let Ok(uuid) = s.parse::<Uuid>() {
|
|
|
|
Ok(uuid.into())
|
|
|
|
} else {
|
2022-10-10 07:15:07 +03:00
|
|
|
// TODO: custom error type so that this shows as a 400
|
2022-09-24 08:53:45 +03:00
|
|
|
Err(anyhow::anyhow!("UserKey was not a ULID or UUID"))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Ulid> for UserKey {
|
|
|
|
fn from(x: Ulid) -> Self {
|
|
|
|
UserKey::Ulid(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<Uuid> for UserKey {
|
|
|
|
fn from(x: Uuid) -> Self {
|
|
|
|
UserKey::Uuid(x)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserKey> for Ulid {
|
|
|
|
fn from(x: UserKey) -> Self {
|
|
|
|
match x {
|
|
|
|
UserKey::Ulid(x) => x,
|
|
|
|
UserKey::Uuid(x) => Ulid::from(x.as_u128()),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
impl From<UserKey> for Uuid {
|
|
|
|
fn from(x: UserKey) -> Self {
|
|
|
|
match x {
|
|
|
|
UserKey::Ulid(x) => Uuid::from_u128(x.0),
|
|
|
|
UserKey::Uuid(x) => x,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
impl AuthorizedKey {
|
|
|
|
pub fn try_new(
|
|
|
|
ip: IpAddr,
|
2022-09-23 08:22:33 +03:00
|
|
|
origin: Option<Origin>,
|
2022-09-23 00:03:37 +03:00
|
|
|
referer: Option<Referer>,
|
|
|
|
user_agent: Option<UserAgent>,
|
2022-10-20 23:26:14 +03:00
|
|
|
user_key_data: UserKeyData,
|
2022-09-23 00:03:37 +03:00
|
|
|
) -> anyhow::Result<Self> {
|
2022-09-23 08:22:33 +03:00
|
|
|
// check ip
|
2022-10-20 23:26:14 +03:00
|
|
|
match &user_key_data.allowed_ips {
|
2022-09-23 08:22:33 +03:00
|
|
|
None => {}
|
|
|
|
Some(allowed_ips) => {
|
|
|
|
if !allowed_ips.iter().any(|x| x.contains(&ip)) {
|
|
|
|
return Err(anyhow::anyhow!("IP is not allowed!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check origin
|
|
|
|
// TODO: do this with the Origin type instead of a String?
|
|
|
|
let origin = origin.map(|x| x.to_string());
|
2022-10-20 23:26:14 +03:00
|
|
|
match (&origin, &user_key_data.allowed_origins) {
|
2022-09-23 08:22:33 +03:00
|
|
|
(None, None) => {}
|
|
|
|
(Some(_), None) => {}
|
|
|
|
(None, Some(_)) => return Err(anyhow::anyhow!("Origin required")),
|
|
|
|
(Some(origin), Some(allowed_origins)) => {
|
|
|
|
let origin = origin.to_string();
|
|
|
|
|
|
|
|
if !allowed_origins.contains(&origin) {
|
|
|
|
return Err(anyhow::anyhow!("IP is not allowed!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check referer
|
2022-10-20 23:26:14 +03:00
|
|
|
match (referer, &user_key_data.allowed_referers) {
|
2022-09-23 08:22:33 +03:00
|
|
|
(None, None) => {}
|
|
|
|
(Some(_), None) => {}
|
|
|
|
(None, Some(_)) => return Err(anyhow::anyhow!("Referer required")),
|
|
|
|
(Some(referer), Some(allowed_referers)) => {
|
|
|
|
if !allowed_referers.contains(&referer) {
|
|
|
|
return Err(anyhow::anyhow!("Referer is not allowed!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check user_agent
|
2022-10-20 23:26:14 +03:00
|
|
|
match (user_agent, &user_key_data.allowed_user_agents) {
|
2022-09-23 08:22:33 +03:00
|
|
|
(None, None) => {}
|
|
|
|
(Some(_), None) => {}
|
|
|
|
(None, Some(_)) => return Err(anyhow::anyhow!("User agent required")),
|
|
|
|
(Some(user_agent), Some(allowed_user_agents)) => {
|
|
|
|
if !allowed_user_agents.contains(&user_agent) {
|
|
|
|
return Err(anyhow::anyhow!("User agent is not allowed!"));
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
2022-09-23 00:03:37 +03:00
|
|
|
|
|
|
|
Ok(Self {
|
|
|
|
ip,
|
2022-09-23 08:22:33 +03:00
|
|
|
origin,
|
2022-10-21 23:59:05 +03:00
|
|
|
user_id: user_key_data.user_id,
|
2022-10-20 23:26:14 +03:00
|
|
|
user_key_id: user_key_data.user_key_id,
|
|
|
|
log_revert_chance: user_key_data.log_revert_chance,
|
2022-09-23 00:03:37 +03:00
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-23 01:34:43 +03:00
|
|
|
impl AuthorizedRequest {
|
2022-09-24 10:04:11 +03:00
|
|
|
/// Only User has a database connection in case it needs to save a revert to the database.
|
2022-09-23 08:22:33 +03:00
|
|
|
pub fn db_conn(&self) -> Option<&DatabaseConnection> {
|
|
|
|
match self {
|
|
|
|
Self::User(x, _) => x.as_ref(),
|
2022-10-21 23:59:05 +03:00
|
|
|
_ => None,
|
2022-09-23 08:22:33 +03:00
|
|
|
}
|
2022-09-23 01:34:43 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-03 23:02:05 +03:00
|
|
|
impl Display for &AuthorizedRequest {
|
|
|
|
fn fmt(&self, f: &mut std::fmt::Formatter<'_>) -> std::fmt::Result {
|
|
|
|
match self {
|
2022-10-07 05:21:34 +03:00
|
|
|
AuthorizedRequest::Internal => f.write_str("int"),
|
2022-10-21 23:59:05 +03:00
|
|
|
AuthorizedRequest::Ip(x, _) => f.write_str(&format!("ip-{}", x)),
|
2022-10-10 07:15:07 +03:00
|
|
|
AuthorizedRequest::User(_, x) => f.write_str(&format!("uk-{}", x.user_key_id)),
|
2022-10-03 23:02:05 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 06:59:21 +03:00
|
|
|
pub async fn login_is_authorized(
|
|
|
|
app: &Web3ProxyApp,
|
|
|
|
ip: IpAddr,
|
2022-10-25 07:01:41 +03:00
|
|
|
) -> Result<AuthorizedRequest, FrontendErrorResponse> {
|
|
|
|
let (ip, _semaphore) = match app.rate_limit_login(ip).await? {
|
2022-09-27 05:01:45 +03:00
|
|
|
RateLimitResult::AllowedIp(x, semaphore) => (x, semaphore),
|
2022-09-24 06:59:21 +03:00
|
|
|
RateLimitResult::RateLimitedIp(x, retry_at) => {
|
|
|
|
return Err(FrontendErrorResponse::RateLimitedIp(x, retry_at));
|
|
|
|
}
|
|
|
|
// TODO: don't panic. give the user an error
|
|
|
|
x => unimplemented!("rate_limit_login shouldn't ever see these: {:?}", x),
|
|
|
|
};
|
|
|
|
|
2022-10-25 07:01:41 +03:00
|
|
|
Ok(AuthorizedRequest::Ip(ip, None))
|
2022-09-24 06:59:21 +03:00
|
|
|
}
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
pub async fn ip_is_authorized(
|
|
|
|
app: &Web3ProxyApp,
|
|
|
|
ip: IpAddr,
|
2022-10-21 23:59:05 +03:00
|
|
|
origin: Option<TypedHeader<Origin>>,
|
2022-09-28 06:35:55 +03:00
|
|
|
) -> Result<(AuthorizedRequest, Option<OwnedSemaphorePermit>), FrontendErrorResponse> {
|
2022-10-21 23:59:05 +03:00
|
|
|
let origin = origin.map(|x| x.0);
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
// TODO: i think we could write an `impl From` for this
|
2022-09-24 00:46:27 +03:00
|
|
|
// TODO: move this to an AuthorizedUser extrator
|
2022-10-21 23:59:05 +03:00
|
|
|
let (ip, semaphore) = match app.rate_limit_by_ip(ip, origin.as_ref()).await? {
|
2022-10-25 07:01:41 +03:00
|
|
|
RateLimitResult::AllowedIp(ip, semaphore) => (ip, semaphore),
|
2022-09-23 00:03:37 +03:00
|
|
|
RateLimitResult::RateLimitedIp(x, retry_at) => {
|
|
|
|
return Err(FrontendErrorResponse::RateLimitedIp(x, retry_at));
|
|
|
|
}
|
|
|
|
// TODO: don't panic. give the user an error
|
|
|
|
x => unimplemented!("rate_limit_by_ip shouldn't ever see these: {:?}", x),
|
|
|
|
};
|
|
|
|
|
2022-09-28 06:35:55 +03:00
|
|
|
// semaphore won't ever be None, but its easier if key auth and ip auth work the same way
|
2022-10-21 23:59:05 +03:00
|
|
|
Ok((AuthorizedRequest::Ip(ip, origin), semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn key_is_authorized(
|
|
|
|
app: &Web3ProxyApp,
|
2022-09-24 08:53:45 +03:00
|
|
|
user_key: UserKey,
|
2022-09-23 00:03:37 +03:00
|
|
|
ip: IpAddr,
|
2022-09-23 08:22:33 +03:00
|
|
|
origin: Option<Origin>,
|
2022-09-23 00:03:37 +03:00
|
|
|
referer: Option<Referer>,
|
|
|
|
user_agent: Option<UserAgent>,
|
2022-09-28 06:35:55 +03:00
|
|
|
) -> Result<(AuthorizedRequest, Option<OwnedSemaphorePermit>), FrontendErrorResponse> {
|
2022-09-23 00:03:37 +03:00
|
|
|
// check the rate limits. error if over the limit
|
2022-09-27 05:01:45 +03:00
|
|
|
let (user_data, semaphore) = match app.rate_limit_by_key(user_key).await? {
|
|
|
|
RateLimitResult::AllowedUser(x, semaphore) => (x, semaphore),
|
2022-09-23 00:03:37 +03:00
|
|
|
RateLimitResult::RateLimitedUser(x, retry_at) => {
|
|
|
|
return Err(FrontendErrorResponse::RateLimitedUser(x, retry_at));
|
|
|
|
}
|
|
|
|
RateLimitResult::UnknownKey => return Err(FrontendErrorResponse::UnknownKey),
|
|
|
|
// TODO: don't panic. give the user an error
|
|
|
|
x => unimplemented!("rate_limit_by_key shouldn't ever see these: {:?}", x),
|
|
|
|
};
|
|
|
|
|
2022-09-23 08:22:33 +03:00
|
|
|
let authorized_user = AuthorizedKey::try_new(ip, origin, referer, user_agent, user_data)?;
|
2022-09-23 00:03:37 +03:00
|
|
|
|
2022-10-20 09:17:20 +03:00
|
|
|
let db_conn = app.db_conn.clone();
|
2022-09-23 01:10:28 +03:00
|
|
|
|
2022-10-20 09:17:20 +03:00
|
|
|
Ok((AuthorizedRequest::User(db_conn, authorized_user), semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
impl Web3ProxyApp {
|
2022-10-25 07:01:41 +03:00
|
|
|
pub async fn ip_semaphore(&self, ip: IpAddr) -> anyhow::Result<Option<OwnedSemaphorePermit>> {
|
|
|
|
if let Some(max_concurrent_requests) = self.config.public_max_concurrent_requests {
|
|
|
|
let semaphore = self
|
|
|
|
.ip_semaphores
|
|
|
|
.get_with(ip, async move {
|
|
|
|
// TODO: set max_concurrent_requests dynamically based on load?
|
|
|
|
let s = Semaphore::const_new(max_concurrent_requests);
|
|
|
|
Arc::new(s)
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
|
|
|
// if semaphore.available_permits() == 0 {
|
|
|
|
// // TODO: concurrent limit hit! emit a stat? less important for anon users
|
|
|
|
// // TODO: there is probably a race here
|
|
|
|
// }
|
|
|
|
|
|
|
|
let semaphore_permit = semaphore.acquire_owned().await?;
|
|
|
|
|
|
|
|
Ok(Some(semaphore_permit))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
2022-09-28 06:35:55 +03:00
|
|
|
}
|
|
|
|
|
|
|
|
pub async fn user_key_semaphore(
|
|
|
|
&self,
|
|
|
|
user_data: &UserKeyData,
|
|
|
|
) -> anyhow::Result<Option<OwnedSemaphorePermit>> {
|
|
|
|
if let Some(max_concurrent_requests) = user_data.max_concurrent_requests {
|
|
|
|
let semaphore = self
|
|
|
|
.user_key_semaphores
|
2022-09-30 07:18:18 +03:00
|
|
|
.try_get_with(user_data.user_key_id, async move {
|
|
|
|
let s = Semaphore::const_new(max_concurrent_requests.try_into()?);
|
2022-09-28 06:35:55 +03:00
|
|
|
trace!("new semaphore for user_key_id {}", user_data.user_key_id);
|
2022-09-30 07:18:18 +03:00
|
|
|
Ok::<_, anyhow::Error>(Arc::new(s))
|
2022-09-28 06:35:55 +03:00
|
|
|
})
|
2022-09-30 07:18:18 +03:00
|
|
|
.await
|
|
|
|
// TODO: is this the best way to handle an arc
|
|
|
|
.map_err(|err| anyhow::anyhow!(err))?;
|
2022-09-28 06:35:55 +03:00
|
|
|
|
2022-10-10 07:15:07 +03:00
|
|
|
// if semaphore.available_permits() == 0 {
|
|
|
|
// // TODO: concurrent limit hit! emit a stat
|
|
|
|
// }
|
|
|
|
|
2022-09-28 06:35:55 +03:00
|
|
|
let semaphore_permit = semaphore.acquire_owned().await?;
|
|
|
|
|
|
|
|
Ok(Some(semaphore_permit))
|
|
|
|
} else {
|
|
|
|
Ok(None)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-09-24 06:59:21 +03:00
|
|
|
pub async fn rate_limit_login(&self, ip: IpAddr) -> anyhow::Result<RateLimitResult> {
|
|
|
|
// TODO: dry this up with rate_limit_by_key
|
2022-10-25 07:12:24 +03:00
|
|
|
// TODO: do we want a semaphore here?
|
2022-09-24 06:59:21 +03:00
|
|
|
if let Some(rate_limiter) = &self.login_rate_limiter {
|
|
|
|
match rate_limiter.throttle_label(&ip.to_string(), None, 1).await {
|
2022-10-25 07:01:41 +03:00
|
|
|
Ok(RedisRateLimitResult::Allowed(_)) => Ok(RateLimitResult::AllowedIp(ip, None)),
|
2022-09-24 06:59:21 +03:00
|
|
|
Ok(RedisRateLimitResult::RetryAt(retry_at, _)) => {
|
|
|
|
// TODO: set headers so they know when they can retry
|
|
|
|
// TODO: debug or trace?
|
|
|
|
// this is too verbose, but a stat might be good
|
|
|
|
trace!(?ip, "login rate limit exceeded until {:?}", retry_at);
|
|
|
|
Ok(RateLimitResult::RateLimitedIp(ip, Some(retry_at)))
|
|
|
|
}
|
|
|
|
Ok(RedisRateLimitResult::RetryNever) => {
|
|
|
|
// TODO: i don't think we'll get here. maybe if we ban an IP forever? seems unlikely
|
|
|
|
trace!(?ip, "login rate limit is 0");
|
|
|
|
Ok(RateLimitResult::RateLimitedIp(ip, None))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// internal error, not rate limit being hit
|
|
|
|
// TODO: i really want axum to do this for us in a single place.
|
|
|
|
error!(?err, "login rate limiter is unhappy. allowing ip");
|
2022-09-27 05:01:45 +03:00
|
|
|
|
2022-10-25 07:01:41 +03:00
|
|
|
Ok(RateLimitResult::AllowedIp(ip, None))
|
2022-09-24 06:59:21 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: if no redis, rate limit with a local cache? "warn!" probably isn't right
|
|
|
|
todo!("no rate limiter");
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-10-21 23:59:05 +03:00
|
|
|
pub async fn rate_limit_by_ip(
|
|
|
|
&self,
|
|
|
|
ip: IpAddr,
|
|
|
|
origin: Option<&Origin>,
|
|
|
|
) -> anyhow::Result<RateLimitResult> {
|
2022-09-23 00:03:37 +03:00
|
|
|
// TODO: dry this up with rate_limit_by_key
|
2022-09-28 06:35:55 +03:00
|
|
|
let semaphore = self.ip_semaphore(ip).await?;
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
if let Some(rate_limiter) = &self.frontend_ip_rate_limiter {
|
2022-10-21 23:59:05 +03:00
|
|
|
let max_requests_per_period = origin
|
|
|
|
.map(|origin| {
|
|
|
|
self.config
|
|
|
|
.allowed_origin_requests_per_minute
|
|
|
|
.get(&origin.to_string())
|
|
|
|
.cloned()
|
|
|
|
})
|
|
|
|
.unwrap_or_default();
|
|
|
|
|
|
|
|
match rate_limiter.throttle(ip, max_requests_per_period, 1).await {
|
2022-09-27 05:01:45 +03:00
|
|
|
Ok(DeferredRateLimitResult::Allowed) => {
|
|
|
|
Ok(RateLimitResult::AllowedIp(ip, semaphore))
|
|
|
|
}
|
2022-09-23 00:03:37 +03:00
|
|
|
Ok(DeferredRateLimitResult::RetryAt(retry_at)) => {
|
|
|
|
// TODO: set headers so they know when they can retry
|
|
|
|
// TODO: debug or trace?
|
|
|
|
// this is too verbose, but a stat might be good
|
|
|
|
trace!(?ip, "rate limit exceeded until {:?}", retry_at);
|
|
|
|
Ok(RateLimitResult::RateLimitedIp(ip, Some(retry_at)))
|
|
|
|
}
|
|
|
|
Ok(DeferredRateLimitResult::RetryNever) => {
|
|
|
|
// TODO: i don't think we'll get here. maybe if we ban an IP forever? seems unlikely
|
|
|
|
trace!(?ip, "rate limit is 0");
|
|
|
|
Ok(RateLimitResult::RateLimitedIp(ip, None))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// internal error, not rate limit being hit
|
|
|
|
// TODO: i really want axum to do this for us in a single place.
|
|
|
|
error!(?err, "rate limiter is unhappy. allowing ip");
|
2022-09-27 05:01:45 +03:00
|
|
|
|
|
|
|
Ok(RateLimitResult::AllowedIp(ip, semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: if no redis, rate limit with a local cache? "warn!" probably isn't right
|
2022-10-10 07:15:07 +03:00
|
|
|
Ok(RateLimitResult::AllowedIp(ip, semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// check the local cache for user data, or query the database
|
2022-09-24 08:53:45 +03:00
|
|
|
pub(crate) async fn user_data(&self, user_key: UserKey) -> anyhow::Result<UserKeyData> {
|
2022-09-23 00:03:37 +03:00
|
|
|
let user_data: Result<_, Arc<anyhow::Error>> = self
|
2022-09-27 05:01:45 +03:00
|
|
|
.user_key_cache
|
2022-09-24 08:53:45 +03:00
|
|
|
.try_get_with(user_key.into(), async move {
|
2022-09-23 00:03:37 +03:00
|
|
|
trace!(?user_key, "user_cache miss");
|
|
|
|
|
2022-10-20 09:17:20 +03:00
|
|
|
let db_conn = self.db_conn().context("Getting database connection")?;
|
2022-09-23 00:03:37 +03:00
|
|
|
|
2022-09-24 08:53:45 +03:00
|
|
|
let user_uuid: Uuid = user_key.into();
|
|
|
|
|
2022-09-23 00:03:37 +03:00
|
|
|
// TODO: join the user table to this to return the User? we don't always need it
|
|
|
|
match user_keys::Entity::find()
|
2022-09-24 08:53:45 +03:00
|
|
|
.filter(user_keys::Column::ApiKey.eq(user_uuid))
|
2022-09-23 00:03:37 +03:00
|
|
|
.filter(user_keys::Column::Active.eq(true))
|
2022-10-20 09:17:20 +03:00
|
|
|
.one(&db_conn)
|
2022-09-23 00:03:37 +03:00
|
|
|
.await?
|
|
|
|
{
|
2022-09-23 08:22:33 +03:00
|
|
|
Some(user_key_model) => {
|
|
|
|
let allowed_ips: Option<Vec<IpNet>> =
|
|
|
|
user_key_model.allowed_ips.map(|allowed_ips| {
|
|
|
|
serde_json::from_str::<Vec<String>>(&allowed_ips)
|
|
|
|
.expect("allowed_ips should always parse")
|
|
|
|
.into_iter()
|
|
|
|
// TODO: try_for_each
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<IpNet>().expect("ip address should always parse")
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
// TODO: should this be an Option<Vec<Origin>>?
|
|
|
|
let allowed_origins =
|
|
|
|
user_key_model.allowed_origins.map(|allowed_origins| {
|
|
|
|
serde_json::from_str::<Vec<String>>(&allowed_origins)
|
|
|
|
.expect("allowed_origins should always parse")
|
|
|
|
});
|
|
|
|
|
|
|
|
let allowed_referers =
|
|
|
|
user_key_model.allowed_referers.map(|allowed_referers| {
|
|
|
|
serde_json::from_str::<Vec<String>>(&allowed_referers)
|
|
|
|
.expect("allowed_referers should always parse")
|
|
|
|
.into_iter()
|
|
|
|
// TODO: try_for_each
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<Referer>().expect("referer should always parse")
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
});
|
|
|
|
|
|
|
|
let allowed_user_agents =
|
|
|
|
user_key_model
|
|
|
|
.allowed_user_agents
|
|
|
|
.map(|allowed_user_agents| {
|
|
|
|
serde_json::from_str::<Vec<String>>(&allowed_user_agents)
|
|
|
|
.expect("allowed_user_agents should always parse")
|
|
|
|
.into_iter()
|
|
|
|
// TODO: try_for_each
|
|
|
|
.map(|x| {
|
|
|
|
x.parse::<UserAgent>()
|
|
|
|
.expect("user agent should always parse")
|
|
|
|
})
|
|
|
|
.collect()
|
|
|
|
});
|
2022-09-23 00:03:37 +03:00
|
|
|
|
|
|
|
Ok(UserKeyData {
|
2022-10-21 23:59:05 +03:00
|
|
|
user_id: user_key_model.user_id,
|
2022-09-23 08:22:33 +03:00
|
|
|
user_key_id: user_key_model.id,
|
2022-09-28 06:35:55 +03:00
|
|
|
max_requests_per_period: user_key_model.requests_per_minute,
|
|
|
|
max_concurrent_requests: user_key_model.max_concurrent_requests,
|
2022-09-23 08:22:33 +03:00
|
|
|
allowed_ips,
|
|
|
|
allowed_origins,
|
|
|
|
allowed_referers,
|
|
|
|
allowed_user_agents,
|
2022-09-24 05:47:44 +03:00
|
|
|
log_revert_chance: user_key_model.log_revert_chance,
|
2022-09-23 00:03:37 +03:00
|
|
|
})
|
|
|
|
}
|
2022-09-24 05:47:44 +03:00
|
|
|
None => Ok(UserKeyData::default()),
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
})
|
|
|
|
.await;
|
|
|
|
|
2022-09-30 07:18:18 +03:00
|
|
|
// TODO: what's the best way to handle this arc? try_unwrap will not work
|
|
|
|
user_data.map_err(|err| anyhow::anyhow!(err))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
|
2022-09-24 08:53:45 +03:00
|
|
|
pub async fn rate_limit_by_key(&self, user_key: UserKey) -> anyhow::Result<RateLimitResult> {
|
2022-09-23 00:03:37 +03:00
|
|
|
let user_data = self.user_data(user_key).await?;
|
|
|
|
|
|
|
|
if user_data.user_key_id == 0 {
|
|
|
|
return Ok(RateLimitResult::UnknownKey);
|
|
|
|
}
|
|
|
|
|
2022-09-28 06:35:55 +03:00
|
|
|
let semaphore = self.user_key_semaphore(&user_data).await?;
|
2022-09-27 05:01:45 +03:00
|
|
|
|
2022-09-28 06:35:55 +03:00
|
|
|
let user_max_requests_per_period = match user_data.max_requests_per_period {
|
|
|
|
None => {
|
2022-09-27 05:01:45 +03:00
|
|
|
return Ok(RateLimitResult::AllowedUser(user_data, semaphore));
|
|
|
|
}
|
2022-09-23 00:03:37 +03:00
|
|
|
Some(x) => x,
|
|
|
|
};
|
|
|
|
|
|
|
|
// user key is valid. now check rate limits
|
|
|
|
if let Some(rate_limiter) = &self.frontend_key_rate_limiter {
|
|
|
|
match rate_limiter
|
2022-09-24 08:53:45 +03:00
|
|
|
.throttle(user_key.into(), Some(user_max_requests_per_period), 1)
|
2022-09-23 00:03:37 +03:00
|
|
|
.await
|
|
|
|
{
|
2022-09-27 05:01:45 +03:00
|
|
|
Ok(DeferredRateLimitResult::Allowed) => {
|
|
|
|
Ok(RateLimitResult::AllowedUser(user_data, semaphore))
|
|
|
|
}
|
2022-09-23 00:03:37 +03:00
|
|
|
Ok(DeferredRateLimitResult::RetryAt(retry_at)) => {
|
|
|
|
// TODO: set headers so they know when they can retry
|
|
|
|
// TODO: debug or trace?
|
|
|
|
// this is too verbose, but a stat might be good
|
|
|
|
// TODO: keys are secrets! use the id instead
|
2022-10-10 07:15:07 +03:00
|
|
|
// TODO: emit a stat
|
2022-09-23 00:03:37 +03:00
|
|
|
trace!(?user_key, "rate limit exceeded until {:?}", retry_at);
|
|
|
|
Ok(RateLimitResult::RateLimitedUser(user_data, Some(retry_at)))
|
|
|
|
}
|
|
|
|
Ok(DeferredRateLimitResult::RetryNever) => {
|
|
|
|
// TODO: keys are secret. don't log them!
|
|
|
|
trace!(?user_key, "rate limit is 0");
|
2022-10-10 07:15:07 +03:00
|
|
|
// TODO: emit a stat
|
2022-09-23 00:03:37 +03:00
|
|
|
Ok(RateLimitResult::RateLimitedUser(user_data, None))
|
|
|
|
}
|
|
|
|
Err(err) => {
|
|
|
|
// internal error, not rate limit being hit
|
|
|
|
// TODO: i really want axum to do this for us in a single place.
|
|
|
|
error!(?err, "rate limiter is unhappy. allowing ip");
|
2022-09-27 05:01:45 +03:00
|
|
|
|
|
|
|
Ok(RateLimitResult::AllowedUser(user_data, semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
// TODO: if no redis, rate limit with just a local cache?
|
2022-10-10 07:15:07 +03:00
|
|
|
Ok(RateLimitResult::AllowedUser(user_data, semaphore))
|
2022-09-23 00:03:37 +03:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|