Skip to content
This repository has been archived by the owner on Aug 1, 2022. It is now read-only.

Commit

Permalink
Merge pull request #1557 from SAP/on-premise-mail
Browse files Browse the repository at this point in the history
Enable connection to on-premise mail server via SOCKS5 proxy
  • Loading branch information
ThuF authored Apr 27, 2022
2 parents 3e197f2 + 0dc48a5 commit 311a6c4
Show file tree
Hide file tree
Showing 5 changed files with 46 additions and 22 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -46,9 +46,13 @@ public static Destination getDestination(String destinationName) {
}
});

URI uri = URI.create((String) fetchedDestination.get("URL").get());
if (fetchedDestination.isHttp()) {
URI uri = URI.create((String) fetchedDestination.get("URL").get());

return new Destination(uri.getHost(), uri.getPort(), uri.getPath(), destinationProperties);
return new Destination(uri.getHost(), uri.getPort(), uri.getPath(), destinationProperties);
} else {
return new Destination(destinationProperties);
}
}

public static String executeRequest(String requestObject, String destinationName, String options)
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -21,6 +21,10 @@ public class Destination {
private String pathPrefix;
private Properties properties = new Properties();

public Destination(Properties properties) {
this.properties = properties;
}

public Destination(String host, int port, String pathPrefix, Properties properties) {
this.host = host;
this.port = port;
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -24,6 +24,8 @@
import org.apache.http.HttpResponse;
import org.apache.http.StatusLine;
import org.apache.http.client.HttpClient;

import static org.junit.Assert.assertNull;
import static org.mockito.ArgumentMatchers.any;

import org.junit.After;
Expand Down Expand Up @@ -57,8 +59,8 @@ public class CloudPlatformDestinationFacadeTest {

@Parameters
public static Collection<Object[]> data() {
return Arrays.asList(new Object[][] {
{ true }, { false }
return Arrays.asList(new Object[][]{
{true}, {false}
});
}

Expand All @@ -74,10 +76,6 @@ public void setup() throws IOException {

@After
public void close() {

if (!isKymaFacadeSet) {
cloudPlatformAccessor.verify(() -> CloudPlatformAccessor.setCloudPlatformFacade(any()));
}
destinationAccessor.close();
httpClientAccessor.close();
cloudPlatformAccessor.close();
Expand All @@ -90,8 +88,10 @@ public void mockAccessors(Boolean isKymaFacadeSet) {
httpClientAccessor = Mockito.mockStatic(HttpClientAccessor.class);
cloudPlatformAccessor = Mockito.mockStatic(CloudPlatformAccessor.class);

com.sap.cloud.sdk.cloudplatform.connectivity.Destination mockedDestination = Mockito.mock(com.sap.cloud.sdk.cloudplatform.connectivity.Destination.class);
com.sap.cloud.sdk.cloudplatform.connectivity.Destination mockedDestination = Mockito.mock(
com.sap.cloud.sdk.cloudplatform.connectivity.Destination.class);
when(mockedDestination.get("URL")).thenReturn(Option.of(DESTINATION_URI));
when(mockedDestination.isHttp()).thenReturn(true, false);

HttpDestination mockedHttpDestination = Mockito.mock(HttpDestination.class);
when(mockedHttpDestination.getAuthenticationType()).thenReturn(AuthenticationType.NO_AUTHENTICATION);
Expand All @@ -100,7 +100,7 @@ public void mockAccessors(Boolean isKymaFacadeSet) {
when(mockedDestination.asHttp()).thenReturn(mockedHttpDestination);

destinationAccessor.when(() -> DestinationAccessor.getDestination(destinationName))
.thenReturn(mockedDestination);
.thenReturn(mockedDestination, mockedDestination);

httpClientAccessor.when(() -> HttpClientAccessor.getHttpClient(any()))
.thenReturn(httpClient);
Expand Down Expand Up @@ -132,6 +132,12 @@ public void getDestinationTest() throws Exception {
assertEquals("test-destination.com", dest.getHost());
assertEquals(8080, dest.getPort());
assertEquals("/destination", dest.getPathPrefix());

// when .isHttp() is false
Destination dest2 = CloudPlatformDestinationFacade.getDestination(destinationName);
assertNull(dest2.getHost());
assertEquals(0, dest2.getPort());
assertNull(dest2.getPathPrefix());
}

@Test
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -17,21 +17,30 @@
import org.eclipse.dirigible.commons.config.Configuration;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import java.util.Arrays;
import java.util.List;
import java.util.Properties;
import java.util.stream.Collectors;
import java.util.stream.Stream;

public class DestMailConfigProvider implements IMailConfigurationProvider {

private static final String MAIL_USER = "mail.user";
private static final String MAIL_PASSWORD = "mail.password";
private static final String MAIL_TRANSPORT_PROTOCOL = "mail.transport.protocol";
private static final String MAIL_SMTPS_HOST = "mail.smtps.host";
private static final String MAIL_SMTPS_PORT = "mail.smtps.port";
private static final String MAIL_SMTPS_AUTH = "mail.smtps.auth";
private static final String MAIL_SMTP_HOST = "mail.smtp.host";
private static final String MAIL_SMTP_PORT = "mail.smtp.port";
private static final String MAIL_SMTP_AUTH = "mail.smtp.auth";
private static List<String> MAIL_PROPERTIES = List.of(MAIL_USER, MAIL_PASSWORD, MAIL_TRANSPORT_PROTOCOL, MAIL_SMTPS_HOST, MAIL_SMTPS_PORT,
MAIL_SMTPS_AUTH, MAIL_SMTP_HOST, MAIL_SMTP_PORT, MAIL_SMTP_AUTH);
private static final String PROXY_TYPE = "ProxyType";
private static final String HOST = "host";
private static final String PORT = "port";
private static final String AUTH = "auth";
private static final String SOCKS_HOST = "socks.host";
private static final String SOCKS_PORT = "socks.port";
private static final String PROXY_USERNAME = "proxy.user";
private static final String PROXY_PASSWORD = "proxy.password";
private static final List<String> PROTOCOL_PROPERTIES = List.of(HOST, PORT, AUTH, SOCKS_HOST, SOCKS_HOST, SOCKS_PORT, PROXY_USERNAME,
PROXY_PASSWORD);
private static final List<String> MAIL_PROPERTIES = Stream.concat(
PROTOCOL_PROPERTIES.stream().map(p -> Arrays.asList("mail.smtp." + p, "mail.smtps." + p))
.flatMap(List::stream), Stream.of(MAIL_USER, MAIL_PASSWORD, MAIL_TRANSPORT_PROTOCOL, PROXY_TYPE)).collect(Collectors.toList());

private static final String PROVIDER_NAME = "destination";
private static final String DESTINATION_NAME = "MAIL_SERVER_DESTINATION_NAME";
Expand All @@ -50,15 +59,16 @@ public Properties getProperties() {
String destinationName = Configuration.get(DESTINATION_NAME);
Properties destinationProperties = CloudPlatformDestinationFacade.getDestination(destinationName).getProperties();
for (String key : MAIL_PROPERTIES) {
if(destinationProperties.containsKey(key)) {
if (destinationProperties.containsKey(key)) {
properties.put(key, destinationProperties.get(key));
}
}
} catch (DestinationAccessException e) {
logger.error("Cannot find destination for mail configuration. Please check if " + DESTINATION_NAME + " is set and the destination exists.");
logger.error(
"Cannot find destination for mail configuration. Please check if " + DESTINATION_NAME + " is set and the destination exists.");
}

if(properties.isEmpty()) {
if (properties.isEmpty()) {
logger.error("Destination object does not contain necessary mail settings!");
}
return properties;
Expand Down
2 changes: 1 addition & 1 deletion pom.xml
Original file line number Diff line number Diff line change
Expand Up @@ -253,7 +253,7 @@
<java.version>11</java.version>
<project.build.sourceEncoding>UTF-8</project.build.sourceEncoding>

<dirigible.version>6.2.18</dirigible.version>
<dirigible.version>6.2.19</dirigible.version>

<!-- Sonar Cloud -->
<sonar.organization>sap-1</sonar.organization>
Expand Down

0 comments on commit 311a6c4

Please sign in to comment.