@@ -402,6 +402,167 @@ public void testRecipientAddressInAllowList_DisallowedBCCAddressesFails() throws
402402 assertThat (EmailService .recipientAddressInAllowList (email , allowedPatterns ), is (false ));
403403 }
404404
405+ public void testAllowedRecipient () throws Exception {
406+ Email email = new Email (
407+ "id" ,
408+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
409+ 410+ randomFrom (Email .Priority .values ()),
411+ ZonedDateTime .now (),
412+ createAddressList (
"[email protected] " ),
413+ null ,
414+ null ,
415+ "subject" ,
416+ "body" ,
417+ "htmlbody" ,
418+ Collections .emptyMap ()
419+ );
420+ assertTrue (EmailService .recipientAddressInAllowList (email , Set .of ("*" )));
421+ assertFalse (EmailService .recipientAddressInAllowList (email , Set .of ()));
422+ assertFalse (EmailService .recipientAddressInAllowList (email , Set .of ("" )));
423+ assertTrue (
EmailService .
recipientAddressInAllowList (
email ,
Set .
of (
"[email protected] " ,
"*[email protected] " )));
424+ assertTrue (
EmailService .
recipientAddressInAllowList (
email ,
Set .
of (
"[email protected] " ,
"*.com" )));
425+ assertTrue (EmailService .recipientAddressInAllowList (email , Set .of ("*.CoM" )));
426+
427+ // Invalid email in CC doesn't blow up
428+ email = new Email (
429+ "id" ,
430+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
431+ 432+ randomFrom (Email .Priority .values ()),
433+ ZonedDateTime .now (),
434+ createAddressList (
"[email protected] " ),
435+ createAddressList ("badEmail" ),
436+ null ,
437+ "subject" ,
438+ "body" ,
439+ "htmlbody" ,
440+ Collections .emptyMap ()
441+ );
442+ assertFalse (
EmailService .
recipientAddressInAllowList (
email ,
Set .
of (
"*@other.com" ,
"*[email protected] " )));
443+
444+ // Check CC
445+ email = new Email (
446+ "id" ,
447+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
448+ 449+ randomFrom (Email .Priority .values ()),
450+ ZonedDateTime .now (),
451+ createAddressList (
"[email protected] " ),
452+ createAddressList (
"[email protected] " ),
453+ null ,
454+ "subject" ,
455+ "body" ,
456+ "htmlbody" ,
457+ Collections .emptyMap ()
458+ );
459+ assertTrue (EmailService .recipientAddressInAllowList (email , Set .of ("*@other.com" , "*@bar.com" )));
460+ assertFalse (
EmailService .
recipientAddressInAllowList (
email ,
Set .
of (
"*[email protected] " )));
461+
462+ // Check BCC
463+ email = new Email (
464+ "id" ,
465+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
466+ 467+ randomFrom (Email .Priority .values ()),
468+ ZonedDateTime .now (),
469+ createAddressList (
"[email protected] " ),
470+ null ,
471+ createAddressList (
"[email protected] " ),
472+ "subject" ,
473+ "body" ,
474+ "htmlbody" ,
475+ Collections .emptyMap ()
476+ );
477+ assertTrue (EmailService .recipientAddressInAllowList (email , Set .of ("*@other.com" , "*@bar.com" )));
478+ assertFalse (
EmailService .
recipientAddressInAllowList (
email ,
Set .
of (
"*[email protected] " )));
479+ }
480+
481+ public void testSendEmailWithRecipientNotInAllowList () throws Exception {
482+ service .updateAllowedRecipientPatterns (Collections .singletonList (randomFrom ("*@bar.*" , "*@bar.com" , "*b*" )));
483+ Email email = new Email (
484+ "id" ,
485+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
486+ 487+ randomFrom (Email .Priority .values ()),
488+ ZonedDateTime .now (),
489+ 490+ null ,
491+ null ,
492+ "subject" ,
493+ "body" ,
494+ "htmlbody" ,
495+ Collections .emptyMap ()
496+ );
497+ when (account .name ()).thenReturn ("account1" );
498+ Authentication auth = new Authentication ("user" , new Secret ("passwd" .toCharArray ()));
499+ Profile profile = randomFrom (Profile .values ());
500+ IllegalArgumentException e = expectThrows (IllegalArgumentException .class , () -> service .send (email , auth , profile , "account1" ));
501+ assertThat (
502+ e .getMessage (),
503+ containsString (
504+ "failed to send email with subject [subject] and recipients [[email protected] , [email protected] ], " 505+ + "one or more recipients is not specified in the domain allow list setting "
506+ + "[xpack.notification.email.recipient_allowlist]."
507+ )
508+ );
509+ }
510+
511+ public void testChangeRecipientAllowListSetting () throws UnsupportedEncodingException , MessagingException {
512+ Settings settings = Settings .builder ()
513+ .put ("xpack.notification.email.account.account1.foo" , "bar" )
514+ // Setting a random SMTP server name and an invalid port so that sending emails is guaranteed to fail:
515+ .put ("xpack.notification.email.account.account1.smtp.host" , randomAlphaOfLength (10 ))
516+ .put ("xpack.notification.email.account.account1.smtp.port" , -100 )
517+ .
putList (
"xpack.notification.email.recipient_allowlist" ,
"*[email protected] " )
518+ .build ();
519+ ClusterSettings clusterSettings = new ClusterSettings (Settings .EMPTY , new HashSet <>(EmailService .getSettings ()));
520+ EmailService emailService = new EmailService (settings , null , mock (SSLService .class ), clusterSettings );
521+ Email email = new Email (
522+ "id" ,
523+ new Email .
Address (
"[email protected] " ,
"Mr. Foo Man" ),
524+ 525+ randomFrom (Email .Priority .values ()),
526+ ZonedDateTime .now (),
527+ 528+ null ,
529+ null ,
530+ "subject" ,
531+ "body" ,
532+ "htmlbody" ,
533+ Collections .emptyMap ()
534+ );
535+ when (account .name ()).thenReturn ("account1" );
536+ Authentication auth = new Authentication ("user" , new Secret ("passwd" .toCharArray ()));
537+ Profile profile = randomFrom (Profile .values ());
538+
539+ // This send will fail because one of the recipients ("[email protected] ") is in a domain that is not in the allowed list 540+ IllegalArgumentException e1 = expectThrows (
541+ IllegalArgumentException .class ,
542+ () -> emailService .send (email , auth , profile , "account1" )
543+ );
544+ assertThat (
545+ e1 .getMessage (),
546+ containsString (
547+ "failed to send email with subject [subject] and recipients [[email protected] , [email protected] ], " 548+ + "one or more recipients is not specified in the domain allow list setting "
549+ + "[xpack.notification.email.recipient_allowlist]."
550+ )
551+ );
552+
553+ // Now dynamically add "invalid.com" to the list of allowed domains:
554+ Settings newSettings = Settings .builder ()
555+ .putList ("xpack.notification.email.recipient_allowlist" , "*@bar.com" , "*@invalid.com" )
556+ .build ();
557+ clusterSettings .applySettings (newSettings );
558+ // Still expect an exception because we're not actually sending the email, but it's no longer because the domain isn't allowed:
559+ IllegalArgumentException e2 = expectThrows (
560+ IllegalArgumentException .class ,
561+ () -> emailService .send (email , auth , profile , "account1" )
562+ );
563+ assertThat (e2 .getMessage (), containsString ("port out of range" ));
564+ }
565+
405566 private Email createTestEmail (String ... recipients ) throws UnsupportedEncodingException {
406567 return new Email (
407568 "id" ,
0 commit comments