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

View file

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

View file

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