diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/Account.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/Account.java index 5005b1b87a8..dffd261a29b 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/Account.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/Account.java @@ -89,11 +89,13 @@ public class Account { Transport transport = session.getTransport(SMTP_PROTOCOL); - String user = auth != null ? auth.user() : null; + String user = auth != null ? auth.user() : config.smtp.user; if (user == null) { - user = config.smtp.user; - if (user == null) { - user = InternetAddress.getLocalAddress(session).getAddress(); + InternetAddress localAddress = InternetAddress.getLocalAddress(session); + // null check needed, because if the local host does not resolve, this may be null + // this can happen in wrongly setup linux distributions + if (localAddress != null) { + user = localAddress.getAddress(); } } diff --git a/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/EmailService.java b/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/EmailService.java index 179757d9285..e86e2bdf879 100644 --- a/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/EmailService.java +++ b/elasticsearch/src/main/java/org/elasticsearch/xpack/notification/email/EmailService.java @@ -37,10 +37,6 @@ public class EmailService extends NotificationService { } - public EmailSent send(Email email, Authentication auth, Profile profile) throws MessagingException { - return send(email, auth, profile, (String) null); - } - public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) throws MessagingException { Account account = getAccount(accountName); if (account == null) { @@ -50,7 +46,7 @@ public class EmailService extends NotificationService { return send(email, auth, profile, account); } - EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException { + private EmailSent send(Email email, Authentication auth, Profile profile, Account account) throws MessagingException { assert account != null; try { email = account.send(email, auth, profile); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountTests.java index aad2c643b0c..8f818a17c63 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountTests.java @@ -9,8 +9,8 @@ import org.elasticsearch.ElasticsearchException; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.unit.TimeValue; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.notification.email.support.EmailServer; import org.elasticsearch.xpack.common.secret.Secret; +import org.elasticsearch.xpack.notification.email.support.EmailServer; import org.junit.After; import org.junit.Before; diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountsTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountsTests.java index 785a603b861..99ac222ddd0 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountsTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/AccountsTests.java @@ -9,7 +9,6 @@ import org.elasticsearch.common.settings.ClusterSettings; import org.elasticsearch.common.settings.Settings; import org.elasticsearch.common.settings.SettingsException; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.notification.NotificationService; import java.util.Collections; @@ -85,17 +84,12 @@ public class AccountsTests extends ESTestCase { } public void testMultipleAccountsUnknownDefault() throws Exception { - Settings.Builder builder = Settings.builder() - .put("xpack.notification.email.default_account", "unknown"); + Settings.Builder builder = Settings.builder().put("xpack.notification.email.default_account", "unknown"); addAccountSettings("account1", builder); addAccountSettings("account2", builder); - try { - new EmailService(builder.build(), null, - new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING))); - fail("Expected SettingsException"); - } catch (SettingsException e) { - assertThat(e.getMessage(), is("could not find default account [unknown]")); - } + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)); + SettingsException e = expectThrows(SettingsException.class, () -> new EmailService(builder.build(), null, clusterSettings)); + assertThat(e.getMessage(), is("could not find default account [unknown]")); } public void testNoAccount() throws Exception { @@ -106,15 +100,10 @@ public class AccountsTests extends ESTestCase { } public void testNoAccountWithDefaultAccount() throws Exception { - Settings.Builder builder = Settings.builder() - .put("xpack.notification.email.default_account", "unknown"); - try { - new EmailService(builder.build(), null, - new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING))); - fail("Expected SettingsException"); - } catch (SettingsException e) { - assertThat(e.getMessage(), is("could not find default account [unknown]")); - } + Settings settings = Settings.builder().put("xpack.notification.email.default_account", "unknown").build(); + ClusterSettings clusterSettings = new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING)); + SettingsException e = expectThrows(SettingsException.class, () -> new EmailService(settings, null, clusterSettings)); + assertThat(e.getMessage(), is("could not find default account [unknown]")); } private void addAccountSettings(String name, Settings.Builder builder) { diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/ManualPublicSmtpServersTester.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/ManualPublicSmtpServersTester.java index 312c1beb628..dcaa8953006 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/ManualPublicSmtpServersTester.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/notification/email/ManualPublicSmtpServersTester.java @@ -23,7 +23,7 @@ public class ManualPublicSmtpServersTester { public static class Gmail { public static void main(String[] args) throws Exception { - test(Profile.GMAIL, Settings.builder() + test("gmail", Profile.GMAIL, Settings.builder() .put("xpack.notification.email.account.gmail.smtp.auth", true) .put("xpack.notification.email.account.gmail.smtp.starttls.enable", true) .put("xpack.notification.email.account.gmail.smtp.host", "smtp.gmail.com") @@ -38,7 +38,7 @@ public class ManualPublicSmtpServersTester { public static class OutlookDotCom { public static void main(String[] args) throws Exception { - test(Profile.STANDARD, Settings.builder() + test("outlook", Profile.STANDARD, Settings.builder() .put("xpack.notification.email.account.outlook.smtp.auth", true) .put("xpack.notification.email.account.outlook.smtp.starttls.enable", true) .put("xpack.notification.email.account.outlook.smtp.host", "smtp-mail.outlook.com") @@ -54,7 +54,7 @@ public class ManualPublicSmtpServersTester { public static class YahooMail { public static void main(String[] args) throws Exception { - test(Profile.STANDARD, Settings.builder() + test("yahoo", Profile.STANDARD, Settings.builder() .put("xpack.notification.email.account.yahoo.smtp.starttls.enable", true) .put("xpack.notification.email.account.yahoo.smtp.auth", true) .put("xpack.notification.email.account.yahoo.smtp.host", "smtp.mail.yahoo.com") @@ -72,7 +72,7 @@ public class ManualPublicSmtpServersTester { public static class SES { public static void main(String[] args) throws Exception { - test(Profile.STANDARD, Settings.builder() + test("ses", Profile.STANDARD, Settings.builder() .put("xpack.notification.email.account.ses.smtp.auth", true) .put("xpack.notification.email.account.ses.smtp.starttls.enable", true) .put("xpack.notification.email.account.ses.smtp.starttls.required", true) @@ -87,7 +87,7 @@ public class ManualPublicSmtpServersTester { } } - static void test(Profile profile, Settings.Builder settingsBuilder) throws Exception { + static void test(String accountName, Profile profile, Settings.Builder settingsBuilder) throws Exception { String path = "/org/elasticsearch/xpack/watcher/actions/email/service/logo.png"; if (EmailServiceTests.class.getResourceAsStream(path) == null) { throw new ElasticsearchException("Could not find logo at path {}", path); @@ -110,7 +110,7 @@ public class ManualPublicSmtpServersTester { () -> EmailServiceTests.class.getResourceAsStream(path))) .build(); - EmailService.EmailSent sent = service.send(email, null, profile); + EmailService.EmailSent sent = service.send(email, null, profile, accountName); terminal.println(String.format(Locale.ROOT, "email sent via account [%s]", sent.account())); } diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java index cae107d5678..052bff57c72 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/email/EmailActionTests.java @@ -15,10 +15,6 @@ import org.elasticsearch.common.xcontent.XContentBuilder; import org.elasticsearch.common.xcontent.XContentParser; import org.elasticsearch.common.xcontent.json.JsonXContent; import org.elasticsearch.test.ESTestCase; -import org.elasticsearch.xpack.common.text.TextTemplateEngine; -import org.elasticsearch.xpack.watcher.actions.Action; -import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; -import org.elasticsearch.xpack.watcher.execution.Wid; import org.elasticsearch.xpack.common.http.HttpClient; import org.elasticsearch.xpack.common.http.HttpRequest; import org.elasticsearch.xpack.common.http.HttpRequestTemplate; @@ -27,10 +23,7 @@ import org.elasticsearch.xpack.common.http.auth.HttpAuthRegistry; import org.elasticsearch.xpack.common.http.auth.basic.BasicAuthFactory; import org.elasticsearch.xpack.common.secret.Secret; import org.elasticsearch.xpack.common.text.TextTemplate; -import org.elasticsearch.xpack.watcher.support.xcontent.WatcherParams; -import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; -import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; -import org.elasticsearch.xpack.watcher.watch.Payload; +import org.elasticsearch.xpack.common.text.TextTemplateEngine; import org.elasticsearch.xpack.notification.email.Attachment; import org.elasticsearch.xpack.notification.email.Authentication; import org.elasticsearch.xpack.notification.email.DataAttachment; @@ -45,6 +38,13 @@ import org.elasticsearch.xpack.notification.email.attachment.EmailAttachments; import org.elasticsearch.xpack.notification.email.attachment.EmailAttachmentsParser; import org.elasticsearch.xpack.notification.email.attachment.HttpEmailAttachementParser; import org.elasticsearch.xpack.notification.email.attachment.HttpRequestAttachment; +import org.elasticsearch.xpack.watcher.actions.Action; +import org.elasticsearch.xpack.watcher.execution.WatchExecutionContext; +import org.elasticsearch.xpack.watcher.execution.Wid; +import org.elasticsearch.xpack.watcher.support.xcontent.WatcherParams; +import org.elasticsearch.xpack.watcher.test.AbstractWatcherIntegrationTestCase; +import org.elasticsearch.xpack.watcher.test.MockTextTemplateEngine; +import org.elasticsearch.xpack.watcher.watch.Payload; import org.jboss.netty.handler.codec.http.HttpHeaders; import org.joda.time.DateTime; import org.joda.time.DateTimeZone; @@ -94,11 +94,6 @@ public class EmailActionTests extends ESTestCase { public void testExecute() throws Exception { final String account = "account1"; EmailService service = new AbstractWatcherIntegrationTestCase.NoopEmailService() { - @Override - public EmailService.EmailSent send(Email email, Authentication auth, Profile profile) { - return new EmailService.EmailSent(account, email); - } - @Override public EmailService.EmailSent send(Email email, Authentication auth, Profile profile, String accountName) { return new EmailService.EmailSent(account, email); diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java index fab0c01752e..a942f138089 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/actions/webhook/WebhookActionTests.java @@ -274,10 +274,6 @@ public class WebhookActionTests extends ESTestCase { mock(WatcherClientProxy.class), ExecuteScenario.Success.client(), new AbstractWatcherIntegrationTestCase.NoopEmailService() { - @Override - public EmailSent send(Email email, Authentication auth, Profile profile) { - return new EmailSent(account, email); - } @Override public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) { diff --git a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java index f9fe3317bfd..2b69f5dfa64 100644 --- a/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java +++ b/elasticsearch/src/test/java/org/elasticsearch/xpack/watcher/test/AbstractWatcherIntegrationTestCase.java @@ -662,11 +662,6 @@ public abstract class AbstractWatcherIntegrationTestCase extends ESIntegTestCase new ClusterSettings(Settings.EMPTY, Collections.singleton(EmailService.EMAIL_ACCOUNT_SETTING))); } - @Override - public EmailService.EmailSent send(Email email, Authentication auth, Profile profile) { - return new EmailSent(auth.user(), email); - } - @Override public EmailSent send(Email email, Authentication auth, Profile profile, String accountName) { return new EmailSent(accountName, email);