-
Notifications
You must be signed in to change notification settings - Fork 25.6k
Introduce Email Address Allow Lists For Watcher #116672
New issue
Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.
By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.
Already on GitHub? Sign in to your account
Merged
lukewhiting
merged 6 commits into
elastic:main
from
lukewhiting:email-address-pattern-allow
Nov 14, 2024
Merged
Changes from 4 commits
Commits
Show all changes
6 commits
Select commit
Hold shift + click to select a range
94087d6
New setting plus mutual exclusiveness validation
lukewhiting ff84328
New domain list checking
lukewhiting 004fb55
Email service tests
lukewhiting 7292283
Documentation updates
lukewhiting 6cbb338
PR Changes
lukewhiting f35999f
Merge branch 'main' of github.com:elastic/elasticsearch into email-ad…
lukewhiting File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
There are no files selected for viewing
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
|
@@ -26,7 +26,9 @@ | |
import java.util.ArrayList; | ||
import java.util.Arrays; | ||
import java.util.HashSet; | ||
import java.util.Iterator; | ||
import java.util.List; | ||
import java.util.Map; | ||
import java.util.Optional; | ||
import java.util.Set; | ||
import java.util.function.Predicate; | ||
|
@@ -56,9 +58,72 @@ public class EmailService extends NotificationService<Account> { | |
(key) -> Setting.simpleString(key, Property.Dynamic, Property.NodeScope) | ||
); | ||
|
||
private static final List<String> ALLOW_ALL_DEFAULT = List.of("*"); | ||
|
||
private static final Setting<List<String>> SETTING_DOMAIN_ALLOWLIST = Setting.stringListSetting( | ||
"xpack.notification.email.account.domain_allowlist", | ||
List.of("*"), | ||
ALLOW_ALL_DEFAULT, | ||
new Setting.Validator<>() { | ||
@Override | ||
public void validate(List<String> value) { | ||
// Ignored | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void validate(List<String> value, Map<Setting<?>, Object> settings) { | ||
List<String> recipientAllowPatterns = (List<String>) settings.get(SETTING_RECIPIENT_ALLOW_PATTERNS); | ||
if (value.equals(ALLOW_ALL_DEFAULT) == false && recipientAllowPatterns.equals(ALLOW_ALL_DEFAULT) == false) { | ||
throw new IllegalArgumentException( | ||
"Cannot set both [" | ||
+ SETTING_RECIPIENT_ALLOW_PATTERNS.getKey() | ||
+ "] and [" | ||
+ SETTING_DOMAIN_ALLOWLIST.getKey() | ||
+ "] to a non [\"*\"] value at the same time." | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<Setting<?>> settings() { | ||
List<Setting<?>> settingRecipientAllowPatterns = List.of(SETTING_RECIPIENT_ALLOW_PATTERNS); | ||
return settingRecipientAllowPatterns.iterator(); | ||
} | ||
}, | ||
Property.Dynamic, | ||
Property.NodeScope | ||
); | ||
|
||
private static final Setting<List<String>> SETTING_RECIPIENT_ALLOW_PATTERNS = Setting.stringListSetting( | ||
"xpack.notification.email.recipient_allowlist", | ||
ALLOW_ALL_DEFAULT, | ||
new Setting.Validator<>() { | ||
@Override | ||
public void validate(List<String> value) { | ||
// Ignored | ||
} | ||
|
||
@Override | ||
@SuppressWarnings("unchecked") | ||
public void validate(List<String> value, Map<Setting<?>, Object> settings) { | ||
List<String> domainAllowList = (List<String>) settings.get(SETTING_DOMAIN_ALLOWLIST); | ||
if (value.equals(ALLOW_ALL_DEFAULT) == false && domainAllowList.equals(ALLOW_ALL_DEFAULT) == false) { | ||
throw new IllegalArgumentException( | ||
"Connect set both [" | ||
+ SETTING_RECIPIENT_ALLOW_PATTERNS.getKey() | ||
+ "] and [" | ||
+ SETTING_DOMAIN_ALLOWLIST.getKey() | ||
+ "] to a non [\"*\"] value at the same time." | ||
); | ||
} | ||
} | ||
|
||
@Override | ||
public Iterator<Setting<?>> settings() { | ||
List<Setting<?>> settingDomainAllowlist = List.of(SETTING_DOMAIN_ALLOWLIST); | ||
return settingDomainAllowlist.iterator(); | ||
} | ||
}, | ||
Property.Dynamic, | ||
Property.NodeScope | ||
); | ||
|
@@ -167,6 +232,7 @@ public class EmailService extends NotificationService<Account> { | |
private final CryptoService cryptoService; | ||
private final SSLService sslService; | ||
private volatile Set<String> allowedDomains; | ||
private volatile Set<String> allowedRecipientPatterns; | ||
|
||
@SuppressWarnings("this-escape") | ||
public EmailService(Settings settings, @Nullable CryptoService cryptoService, SSLService sslService, ClusterSettings clusterSettings) { | ||
|
@@ -192,7 +258,9 @@ public EmailService(Settings settings, @Nullable CryptoService cryptoService, SS | |
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_SEND_PARTIAL, (s, o) -> {}, (s, o) -> {}); | ||
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_WAIT_ON_QUIT, (s, o) -> {}, (s, o) -> {}); | ||
this.allowedDomains = new HashSet<>(SETTING_DOMAIN_ALLOWLIST.get(settings)); | ||
this.allowedRecipientPatterns = new HashSet<>(SETTING_RECIPIENT_ALLOW_PATTERNS.get(settings)); | ||
clusterSettings.addSettingsUpdateConsumer(SETTING_DOMAIN_ALLOWLIST, this::updateAllowedDomains); | ||
clusterSettings.addSettingsUpdateConsumer(SETTING_RECIPIENT_ALLOW_PATTERNS, this::updateAllowedRecipientPatterns); | ||
// do an initial load | ||
reload(settings); | ||
} | ||
|
@@ -201,6 +269,10 @@ void updateAllowedDomains(List<String> newDomains) { | |
this.allowedDomains = new HashSet<>(newDomains); | ||
} | ||
|
||
void updateAllowedRecipientPatterns(List<String> newPatterns) { | ||
this.allowedRecipientPatterns = new HashSet<>(newPatterns); | ||
} | ||
|
||
@Override | ||
protected Account createAccount(String name, Settings accountSettings) { | ||
Account.Config config = new Account.Config(name, accountSettings, getSmtpSslSocketFactory(), logger); | ||
|
@@ -228,46 +300,77 @@ public EmailSent send(Email email, Authentication auth, Profile profile, String | |
"failed to send email with subject [" | ||
+ email.subject() | ||
+ "] and recipient domains " | ||
+ getRecipientDomains(email) | ||
+ getRecipients(email, true) | ||
+ ", one or more recipients is not specified in the domain allow list setting [" | ||
+ SETTING_DOMAIN_ALLOWLIST.getKey() | ||
+ "]." | ||
); | ||
} | ||
if (recipientAddressInAllowList(email, this.allowedRecipientPatterns) == false) { | ||
throw new IllegalArgumentException( | ||
"failed to send email with subject [" | ||
+ email.subject() | ||
+ "] and recipients " | ||
+ getRecipients(email, false) | ||
+ ", one or more recipients is not specified in the domain allow list setting [" | ||
+ SETTING_RECIPIENT_ALLOW_PATTERNS.getKey() | ||
+ "]." | ||
); | ||
} | ||
return send(email, auth, profile, account); | ||
} | ||
|
||
// Visible for testing | ||
static Set<String> getRecipientDomains(Email email) { | ||
return Stream.concat( | ||
static Set<String> getRecipients(Email email, boolean domainsOnly) { | ||
var stream = Stream.concat( | ||
Optional.ofNullable(email.to()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty()), | ||
Stream.concat( | ||
Optional.ofNullable(email.cc()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty()), | ||
Optional.ofNullable(email.bcc()).map(addrs -> Arrays.stream(addrs.toArray())).orElse(Stream.empty()) | ||
) | ||
) | ||
.map(InternetAddress::getAddress) | ||
// Pull out only the domain of the email address, so [email protected] -> bar.com | ||
.map(emailAddress -> emailAddress.substring(emailAddress.lastIndexOf('@') + 1)) | ||
.collect(Collectors.toSet()); | ||
).map(InternetAddress::getAddress); | ||
|
||
if (domainsOnly) { | ||
// Pull out only the domain of the email address, so [email protected] | ||
stream = stream.map(emailAddress -> emailAddress.substring(emailAddress.lastIndexOf('@') + 1)); | ||
} | ||
|
||
return stream.collect(Collectors.toSet()); | ||
} | ||
|
||
// Visible for testing | ||
static boolean recipientDomainsInAllowList(Email email, Set<String> allowedDomainSet) { | ||
if (allowedDomainSet.size() == 0) { | ||
if (allowedDomainSet.isEmpty()) { | ||
// Nothing is allowed | ||
return false; | ||
} | ||
if (allowedDomainSet.contains("*")) { | ||
// Don't bother checking, because there is a wildcard all | ||
return true; | ||
} | ||
final Set<String> domains = getRecipientDomains(email); | ||
final Set<String> domains = getRecipients(email, true); | ||
final Predicate<String> matchesAnyAllowedDomain = domain -> allowedDomainSet.stream() | ||
.anyMatch(allowedDomain -> Regex.simpleMatch(allowedDomain, domain, true)); | ||
return domains.stream().allMatch(matchesAnyAllowedDomain); | ||
} | ||
|
||
// Visible for testing | ||
static boolean recipientAddressInAllowList(Email email, Set<String> allowedRecipientPatterns) { | ||
if (allowedRecipientPatterns.isEmpty()) { | ||
// Nothing is allowed | ||
return false; | ||
} | ||
if (allowedRecipientPatterns.contains("*")) { | ||
// Don't bother checking, because there is a wildcard all | ||
return true; | ||
} | ||
|
||
final Set<String> recipients = getRecipients(email, false); | ||
final Predicate<String> matchesAnyAllowedRecipient = recipient -> allowedRecipientPatterns.stream() | ||
.anyMatch(pattern -> Regex.simpleMatch(pattern, recipient, true)); | ||
return recipients.stream().allMatch(matchesAnyAllowedRecipient); | ||
} | ||
|
||
private static EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException { | ||
assert account != null; | ||
try { | ||
|
@@ -304,6 +407,7 @@ private static List<Setting<?>> getDynamicSettings() { | |
return Arrays.asList( | ||
SETTING_DEFAULT_ACCOUNT, | ||
SETTING_DOMAIN_ALLOWLIST, | ||
SETTING_RECIPIENT_ALLOW_PATTERNS, | ||
SETTING_PROFILE, | ||
SETTING_EMAIL_DEFAULTS, | ||
SETTING_SMTP_AUTH, | ||
|
Oops, something went wrong.
Add this suggestion to a batch that can be applied as a single commit.
This suggestion is invalid because no changes were made to the code.
Suggestions cannot be applied while the pull request is closed.
Suggestions cannot be applied while viewing a subset of changes.
Only one suggestion per line can be applied in a batch.
Add this suggestion to a batch that can be applied as a single commit.
Applying suggestions on deleted lines is not supported.
You must change the existing code in this line in order to create a valid suggestion.
Outdated suggestions cannot be applied.
This suggestion has been applied or marked resolved.
Suggestions cannot be applied from pending reviews.
Suggestions cannot be applied on multi-line comments.
Suggestions cannot be applied while the pull request is queued to merge.
Suggestion cannot be applied right now. Please check back later.
Uh oh!
There was an error while loading. Please reload this page.