diff options
Diffstat (limited to 'exes/webhook/src')
| -rw-r--r-- | exes/webhook/src/config.rs | 1 | ||||
| -rw-r--r-- | exes/webhook/src/handler/mod.rs | 12 | ||||
| -rw-r--r-- | exes/webhook/src/handler/signature.rs | 38 | ||||
| -rw-r--r-- | exes/webhook/src/handler/tests/handler.rs | 1 | ||||
| -rw-r--r-- | exes/webhook/src/handler/tests/mod.rs | 2 | ||||
| -rw-r--r-- | exes/webhook/src/lib.rs | 7 | 
6 files changed, 14 insertions, 47 deletions
diff --git a/exes/webhook/src/config.rs b/exes/webhook/src/config.rs index d1b3fb6..02543e6 100644 --- a/exes/webhook/src/config.rs +++ b/exes/webhook/src/config.rs @@ -9,7 +9,6 @@ fn default_listening_address() -> SocketAddr {  #[derive(Debug, Deserialize, Clone, Copy)]  pub struct ServerSettings { -    #[serde(default = "default_listening_address")]      pub listening_adress: SocketAddr,  }  impl Default for ServerSettings { diff --git a/exes/webhook/src/handler/mod.rs b/exes/webhook/src/handler/mod.rs index 3ef859e..594919b 100644 --- a/exes/webhook/src/handler/mod.rs +++ b/exes/webhook/src/handler/mod.rs @@ -1,4 +1,5 @@  use crate::config::WebhookConfig; +use async_nats::Client;  use ed25519_dalek::PublicKey;  use error::WebhookError;  use hyper::{ @@ -6,11 +7,7 @@ use hyper::{      service::Service,      Body, Method, Request, Response, StatusCode,  }; -use shared::nats_crate::Client; -use shared::{ -    log::{debug, error}, -    payloads::{CachePayload, DispatchEventTagged, Tracing}, -}; +use shared::payloads::{CachePayload, DispatchEventTagged};  use signature::validate_signature;  use std::{      future::Future, @@ -18,6 +15,7 @@ use std::{      str::from_utf8,      task::{Context, Poll},  }; +use tracing::{debug, error};  use twilight_model::gateway::event::DispatchEvent;  use twilight_model::{      application::interaction::{Interaction, InteractionType}, @@ -98,10 +96,6 @@ impl WebhookService {                                      // this should hopefully not fail ?                                      let data = CachePayload { -                                        tracing: Tracing { -                                            node_id: "".to_string(), -                                            span: None, -                                        },                                          data: DispatchEventTagged {                                              data: DispatchEvent::InteractionCreate(Box::new(                                                  InteractionCreate(value), diff --git a/exes/webhook/src/handler/signature.rs b/exes/webhook/src/handler/signature.rs index fc5555f..ece7b85 100644 --- a/exes/webhook/src/handler/signature.rs +++ b/exes/webhook/src/handler/signature.rs @@ -1,41 +1,13 @@ -use shared::prometheus::{Counter, HistogramVec, labels, opts, register_counter, register_histogram_vec}; -use ed25519_dalek::PublicKey; -use ed25519_dalek::Verifier; -use ed25519_dalek::Signature; -use std::convert::TryInto; - -lazy_static::lazy_static! { -    static ref SIGNATURE_TIME_HISTOGRAM: HistogramVec = register_histogram_vec!( -        "nova_webhook_signature_time", -        "The time taken by the signature verification", -        &["signature"] -    ).unwrap(); - -    static ref SIGNATURE_COUNTER: Counter = register_counter!(opts!( -        "nova_webhook_signatures_verify", -        "number of signatures verification issued by the service", -        labels! {"handler" => "webhook_main"} -    )).unwrap(); -} - -fn demo<T, const N: usize>(v: Vec<T>) -> [T; N] { -    v.try_into() -        .unwrap_or_else(|v: Vec<T>| panic!("Expected a Vec of length {} but it was {}", N, v.len())) -} +use ed25519_dalek::{PublicKey, Signature, Verifier};  pub fn validate_signature(public_key: &PublicKey, data: &[u8], hex_signature: &str) -> bool { -    SIGNATURE_COUNTER.inc(); -    let timer = SIGNATURE_TIME_HISTOGRAM.with_label_values(&["webhook_main"]).start_timer(); - -    let signature_result = hex::decode(hex_signature); +    let mut slice: [u8; Signature::BYTE_SIZE] = [0; Signature::BYTE_SIZE]; +    let signature_result = hex::decode_to_slice(hex_signature, &mut slice);      let mut result = false; -    if let Ok(signature) = signature_result { -        let sig = Signature::from(demo(signature)); - -        result = public_key.verify(data, &sig).is_ok(); +    if signature_result.is_ok() { +        result = public_key.verify(data, &Signature::from(slice)).is_ok();      } -    timer.observe_duration();      result  } diff --git a/exes/webhook/src/handler/tests/handler.rs b/exes/webhook/src/handler/tests/handler.rs index e69de29..8b13789 100644 --- a/exes/webhook/src/handler/tests/handler.rs +++ b/exes/webhook/src/handler/tests/handler.rs @@ -0,0 +1 @@ + diff --git a/exes/webhook/src/handler/tests/mod.rs b/exes/webhook/src/handler/tests/mod.rs index cf7f558..60ae6d3 100644 --- a/exes/webhook/src/handler/tests/mod.rs +++ b/exes/webhook/src/handler/tests/mod.rs @@ -1,2 +1,2 @@ -pub mod signature;  pub mod handler; +pub mod signature; diff --git a/exes/webhook/src/lib.rs b/exes/webhook/src/lib.rs index 43ab9c4..057e70f 100644 --- a/exes/webhook/src/lib.rs +++ b/exes/webhook/src/lib.rs @@ -6,11 +6,12 @@ use crate::{      config::WebhookConfig,      handler::{make_service::MakeSvc, WebhookService},  }; +use async_nats::Client;  use hyper::Server;  use leash::{AnyhowResultFuture, Component}; -use shared::{config::Settings, log::info, nats_crate::Client}; +use shared::config::Settings;  use tokio::sync::oneshot; - +use tracing::info;  #[derive(Clone, Copy)]  pub struct WebhookServer {} @@ -27,7 +28,7 @@ impl Component for WebhookServer {              info!("Starting server on {}", settings.server.listening_adress);              let bind = settings.server.listening_adress; -            info!("NAts connected!"); +            info!("Nats connected!");              let nats = Into::<Pin<Box<dyn Future<Output = anyhow::Result<Client>> + Send>>>::into(                  settings.nats,              )  | 
