cleanup
This commit is contained in:
parent
e7e949a53e
commit
8278fe006b
1
TODO.md
1
TODO.md
@ -243,6 +243,7 @@ These are roughly in order of completition
|
|||||||
|
|
||||||
These are not yet ordered.
|
These are not yet ordered.
|
||||||
|
|
||||||
|
- [ ] admin-only page for viewing user stat pages
|
||||||
- [ ] geth sometimes gives an empty response instead of an error response. figure out a good way to catch this and not serve it
|
- [ ] geth sometimes gives an empty response instead of an error response. figure out a good way to catch this and not serve it
|
||||||
- [ ] GET balance endpoint
|
- [ ] GET balance endpoint
|
||||||
- [ ] POST balance endpoint
|
- [ ] POST balance endpoint
|
||||||
|
@ -7,7 +7,7 @@ use anyhow::Context;
|
|||||||
use axum::headers::{authorization::Bearer, Origin, Referer, UserAgent};
|
use axum::headers::{authorization::Bearer, Origin, Referer, UserAgent};
|
||||||
use chrono::Utc;
|
use chrono::Utc;
|
||||||
use deferred_rate_limiter::DeferredRateLimitResult;
|
use deferred_rate_limiter::DeferredRateLimitResult;
|
||||||
use entities::user_keys;
|
use entities::{user, user_keys};
|
||||||
use ipnet::IpNet;
|
use ipnet::IpNet;
|
||||||
use redis_rate_limiter::redis::AsyncCommands;
|
use redis_rate_limiter::redis::AsyncCommands;
|
||||||
use redis_rate_limiter::RedisRateLimitResult;
|
use redis_rate_limiter::RedisRateLimitResult;
|
||||||
@ -282,7 +282,7 @@ pub async fn bearer_is_authorized(
|
|||||||
let bearer_cache_key = format!("bearer:{}", bearer.token());
|
let bearer_cache_key = format!("bearer:{}", bearer.token());
|
||||||
|
|
||||||
// turn bearer into a user key id
|
// turn bearer into a user key id
|
||||||
let user_key_id: u64 = redis_conn
|
let user_id: u64 = redis_conn
|
||||||
.get(bearer_cache_key)
|
.get(bearer_cache_key)
|
||||||
.await
|
.await
|
||||||
.context("unknown bearer token")?;
|
.context("unknown bearer token")?;
|
||||||
@ -290,21 +290,22 @@ pub async fn bearer_is_authorized(
|
|||||||
let db_conn = app.db_conn().context("Getting database connection")?;
|
let db_conn = app.db_conn().context("Getting database connection")?;
|
||||||
|
|
||||||
// turn user key id into a user key
|
// turn user key id into a user key
|
||||||
let user_key_data = user_keys::Entity::find_by_id(user_key_id)
|
let user_key_data = user::Entity::find_by_id(user_id)
|
||||||
.one(&db_conn)
|
.one(&db_conn)
|
||||||
.await
|
.await
|
||||||
.context("fetching user key by id")?
|
.context("fetching user by id")?
|
||||||
.context("unknown user id")?;
|
.context("unknown user id")?;
|
||||||
|
|
||||||
key_is_authorized(
|
todo!("api_key is wrong. we should check user ids instead")
|
||||||
app,
|
// key_is_authorized(
|
||||||
user_key_data.api_key.into(),
|
// app,
|
||||||
ip,
|
// user_key_data.api_key.into(),
|
||||||
origin,
|
// ip,
|
||||||
referer,
|
// origin,
|
||||||
user_agent,
|
// referer,
|
||||||
)
|
// user_agent,
|
||||||
.await
|
// )
|
||||||
|
// .await
|
||||||
}
|
}
|
||||||
|
|
||||||
pub async fn ip_is_authorized(
|
pub async fn ip_is_authorized(
|
||||||
|
@ -1,6 +1,6 @@
|
|||||||
//! Take a user's HTTP JSON-RPC requests and either respond from local data or proxy the request to a backend rpc server.
|
//! Take a user's HTTP JSON-RPC requests and either respond from local data or proxy the request to a backend rpc server.
|
||||||
|
|
||||||
use super::authorization::{bearer_is_authorized, ip_is_authorized, key_is_authorized};
|
use super::authorization::{ip_is_authorized, key_is_authorized};
|
||||||
use super::errors::FrontendResult;
|
use super::errors::FrontendResult;
|
||||||
use crate::{app::Web3ProxyApp, jsonrpc::JsonRpcRequestEnum};
|
use crate::{app::Web3ProxyApp, jsonrpc::JsonRpcRequestEnum};
|
||||||
use axum::extract::Path;
|
use axum::extract::Path;
|
||||||
|
@ -324,8 +324,8 @@ pub async fn user_profile_post(
|
|||||||
// give these named variables so that we drop them at the very end of this function
|
// give these named variables so that we drop them at the very end of this function
|
||||||
let (_, _semaphore) = login_is_authorized(&app, ip).await?;
|
let (_, _semaphore) = login_is_authorized(&app, ip).await?;
|
||||||
|
|
||||||
let user = ProtectedAction::PostUser
|
let user = ProtectedAction::PostUser(payload.primary_address)
|
||||||
.verify(app.as_ref(), bearer_token, &payload.primary_address)
|
.verify(app.as_ref(), bearer_token)
|
||||||
.await?;
|
.await?;
|
||||||
|
|
||||||
let mut user: user::ActiveModel = user.into();
|
let mut user: user::ActiveModel = user.into();
|
||||||
@ -424,10 +424,65 @@ pub async fn user_profile_get(
|
|||||||
/// TODO: this will change as we add better support for secondary users.
|
/// TODO: this will change as we add better support for secondary users.
|
||||||
#[debug_handler]
|
#[debug_handler]
|
||||||
pub async fn user_stats_get(
|
pub async fn user_stats_get(
|
||||||
TypedHeader(Authorization(bearer_token)): TypedHeader<Authorization<Bearer>>,
|
// TODO: turn this back on when done debugging. maybe add a path field for this
|
||||||
|
// TypedHeader(Authorization(bearer)): TypedHeader<Authorization<Bearer>>,
|
||||||
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
Extension(app): Extension<Arc<Web3ProxyApp>>,
|
||||||
|
Query(params): Query<HashMap<String, String>>,
|
||||||
) -> FrontendResult {
|
) -> FrontendResult {
|
||||||
todo!("user_stats_get");
|
/*
|
||||||
|
// get the attached address from redis for the given auth_token.
|
||||||
|
let mut redis_conn = app.redis_conn().await?;
|
||||||
|
|
||||||
|
// check for the bearer cache key
|
||||||
|
// TODO: move this to a helper function
|
||||||
|
let bearer_cache_key = format!("bearer:{}", bearer.token());
|
||||||
|
let user_id = redis_conn
|
||||||
|
.get::<_, u64>(bearer_cache_key)
|
||||||
|
.await
|
||||||
|
// TODO: this should be a 403
|
||||||
|
.context("fetching user_key_id from redis with bearer_cache_key")?;
|
||||||
|
|
||||||
|
*/
|
||||||
|
// TODO: remove this and get the user id that matches the bearer
|
||||||
|
let user_id = params.get("user_id").unwrap().parse().unwrap();
|
||||||
|
|
||||||
|
let db = app.db_conn.clone().context("no db")?;
|
||||||
|
|
||||||
|
let chain_id = params
|
||||||
|
.get("chain_id")
|
||||||
|
.map_or_else::<anyhow::Result<u64>, _, _>(
|
||||||
|
|| Ok(app.config.chain_id),
|
||||||
|
|c| {
|
||||||
|
let c = c.parse()?;
|
||||||
|
|
||||||
|
Ok(c)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let query_start = params
|
||||||
|
.get("timestamp")
|
||||||
|
.map_or_else::<anyhow::Result<NaiveDateTime>, _, _>(
|
||||||
|
|| {
|
||||||
|
// no timestamp in params. set default
|
||||||
|
let x = chrono::Utc::now() - chrono::Duration::days(30);
|
||||||
|
|
||||||
|
Ok(x.naive_utc())
|
||||||
|
},
|
||||||
|
|x: &String| {
|
||||||
|
// parse the given timestamp
|
||||||
|
let x = x.parse::<i64>().context("parsing timestamp query param")?;
|
||||||
|
|
||||||
|
// TODO: error code 401
|
||||||
|
let x = NaiveDateTime::from_timestamp_opt(x, 0)
|
||||||
|
.context("parsing timestamp query param")?;
|
||||||
|
|
||||||
|
Ok(x)
|
||||||
|
},
|
||||||
|
)?;
|
||||||
|
|
||||||
|
let x = get_aggregate_stats(chain_id, &db, query_start, Some(user_id)).await?;
|
||||||
|
|
||||||
|
Ok(Json(x).into_response())
|
||||||
}
|
}
|
||||||
|
|
||||||
/// `GET /user/stats/aggregate` -- Public endpoint for aggregate stats such as bandwidth used and methods requested.
|
/// `GET /user/stats/aggregate` -- Public endpoint for aggregate stats such as bandwidth used and methods requested.
|
||||||
@ -451,7 +506,7 @@ pub async fn user_stats_aggregate_get(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
let query_start = params
|
let query_start = params
|
||||||
.get("start_timestamp")
|
.get("timestamp")
|
||||||
.map_or_else::<anyhow::Result<NaiveDateTime>, _, _>(
|
.map_or_else::<anyhow::Result<NaiveDateTime>, _, _>(
|
||||||
|| {
|
|| {
|
||||||
// no timestamp in params. set default
|
// no timestamp in params. set default
|
||||||
@ -472,7 +527,7 @@ pub async fn user_stats_aggregate_get(
|
|||||||
)?;
|
)?;
|
||||||
|
|
||||||
// TODO: optionally no chain id?
|
// TODO: optionally no chain id?
|
||||||
let x = get_aggregate_stats(chain_id, &db, query_start).await?;
|
let x = get_aggregate_stats(chain_id, &db, query_start, None).await?;
|
||||||
|
|
||||||
Ok(Json(x).into_response())
|
Ok(Json(x).into_response())
|
||||||
}
|
}
|
||||||
@ -481,7 +536,7 @@ pub async fn user_stats_aggregate_get(
|
|||||||
/// Handle authorization for a given address and bearer token.
|
/// Handle authorization for a given address and bearer token.
|
||||||
// TODO: what roles should exist?
|
// TODO: what roles should exist?
|
||||||
enum ProtectedAction {
|
enum ProtectedAction {
|
||||||
PostUser,
|
PostUser(Address),
|
||||||
}
|
}
|
||||||
|
|
||||||
impl ProtectedAction {
|
impl ProtectedAction {
|
||||||
@ -491,15 +546,15 @@ impl ProtectedAction {
|
|||||||
app: &Web3ProxyApp,
|
app: &Web3ProxyApp,
|
||||||
// TODO: i don't think we want Bearer here. we want user_key and a helper for bearer -> user_key
|
// TODO: i don't think we want Bearer here. we want user_key and a helper for bearer -> user_key
|
||||||
bearer: Bearer,
|
bearer: Bearer,
|
||||||
// TODO: what about secondary addresses? maybe an enum for primary or secondary?
|
|
||||||
primary_address: &Address,
|
|
||||||
) -> anyhow::Result<user::Model> {
|
) -> anyhow::Result<user::Model> {
|
||||||
// get the attached address from redis for the given auth_token.
|
// get the attached address from redis for the given auth_token.
|
||||||
let mut redis_conn = app.redis_conn().await?;
|
let mut redis_conn = app.redis_conn().await?;
|
||||||
|
|
||||||
|
// TODO: move this to a helper function
|
||||||
let bearer_cache_key = format!("bearer:{}", bearer.token());
|
let bearer_cache_key = format!("bearer:{}", bearer.token());
|
||||||
|
|
||||||
let user_key_id: Option<u64> = redis_conn
|
// TODO: move this to a helper function
|
||||||
|
let user_id: Option<u64> = redis_conn
|
||||||
.get(bearer_cache_key)
|
.get(bearer_cache_key)
|
||||||
.await
|
.await
|
||||||
.context("fetching bearer cache key from redis")?;
|
.context("fetching bearer cache key from redis")?;
|
||||||
|
@ -1,12 +1,19 @@
|
|||||||
use entities::rpc_accounting;
|
use entities::{rpc_accounting, user, user_keys};
|
||||||
use num::Zero;
|
use num::Zero;
|
||||||
use sea_orm::{ColumnTrait, Condition, DatabaseConnection, EntityTrait, QueryFilter, QuerySelect};
|
use sea_orm::{
|
||||||
|
ColumnTrait, Condition, DatabaseConnection, EntityTrait, JoinType, QueryFilter, QuerySelect,
|
||||||
|
RelationTrait,
|
||||||
|
};
|
||||||
|
use tracing::debug;
|
||||||
|
|
||||||
pub async fn get_aggregate_stats(
|
pub async fn get_aggregate_stats(
|
||||||
chain_id: u64,
|
chain_id: u64,
|
||||||
db: &DatabaseConnection,
|
db: &DatabaseConnection,
|
||||||
query_start: chrono::NaiveDateTime,
|
query_start: chrono::NaiveDateTime,
|
||||||
|
user_id: Option<u64>,
|
||||||
) -> anyhow::Result<Vec<serde_json::Value>> {
|
) -> anyhow::Result<Vec<serde_json::Value>> {
|
||||||
|
debug!(?chain_id, %query_start, ?user_id, "get_aggregate_stats");
|
||||||
|
|
||||||
// TODO: how do we get count reverts compared to other errors? does it matter? what about http errors to our users?
|
// TODO: how do we get count reverts compared to other errors? does it matter? what about http errors to our users?
|
||||||
// TODO: how do we count uptime?
|
// TODO: how do we count uptime?
|
||||||
let q = rpc_accounting::Entity::find()
|
let q = rpc_accounting::Entity::find()
|
||||||
@ -40,8 +47,10 @@ pub async fn get_aggregate_stats(
|
|||||||
|
|
||||||
let condition = Condition::all().add(rpc_accounting::Column::PeriodDatetime.gte(query_start));
|
let condition = Condition::all().add(rpc_accounting::Column::PeriodDatetime.gte(query_start));
|
||||||
|
|
||||||
|
/*
|
||||||
let (q, condition) = if chain_id.is_zero() {
|
let (q, condition) = if chain_id.is_zero() {
|
||||||
// fetch all the chains. don't filter
|
// fetch all the chains. don't filter
|
||||||
|
// TODO: wait. do we want chain id on the logs? we can get that by joining key
|
||||||
let q = q
|
let q = q
|
||||||
.column(rpc_accounting::Column::ChainId)
|
.column(rpc_accounting::Column::ChainId)
|
||||||
.group_by(rpc_accounting::Column::ChainId);
|
.group_by(rpc_accounting::Column::ChainId);
|
||||||
@ -52,9 +61,25 @@ pub async fn get_aggregate_stats(
|
|||||||
|
|
||||||
(q, condition)
|
(q, condition)
|
||||||
};
|
};
|
||||||
|
*/
|
||||||
|
|
||||||
let q = q.filter(condition);
|
let q = q.filter(condition);
|
||||||
|
|
||||||
|
// // TODO: also check secondary users
|
||||||
|
// let q = if let Some(user_id) = user_id {
|
||||||
|
// // TODO: authentication here? or should that be higher in the stack? here sems safest
|
||||||
|
// // TODO: only join some columns
|
||||||
|
// // TODO: are these joins correct?
|
||||||
|
// q.join(
|
||||||
|
// JoinType::InnerJoin,
|
||||||
|
// rpc_accounting::Relation::UserKeys.def(),
|
||||||
|
// )
|
||||||
|
// .join(JoinType::InnerJoin, user_keys::Relation::User.def())
|
||||||
|
// .filter(user::Column::Id.eq(user_id))
|
||||||
|
// } else {
|
||||||
|
// q
|
||||||
|
// };
|
||||||
|
|
||||||
// TODO: if user key id is set, use that
|
// TODO: if user key id is set, use that
|
||||||
// TODO: if user id is set, use that
|
// TODO: if user id is set, use that
|
||||||
// TODO: handle secondary users, too
|
// TODO: handle secondary users, too
|
||||||
|
Loading…
Reference in New Issue
Block a user