Fix execution of scripts

For better script and argument support `run_script` is used instead of
`process::Command`.
This commit is contained in:
finga 2021-04-16 17:42:40 +02:00
parent 6af7d29833
commit 17778cf8b4
3 changed files with 42 additions and 34 deletions

30
Cargo.lock generated
View file

@ -331,6 +331,16 @@ dependencies = [
"termcolor",
]
[[package]]
name = "fsio"
version = "0.2.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "a50045aa8931ae01afbc5d72439e8f57f326becb8c70d07dfc816778eff3d167"
dependencies = [
"rand",
"users",
]
[[package]]
name = "funty"
version = "1.1.0"
@ -844,6 +854,15 @@ dependencies = [
"unicode-xid 0.1.0",
]
[[package]]
name = "run_script"
version = "0.7.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "e1c022176d696304ff6655716c2c09e1b888c00ac66a7c53c037ae7c9511ce6c"
dependencies = [
"fsio",
]
[[package]]
name = "rust-argon2"
version = "0.8.3"
@ -1147,6 +1166,16 @@ dependencies = [
"percent-encoding 1.0.1",
]
[[package]]
name = "users"
version = "0.11.0"
source = "registry+https://github.com/rust-lang/crates.io-index"
checksum = "24cc0f6d6f267b73e5a2cadf007ba8f9bc39c6a6f9666f8cf25ea809a153b032"
dependencies = [
"libc",
"log 0.4.14",
]
[[package]]
name = "version_check"
version = "0.1.5"
@ -1185,6 +1214,7 @@ dependencies = [
"nom",
"regex",
"rocket",
"run_script",
"serde",
"serde_json",
"serde_yaml",

View file

@ -26,3 +26,4 @@ sha2 = "0.9"
hex = "0.4"
ipnet = { version = "2.3", features = ["serde"] }
thiserror = "1.0"
run_script = "0.7"

View file

@ -22,6 +22,7 @@ use rocket::{
Outcome::{Failure, Success},
Request, Response, State,
};
use run_script::ScriptOptions;
use serde::{Deserialize, Serialize};
use sha2::Sha256;
use thiserror::Error;
@ -31,8 +32,6 @@ use std::{
fs::File,
io::{BufReader, Read},
net::{IpAddr, Ipv4Addr, SocketAddr},
process::Command,
str::from_utf8,
};
#[derive(Debug, Deserialize, Serialize)]
@ -225,28 +224,18 @@ fn filter_match(
let regex = Regex::new(&filter.regex)?;
let parameters = hook.command.clone();
let parameters = get_parameter(&parameters)?;
for parameter in parameters {
for parameter in get_parameter(&hook.command)? {
let parameter = parameter.trim();
trace!("Replacing parameter `{}`", parameter);
trace!("Pointer parameter `{:?}`", data.pointer(parameter));
if let Some(json_value) = data.pointer(parameter) {
*data.pointer_mut(parameter).unwrap() = match json_value {
serde_json::Value::String(string) => {
serde_json::Value::String(string.to_string())
}
serde_json::Value::Number(number) => {
serde_json::Value::String(number.to_string())
}
serde_json::Value::String(string) => serde_json::Value::String(string.to_string()),
serde_json::Value::Number(number) => serde_json::Value::String(number.to_string()),
x => {
error!("Could not get string from: {:?}", x);
unimplemented!()
}
}
} else {
trace!("Ignoring parameter `{}`", parameter);
}
}
@ -375,26 +364,14 @@ fn receive_hook<'a>(address: SocketAddr, hooks: Hooks) -> Result<Response<'a>> {
for hook in hooks.0 {
info!("Execute `{}` from hook `{}`", &hook.1, &hook.0);
let command = hook.1.split(' ').collect::<Vec<&str>>();
match Command::new(&command[0]).args(&command[1..]).output() {
Ok(executed) => {
info!(
"Command `{}` exited with return code: {}",
&command[0], &executed.status
);
trace!(
"Output of command `{}` on stdout: {:?}",
&command[0],
from_utf8(&executed.stdout)?
);
debug!(
"Output of command `{}` on stderr: {:?}",
&command[0],
from_utf8(&executed.stderr)?
);
match run_script::run(&hook.1, &vec![], &ScriptOptions::new()) {
Ok((status, stdout, stderr)) => {
info!("Command `{}` exited with return code: {}", &hook.1, status);
trace!("Output of command `{}` on stdout: {:?}", &hook.1, &stdout);
debug!("Output of command `{}` on stderr: {:?}", &hook.1, &stderr);
}
Err(e) => {
error!("Execution of `{}` failed: {}", command[0], e);
error!("Execution of `{}` failed: {}", &hook.1, e);
}
}
}