Skip to content

Commit 817e168

Browse files
authored
Allowing xpack.notification.email.account.domain_allowlist to be set dynamically (#90426) (#90454)
1 parent a6f5e18 commit 817e168

File tree

3 files changed

+58
-1
lines changed

3 files changed

+58
-1
lines changed

docs/changelog/90426.yaml

Lines changed: 6 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,6 @@
1+
pr: 90426
2+
summary: Allowing `xpack.notification.email.account.domain_allowlist` to be set dynamically
3+
area: Watcher
4+
type: bug
5+
issues:
6+
- 89913

x-pack/plugin/watcher/src/main/java/org/elasticsearch/xpack/watcher/notification/email/EmailService.java

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -193,7 +193,7 @@ public EmailService(Settings settings, @Nullable CryptoService cryptoService, SS
193193
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_SEND_PARTIAL, (s, o) -> {}, (s, o) -> {});
194194
clusterSettings.addAffixUpdateConsumer(SETTING_SMTP_WAIT_ON_QUIT, (s, o) -> {}, (s, o) -> {});
195195
this.allowedDomains = new HashSet<>(SETTING_DOMAIN_ALLOWLIST.get(settings));
196-
clusterSettings.addSettingsUpdateConsumer(SETTING_DOMAIN_ALLOWLIST, (s) -> {});
196+
clusterSettings.addSettingsUpdateConsumer(SETTING_DOMAIN_ALLOWLIST, this::updateAllowedDomains);
197197
// do an initial load
198198
reload(settings);
199199
}

x-pack/plugin/watcher/src/test/java/org/elasticsearch/xpack/watcher/notification/email/EmailServiceTests.java

Lines changed: 51 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -21,6 +21,8 @@
2121
import java.util.List;
2222
import java.util.Properties;
2323

24+
import javax.mail.MessagingException;
25+
2426
import static org.hamcrest.Matchers.containsInAnyOrder;
2527
import static org.hamcrest.Matchers.containsString;
2628
import static org.hamcrest.Matchers.hasEntry;
@@ -264,6 +266,55 @@ public void testSendEmailWithDomainNotInAllowList() throws Exception {
264266
);
265267
}
266268

269+
public void testChangeDomainAllowListSetting() throws UnsupportedEncodingException, MessagingException {
270+
Settings settings = Settings.builder()
271+
.put("xpack.notification.email.account.account1.foo", "bar")
272+
.put("xpack.notification.email.account.account1.smtp.host", "localhost")
273+
.putList("xpack.notification.email.account.domain_allowlist", "bar.com")
274+
.build();
275+
ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, new HashSet<>(EmailService.getSettings()));
276+
EmailService emailService = new EmailService(settings, null, mock(SSLService.class), clusterSettings);
277+
Email email = new Email(
278+
"id",
279+
new Email.Address("[email protected]", "Mr. Foo Man"),
280+
createAddressList("[email protected]", "[email protected]"),
281+
randomFrom(Email.Priority.values()),
282+
ZonedDateTime.now(),
283+
createAddressList("[email protected]", "[email protected]"),
284+
null,
285+
null,
286+
"subject",
287+
"body",
288+
"htmlbody",
289+
Collections.emptyMap()
290+
);
291+
when(account.name()).thenReturn("account1");
292+
Authentication auth = new Authentication("user", new Secret("passwd".toCharArray()));
293+
Profile profile = randomFrom(Profile.values());
294+
295+
// This send will fail because one of the recipients ("[email protected]") is in a domain that is not in the allowed list
296+
IllegalArgumentException e = expectThrows(
297+
IllegalArgumentException.class,
298+
() -> emailService.send(email, auth, profile, "account1")
299+
);
300+
assertThat(
301+
e.getMessage(),
302+
containsString(
303+
"failed to send email with subject [subject] and recipient domains "
304+
+ "[bar.com, invalid.com], one or more recipients is not specified in the domain allow list setting "
305+
+ "[xpack.notification.email.account.domain_allowlist]."
306+
)
307+
);
308+
309+
// Now dynamically add "invalid.com" to the list of allowed domains:
310+
Settings newSettings = Settings.builder()
311+
.putList("xpack.notification.email.account.domain_allowlist", "bar.com", "invalid.com")
312+
.build();
313+
clusterSettings.applySettings(newSettings);
314+
// Still expect an exception because we're not actually sending the email, but it's no longer because the domain isn't allowed:
315+
expectThrows(MessagingException.class, () -> emailService.send(email, auth, profile, "account1"));
316+
}
317+
267318
private static Email.AddressList createAddressList(String... emails) throws UnsupportedEncodingException {
268319
List<Email.Address> addresses = new ArrayList<>();
269320
for (String email : emails) {

0 commit comments

Comments
 (0)