more things should be BadRequest instead of 500

This commit is contained in:
Bryan Stitt 2023-06-08 13:42:45 -07:00
parent 50ec653557
commit 68f73ec0b1
2 changed files with 43 additions and 47 deletions

View File

@ -354,23 +354,33 @@ pub async fn admin_login_post(
// we can't trust that they didn't tamper with the message in some way. like some clients return it hex encoded // we can't trust that they didn't tamper with the message in some way. like some clients return it hex encoded
// TODO: checking 0x seems fragile, but I think it will be fine. siwe message text shouldn't ever start with 0x // TODO: checking 0x seems fragile, but I think it will be fine. siwe message text shouldn't ever start with 0x
let their_msg: Message = if payload.msg.starts_with("0x") { let their_msg: Message = if payload.msg.starts_with("0x") {
let their_msg_bytes = let their_msg_bytes = Bytes::from_str(&payload.msg).map_err(|err| {
Bytes::from_str(&payload.msg).web3_context("parsing payload message")?; Web3ProxyError::BadRequest(
format!("error parsing payload message as Bytes: {}", err).into(),
)
})?;
// TODO: lossy or no? // TODO: lossy or no?
String::from_utf8_lossy(their_msg_bytes.as_ref()) String::from_utf8_lossy(their_msg_bytes.as_ref())
.parse::<siwe::Message>() .parse::<siwe::Message>()
.web3_context("parsing hex string message")? .map_err(|err| {
Web3ProxyError::BadRequest(
format!("error parsing bytes as siwe message: {}", err).into(),
)
})?
} else { } else {
payload payload.msg.parse::<siwe::Message>().map_err(|err| {
.msg Web3ProxyError::BadRequest(
.parse::<siwe::Message>() format!("error parsing string as siwe message: {}", err).into(),
.web3_context("parsing string message")? )
})?
}; };
// the only part of the message we will trust is their nonce // the only part of the message we will trust is their nonce
// TODO: this is fragile. have a helper function/struct for redis keys // TODO: this is fragile. have a helper function/struct for redis keys
let login_nonce = UserBearerToken::from_str(&their_msg.nonce)?; let login_nonce = UserBearerToken::from_str(&their_msg.nonce).map_err(|err| {
Web3ProxyError::BadRequest(format!("error parsing nonce: {}", err).into())
})?;
// fetch the message we gave them from our database // fetch the message we gave them from our database
let db_replica = app let db_replica = app

View File

@ -197,7 +197,10 @@ pub async fn query_user_stats<'a>(
let raw_influx_responses: Vec<FluxRecord> = influxdb_client let raw_influx_responses: Vec<FluxRecord> = influxdb_client
.query_raw(Some(query.clone())) .query_raw(Some(query.clone()))
.await .await
.context("failed parsing query result into a FluxRecord")?; .context(format!(
"failed parsing query result into a FluxRecord. Query={:?}",
query
))?;
// Basically rename all items to be "total", // Basically rename all items to be "total",
// calculate number of "archive_needed" and "error_responses" through their boolean representations ... // calculate number of "archive_needed" and "error_responses" through their boolean representations ...
@ -206,30 +209,28 @@ pub async fn query_user_stats<'a>(
// TODO: I must be able to probably zip the balance query... // TODO: I must be able to probably zip the balance query...
let datapoints = raw_influx_responses let datapoints = raw_influx_responses
.into_iter() .into_iter()
// .into_values()
.map(|x| x.values) .map(|x| x.values)
.map(|value_map| { .map(|value_map| {
// Unwrap all relevant numbers // Unwrap all relevant numbers
// BTreeMap<String, value::Value> let mut out: HashMap<&str, serde_json::Value> = HashMap::new();
let mut out: HashMap<String, serde_json::Value> = HashMap::new();
value_map.into_iter().for_each(|(key, value)| { value_map.into_iter().for_each(|(key, value)| {
if key == "_measurement" { if key == "_measurement" {
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
if inner == "opt_in_proxy" { if inner == "opt_in_proxy" {
out.insert( out.insert(
"collection".to_owned(), "collection",
serde_json::Value::String("opt-in".to_owned()), serde_json::Value::String("opt-in".to_owned()),
); );
} else if inner == "global_proxy" { } else if inner == "global_proxy" {
out.insert( out.insert(
"collection".to_owned(), "collection",
serde_json::Value::String("global".to_owned()), serde_json::Value::String("global".to_owned()),
); );
} else { } else {
warn!("Some datapoints are not part of any _measurement!"); warn!("Some datapoints are not part of any _measurement!");
out.insert( out.insert(
"collection".to_owned(), "collection",
serde_json::Value::String("unknown".to_owned()), serde_json::Value::String("unknown".to_owned()),
); );
} }
@ -241,10 +242,7 @@ pub async fn query_user_stats<'a>(
} else if key == "_stop" { } else if key == "_stop" {
match value { match value {
influxdb2_structmap::value::Value::TimeRFC(inner) => { influxdb2_structmap::value::Value::TimeRFC(inner) => {
out.insert( out.insert("stop_time", serde_json::Value::String(inner.to_string()));
"stop_time".to_owned(),
serde_json::Value::String(inner.to_string()),
);
} }
_ => { _ => {
error!("_stop should always be a TimeRFC!"); error!("_stop should always be a TimeRFC!");
@ -253,10 +251,7 @@ pub async fn query_user_stats<'a>(
} else if key == "_time" { } else if key == "_time" {
match value { match value {
influxdb2_structmap::value::Value::TimeRFC(inner) => { influxdb2_structmap::value::Value::TimeRFC(inner) => {
out.insert( out.insert("time", serde_json::Value::String(inner.to_string()));
"time".to_owned(),
serde_json::Value::String(inner.to_string()),
);
} }
_ => { _ => {
error!("_stop should always be a TimeRFC!"); error!("_stop should always be a TimeRFC!");
@ -266,7 +261,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_backend_requests".to_owned(), "total_backend_requests",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -277,7 +272,7 @@ pub async fn query_user_stats<'a>(
} else if key == "balance" { } else if key == "balance" {
match value { match value {
influxdb2_structmap::value::Value::Double(inner) => { influxdb2_structmap::value::Value::Double(inner) => {
out.insert("balance".to_owned(), json!(f64::from(inner))); out.insert("balance", json!(f64::from(inner)));
} }
_ => { _ => {
error!("balance should always be a Double!"); error!("balance should always be a Double!");
@ -286,10 +281,7 @@ pub async fn query_user_stats<'a>(
} else if key == "cache_hits" { } else if key == "cache_hits" {
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert("total_cache_hits", serde_json::Value::Number(inner.into()));
"total_cache_hits".to_owned(),
serde_json::Value::Number(inner.into()),
);
} }
_ => { _ => {
error!("cache_hits should always be a Long!"); error!("cache_hits should always be a Long!");
@ -299,7 +291,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_cache_misses".to_owned(), "total_cache_misses",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -311,7 +303,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_frontend_requests".to_owned(), "total_frontend_requests",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -322,10 +314,7 @@ pub async fn query_user_stats<'a>(
} else if key == "no_servers" { } else if key == "no_servers" {
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert("no_servers", serde_json::Value::Number(inner.into()));
"no_servers".to_owned(),
serde_json::Value::Number(inner.into()),
);
} }
_ => { _ => {
error!("no_servers should always be a Long!"); error!("no_servers should always be a Long!");
@ -334,7 +323,7 @@ pub async fn query_user_stats<'a>(
} else if key == "sum_credits_used" { } else if key == "sum_credits_used" {
match value { match value {
influxdb2_structmap::value::Value::Double(inner) => { influxdb2_structmap::value::Value::Double(inner) => {
out.insert("total_credits_used".to_owned(), json!(f64::from(inner))); out.insert("total_credits_used", json!(f64::from(inner)));
} }
_ => { _ => {
error!("sum_credits_used should always be a Double!"); error!("sum_credits_used should always be a Double!");
@ -344,7 +333,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_request_bytes".to_owned(), "total_request_bytes",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -356,7 +345,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_response_bytes".to_owned(), "total_response_bytes",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -368,7 +357,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
out.insert( out.insert(
"rpc_key".to_owned(), "rpc_key",
serde_json::Value::String( serde_json::Value::String(
rpc_key_id_to_key.get(&inner).unwrap().to_string(), rpc_key_id_to_key.get(&inner).unwrap().to_string(),
), ),
@ -382,7 +371,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::Long(inner) => { influxdb2_structmap::value::Value::Long(inner) => {
out.insert( out.insert(
"total_response_millis".to_owned(), "total_response_millis",
serde_json::Value::Number(inner.into()), serde_json::Value::Number(inner.into()),
); );
} }
@ -395,7 +384,7 @@ pub async fn query_user_stats<'a>(
else if stat_response_type == StatType::Detailed && key == "method" { else if stat_response_type == StatType::Detailed && key == "method" {
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
out.insert("method".to_owned(), serde_json::Value::String(inner)); out.insert("method", serde_json::Value::String(inner));
} }
_ => { _ => {
error!("method should always be a String!"); error!("method should always be a String!");
@ -404,7 +393,7 @@ pub async fn query_user_stats<'a>(
} else if key == "chain_id" { } else if key == "chain_id" {
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
out.insert("chain_id".to_owned(), serde_json::Value::String(inner)); out.insert("chain_id", serde_json::Value::String(inner));
} }
_ => { _ => {
error!("chain_id should always be a String!"); error!("chain_id should always be a String!");
@ -414,7 +403,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
out.insert( out.insert(
"archive_needed".to_owned(), "archive_needed",
if inner == "true" { if inner == "true" {
serde_json::Value::Bool(true) serde_json::Value::Bool(true)
} else if inner == "false" { } else if inner == "false" {
@ -432,7 +421,7 @@ pub async fn query_user_stats<'a>(
match value { match value {
influxdb2_structmap::value::Value::String(inner) => { influxdb2_structmap::value::Value::String(inner) => {
out.insert( out.insert(
"error_response".to_owned(), "error_response",
if inner == "true" { if inner == "true" {
serde_json::Value::Bool(true) serde_json::Value::Bool(true)
} else if inner == "false" { } else if inner == "false" {
@ -484,9 +473,6 @@ pub async fn query_user_stats<'a>(
} }
let response = Json(json!(response_body)).into_response(); let response = Json(json!(response_body)).into_response();
// Add the requests back into out
// TODO: Now impplement the proper response type
Ok(response) Ok(response)
} }