summaryrefslogtreecommitdiff
path: root/exes/webhook
diff options
context:
space:
mode:
authorMatthieuCoder <matthieu@matthieu-dev.xyz>2023-01-05 18:33:53 +0400
committerMatthieuCoder <matthieu@matthieu-dev.xyz>2023-01-05 18:33:53 +0400
commit038add4d5e8465f8bb36f1a1fa5817a02cab833b (patch)
tree2bcab259fc3b7a57ff9de4b043fa0c5571c85622 /exes/webhook
parent63565094f480154556be69a6b3625e47c3b28f04 (diff)
base for tracing
Diffstat (limited to 'exes/webhook')
-rw-r--r--exes/webhook/Cargo.toml14
-rw-r--r--exes/webhook/src/config.rs1
-rw-r--r--exes/webhook/src/handler/mod.rs12
-rw-r--r--exes/webhook/src/handler/signature.rs38
-rw-r--r--exes/webhook/src/handler/tests/handler.rs1
-rw-r--r--exes/webhook/src/handler/tests/mod.rs2
-rw-r--r--exes/webhook/src/lib.rs7
7 files changed, 20 insertions, 55 deletions
diff --git a/exes/webhook/Cargo.toml b/exes/webhook/Cargo.toml
index 589b5bd..0c50009 100644
--- a/exes/webhook/Cargo.toml
+++ b/exes/webhook/Cargo.toml
@@ -4,21 +4,19 @@ version = "0.1.0"
edition = "2018"
[dependencies]
-hyper = { version = "0.14", features = ["full"] }
-tokio = { version = "1", features = ["full"] }
+hyper = "0.14"
+tokio = { version = "1", features = ["rt"] }
shared = { path = "../../libs/shared" }
proto = { path = "../../libs/proto" }
leash = { path = "../../libs/leash" }
+tracing = "0.1.37"
serde = { version = "1.0.8", features = ["derive"] }
-hex = "0.4.3"
serde_json = { version = "1.0" }
-lazy_static = "1.4.0"
+
+hex = "0.4.3"
ed25519-dalek = "1"
twilight-model = { version = "0.14" }
anyhow = "1.0.68"
-futures-util = "0.3.25"
-[[bin]]
-name = "webhook"
-path = "src/main.rs"
+async-nats = "0.25.1"
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,
)