Rename webook.rs to filter.rs

Also improve code separation, still more to come.
This commit is contained in:
finga 2021-11-19 11:28:37 +01:00
parent b7ad590d39
commit 3a95ecfd11
2 changed files with 42 additions and 43 deletions

View file

@ -1,3 +1,4 @@
use crate::WebhookeyError;
use anyhow::Result; use anyhow::Result;
use ipnet::IpNet; use ipnet::IpNet;
use log::{debug, error, trace}; use log::{debug, error, trace};
@ -5,23 +6,6 @@ use regex::Regex;
use rocket::{http::HeaderMap, Request}; use rocket::{http::HeaderMap, Request};
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use std::net::IpAddr; use std::net::IpAddr;
use thiserror::Error;
#[derive(Debug, Error)]
pub enum WebhookeyError {
#[error("Could not extract signature from header")]
InvalidSignature,
#[error("Unauthorized request from `{0}`")]
Unauthorized(IpAddr),
#[error("Unmatched hook from `{0}`")]
UnmatchedHook(IpAddr),
#[error("Could not evaluate filter request")]
InvalidFilter,
#[error("IO Error")]
Io(std::io::Error),
#[error("Serde Error")]
Serde(serde_json::Error),
}
#[derive(Debug, Deserialize, Serialize)] #[derive(Debug, Deserialize, Serialize)]
#[serde(deny_unknown_fields, untagged)] #[serde(deny_unknown_fields, untagged)]
@ -104,7 +88,7 @@ impl JsonFilter {
); );
if let Some(value) = data.pointer(&self.pointer) { if let Some(value) = data.pointer(&self.pointer) {
if self.regex.is_match(&get_string(value)?) { if self.regex.is_match(&crate::get_string(value)?) {
debug!("Regex `{}` for `{}` matches", &self.regex, &self.pointer); debug!("Regex `{}` for `{}` matches", &self.regex, &self.pointer);
return Ok(true); return Ok(true);
@ -171,15 +155,3 @@ impl FilterType {
} }
} }
} }
pub fn get_string(data: &serde_json::Value) -> Result<String, WebhookeyError> {
match &data {
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
serde_json::Value::Number(number) => Ok(number.to_string()),
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
x => {
error!("Could not get string from: {:?}", x);
unimplemented!()
}
}
}

View file

@ -1,3 +1,10 @@
mod cli;
mod filters;
use crate::{
cli::Opts,
filters::{FilterType, IpFilter},
};
use anyhow::{anyhow, bail, Result}; use anyhow::{anyhow, bail, Result};
use clap::Parser; use clap::Parser;
use hmac::{Hmac, Mac, NewMac}; use hmac::{Hmac, Mac, NewMac};
@ -15,7 +22,6 @@ use rocket::{
use run_script::ScriptOptions; use run_script::ScriptOptions;
use serde::{Deserialize, Serialize}; use serde::{Deserialize, Serialize};
use sha2::Sha256; use sha2::Sha256;
use std::{ use std::{
collections::BTreeMap, collections::BTreeMap,
fs::File, fs::File,
@ -23,14 +29,23 @@ use std::{
net::{IpAddr, Ipv4Addr, SocketAddr}, net::{IpAddr, Ipv4Addr, SocketAddr},
sync::atomic::{AtomicUsize, Ordering}, sync::atomic::{AtomicUsize, Ordering},
}; };
use thiserror::Error;
mod cli; #[derive(Debug, Error)]
mod webhooks; pub enum WebhookeyError {
#[error("Could not extract signature from header")]
use crate::{ InvalidSignature,
cli::Opts, #[error("Unauthorized request from `{0}`")]
webhooks::{FilterType, IpFilter, WebhookeyError}, Unauthorized(IpAddr),
}; #[error("Unmatched hook from `{0}`")]
UnmatchedHook(IpAddr),
#[error("Could not evaluate filter request")]
InvalidFilter,
#[error("IO Error")]
Io(std::io::Error),
#[error("Serde Error")]
Serde(serde_json::Error),
}
#[derive(Debug, Default)] #[derive(Debug, Default)]
struct WebhookeyMetrics { struct WebhookeyMetrics {
@ -107,14 +122,14 @@ impl Hook {
})?, })?,
)? )?
.to_string(), .to_string(),
Some(pointer) => webhooks::get_string( Some(pointer) => {
data.pointer(pointer).ok_or_else(|| { get_string(data.pointer(pointer).ok_or_else(|| {
anyhow!( anyhow!(
"Could not find field refered to in parameter `{}`", "Could not find field refered to in parameter `{}`",
pointer pointer
) )
})?, })?)?
)?, }
None => bail!("Missing expression in variable `{}`", token), None => bail!("Missing expression in variable `{}`", token),
}; };
@ -251,6 +266,18 @@ fn get_header_field<'a>(headers: &'a HeaderMap, param: &str) -> Result<&'a str>
.ok_or_else(|| anyhow!("Could not extract event parameter from header")) .ok_or_else(|| anyhow!("Could not extract event parameter from header"))
} }
pub fn get_string(data: &serde_json::Value) -> Result<String, WebhookeyError> {
match &data {
serde_json::Value::Bool(bool) => Ok(bool.to_string()),
serde_json::Value::Number(number) => Ok(number.to_string()),
serde_json::Value::String(string) => Ok(string.as_str().to_string()),
x => {
error!("Could not get string from: {:?}", x);
unimplemented!()
}
}
}
fn get_config() -> Result<File> { fn get_config() -> Result<File> {
// Look for config in CWD.. // Look for config in CWD..
if let Ok(config) = File::open("config.yml") { if let Ok(config) = File::open("config.yml") {
@ -487,7 +514,7 @@ async fn main() -> Result<()> {
#[cfg(test)] #[cfg(test)]
mod tests { mod tests {
use super::*; use super::*;
use crate::webhooks::{AddrType, HeaderFilter, JsonFilter}; use filters::{AddrType, HeaderFilter, JsonFilter};
use regex::Regex; use regex::Regex;
use rocket::{ use rocket::{
http::{ContentType, Header}, http::{ContentType, Header},