Fix json parsing bug
Increase version for building a new Debian package.
This commit is contained in:
parent
3a482a3eb9
commit
4b9186b10d
3 changed files with 42 additions and 17 deletions
|
@ -1,6 +1,6 @@
|
||||||
[package]
|
[package]
|
||||||
name = "webhookey"
|
name = "webhookey"
|
||||||
version = "0.1.3"
|
version = "0.1.4"
|
||||||
authors = ["finga <webhookey@onders.org>"]
|
authors = ["finga <webhookey@onders.org>"]
|
||||||
edition = "2021"
|
edition = "2021"
|
||||||
license = "GPL-3.0-or-later"
|
license = "GPL-3.0-or-later"
|
||||||
|
|
29
src/main.rs
29
src/main.rs
|
@ -82,12 +82,35 @@ impl Hook {
|
||||||
&self,
|
&self,
|
||||||
hook_name: &str,
|
hook_name: &str,
|
||||||
request: &Request,
|
request: &Request,
|
||||||
data: &serde_json::Value,
|
data: &mut serde_json::Value,
|
||||||
) -> Result<String> {
|
) -> Result<String> {
|
||||||
trace!("Replacing parameters for command of hook `{}`", hook_name);
|
trace!("Replacing parameters for command of hook `{}`", hook_name);
|
||||||
|
|
||||||
|
for parameter in self.get_parameter()? {
|
||||||
|
let parameter = parameter.trim();
|
||||||
|
|
||||||
|
if let Some(json_value) = data.pointer(parameter) {
|
||||||
|
*data.pointer_mut(parameter).ok_or_else(|| {
|
||||||
|
WebhookeyError::InvalidParameterPointer(parameter.to_string())
|
||||||
|
})? = serde_json::Value::String(webhooks::get_string(json_value)?);
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
replace_parameters(&self.command, request.headers(), data)
|
replace_parameters(&self.command, request.headers(), data)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
fn get_parameter(&self) -> Result<Vec<&str>> {
|
||||||
|
let parse: IResult<&str, Vec<&str>> = many0(alt((
|
||||||
|
delimited(tag("{{"), take_until("}}"), tag("}}")),
|
||||||
|
take_until("{{"),
|
||||||
|
)))(&self.command);
|
||||||
|
|
||||||
|
let (_last, result) = parse
|
||||||
|
.finish()
|
||||||
|
.map_err(|e| anyhow!("Could not get parameters from command: {}", e))?;
|
||||||
|
|
||||||
|
Ok(result)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug)]
|
#[derive(Debug)]
|
||||||
|
@ -142,11 +165,11 @@ impl Hooks {
|
||||||
|
|
||||||
valid = true;
|
valid = true;
|
||||||
|
|
||||||
let data: serde_json::Value =
|
let mut data: serde_json::Value =
|
||||||
serde_json::from_slice(&buffer).map_err(WebhookeyError::Serde)?;
|
serde_json::from_slice(&buffer).map_err(WebhookeyError::Serde)?;
|
||||||
|
|
||||||
match hook.filter.evaluate(&data) {
|
match hook.filter.evaluate(&data) {
|
||||||
Ok(true) => match hook.get_command(hook_name, request, &data) {
|
Ok(true) => match hook.get_command(hook_name, request, &mut data) {
|
||||||
Ok(command) => {
|
Ok(command) => {
|
||||||
info!("Filter for `{}` matched", &hook_name);
|
info!("Filter for `{}` matched", &hook_name);
|
||||||
result.insert(hook_name.to_string(), command);
|
result.insert(hook_name.to_string(), command);
|
||||||
|
|
|
@ -14,6 +14,8 @@ pub enum WebhookeyError {
|
||||||
Unauthorized(IpAddr),
|
Unauthorized(IpAddr),
|
||||||
#[error("Unmatched hook from `{0}`")]
|
#[error("Unmatched hook from `{0}`")]
|
||||||
UnmatchedHook(IpAddr),
|
UnmatchedHook(IpAddr),
|
||||||
|
#[error("Could not find field refered to in parameter `{0}`")]
|
||||||
|
InvalidParameterPointer(String),
|
||||||
#[error("Could not evaluate filter request")]
|
#[error("Could not evaluate filter request")]
|
||||||
InvalidFilter,
|
InvalidFilter,
|
||||||
#[error("IO Error")]
|
#[error("IO Error")]
|
||||||
|
@ -74,7 +76,7 @@ impl JsonFilter {
|
||||||
let regex = Regex::new(&self.regex).map_err(WebhookeyError::Regex)?;
|
let regex = Regex::new(&self.regex).map_err(WebhookeyError::Regex)?;
|
||||||
|
|
||||||
if let Some(value) = data.pointer(&self.pointer) {
|
if let Some(value) = data.pointer(&self.pointer) {
|
||||||
if regex.is_match(&self.get_string(value)?) {
|
if regex.is_match(&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);
|
||||||
|
@ -88,18 +90,6 @@ impl JsonFilter {
|
||||||
|
|
||||||
Ok(false)
|
Ok(false)
|
||||||
}
|
}
|
||||||
|
|
||||||
fn get_string(&self, 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!()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
#[derive(Debug, Deserialize, Serialize)]
|
#[derive(Debug, Deserialize, Serialize)]
|
||||||
|
@ -161,3 +151,15 @@ 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!()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
Loading…
Reference in a new issue