use std::sync::Mutex;
use std::time::Duration;
use futures::future::BoxFuture;
use lettre::message::{Mailbox, header::ContentType};
use lettre::transport::smtp::authentication::Credentials;
use lettre::{AsyncSmtpTransport, AsyncTransport, Message, Tokio1Executor};
use serde_json::json;
use crate::Alert;
use crate::config::{SmtpConfig, WebhookConfig, resolve_secret};
pub trait Sink: Send + Sync {
fn name(&self) -> &'static str;
fn send<'a>(&'a self, product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>>;
}
pub struct LogSink;
impl Sink for LogSink {
fn name(&self) -> &'static str {
"log"
}
fn send<'a>(&'a self, product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move {
tracing::warn!(key = %alert.key, status = ?alert.status, "{}", alert.subject(product));
Ok(())
})
}
}
#[derive(Default)]
pub struct MemorySink {
sent: Mutex<Vec<Alert>>,
}
impl MemorySink {
pub fn sent(&self) -> Vec<Alert> {
self.sent.lock().unwrap_or_else(|e| e.into_inner()).clone()
}
}
impl Sink for MemorySink {
fn name(&self) -> &'static str {
"memory"
}
fn send<'a>(&'a self, _product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>> {
self.sent.lock().unwrap_or_else(|e| e.into_inner()).push(alert.clone());
Box::pin(async { Ok(()) })
}
}
#[cfg(feature = "push")]
pub struct PushSink {
cards: Vec<weirdnet_push::WakeCard>,
dry: Option<Mutex<Vec<String>>>,
}
#[cfg(feature = "push")]
impl PushSink {
pub fn new(cards: Vec<weirdnet_push::WakeCard>, dry_run: bool) -> Result<Self, String> {
if cards.is_empty() {
return Err("push: no wake cards".into());
}
for c in &cards {
c.public_key().map_err(|e| format!("push card {}: {e}", c.handle.digest()))?;
}
Ok(Self { cards, dry: dry_run.then(|| Mutex::new(Vec::new())) })
}
pub fn dry_sent(&self) -> Vec<String> {
self.dry.as_ref().map(|d| d.lock().unwrap_or_else(|e| e.into_inner()).clone()).unwrap_or_default()
}
}
#[cfg(feature = "push")]
impl Sink for PushSink {
fn name(&self) -> &'static str {
"push"
}
fn send<'a>(&'a self, product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move {
let mut failed = Vec::new();
for card in &self.cards {
let who = card.handle.digest();
if let Some(dry) = &self.dry {
tracing::info!(device = %who, "dry run: would wake for: {}", alert.subject(product));
dry.lock().unwrap_or_else(|e| e.into_inner()).push(who);
continue;
}
let client = weirdnet_push::PushClient::new(card.service.clone());
let r = tokio::time::timeout(Duration::from_secs(15), client.wake_card(card, weirdnet_push::Reason::MessageWaiting)).await;
match r {
Ok(Ok(())) => {}
Ok(Err(e)) => failed.push(format!("{who}: {e}")),
Err(_) => failed.push(format!("{who}: timed out")),
}
}
if failed.is_empty() { Ok(()) } else { Err(format!("push: {}", failed.join("; "))) }
})
}
}
pub struct SmtpSink {
cfg: SmtpConfig,
from: Mailbox,
to: Vec<Mailbox>,
transport: Option<AsyncSmtpTransport<Tokio1Executor>>,
dry: Option<Mutex<Vec<String>>>,
}
impl SmtpSink {
pub fn new(cfg: &SmtpConfig, dry_run: bool) -> Result<Self, String> {
let from: Mailbox = cfg.from.parse().map_err(|e| format!("smtp from `{}`: {e}", cfg.from))?;
let to = cfg.to.iter().map(|t| t.parse::<Mailbox>().map_err(|e| format!("smtp to `{t}`: {e}"))).collect::<Result<Vec<_>, _>>()?;
if to.is_empty() {
return Err("smtp: `to` is empty".into());
}
let transport = if dry_run {
None
} else {
let builder = match cfg.security.as_str() {
"starttls" => AsyncSmtpTransport::<Tokio1Executor>::starttls_relay(&cfg.server).map_err(|e| format!("smtp {}: {e}", cfg.server))?,
"tls" => AsyncSmtpTransport::<Tokio1Executor>::relay(&cfg.server).map_err(|e| format!("smtp {}: {e}", cfg.server))?,
"none" if matches!(cfg.server.as_str(), "127.0.0.1" | "localhost" | "::1") => {
AsyncSmtpTransport::<Tokio1Executor>::builder_dangerous(&cfg.server)
}
"none" => return Err("smtp: security = \"none\" is only allowed for a loopback relay".into()),
other => return Err(format!("smtp: unknown security `{other}` (starttls, tls or none)")),
};
let mut builder = builder.port(cfg.port).timeout(Some(Duration::from_secs(20)));
if let Some(user) = &cfg.user {
let source = cfg.password.as_deref().ok_or("smtp: `user` is set but `password` (env:NAME or file:/path) is not")?;
let password = resolve_secret(source).map_err(|e| format!("smtp password: {e}"))?;
builder = builder.credentials(Credentials::new(user.clone(), password));
}
Some(builder.build())
};
Ok(Self { cfg: cfg.clone(), from, to, transport, dry: dry_run.then(|| Mutex::new(Vec::new())) })
}
pub fn message(&self, product: &str, alert: &Alert) -> Result<Message, String> {
let mut b = Message::builder().from(self.from.clone()).subject(alert.subject(product)).header(ContentType::TEXT_PLAIN);
for t in &self.to {
b = b.to(t.clone());
}
b.body(alert.body()).map_err(|e| format!("building the email: {e}"))
}
pub fn dry_sent(&self) -> Vec<String> {
self.dry.as_ref().map(|d| d.lock().unwrap_or_else(|e| e.into_inner()).clone()).unwrap_or_default()
}
}
impl Sink for SmtpSink {
fn name(&self) -> &'static str {
"smtp"
}
fn send<'a>(&'a self, product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move {
let msg = self.message(product, alert)?;
if let Some(dry) = &self.dry {
let text = String::from_utf8_lossy(&msg.formatted()).into_owned();
tracing::info!(server = %self.cfg.server, to = ?self.cfg.to, "dry run: would email: {}", alert.subject(product));
dry.lock().unwrap_or_else(|e| e.into_inner()).push(text);
return Ok(());
}
let t = self.transport.as_ref().ok_or("smtp transport missing")?;
t.send(msg).await.map(|_| ()).map_err(|e| format!("smtp {}: {e}", self.cfg.server))
})
}
}
type HttpClient = hyper_util::client::legacy::Client<
hyper_rustls::HttpsConnector<hyper_util::client::legacy::connect::HttpConnector>,
http_body_util::Full<hyper::body::Bytes>,
>;
pub struct WebhookSink {
url: String,
token: Option<String>,
http: HttpClient,
dry: Option<Mutex<Vec<serde_json::Value>>>,
}
impl WebhookSink {
pub fn new(cfg: &WebhookConfig, dry_run: bool) -> Result<Self, String> {
let loopback = ["http://127.0.0.1", "http://localhost", "http://[::1]"].iter().any(|p| cfg.url.starts_with(p));
if !cfg.url.starts_with("https://") && !loopback {
return Err(format!("webhook {}: use https (plain http only to loopback)", cfg.url));
}
let token = match (&cfg.token, dry_run) {
(Some(src), false) => Some(resolve_secret(src).map_err(|e| format!("webhook token: {e}"))?),
_ => None,
};
let https = hyper_rustls::HttpsConnectorBuilder::new()
.with_provider_and_webpki_roots(rustls::crypto::ring::default_provider())
.map_err(|e| format!("webhook tls: {e}"))?
.https_or_http()
.enable_http1()
.build();
let http = hyper_util::client::legacy::Client::builder(hyper_util::rt::TokioExecutor::new()).build(https);
Ok(Self { url: cfg.url.clone(), token, http, dry: dry_run.then(|| Mutex::new(Vec::new())) })
}
pub fn payload(product: &str, alert: &Alert) -> serde_json::Value {
json!({
"source": product,
"key": alert.key,
"status": alert.status,
"host": alert.host,
"title": alert.title,
"subject": alert.subject(product),
"at": alert.at,
"observation": alert.observation,
})
}
pub fn dry_sent(&self) -> Vec<serde_json::Value> {
self.dry.as_ref().map(|d| d.lock().unwrap_or_else(|e| e.into_inner()).clone()).unwrap_or_default()
}
}
impl Sink for WebhookSink {
fn name(&self) -> &'static str {
"webhook"
}
fn send<'a>(&'a self, product: &'a str, alert: &'a Alert) -> BoxFuture<'a, Result<(), String>> {
Box::pin(async move {
let body = Self::payload(product, alert);
if let Some(dry) = &self.dry {
tracing::info!(url = %self.url, "dry run: would post: {}", alert.subject(product));
dry.lock().unwrap_or_else(|e| e.into_inner()).push(body);
return Ok(());
}
let bytes = serde_json::to_vec(&body).map_err(|e| format!("webhook body: {e}"))?;
let mut req = hyper::Request::post(&self.url).header(hyper::header::CONTENT_TYPE, "application/json");
if let Some(t) = &self.token {
req = req.header(hyper::header::AUTHORIZATION, format!("Bearer {t}"));
}
let req = req.body(http_body_util::Full::new(hyper::body::Bytes::from(bytes))).map_err(|e| format!("webhook {}: {e}", self.url))?;
let resp = tokio::time::timeout(Duration::from_secs(15), self.http.request(req))
.await
.map_err(|_| format!("webhook {}: timed out", self.url))?
.map_err(|e| format!("webhook {}: {e}", self.url))?;
if resp.status().is_success() { Ok(()) } else { Err(format!("webhook {}: {}", self.url, resp.status())) }
})
}
}
#[cfg(test)]
mod tests {
use std::time::Instant;
use serde_json::json;
use tokio::io::{AsyncReadExt, AsyncWriteExt};
use super::*;
use crate::{Alerter, Observation, rules};
fn alert() -> Alert {
let a = Alerter::new("node-a", rules::default_rules(&rules::Thresholds::default()), Vec::new());
a.evaluate(&Observation::new("net.route", json!({"state": "degraded", "dev": "wlan0"})), Instant::now()).remove(0)
}
#[tokio::test]
async fn smtp_dry_run_builds_the_email() {
let cfg = SmtpConfig {
server: "smtp.example.com".into(),
port: 587,
security: "starttls".into(),
user: Some("alerts@example.com".into()),
password: Some("env:WEIRDNET_TEST_NEVER_SET".into()),
from: "Alerts <alerts@example.com>".into(),
to: vec!["owner@example.com".into()],
};
assert!(SmtpSink::new(&cfg, false).is_err(), "a real sink needs the password");
let s = SmtpSink::new(&cfg, true).unwrap();
s.send("node", &alert()).await.unwrap();
let sent = s.dry_sent();
assert_eq!(sent.len(), 1);
let unfolded = sent[0].replace("\r\n ", " ");
assert!(unfolded.contains("Subject: [node node-a] ALERT: DEGRADED but up: node-a default route uses Wi-Fi (wlan0)"), "{unfolded}");
assert!(sent[0].contains("To: owner@example.com"));
let insecure = SmtpConfig { security: "none".into(), ..cfg };
assert!(SmtpSink::new(&insecure, false).is_err(), "plain SMTP only to loopback");
}
#[tokio::test]
async fn webhook_posts_json_to_a_mock() {
assert!(WebhookSink::new(&WebhookConfig { url: "http://hooks.example.com/x".into(), token: None }, false).is_err());
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let server = tokio::spawn(async move {
let (mut s, _) = listener.accept().await.unwrap();
let mut buf = vec![0u8; 16384];
let mut got = Vec::new();
loop {
let n = s.read(&mut buf).await.unwrap();
got.extend_from_slice(&buf[..n]);
let text = String::from_utf8_lossy(&got).to_string();
if let Some(i) = text.find("\r\n\r\n") {
let len: usize =
text.lines().find_map(|l| l.to_lowercase().strip_prefix("content-length:").map(|v| v.trim().parse().unwrap())).unwrap_or(0);
if got.len() >= i + 4 + len {
break;
}
}
}
s.write_all(b"HTTP/1.1 204 No Content\r\ncontent-length: 0\r\n\r\n").await.unwrap();
String::from_utf8_lossy(&got).to_string()
});
let w = WebhookSink::new(&WebhookConfig { url: format!("http://127.0.0.1:{port}/hook"), token: None }, false).unwrap();
w.send("node", &alert()).await.unwrap();
let req = server.await.unwrap();
assert!(req.starts_with("POST /hook"));
assert!(req.contains("\"key\":\"net.route\"") && req.contains("\"status\":\"failing\""), "{req}");
let dry = WebhookSink::new(&WebhookConfig { url: "https://hooks.example.com/x".into(), token: Some("env:NOPE".into()) }, true).unwrap();
dry.send("node", &alert()).await.unwrap();
assert_eq!(dry.dry_sent()[0]["host"], "node-a");
}
#[cfg(feature = "push")]
#[tokio::test]
async fn push_wakes_each_card() {
use weirdnet_push::{WakeCard, WakeHandle};
let listener = tokio::net::TcpListener::bind("127.0.0.1:0").await.unwrap();
let port = listener.local_addr().unwrap().port();
let server = tokio::spawn(async move {
let (mut s, _) = listener.accept().await.unwrap();
let mut buf = vec![0u8; 16384];
let mut got = Vec::new();
loop {
let n = s.read(&mut buf).await.unwrap();
got.extend_from_slice(&buf[..n]);
let text = String::from_utf8_lossy(&got).to_string();
if let Some(i) = text.find("\r\n\r\n") {
let len: usize =
text.lines().find_map(|l| l.to_lowercase().strip_prefix("content-length:").map(|v| v.trim().parse().unwrap())).unwrap_or(0);
if got.len() >= i + 4 + len {
break;
}
}
}
s.write_all(b"HTTP/1.1 204 No Content\r\ncontent-length: 0\r\n\r\n").await.unwrap();
String::from_utf8_lossy(&got).to_string()
});
let handle = WakeHandle::generate();
let card = WakeCard { service: format!("http://127.0.0.1:{port}"), handle: handle.clone(), key: "11".repeat(32) };
assert!(PushSink::new(vec![], false).is_err());
assert!(PushSink::new(vec![WakeCard { key: "zz".into(), ..card.clone() }], false).is_err(), "bad key");
let dry = PushSink::new(vec![card.clone()], true).unwrap();
dry.send("node", &alert()).await.unwrap();
assert_eq!(dry.dry_sent(), vec![handle.digest()]);
PushSink::new(vec![card], false).unwrap().send("node", &alert()).await.unwrap();
let req = server.await.unwrap();
assert!(req.starts_with("POST /v1/wake"), "{req}");
assert!(req.contains(handle.as_str()), "the wake names the handle");
assert!(!req.contains("Wi-Fi"), "no alert content in a wake");
}
}