2021-03-03 15:24:46 +01:00
|
|
|
use anyhow::{anyhow, bail, Result};
|
2021-11-11 21:09:47 +01:00
|
|
|
use clap::Parser;
|
2021-03-28 03:50:52 +02:00
|
|
|
use hmac::{Hmac, Mac, NewMac};
|
|
|
|
use log::{debug, error, info, trace, warn};
|
|
|
|
use rocket::{
|
2021-11-03 13:09:44 +01:00
|
|
|
data::{FromData, ToByteUnit},
|
|
|
|
futures::TryFutureExt,
|
2021-11-07 16:55:11 +01:00
|
|
|
get,
|
2021-03-28 03:50:52 +02:00
|
|
|
http::{HeaderMap, Status},
|
2021-11-03 13:09:44 +01:00
|
|
|
outcome::Outcome::{self, Failure, Success},
|
|
|
|
post, routes,
|
|
|
|
tokio::io::AsyncReadExt,
|
|
|
|
Data, Request, State,
|
2021-03-28 03:50:52 +02:00
|
|
|
};
|
2021-04-16 17:42:40 +02:00
|
|
|
use run_script::ScriptOptions;
|
2021-02-02 11:05:50 +01:00
|
|
|
use serde::{Deserialize, Serialize};
|
2021-03-28 03:50:52 +02:00
|
|
|
use sha2::Sha256;
|
2021-02-02 11:05:50 +01:00
|
|
|
|
2021-03-22 11:12:45 +01:00
|
|
|
use std::{
|
2021-11-09 13:58:57 +01:00
|
|
|
collections::BTreeMap,
|
2021-03-28 03:50:52 +02:00
|
|
|
fs::File,
|
2021-11-03 13:09:44 +01:00
|
|
|
io::BufReader,
|
2021-04-02 00:25:39 +02:00
|
|
|
net::{IpAddr, Ipv4Addr, SocketAddr},
|
2021-11-13 14:35:40 +01:00
|
|
|
sync::atomic::{AtomicUsize, Ordering},
|
2021-03-22 11:12:45 +01:00
|
|
|
};
|
2021-03-03 15:24:46 +01:00
|
|
|
|
2021-11-11 21:09:47 +01:00
|
|
|
mod cli;
|
|
|
|
mod webhooks;
|
2021-06-30 23:55:21 +02:00
|
|
|
|
2021-11-11 21:09:47 +01:00
|
|
|
use crate::{
|
|
|
|
cli::Opts,
|
|
|
|
webhooks::{FilterType, IpFilter, WebhookeyError},
|
|
|
|
};
|
2021-06-30 23:55:21 +02:00
|
|
|
|
2021-11-07 16:55:11 +01:00
|
|
|
#[derive(Debug, Default)]
|
|
|
|
struct WebhookeyMetrics {
|
2021-11-13 14:35:40 +01:00
|
|
|
requests_received: AtomicUsize,
|
|
|
|
requests_invalid: AtomicUsize,
|
|
|
|
hooks_successful: AtomicUsize,
|
|
|
|
hooks_forbidden: AtomicUsize,
|
|
|
|
hooks_unmatched: AtomicUsize,
|
|
|
|
commands_executed: AtomicUsize,
|
|
|
|
commands_execution_failed: AtomicUsize,
|
|
|
|
commands_successful: AtomicUsize,
|
|
|
|
commands_failed: AtomicUsize,
|
2021-11-07 16:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:50:02 +01:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
struct MetricsConfig {
|
|
|
|
enabled: bool,
|
|
|
|
ip_filter: Option<IpFilter>,
|
|
|
|
}
|
|
|
|
|
2021-06-30 23:55:21 +02:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
|
|
|
#[serde(deny_unknown_fields)]
|
|
|
|
struct Config {
|
2021-11-09 14:50:02 +01:00
|
|
|
metrics: Option<MetricsConfig>,
|
2021-11-09 13:58:57 +01:00
|
|
|
hooks: BTreeMap<String, Hook>,
|
2021-06-30 23:55:21 +02:00
|
|
|
}
|
|
|
|
|
2021-02-02 11:05:50 +01:00
|
|
|
#[derive(Debug, Deserialize, Serialize)]
|
2021-04-02 00:25:39 +02:00
|
|
|
#[serde(deny_unknown_fields)]
|
2021-03-03 15:24:46 +01:00
|
|
|
struct Hook {
|
2021-03-28 03:50:52 +02:00
|
|
|
command: String,
|
2021-03-29 04:21:31 +02:00
|
|
|
signature: String,
|
2021-04-02 00:25:39 +02:00
|
|
|
ip_filter: Option<IpFilter>,
|
2021-03-19 10:16:46 +01:00
|
|
|
secrets: Vec<String>,
|
2021-05-31 02:16:22 +02:00
|
|
|
filter: FilterType,
|
2021-03-03 15:24:46 +01:00
|
|
|
}
|
|
|
|
|
2021-06-02 03:35:43 +02:00
|
|
|
impl Hook {
|
|
|
|
fn get_command(
|
|
|
|
&self,
|
|
|
|
hook_name: &str,
|
|
|
|
request: &Request,
|
2021-11-16 14:39:22 +01:00
|
|
|
data: &mut serde_json::Value,
|
2021-06-02 03:35:43 +02:00
|
|
|
) -> Result<String> {
|
2021-11-17 13:17:15 +01:00
|
|
|
debug!("Replacing parameters for command of hook `{}`", hook_name);
|
2021-06-02 03:35:43 +02:00
|
|
|
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(&self.command, request.headers(), data)
|
|
|
|
}
|
2021-11-16 14:39:22 +01:00
|
|
|
|
2021-11-17 13:17:15 +01:00
|
|
|
fn replace_parameters(
|
|
|
|
input: &str,
|
|
|
|
headers: &HeaderMap,
|
|
|
|
data: &serde_json::Value,
|
|
|
|
) -> Result<String> {
|
|
|
|
let mut command = String::new();
|
|
|
|
let command_template = &mut input.chars();
|
|
|
|
|
|
|
|
while let Some(i) = command_template.next() {
|
|
|
|
if i == '{' {
|
|
|
|
if let Some('{') = command_template.next() {
|
|
|
|
let mut token = String::new();
|
|
|
|
|
|
|
|
while let Some(i) = command_template.next() {
|
|
|
|
if i == '}' {
|
|
|
|
if let Some('}') = command_template.next() {
|
|
|
|
let expr = token.trim().split(' ').collect::<Vec<&str>>();
|
|
|
|
|
|
|
|
let replaced = match expr.get(0) {
|
|
|
|
Some(&"header") => get_header_field(
|
|
|
|
headers,
|
|
|
|
expr.get(1).ok_or_else(|| {
|
|
|
|
anyhow!("Missing parameter for `header` expression")
|
|
|
|
})?,
|
|
|
|
)?
|
|
|
|
.to_string(),
|
|
|
|
Some(pointer) => webhooks::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),
|
|
|
|
};
|
|
|
|
|
|
|
|
command.push_str(&replaced);
|
|
|
|
|
|
|
|
trace!("Replace `{}` with: {}", token, replaced);
|
|
|
|
|
|
|
|
break;
|
|
|
|
} else {
|
|
|
|
command.push('}');
|
|
|
|
command.push(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
token.push(i);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command.push('{');
|
|
|
|
command.push(i);
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
command.push(i);
|
2021-11-16 14:39:22 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:17:15 +01:00
|
|
|
Ok(command)
|
2021-11-16 14:39:22 +01:00
|
|
|
}
|
2021-04-03 01:10:50 +02:00
|
|
|
}
|
|
|
|
|
2021-03-28 03:50:52 +02:00
|
|
|
#[derive(Debug)]
|
2021-05-29 00:50:48 +02:00
|
|
|
struct Hooks {
|
2021-11-09 13:58:57 +01:00
|
|
|
inner: BTreeMap<String, String>,
|
2021-05-29 00:50:48 +02:00
|
|
|
}
|
2021-02-02 11:05:50 +01:00
|
|
|
|
2021-06-02 03:35:43 +02:00
|
|
|
impl Hooks {
|
2021-11-03 13:09:44 +01:00
|
|
|
async fn get_commands(request: &Request<'_>, data: Data<'_>) -> Result<Self, WebhookeyError> {
|
2021-06-02 03:35:43 +02:00
|
|
|
let mut buffer = Vec::new();
|
|
|
|
let size = data
|
2021-11-03 13:09:44 +01:00
|
|
|
.open(256_i32.kilobytes())
|
2021-06-02 03:35:43 +02:00
|
|
|
.read_to_end(&mut buffer)
|
2021-11-03 13:09:44 +01:00
|
|
|
.map_err(WebhookeyError::Io)
|
|
|
|
.await?;
|
2021-06-02 03:35:43 +02:00
|
|
|
info!("Data of size {} received", size);
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
let config = request.guard::<&State<Config>>().await.unwrap(); // should never fail
|
2021-06-02 03:35:43 +02:00
|
|
|
let mut valid = false;
|
2021-11-09 13:58:57 +01:00
|
|
|
let mut result = BTreeMap::new();
|
2021-06-02 03:35:43 +02:00
|
|
|
let client_ip = &request
|
|
|
|
.client_ip()
|
|
|
|
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
|
|
|
|
|
|
|
|
let hooks = config.hooks.iter().filter(|(name, hook)| {
|
|
|
|
if let Some(ip) = &hook.ip_filter {
|
2021-06-30 23:52:19 +02:00
|
|
|
accept_ip(name, client_ip, ip)
|
2021-05-29 00:50:48 +02:00
|
|
|
} else {
|
2021-06-02 03:35:43 +02:00
|
|
|
info!(
|
|
|
|
"Allow hook `{}` from {}, no IP filter was configured",
|
|
|
|
&name, &client_ip
|
|
|
|
);
|
|
|
|
true
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
for (hook_name, hook) in hooks {
|
|
|
|
let signature = request
|
|
|
|
.headers()
|
|
|
|
.get_one(&hook.signature)
|
|
|
|
.ok_or(WebhookeyError::InvalidSignature)?;
|
|
|
|
|
|
|
|
let secrets = hook
|
|
|
|
.secrets
|
|
|
|
.iter()
|
2021-06-30 23:52:19 +02:00
|
|
|
.map(|secret| validate_request(secret, signature, &buffer));
|
2021-06-02 03:35:43 +02:00
|
|
|
|
|
|
|
for secret in secrets {
|
|
|
|
match secret {
|
|
|
|
Ok(()) => {
|
|
|
|
trace!("Valid signature found for hook `{}`", hook_name);
|
|
|
|
|
|
|
|
valid = true;
|
|
|
|
|
2021-11-16 14:39:22 +01:00
|
|
|
let mut data: serde_json::Value =
|
2021-06-02 03:35:43 +02:00
|
|
|
serde_json::from_slice(&buffer).map_err(WebhookeyError::Serde)?;
|
|
|
|
|
2021-11-11 21:09:47 +01:00
|
|
|
match hook.filter.evaluate(&data) {
|
2021-11-16 14:39:22 +01:00
|
|
|
Ok(true) => match hook.get_command(hook_name, request, &mut data) {
|
2021-06-02 03:35:43 +02:00
|
|
|
Ok(command) => {
|
|
|
|
info!("Filter for `{}` matched", &hook_name);
|
|
|
|
result.insert(hook_name.to_string(), command);
|
|
|
|
break;
|
|
|
|
}
|
|
|
|
Err(e) => error!("{}", e),
|
|
|
|
},
|
|
|
|
Ok(false) => info!("Filter for `{}` did not match", &hook_name),
|
|
|
|
Err(error) => {
|
|
|
|
error!("Could not match filter for `{}`: {}", &hook_name, error)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
Err(e) => trace!("Hook `{}` could not validate request: {}", &hook_name, e),
|
|
|
|
}
|
2021-04-02 00:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
2021-06-02 03:35:43 +02:00
|
|
|
|
|
|
|
if !valid {
|
|
|
|
return Err(WebhookeyError::Unauthorized(*client_ip));
|
2021-04-02 00:25:39 +02:00
|
|
|
}
|
2021-06-02 03:35:43 +02:00
|
|
|
|
|
|
|
Ok(Hooks { inner: result })
|
2021-04-02 00:25:39 +02:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-06-02 03:35:43 +02:00
|
|
|
fn accept_ip(hook_name: &str, client_ip: &IpAddr, ip: &IpFilter) -> bool {
|
|
|
|
if ip.validate(client_ip) {
|
|
|
|
info!("Allow hook `{}` from {}", &hook_name, &client_ip);
|
|
|
|
return true;
|
|
|
|
}
|
|
|
|
|
|
|
|
warn!("Deny hook `{}` from {}", &hook_name, &client_ip);
|
|
|
|
false
|
|
|
|
}
|
|
|
|
|
2021-03-29 02:19:30 +02:00
|
|
|
fn validate_request(secret: &str, signature: &str, data: &[u8]) -> Result<()> {
|
2021-11-06 12:06:10 +01:00
|
|
|
let mut mac = Hmac::<Sha256>::new_from_slice(secret.as_bytes())
|
2021-03-29 02:19:30 +02:00
|
|
|
.map_err(|e| anyhow!("Could not create hasher with secret: {}", e))?;
|
2021-06-30 23:52:19 +02:00
|
|
|
mac.update(data);
|
2021-03-29 02:19:30 +02:00
|
|
|
let raw_signature = hex::decode(signature.as_bytes())?;
|
|
|
|
mac.verify(&raw_signature).map_err(|e| anyhow!("{}", e))
|
|
|
|
}
|
|
|
|
|
2021-11-17 13:17:15 +01:00
|
|
|
fn get_header_field<'a>(headers: &'a HeaderMap, param: &str) -> Result<&'a str> {
|
2021-05-29 00:50:48 +02:00
|
|
|
headers
|
2021-11-17 13:17:15 +01:00
|
|
|
.get_one(param)
|
2021-05-29 00:50:48 +02:00
|
|
|
.ok_or_else(|| anyhow!("Could not extract event parameter from header"))
|
|
|
|
}
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
fn get_config() -> Result<File> {
|
2021-05-31 02:16:22 +02:00
|
|
|
// Look for config in CWD..
|
|
|
|
if let Ok(config) = File::open("config.yml") {
|
|
|
|
info!("Loading configuration from `./config.yml`");
|
2021-03-03 16:14:54 +01:00
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
|
2021-05-29 00:50:48 +02:00
|
|
|
// ..look for user path config..
|
2021-03-03 15:24:46 +01:00
|
|
|
if let Some(mut path) = dirs::config_dir() {
|
|
|
|
path.push("webhookey/config.yml");
|
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
if let Ok(config) = File::open(&path) {
|
|
|
|
info!(
|
|
|
|
"Loading configuration from `{}`",
|
2021-03-21 15:51:58 +01:00
|
|
|
path.to_str().unwrap_or("<path unprintable>"),
|
2021-03-03 16:14:54 +01:00
|
|
|
);
|
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-05-31 02:16:22 +02:00
|
|
|
// ..look for systemwide config..
|
|
|
|
if let Ok(config) = File::open("/etc/webhookey/config.yml") {
|
|
|
|
info!("Loading configuration from `/etc/webhookey/config.yml`");
|
2021-03-03 16:14:54 +01:00
|
|
|
|
2021-03-03 15:24:46 +01:00
|
|
|
return Ok(config);
|
|
|
|
}
|
|
|
|
|
2021-05-29 00:50:48 +02:00
|
|
|
// ..you had your chance.
|
2021-03-30 01:16:15 +02:00
|
|
|
bail!("No configuration file found.");
|
2021-03-03 15:24:46 +01:00
|
|
|
}
|
|
|
|
|
2021-11-09 14:50:02 +01:00
|
|
|
fn get_metrics(metrics: &WebhookeyMetrics) -> String {
|
|
|
|
format!(
|
|
|
|
r"# HELP webhookey_requests_received Number of requests received
|
|
|
|
# TYPE webhookey_requests_received gauge
|
|
|
|
webhookey_requests_received {}
|
|
|
|
# HELP webhookey_requests_invalid Number of invalid requests received
|
|
|
|
# TYPE webhookey_requests_invalid gauge
|
|
|
|
webhookey_requests_invalid {}
|
|
|
|
# HELP webhookey_hooks_successful Number of successfully executed hooks
|
|
|
|
# TYPE webhookey_hooks_successful gauge
|
|
|
|
webhookey_hooks_sucessful {}
|
|
|
|
# HELP webhookey_hooks_forbidden Number of forbidden requests
|
|
|
|
# TYPE webhookey_hooks_forbidden gauge
|
|
|
|
webhookey_hooks_forbidden {}
|
|
|
|
# HELP webhookey_hooks_unmatched Number of unmatched requests
|
|
|
|
# TYPE webhookey_hooks_unmatched gauge
|
|
|
|
webhookey_hooks_unmatched {}
|
|
|
|
# HELP webhookey_commands_executed Number of commands executed
|
|
|
|
# TYPE webhookey_commands_executed gauge
|
|
|
|
webhookey_commands_executed {}
|
|
|
|
# HELP webhookey_commands_execution_failed Number of commands failed to execute
|
|
|
|
# TYPE webhookey_commands_execution_failed gauge
|
|
|
|
webhookey_commands_execution_failed {}
|
|
|
|
# HELP webhookey_commands_successful Number of executed commands returning return code 0
|
|
|
|
# TYPE webhookey_commands_successful gauge
|
|
|
|
webhookey_commands_successful {}
|
|
|
|
# HELP webhookey_commands_failed Number of executed commands returning different return code than 0
|
|
|
|
# TYPE webhookey_commands_failed gauge
|
|
|
|
webhookey_commands_failed {}
|
|
|
|
",
|
2021-11-13 14:35:40 +01:00
|
|
|
metrics.requests_received.load(Ordering::Relaxed),
|
|
|
|
metrics.requests_invalid.load(Ordering::Relaxed),
|
|
|
|
metrics.hooks_successful.load(Ordering::Relaxed),
|
|
|
|
metrics.hooks_forbidden.load(Ordering::Relaxed),
|
|
|
|
metrics.hooks_unmatched.load(Ordering::Relaxed),
|
|
|
|
metrics.commands_executed.load(Ordering::Relaxed),
|
|
|
|
metrics.commands_execution_failed.load(Ordering::Relaxed),
|
|
|
|
metrics.commands_successful.load(Ordering::Relaxed),
|
|
|
|
metrics.commands_failed.load(Ordering::Relaxed),
|
2021-11-09 14:50:02 +01:00
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:43:10 +01:00
|
|
|
#[rocket::async_trait]
|
|
|
|
impl<'r> FromData<'r> for Hooks {
|
|
|
|
type Error = WebhookeyError;
|
|
|
|
|
|
|
|
async fn from_data(
|
|
|
|
request: &'r Request<'_>,
|
|
|
|
data: Data<'r>,
|
|
|
|
) -> Outcome<Self, (Status, Self::Error), Data<'r>> {
|
2021-11-07 16:55:11 +01:00
|
|
|
{
|
2021-11-13 14:35:40 +01:00
|
|
|
request
|
2021-11-07 16:55:11 +01:00
|
|
|
.guard::<&State<WebhookeyMetrics>>()
|
|
|
|
.await
|
|
|
|
.unwrap() // TODO: Check if unwrap need to be fixed
|
|
|
|
.requests_received
|
2021-11-13 14:35:40 +01:00
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-07 16:43:10 +01:00
|
|
|
match Hooks::get_commands(request, data).await {
|
|
|
|
Ok(hooks) => {
|
|
|
|
if hooks.inner.is_empty() {
|
|
|
|
let client_ip = &request
|
|
|
|
.client_ip()
|
|
|
|
.unwrap_or(IpAddr::V4(Ipv4Addr::UNSPECIFIED));
|
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
request
|
2021-11-07 16:55:11 +01:00
|
|
|
.guard::<&State<WebhookeyMetrics>>()
|
|
|
|
.await
|
|
|
|
.unwrap() // TODO: Check if unwrap need to be fixed
|
|
|
|
.hooks_unmatched
|
2021-11-13 14:35:40 +01:00
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-07 16:43:10 +01:00
|
|
|
warn!("Unmatched hook from {}", &client_ip);
|
|
|
|
return Failure((Status::NotFound, WebhookeyError::UnmatchedHook(*client_ip)));
|
|
|
|
}
|
|
|
|
|
|
|
|
Success(hooks)
|
|
|
|
}
|
|
|
|
Err(WebhookeyError::Unauthorized(e)) => {
|
|
|
|
error!("{}", WebhookeyError::Unauthorized(e));
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
request
|
2021-11-07 16:55:11 +01:00
|
|
|
.guard::<&State<WebhookeyMetrics>>()
|
|
|
|
.await
|
|
|
|
.unwrap() // TODO: Check if unwrap need to be fixed
|
|
|
|
.hooks_forbidden
|
2021-11-13 14:35:40 +01:00
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-07 16:43:10 +01:00
|
|
|
Failure((Status::Unauthorized, WebhookeyError::Unauthorized(e)))
|
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("{}", e);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
request
|
2021-11-07 16:55:11 +01:00
|
|
|
.guard::<&State<WebhookeyMetrics>>()
|
|
|
|
.await
|
|
|
|
.unwrap() // TODO: Check if unwrap need to be fixed
|
|
|
|
.requests_invalid
|
2021-11-13 14:35:40 +01:00
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-07 16:43:10 +01:00
|
|
|
Failure((Status::BadRequest, e))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
#[post("/", format = "json", data = "<hooks>")]
|
2021-11-07 16:55:11 +01:00
|
|
|
async fn receive_hook<'a>(
|
|
|
|
address: SocketAddr,
|
|
|
|
hooks: Hooks,
|
|
|
|
metrics: &State<WebhookeyMetrics>,
|
|
|
|
) -> Status {
|
2021-11-07 16:43:10 +01:00
|
|
|
info!("Post request received from: {}", address);
|
|
|
|
|
|
|
|
hooks.inner.iter().for_each(|(name, command)| {
|
|
|
|
info!("Execute `{}` from hook `{}`", &command, &name);
|
|
|
|
|
|
|
|
match run_script::run(command, &vec![], &ScriptOptions::new()) {
|
|
|
|
Ok((status, stdout, stderr)) => {
|
|
|
|
info!("Command `{}` exited with return code: {}", &command, status);
|
|
|
|
trace!("Output of command `{}` on stdout: {:?}", &command, &stdout);
|
|
|
|
debug!("Output of command `{}` on stderr: {:?}", &command, &stderr);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
metrics.commands_executed.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
let _ = match status {
|
|
|
|
0 => metrics.commands_successful.fetch_add(1, Ordering::Relaxed),
|
|
|
|
_ => metrics.commands_failed.fetch_add(1, Ordering::Relaxed),
|
|
|
|
};
|
2021-11-07 16:43:10 +01:00
|
|
|
}
|
|
|
|
Err(e) => {
|
|
|
|
error!("Execution of `{}` failed: {}", &command, e);
|
2021-11-07 16:55:11 +01:00
|
|
|
|
2021-11-13 14:35:40 +01:00
|
|
|
metrics
|
|
|
|
.commands_execution_failed
|
|
|
|
.fetch_add(1, Ordering::Relaxed);
|
2021-11-07 16:43:10 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
});
|
|
|
|
|
|
|
|
Status::Ok
|
|
|
|
}
|
|
|
|
|
2021-11-07 16:55:11 +01:00
|
|
|
#[get("/metrics")]
|
2021-11-09 14:50:02 +01:00
|
|
|
async fn metrics(
|
|
|
|
address: SocketAddr,
|
|
|
|
metrics: &State<WebhookeyMetrics>,
|
|
|
|
config: &State<Config>,
|
|
|
|
) -> Option<String> {
|
|
|
|
if let Some(metrics_config) = &config.metrics {
|
2021-11-13 19:43:39 +01:00
|
|
|
if metrics_config.enabled {
|
|
|
|
if let Some(filter) = &metrics_config.ip_filter {
|
|
|
|
if filter.validate(&address.ip()) {
|
|
|
|
return Some(get_metrics(metrics));
|
|
|
|
}
|
|
|
|
} else {
|
2021-11-13 14:35:40 +01:00
|
|
|
return Some(get_metrics(metrics));
|
2021-11-09 14:50:02 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
warn!("Forbidden request for metrics: {:?}", address);
|
|
|
|
|
|
|
|
None
|
2021-11-07 16:55:11 +01:00
|
|
|
}
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
#[rocket::main]
|
|
|
|
async fn main() -> Result<()> {
|
2021-03-03 16:14:54 +01:00
|
|
|
env_logger::init();
|
|
|
|
|
2021-06-30 23:55:21 +02:00
|
|
|
let cli: Opts = Opts::parse();
|
2021-05-31 16:14:19 +02:00
|
|
|
|
2021-06-30 23:55:21 +02:00
|
|
|
let config: Config = match cli.config {
|
2021-05-31 16:14:19 +02:00
|
|
|
Some(config) => serde_yaml::from_reader(BufReader::new(File::open(config)?))?,
|
|
|
|
_ => serde_yaml::from_reader(BufReader::new(get_config()?))?,
|
|
|
|
};
|
2021-03-03 15:24:46 +01:00
|
|
|
|
2021-03-03 16:14:54 +01:00
|
|
|
trace!("Parsed configuration:\n{}", serde_yaml::to_string(&config)?);
|
2021-03-03 15:24:46 +01:00
|
|
|
|
2021-06-30 23:55:21 +02:00
|
|
|
if cli.command.is_some() {
|
2021-05-31 16:14:19 +02:00
|
|
|
debug!("Configtest succeded.");
|
|
|
|
println!("Config is OK");
|
2021-06-02 03:35:43 +02:00
|
|
|
return Ok(());
|
2021-05-31 16:14:19 +02:00
|
|
|
}
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
rocket::build()
|
2021-11-07 16:55:11 +01:00
|
|
|
.mount("/", routes![receive_hook, metrics])
|
2021-11-03 13:09:44 +01:00
|
|
|
.manage(config)
|
2021-11-07 16:55:11 +01:00
|
|
|
.manage(WebhookeyMetrics::default())
|
2021-11-03 13:09:44 +01:00
|
|
|
.launch()
|
|
|
|
.await?;
|
2021-03-03 15:24:46 +01:00
|
|
|
|
|
|
|
Ok(())
|
2021-02-02 11:05:50 +01:00
|
|
|
}
|
2021-03-20 00:12:01 +01:00
|
|
|
|
|
|
|
#[cfg(test)]
|
|
|
|
mod tests {
|
|
|
|
use super::*;
|
2021-11-11 21:09:47 +01:00
|
|
|
use crate::webhooks::{AddrType, JsonFilter};
|
2021-03-28 03:50:52 +02:00
|
|
|
use rocket::{
|
|
|
|
http::{ContentType, Header},
|
2021-11-03 13:09:44 +01:00
|
|
|
local::asynchronous::Client,
|
2021-03-28 03:50:52 +02:00
|
|
|
};
|
2021-03-21 15:51:58 +01:00
|
|
|
use serde_json::json;
|
2021-03-20 00:12:01 +01:00
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
#[rocket::async_test]
|
|
|
|
async fn secret() {
|
2021-11-09 13:58:57 +01:00
|
|
|
let mut hooks = BTreeMap::new();
|
2021-05-31 02:16:22 +02:00
|
|
|
|
2021-03-20 00:12:01 +01:00
|
|
|
hooks.insert(
|
|
|
|
"test_hook".to_string(),
|
|
|
|
Hook {
|
2021-03-28 03:50:52 +02:00
|
|
|
command: "".to_string(),
|
2021-03-29 04:21:31 +02:00
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
2021-04-02 00:25:39 +02:00
|
|
|
ip_filter: None,
|
2021-03-20 00:12:01 +01:00
|
|
|
secrets: vec!["valid".to_string()],
|
2021-05-31 02:16:22 +02:00
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "*".to_string(),
|
|
|
|
regex: "*".to_string(),
|
|
|
|
}),
|
2021-03-20 00:12:01 +01:00
|
|
|
},
|
|
|
|
);
|
2021-05-31 02:16:22 +02:00
|
|
|
|
2021-11-09 14:50:02 +01:00
|
|
|
let config = Config {
|
|
|
|
metrics: None,
|
|
|
|
hooks: hooks,
|
|
|
|
};
|
2021-03-20 00:12:01 +01:00
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
let rocket = rocket::build()
|
2021-03-20 00:12:01 +01:00
|
|
|
.mount("/", routes![receive_hook])
|
2021-11-07 16:55:11 +01:00
|
|
|
.manage(config)
|
|
|
|
.manage(WebhookeyMetrics::default());
|
2021-03-20 00:12:01 +01:00
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
let client = Client::tracked(rocket).await.unwrap();
|
2021-03-20 00:12:01 +01:00
|
|
|
let response = client
|
|
|
|
.post("/")
|
2021-03-28 03:50:52 +02:00
|
|
|
.header(Header::new(
|
|
|
|
"X-Gitea-Signature",
|
|
|
|
"28175a0035f637f3cbb85afee9f9d319631580e7621cf790cd16ca063a2f820e",
|
|
|
|
))
|
2021-03-20 00:12:01 +01:00
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
2021-03-28 03:50:52 +02:00
|
|
|
.body(&serde_json::to_string(&json!({ "foo": "bar" })).unwrap())
|
2021-03-20 00:12:01 +01:00
|
|
|
.dispatch();
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
assert_eq!(response.await.status(), Status::NotFound);
|
2021-03-20 00:12:01 +01:00
|
|
|
|
|
|
|
let response = client
|
|
|
|
.post("/")
|
2021-03-28 03:50:52 +02:00
|
|
|
.header(Header::new("X-Gitea-Signature", "beef"))
|
2021-03-20 00:12:01 +01:00
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
2021-03-28 03:50:52 +02:00
|
|
|
.body(&serde_json::to_string(&json!({ "foo": "bar" })).unwrap())
|
2021-03-20 00:12:01 +01:00
|
|
|
.dispatch();
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
assert_eq!(response.await.status(), Status::Unauthorized);
|
2021-03-20 00:12:01 +01:00
|
|
|
|
|
|
|
let response = client
|
|
|
|
.post("/")
|
2021-03-28 03:50:52 +02:00
|
|
|
.header(Header::new(
|
|
|
|
"X-Gitea-Signature",
|
|
|
|
"c5c315d76318362ec129ca629b50b626bba09ad3d7ba4cc0f4c0afe4a90537a0",
|
|
|
|
))
|
2021-03-20 00:12:01 +01:00
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
|
|
|
.body(r#"{ "not_secret": "invalid" "#)
|
|
|
|
.dispatch();
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
assert_eq!(response.await.status(), Status::BadRequest);
|
2021-03-29 02:19:30 +02:00
|
|
|
|
|
|
|
let response = client
|
|
|
|
.post("/")
|
|
|
|
.header(Header::new("X-Gitea-Signature", "foobar"))
|
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
|
|
|
.dispatch();
|
|
|
|
|
2021-11-03 13:09:44 +01:00
|
|
|
assert_eq!(response.await.status(), Status::Unauthorized);
|
2021-03-20 00:12:01 +01:00
|
|
|
}
|
2021-03-21 15:51:58 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_command() {
|
2021-11-05 13:51:31 +01:00
|
|
|
let mut headers = HeaderMap::new();
|
|
|
|
headers.add_raw("X-Gitea-Event", "something");
|
2021-03-28 03:50:52 +02:00
|
|
|
|
2021-03-21 15:51:58 +01:00
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters("command", &headers, &serde_json::Value::Null).unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
"command"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(" command", &headers, &serde_json::Value::Null).unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
" command"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters("command ", &headers, &serde_json::Value::Null).unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
"command "
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(" command ", &headers, &serde_json::Value::Null)
|
|
|
|
.unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
" command "
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters("command command ", &headers, &serde_json::Value::Null)
|
|
|
|
.unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
"command command "
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters("{{ /foo }} command", &headers, &json!({ "foo": "bar" }))
|
|
|
|
.unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
"bar command"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-11-05 13:51:31 +01:00
|
|
|
" command {{ /foo }} ",
|
|
|
|
&headers,
|
|
|
|
&json!({ "foo": "bar" })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
" command bar "
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-03-21 15:51:58 +01:00
|
|
|
"{{ /foo }} command{{/field1/foo}}",
|
2021-11-05 13:51:31 +01:00
|
|
|
&headers,
|
2021-03-21 15:51:58 +01:00
|
|
|
&json!({ "foo": "bar", "field1": { "foo": "baz" } })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
"bar commandbaz"
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-11-05 13:51:31 +01:00
|
|
|
" command {{ /foo }} ",
|
|
|
|
&headers,
|
|
|
|
&json!({ "foo": "bar" })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
2021-03-21 15:51:58 +01:00
|
|
|
" command bar "
|
|
|
|
);
|
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-03-21 15:51:58 +01:00
|
|
|
" {{ /field1/foo }} command",
|
2021-11-05 13:51:31 +01:00
|
|
|
&headers,
|
2021-03-21 15:51:58 +01:00
|
|
|
&json!({ "field1": { "foo": "bar" } })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
" bar command"
|
|
|
|
);
|
2021-03-28 03:50:52 +02:00
|
|
|
|
2021-03-29 04:21:31 +02:00
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-03-30 01:16:15 +02:00
|
|
|
" {{ header X-Gitea-Event }} command",
|
2021-11-05 13:51:31 +01:00
|
|
|
&headers,
|
2021-03-29 04:21:31 +02:00
|
|
|
&json!({ "field1": { "foo": "bar" } })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
" something command"
|
|
|
|
);
|
2021-11-06 11:03:27 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
2021-11-17 13:17:15 +01:00
|
|
|
Hook::replace_parameters(
|
2021-11-06 11:03:27 +01:00
|
|
|
" {{ header X-Gitea-Event }} {{ /field1/foo }} command",
|
|
|
|
&headers,
|
|
|
|
&json!({ "field1": { "foo": "bar" } })
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
" something bar command"
|
|
|
|
);
|
2021-11-17 13:17:15 +01:00
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
Hook::replace_parameters(
|
|
|
|
" {{ header X-Gitea-Event }} {{ /field1/foo }} {{ /field1/bar }} {{ /field2/foo }} --command{{ /cmd }}",
|
|
|
|
&headers,
|
|
|
|
&json!({ "field1": { "foo": "bar", "bar": "baz" }, "field2": { "foo": "qux" }, "cmd": " else"})
|
|
|
|
)
|
|
|
|
.unwrap(),
|
|
|
|
" something bar baz qux --command else"
|
|
|
|
);
|
2021-11-06 11:03:27 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_test]
|
|
|
|
async fn parse_command_request() {
|
2021-11-09 13:58:57 +01:00
|
|
|
let mut hooks = BTreeMap::new();
|
2021-11-06 11:03:27 +01:00
|
|
|
|
|
|
|
hooks.insert(
|
|
|
|
"test_hook".to_string(),
|
|
|
|
Hook {
|
|
|
|
command:
|
|
|
|
"/usr/bin/echo {{ /repository/full_name }} --foo {{ /pull_request/base/ref }}"
|
|
|
|
.to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: None,
|
|
|
|
secrets: vec!["valid".to_string()],
|
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/foo".to_string(),
|
|
|
|
regex: "bar".to_string(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
hooks.insert(
|
|
|
|
"test_hook".to_string(),
|
|
|
|
Hook {
|
|
|
|
command: "/usr/bin/echo {{ /repository/full_name }} {{ /pull_request/base/ref }}"
|
|
|
|
.to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: None,
|
|
|
|
secrets: vec!["valid".to_string()],
|
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/foo".to_string(),
|
|
|
|
regex: "bar".to_string(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = Config {
|
2021-11-09 14:50:02 +01:00
|
|
|
metrics: None,
|
2021-11-06 11:03:27 +01:00
|
|
|
hooks: hooks,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rocket = rocket::build()
|
|
|
|
.mount("/", routes![receive_hook])
|
2021-11-07 16:55:11 +01:00
|
|
|
.manage(config)
|
|
|
|
.manage(WebhookeyMetrics::default());
|
2021-11-06 11:03:27 +01:00
|
|
|
|
|
|
|
let client = Client::tracked(rocket).await.unwrap();
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
.post("/")
|
|
|
|
.header(Header::new(
|
|
|
|
"X-Gitea-Signature",
|
|
|
|
"693b733871ecb684651a813c82936df683c9e4a816581f385353e06170545400",
|
|
|
|
))
|
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
|
|
|
.body(
|
|
|
|
&serde_json::to_string(&json!({
|
|
|
|
"foo": "bar",
|
|
|
|
"repository": {
|
|
|
|
"full_name": "keith"
|
|
|
|
},
|
|
|
|
"pull_request": {
|
|
|
|
"base": {
|
|
|
|
"ref": "main"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.await.status(), Status::Ok);
|
|
|
|
}
|
|
|
|
|
|
|
|
#[rocket::async_test]
|
|
|
|
async fn parse_invalid_command_request() {
|
2021-11-09 13:58:57 +01:00
|
|
|
let mut hooks = BTreeMap::new();
|
2021-11-06 11:03:27 +01:00
|
|
|
|
|
|
|
hooks.insert(
|
|
|
|
"test_hook".to_string(),
|
|
|
|
Hook {
|
|
|
|
command: "/usr/bin/echo {{ /repository/full }} {{ /pull_request/base/ref }}"
|
|
|
|
.to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: None,
|
|
|
|
secrets: vec!["valid".to_string()],
|
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/foo".to_string(),
|
|
|
|
regex: "bar".to_string(),
|
|
|
|
}),
|
|
|
|
},
|
|
|
|
);
|
|
|
|
|
|
|
|
let config = Config {
|
2021-11-09 14:50:02 +01:00
|
|
|
metrics: None,
|
2021-11-06 11:03:27 +01:00
|
|
|
hooks: hooks,
|
|
|
|
};
|
|
|
|
|
|
|
|
let rocket = rocket::build()
|
|
|
|
.mount("/", routes![receive_hook])
|
2021-11-07 16:55:11 +01:00
|
|
|
.manage(config)
|
|
|
|
.manage(WebhookeyMetrics::default());
|
2021-11-06 11:03:27 +01:00
|
|
|
|
|
|
|
let client = Client::tracked(rocket).await.unwrap();
|
|
|
|
|
|
|
|
let response = client
|
|
|
|
.post("/")
|
|
|
|
.header(Header::new(
|
|
|
|
"X-Gitea-Signature",
|
|
|
|
"693b733871ecb684651a813c82936df683c9e4a816581f385353e06170545400",
|
|
|
|
))
|
|
|
|
.header(ContentType::JSON)
|
|
|
|
.remote("127.0.0.1:8000".parse().unwrap())
|
|
|
|
.body(
|
|
|
|
&serde_json::to_string(&json!({
|
|
|
|
"foo": "bar",
|
|
|
|
"repository": {
|
|
|
|
"full_name": "keith"
|
|
|
|
},
|
|
|
|
"pull_request": {
|
|
|
|
"base": {
|
|
|
|
"ref": "main"
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}))
|
|
|
|
.unwrap(),
|
|
|
|
)
|
|
|
|
.dispatch();
|
|
|
|
|
|
|
|
assert_eq!(response.await.status(), Status::NotFound);
|
2021-03-21 15:51:58 +01:00
|
|
|
}
|
2021-11-09 14:50:02 +01:00
|
|
|
|
|
|
|
#[test]
|
|
|
|
fn parse_config() {
|
|
|
|
let config: Config = serde_yaml::from_str(
|
|
|
|
r#"---
|
|
|
|
hooks:
|
|
|
|
hook1:
|
|
|
|
command: /usr/bin/local/script_xy.sh {{ /field2/foo }} asdfasdf
|
|
|
|
signature: X-Gitea-Signature
|
|
|
|
ip_filter:
|
|
|
|
allow:
|
|
|
|
- 127.0.0.1/31
|
|
|
|
secrets:
|
|
|
|
- secret_key_01
|
|
|
|
- secret_key_02
|
|
|
|
filter:
|
|
|
|
json:
|
|
|
|
pointer: /ref
|
|
|
|
regex: refs/heads/master
|
|
|
|
hook2:
|
|
|
|
command: /usr/bin/local/script_xy.sh asdfasdf
|
|
|
|
signature: X-Gitea-Signature
|
|
|
|
secrets:
|
|
|
|
- secret_key_01
|
|
|
|
- secret_key_02
|
|
|
|
filter:
|
|
|
|
and:
|
|
|
|
- json:
|
|
|
|
pointer: /ref
|
|
|
|
regex: refs/heads/master
|
|
|
|
- json:
|
|
|
|
pointer: /after
|
|
|
|
regex: f6e5fe4fe37df76629112d55cc210718b6a55e7e"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
serde_yaml::to_string(&config).unwrap(),
|
|
|
|
serde_yaml::to_string(&Config {
|
|
|
|
metrics: None,
|
|
|
|
hooks: BTreeMap::from([
|
|
|
|
(
|
|
|
|
"hook1".to_string(),
|
|
|
|
Hook {
|
|
|
|
command: "/usr/bin/local/script_xy.sh {{ /field2/foo }} asdfasdf"
|
|
|
|
.to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: Some(IpFilter::Allow(vec![AddrType::IpNet(
|
|
|
|
"127.0.0.1/31".parse().unwrap()
|
|
|
|
)])),
|
|
|
|
secrets: vec!["secret_key_01".to_string(), "secret_key_02".to_string()],
|
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/ref".to_string(),
|
|
|
|
regex: "refs/heads/master".to_string(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
),
|
|
|
|
(
|
|
|
|
"hook2".to_string(),
|
|
|
|
Hook {
|
|
|
|
command: "/usr/bin/local/script_xy.sh asdfasdf".to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: None,
|
|
|
|
secrets: vec!["secret_key_01".to_string(), "secret_key_02".to_string()],
|
|
|
|
filter: FilterType::And(vec![
|
|
|
|
FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/ref".to_string(),
|
|
|
|
regex: "refs/heads/master".to_string(),
|
|
|
|
}),
|
|
|
|
FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/after".to_string(),
|
|
|
|
regex: "f6e5fe4fe37df76629112d55cc210718b6a55e7e".to_string(),
|
|
|
|
}),
|
|
|
|
]),
|
|
|
|
}
|
|
|
|
)
|
|
|
|
])
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
|
|
|
|
let config: Config = serde_yaml::from_str(
|
|
|
|
r#"---
|
|
|
|
metrics:
|
|
|
|
enabled: true
|
|
|
|
hooks:
|
|
|
|
hook1:
|
|
|
|
command: /usr/bin/local/script_xy.sh {{ /field2/foo }} asdfasdf
|
|
|
|
signature: X-Gitea-Signature
|
|
|
|
ip_filter:
|
|
|
|
allow:
|
|
|
|
- 127.0.0.1/31
|
|
|
|
secrets:
|
|
|
|
- secret_key_01
|
|
|
|
- secret_key_02
|
|
|
|
filter:
|
|
|
|
json:
|
|
|
|
pointer: /ref
|
|
|
|
regex: refs/heads/master"#,
|
|
|
|
)
|
|
|
|
.unwrap();
|
|
|
|
|
|
|
|
assert_eq!(
|
|
|
|
serde_yaml::to_string(&config).unwrap(),
|
|
|
|
serde_yaml::to_string(&Config {
|
|
|
|
metrics: Some(MetricsConfig {
|
|
|
|
enabled: true,
|
|
|
|
ip_filter: None
|
|
|
|
}),
|
|
|
|
hooks: BTreeMap::from([(
|
|
|
|
"hook1".to_string(),
|
|
|
|
Hook {
|
|
|
|
command: "/usr/bin/local/script_xy.sh {{ /field2/foo }} asdfasdf"
|
|
|
|
.to_string(),
|
|
|
|
signature: "X-Gitea-Signature".to_string(),
|
|
|
|
ip_filter: Some(IpFilter::Allow(vec![AddrType::IpNet(
|
|
|
|
"127.0.0.1/31".parse().unwrap()
|
|
|
|
)])),
|
|
|
|
secrets: vec!["secret_key_01".to_string(), "secret_key_02".to_string()],
|
|
|
|
filter: FilterType::JsonFilter(JsonFilter {
|
|
|
|
pointer: "/ref".to_string(),
|
|
|
|
regex: "refs/heads/master".to_string(),
|
|
|
|
}),
|
|
|
|
}
|
|
|
|
),])
|
|
|
|
})
|
|
|
|
.unwrap()
|
|
|
|
);
|
|
|
|
}
|
2021-03-20 00:12:01 +01:00
|
|
|
}
|