|
21 | 21 | import java.util.List;
|
22 | 22 | import java.util.Properties;
|
23 | 23 |
|
| 24 | +import javax.mail.MessagingException; |
| 25 | + |
24 | 26 | import static org.hamcrest.Matchers.containsInAnyOrder;
|
25 | 27 | import static org.hamcrest.Matchers.containsString;
|
26 | 28 | import static org.hamcrest.Matchers.hasEntry;
|
@@ -264,6 +266,55 @@ public void testSendEmailWithDomainNotInAllowList() throws Exception {
|
264 | 266 | );
|
265 | 267 | }
|
266 | 268 |
|
| 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 | + |
| 281 | + randomFrom(Email.Priority.values()), |
| 282 | + ZonedDateTime.now(), |
| 283 | + |
| 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 | + |
267 | 318 | private static Email.AddressList createAddressList(String... emails) throws UnsupportedEncodingException {
|
268 | 319 | List<Email.Address> addresses = new ArrayList<>();
|
269 | 320 | for (String email : emails) {
|
|
0 commit comments