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 ipnet::IpNet;
use log::{debug, error, trace};
@ -5,23 +6,6 @@ use regex::Regex;
use rocket::{http::HeaderMap, Request};
use serde::{Deserialize, Serialize};
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)]
#[serde(deny_unknown_fields, untagged)]
@ -104,7 +88,7 @@ impl JsonFilter {
);
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);
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 clap::Parser;
use hmac::{Hmac, Mac, NewMac};
@ -15,7 +22,6 @@ use rocket::{
use run_script::ScriptOptions;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use std::{
collections::BTreeMap,
fs::File,
@ -23,14 +29,23 @@ use std::{
net::{IpAddr, Ipv4Addr, SocketAddr},
sync::atomic::{AtomicUsize, Ordering},
};
use thiserror::Error;
mod cli;
mod webhooks;
use crate::{
cli::Opts,
webhooks::{FilterType, IpFilter, WebhookeyError},
};
#[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, Default)]
struct WebhookeyMetrics {
@ -107,14 +122,14 @@ impl Hook {
})?,
)?
.to_string(),
Some(pointer) => webhooks::get_string(
data.pointer(pointer).ok_or_else(|| {
Some(pointer) => {
get_string(data.pointer(pointer).ok_or_else(|| {
anyhow!(
"Could not find field refered to in parameter `{}`",
pointer
)
})?,
)?,
})?)?
}
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"))
}
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> {
// Look for config in CWD..
if let Ok(config) = File::open("config.yml") {
@ -487,7 +514,7 @@ async fn main() -> Result<()> {
#[cfg(test)]
mod tests {
use super::*;
use crate::webhooks::{AddrType, HeaderFilter, JsonFilter};
use filters::{AddrType, HeaderFilter, JsonFilter};
use regex::Regex;
use rocket::{
http::{ContentType, Header},