diff options
author | Julien HENRY <julien.henry@sonarsource.com> | 2013-12-05 09:20:31 +0100 |
---|---|---|
committer | Julien HENRY <julien.henry@sonarsource.com> | 2013-12-05 09:21:23 +0100 |
commit | 7e63f87a902d2090654bb9aa26791f3f8283981d (patch) | |
tree | 54a1c0a1ad7e327df014c72787bde551bbfb3221 /plugins/sonar-email-notifications-plugin | |
parent | 4daebb00703a8dfeb1d558d93c387eea27165ca6 (diff) | |
download | sonarqube-7e63f87a902d2090654bb9aa26791f3f8283981d.tar.gz sonarqube-7e63f87a902d2090654bb9aa26791f3f8283981d.zip |
SONAR-4647 Add support for STARTTLS
Diffstat (limited to 'plugins/sonar-email-notifications-plugin')
3 files changed, 37 insertions, 11 deletions
diff --git a/plugins/sonar-email-notifications-plugin/pom.xml b/plugins/sonar-email-notifications-plugin/pom.xml index 04eb2a70ace..61072f04472 100644 --- a/plugins/sonar-email-notifications-plugin/pom.xml +++ b/plugins/sonar-email-notifications-plugin/pom.xml @@ -25,7 +25,7 @@ <dependency> <groupId>org.apache.commons</groupId> <artifactId>commons-email</artifactId> - <version>1.2</version> + <version>1.3.2</version> </dependency> <!-- unit tests --> @@ -37,7 +37,7 @@ <dependency> <groupId>org.subethamail</groupId> <artifactId>subethasmtp</artifactId> - <version>3.1.6</version> + <version>3.1.7</version> <scope>test</scope> </dependency> </dependencies> @@ -66,8 +66,8 @@ <configuration> <rules> <requireFilesSize> - <maxsize>530000</maxsize> - <minsize>510000</minsize> + <maxsize>600000</maxsize> + <minsize>590000</minsize> <files> <file>${project.build.directory}/${project.build.finalName}.jar</file> </files> diff --git a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java index 21c3b4fcd29..757295d0818 100644 --- a/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java +++ b/plugins/sonar-email-notifications-plugin/src/main/java/org/sonar/plugins/emailnotifications/EmailNotificationChannel.java @@ -150,9 +150,9 @@ public class EmailNotificationChannel extends NotificationChannel { SimpleEmail email = new SimpleEmail(); if (StringUtils.isNotBlank(host)) { /* - * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and - * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook - */ + * Set headers for proper threading: GMail will not group messages, even if they have same subject, but don't have "In-Reply-To" and + * "References" headers. TODO investigate threading in other clients like KMail, Thunderbird, Outlook + */ if (StringUtils.isNotEmpty(emailMessage.getMessageId())) { String messageId = "<" + emailMessage.getMessageId() + "@" + host + ">"; email.addHeader(IN_REPLY_TO_HEADER, messageId); @@ -168,18 +168,22 @@ public class EmailNotificationChannel extends NotificationChannel { email.setFrom(configuration.getFrom(), from); email.addTo(emailMessage.getTo(), " "); String subject = StringUtils.defaultIfBlank(StringUtils.trimToEmpty(configuration.getPrefix()) + " ", "") - + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT); + + StringUtils.defaultString(emailMessage.getSubject(), SUBJECT_DEFAULT); email.setSubject(subject); email.setMsg(emailMessage.getMessage()); // Send email.setHostName(configuration.getSmtpHost()); - if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "SSL")) { - email.setSSL(true); + if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "ssl")) { + email.setSSLOnConnect(true); email.setSslSmtpPort(String.valueOf(configuration.getSmtpPort())); // this port is not used except in EmailException message, that's why it's set with the same value than SSL port. // It prevents from getting bad message. email.setSmtpPort(configuration.getSmtpPort()); + } else if (StringUtils.equalsIgnoreCase(configuration.getSecureConnection(), "starttls")) { + email.setStartTLSEnabled(true); + email.setStartTLSRequired(true); + email.setSmtpPort(configuration.getSmtpPort()); } else if (StringUtils.isBlank(configuration.getSecureConnection())) { email.setSmtpPort(configuration.getSmtpPort()); } else { diff --git a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java index 057bba86441..46a73587dd0 100644 --- a/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java +++ b/plugins/sonar-email-notifications-plugin/src/test/java/org/sonar/plugins/emailnotifications/EmailNotificationChannelTest.java @@ -22,24 +22,30 @@ package org.sonar.plugins.emailnotifications; import org.apache.commons.mail.EmailException; import org.junit.After; import org.junit.Before; +import org.junit.Rule; import org.junit.Test; +import org.junit.rules.ExpectedException; import org.sonar.api.config.EmailSettings; import org.sonar.plugins.emailnotifications.api.EmailMessage; import org.subethamail.wiser.Wiser; import org.subethamail.wiser.WiserMessage; import javax.mail.internet.MimeMessage; + import java.io.IOException; import java.net.ServerSocket; import java.util.List; +import static junit.framework.Assert.fail; import static org.fest.assertions.Assertions.assertThat; -import static org.fest.assertions.Fail.fail; import static org.mockito.Mockito.mock; import static org.mockito.Mockito.when; public class EmailNotificationChannelTest { + @Rule + public ExpectedException thrown = ExpectedException.none(); + private int port; private Wiser server; private EmailSettings configuration; @@ -181,6 +187,22 @@ public class EmailNotificationChannelTest { channel.deliver(emailMessage); } + @Test + public void shouldSendTestEmailWithSTARTTLS() throws Exception { + server.getServer().setEnableTLS(true); + server.getServer().setRequireTLS(true); + configure(); + when(configuration.getSecureConnection()).thenReturn("STARTTLS"); + + try { + channel.sendTestEmail("user@nowhere", "Test Message from SonarQube", "This is a test message from SonarQube."); + fail("An SSL exception was expected a a proof that STARTTLS is enabled"); + } catch (EmailException e) { + // We don't have a SSL certificate so we are expecting a SSL error + assertThat(e.getCause().getMessage()).isEqualTo("Could not convert socket to TLS"); + } + } + private void configure() { when(configuration.getSmtpHost()).thenReturn("localhost"); when(configuration.getSmtpPort()).thenReturn(port); |