From 69d3e0e6564b416637978a69f0a035066aea4759 Mon Sep 17 00:00:00 2001 From: FxQnLr Date: Wed, 10 Apr 2024 20:15:39 +0200 Subject: Closes #30 and #27. At least a little --- src/auth.rs | 5 +++++ src/error.rs | 13 +++++++++---- src/main.rs | 5 +++-- src/storage.rs | 7 ++++++- 4 files changed, 23 insertions(+), 7 deletions(-) (limited to 'src') diff --git a/src/auth.rs b/src/auth.rs index 74008b5..c662e36 100644 --- a/src/auth.rs +++ b/src/auth.rs @@ -6,6 +6,7 @@ use axum::{ response::Response, }; use serde::Deserialize; +use tracing::trace; #[derive(Debug, Clone, Deserialize)] pub enum Methods { @@ -20,15 +21,19 @@ pub async fn auth( next: Next, ) -> Result { let auth = state.config.auth; + trace!(?auth.method, "auth request"); match auth.method { Methods::Key => { if let Some(secret) = headers.get("authorization") { if auth.secret.as_str() != secret { + trace!("auth failed, unknown secret"); return Err(StatusCode::UNAUTHORIZED); }; + trace!("auth successfull"); let response = next.run(request).await; Ok(response) } else { + trace!("auth failed, no secret"); Err(StatusCode::UNAUTHORIZED) } } diff --git a/src/error.rs b/src/error.rs index 8a011bf..2d70592 100644 --- a/src/error.rs +++ b/src/error.rs @@ -7,7 +7,7 @@ use mac_address::MacParseError; use serde_json::json; use utoipa::ToSchema; use std::io; -use tracing::error; +use tracing::{error, warn}; #[derive(Debug, thiserror::Error, ToSchema)] pub enum Error { @@ -50,15 +50,20 @@ pub enum Error { impl IntoResponse for Error { fn into_response(self) -> Response { - error!("{}", self.to_string()); + // error!("{}", self.to_string()); let (status, error_message) = match self { Self::Json { source } => { error!("{source}"); (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") } Self::Io { source } => { - error!("{source}"); - (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + if source.kind() == io::ErrorKind::NotFound { + warn!("unknown device requested"); + (StatusCode::NOT_FOUND, "Requested device not found") + } else { + error!("{source}"); + (StatusCode::INTERNAL_SERVER_ERROR, "Server Error") + } } Self::ParseHeader { source } => { error!("{source}"); diff --git a/src/main.rs b/src/main.rs index 8af8c63..204c318 100644 --- a/src/main.rs +++ b/src/main.rs @@ -10,7 +10,7 @@ use dashmap::DashMap; use std::{env, sync::Arc}; use time::UtcOffset; use tokio::sync::broadcast::{channel, Sender}; -use tracing::{info, level_filters::LevelFilter}; +use tracing::{info, level_filters::LevelFilter, trace}; use tracing_subscriber::{ fmt::{self, time::OffsetTime}, prelude::*, @@ -89,11 +89,12 @@ async fn main() -> color_eyre::eyre::Result<()> { .from_env_lossy(), ) .init(); + trace!("logging initialized"); Device::setup()?; let version = env!("CARGO_PKG_VERSION"); - info!("start webol v{}", version); + info!(?version, "start webol"); let (tx, _) = channel(32); diff --git a/src/storage.rs b/src/storage.rs index 6ba5ee1..0da245b 100644 --- a/src/storage.rs +++ b/src/storage.rs @@ -8,7 +8,7 @@ use ipnetwork::IpNetwork; use mac_address::MacAddress; use serde::{Deserialize, Serialize}; use serde_json::json; -use tracing::{debug, warn}; +use tracing::{debug, trace, warn}; use utoipa::ToSchema; use crate::error::Error; @@ -26,6 +26,7 @@ impl Device { const STORAGE_PATH: &'static str = "devices"; pub fn setup() -> Result { + trace!("check for storage at {}", Self::STORAGE_PATH); let sp = Path::new(Self::STORAGE_PATH); if !sp.exists() { warn!("device storage path doesn't exist, creating it"); @@ -38,17 +39,21 @@ impl Device { } pub fn read(id: &str) -> Result { + trace!(?id, "attempt to read file"); let mut file = File::open(format!("{}/{id}.json", Self::STORAGE_PATH))?; let mut buf = String::new(); file.read_to_string(&mut buf)?; + trace!(?id, ?buf, "read successfully from file"); let dev = serde_json::from_str(&buf)?; Ok(dev) } pub fn write(&self) -> Result<(), Error> { + trace!(?self.id, ?self, "attempt to write to file"); let mut file = File::create(format!("{}/{}.json", Self::STORAGE_PATH, self.id))?; file.write_all(json!(self).to_string().as_bytes())?; + trace!(?self.id, "wrote successfully to file"); Ok(()) } -- cgit v1.2.3