Skip to content

Commit

Permalink
fix(smtp-notifier): sender configuration
Browse files Browse the repository at this point in the history
  • Loading branch information
banditopazzo committed Sep 25, 2023
1 parent ed7197b commit 7ab018b
Show file tree
Hide file tree
Showing 2 changed files with 27 additions and 23 deletions.
4 changes: 2 additions & 2 deletions crates/modules/smtp-notifier/README.md
Original file line number Diff line number Diff line change
Expand Up @@ -6,13 +6,13 @@ This module will send an email notification when Pulsar identifies a threat.

|Config|Type|Description|
|------|----|-----------|
|`user` (required)|string|user credential for smtp server|
|`username` (required)|string|user credential for smtp server. Usually it's your email address, otherwise `sender` field must be set|
|`password` (required)|string|password credential for smtp server|
|`server` (required)|string|smtp server url to use|
|`receivers` (required)|string|comma separated emails to send notifications to|
|`port`|int|port for smtp server|
|`encryption`|string|encryption type to use for smtp: tls, starttls, none|
|`sender`|string|set a different email sender (should be allowed by email provider)|
|`sender`|string|set this if `username` is not your email address or if you want a custom sender (custom sender address must be allowed by your email provider)|

Default configuration:

Expand Down
46 changes: 25 additions & 21 deletions crates/modules/smtp-notifier/src/lib.rs
Original file line number Diff line number Diff line change
Expand Up @@ -53,16 +53,13 @@ async fn smtp_notifier_task(
let body = format!("{description}\n Source event: {payload}");

let mut message_builder = Message::builder()
.subject(subject);
.subject(subject)
.from(config.sender.clone());

for receiver in config.receivers.iter() {
message_builder = message_builder.to(receiver.clone())
}

if let Some(sender) = &config.sender {
message_builder = message_builder.from(sender.clone());
}

let message = message_builder.body(body)?;

let smtp_transport = match config.encryption {
Expand All @@ -76,7 +73,7 @@ async fn smtp_notifier_task(
};

smtp_transport
.credentials(Credentials::new(config.user.clone(), config.password.clone()))
.credentials(Credentials::new(config.username.clone(), config.password.clone()))
.port(config.port)
.build()
.send(message)
Expand Down Expand Up @@ -128,31 +125,38 @@ impl FromStr for Encryption {
#[derive(Clone, Debug)]
struct SmtpNotifierConfig {
server: String,
user: String,
username: String,
password: String,
receivers: Vec<Mailbox>,
port: u16,
encryption: Encryption,
sender: Option<Mailbox>,
sender: Mailbox,
}

impl TryFrom<&ModuleConfig> for SmtpNotifierConfig {
type Error = ConfigError;

fn try_from(config: &ModuleConfig) -> Result<Self, Self::Error> {
let username = config.required::<String>("username")?;

// Get sender from `sender` field or try to parse `username` as an email
let sender = match config.get_raw("sender") {
Some(s) => {
let mailbox = s
.parse::<Mailbox>()
.map_err(|err| ConfigError::InvalidValue {
field: "sender".to_string(),
value: s.to_string(),
err: err.to_string(),
})?;

Some(mailbox)
}
None => None,
Some(s) => s
.parse::<Mailbox>()
.map_err(|err| ConfigError::InvalidValue {
field: "sender".to_string(),
value: s.to_string(),
err: err.to_string(),
})?,
None => username
.parse::<Mailbox>()
.map_err(|err| ConfigError::InvalidValue {
field: "username".to_string(),
value: username.to_string(),
err: format!(
"if `username` is not the email address, a `sender` must be set: {err}"
),
})?,
};

let receivers = config.get_list::<Mailbox>("receivers")?;
Expand All @@ -165,7 +169,7 @@ impl TryFrom<&ModuleConfig> for SmtpNotifierConfig {

Ok(SmtpNotifierConfig {
server: config.required::<String>("server")?,
user: config.required::<String>("user")?,
username,
password: config.required::<String>("password")?,
receivers,
port: config.with_default::<u16>("port", 465)?,
Expand Down

0 comments on commit 7ab018b

Please sign in to comment.