Skip to content

Commit

Permalink
[2.19.x] DDF-6232 decrypt password parameter in SmtpClientImpl (#6231)
Browse files Browse the repository at this point in the history
  • Loading branch information
rymach authored Aug 17, 2020
1 parent 455edf3 commit f4637a6
Show file tree
Hide file tree
Showing 4 changed files with 37 additions and 7 deletions.
11 changes: 11 additions & 0 deletions platform/email/platform-email-impl/pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -72,6 +72,17 @@
<groupId>ddf.platform.util</groupId>
<artifactId>platform-util</artifactId>
</dependency>
<dependency>
<groupId>ddf.security.encryption</groupId>
<artifactId>security-encryption-api</artifactId>
<version>${project.version}</version>
</dependency>
<dependency>
<groupId>org.mockito</groupId>
<artifactId>mockito-core</artifactId>
<version>${mockito-core.version}</version>
<scope>test</scope>
</dependency>
</dependencies>
<build>
<plugins>
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,6 +18,7 @@
import static org.apache.commons.lang.Validate.notNull;

import ddf.security.common.audit.SecurityLogger;
import ddf.security.encryption.EncryptionService;
import java.util.Arrays;
import java.util.Properties;
import java.util.concurrent.ExecutorService;
Expand Down Expand Up @@ -69,6 +70,8 @@ public class SmtpClientImpl implements SmtpClient {
Executors.newFixedThreadPool(
1, StandardThreadFactoryBuilder.newThreadFactory("smtpClientImplThread"));

private final EncryptionService encryptionService;

private String hostName;

private Integer portNumber;
Expand All @@ -77,6 +80,10 @@ public class SmtpClientImpl implements SmtpClient {

private String password;

public SmtpClientImpl(EncryptionService encryptionService) {
this.encryptionService = encryptionService;
}

/**
* Set the username for the email server.
*
Expand All @@ -92,7 +99,7 @@ public void setUserName(@Nullable String userName) {
* @param password the password for the email server
*/
public void setPassword(@Nullable String password) {
this.password = password;
this.password = encryptionService.decryptValue(password);
}

/** @param hostName must be non-empty */
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -18,7 +18,12 @@
xsi:schemaLocation="http://www.osgi.org/xmlns/blueprint/v1.0.0
http://www.osgi.org/xmlns/blueprint/v1.0.0/blueprint.xsd">

<reference id="encryptionService"
interface="ddf.security.encryption.EncryptionService"
availability="mandatory"/>

<bean id="smtpClient" class="org.codice.ddf.platform.email.impl.SmtpClientImpl">
<argument ref="encryptionService"/>
<cm:managed-properties
persistent-id="org.codice.ddf.platform.email.impl.SmtpClientImpl"
update-strategy="container-managed"/>
Expand All @@ -32,4 +37,4 @@
</service-properties>
</service>

</blueprint>
</blueprint>
Original file line number Diff line number Diff line change
Expand Up @@ -16,9 +16,14 @@
import static org.hamcrest.Matchers.containsString;
import static org.hamcrest.Matchers.is;
import static org.junit.Assert.assertThat;
import static org.mockito.AdditionalAnswers.returnsFirstArg;
import static org.mockito.Matchers.anyString;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.when;

import com.dumbster.smtp.SimpleSmtpServer;
import com.dumbster.smtp.SmtpMessage;
import ddf.security.encryption.EncryptionService;
import java.io.File;
import java.io.FileOutputStream;
import java.io.IOException;
Expand Down Expand Up @@ -76,7 +81,7 @@ public void testSendWithAttachments()

SimpleSmtpServer server = SimpleSmtpServer.start(port);

SmtpClientImpl emailService = new SmtpClientImpl();
SmtpClientImpl emailService = new SmtpClientImpl(null);

emailService.setHostName(HOSTNAME);
emailService.setPortNumber(port);
Expand Down Expand Up @@ -130,7 +135,7 @@ public void testSend()

SimpleSmtpServer server = SimpleSmtpServer.start(port);

SmtpClientImpl emailService = new SmtpClientImpl();
SmtpClientImpl emailService = new SmtpClientImpl(null);

emailService.setHostName(HOSTNAME);
emailService.setPortNumber(port);
Expand Down Expand Up @@ -189,18 +194,20 @@ public void testWithUsernameEmptyPassword() throws UnknownHostException {

@Test(expected = IllegalArgumentException.class)
public void testWithNullHostname() {
SmtpClientImpl emailService = new SmtpClientImpl();
SmtpClientImpl emailService = new SmtpClientImpl(null);
emailService.createSession();
}

@Test(expected = IllegalArgumentException.class)
public void throwsIllegalArgumentExceptionWhenPortNumberIsLessThanOne() {
new SmtpClientImpl().setPortNumber(0);
new SmtpClientImpl(null).setPortNumber(0);
}

private void validateUsernamePassword(String username, String password)
throws UnknownHostException {
SmtpClientImpl emailService = new SmtpClientImpl();
EncryptionService mockEncryptionService = mock(EncryptionService.class);
when(mockEncryptionService.decryptValue(anyString())).then(returnsFirstArg());
SmtpClientImpl emailService = new SmtpClientImpl(mockEncryptionService);

emailService.setHostName("host.com");
emailService.setPortNumber(25);
Expand Down

0 comments on commit f4637a6

Please sign in to comment.