use std::collections::BTreeMap;
use serde::Deserialize;
use super::empty_string_is_no_values;
#[derive(Debug, Clone, Default, Deserialize)]
#[serde(default)]
pub struct NotifyConfig {
#[serde(deserialize_with = "empty_string_is_no_values")]
pub enabled: Vec<String>,
pub email: EmailNotifyConfig,
pub expiry: ExpiryNotifyConfig,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub webhook_enabled: Vec<String>,
pub webhook: BTreeMap<String, WebhookNotifyConfig>,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub custom_enabled: Vec<String>,
pub custom: BTreeMap<String, CustomNotifyConfig>,
pub template_dir: String,
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct EmailNotifyConfig {
pub smtp_host: String,
pub smtp_port: u16,
pub smtp_username: String,
pub smtp_password: String,
pub smtp_security: String,
pub from: String,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub to: Vec<String>,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub events: Vec<String>,
pub timeout_ms: u64,
}
impl Default for EmailNotifyConfig {
fn default() -> Self {
Self {
smtp_host: String::new(),
smtp_port: 587,
smtp_username: String::new(),
smtp_password: String::new(),
smtp_security: "starttls".to_string(),
from: String::new(),
to: Vec::new(),
events: all_notify_events(),
timeout_ms: 5000,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct ExpiryNotifyConfig {
pub lead_days: u64,
pub interval_days: u64,
pub max_entries: usize,
}
impl Default for ExpiryNotifyConfig {
fn default() -> Self {
Self {
lead_days: 0,
interval_days: 7,
max_entries: 50,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct WebhookNotifyConfig {
pub url: String,
pub method: String,
pub headers: BTreeMap<String, String>,
pub body: String,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub events: Vec<String>,
pub timeout_ms: u64,
}
pub const DEFAULT_WEBHOOK_BODY: &str = r#"{"text": {{ message | tojson }}}"#;
impl Default for WebhookNotifyConfig {
fn default() -> Self {
Self {
url: String::new(),
method: "POST".to_string(),
headers: BTreeMap::new(),
body: DEFAULT_WEBHOOK_BODY.to_string(),
events: all_notify_events(),
timeout_ms: 5000,
}
}
}
#[derive(Debug, Clone, Deserialize)]
#[serde(default)]
pub struct CustomNotifyConfig {
pub script_path: String,
pub timeout_ms: u64,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub args: Vec<String>,
#[serde(deserialize_with = "empty_string_is_no_values")]
pub events: Vec<String>,
}
impl Default for CustomNotifyConfig {
fn default() -> Self {
Self {
script_path: String::new(),
timeout_ms: 5000,
args: Vec::new(),
events: all_notify_events(),
}
}
}
pub const ALL_NOTIFY_EVENTS: [&str; 7] = [
"profile_mounted",
"account_created",
"account_deactivated",
"certificate_issued",
"certificate_revoked",
"challenge_failed",
"certificates_expiring",
];
fn all_notify_events() -> Vec<String> {
ALL_NOTIFY_EVENTS.iter().map(|s| s.to_string()).collect()
}