[ACS-4460] integration tests fix

This commit is contained in:
kcichonczyk
2023-03-07 12:28:16 +01:00
parent 4c77021796
commit 807699beda
3 changed files with 77 additions and 5 deletions

View File

@@ -0,0 +1,69 @@
package org.alfresco.transform.base;
import org.apache.http.conn.ssl.SSLConnectionSocketFactory;
import org.apache.http.conn.ssl.TrustAllStrategy;
import org.apache.http.impl.client.CloseableHttpClient;
import org.apache.http.impl.client.HttpClients;
import org.apache.http.ssl.SSLContextBuilder;
import org.springframework.http.client.ClientHttpRequestFactory;
import org.springframework.http.client.HttpComponentsClientHttpRequestFactory;
import org.springframework.web.client.RestTemplate;
import javax.net.ssl.SSLContext;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.security.KeyManagementException;
import java.security.KeyStore;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.security.UnrecoverableKeyException;
import java.security.cert.CertificateException;
public class MtlsTestUtils {
public static boolean isMtlsEnabled()
{
return Boolean.parseBoolean(System.getProperty("mtls-enabled"));
}
public static CloseableHttpClient httpClientWithMtls() throws NoSuchAlgorithmException, KeyManagementException, UnrecoverableKeyException, KeyStoreException, IOException, CertificateException {
String keyStoreFile = System.getProperty("mtls-keystore-file");
String keyStoreType = System.getProperty("mtls-keystore-type");
char[] keyStorePassword = System.getProperty("mtls-keystore-password").toCharArray();
String trustStoreFile = System.getProperty("mtls-truststore-file");
String trustStoreType = System.getProperty("mtls-truststore-type");
char[] trustStorePassword = System.getProperty("mtls-truststore-password").toCharArray();
SSLContextBuilder sslContextBuilder = new SSLContextBuilder();
KeyStore keyStore = KeyStore.getInstance(keyStoreType);
try (InputStream keyStoreInputStream = new FileInputStream(keyStoreFile))
{
keyStore.load(keyStoreInputStream, keyStorePassword);
sslContextBuilder.loadKeyMaterial(keyStore, keyStorePassword);
}
KeyStore trustStore = KeyStore.getInstance(trustStoreType);
try (InputStream trustStoreInputStream = new FileInputStream(trustStoreFile))
{
trustStore.load(trustStoreInputStream, trustStorePassword);
sslContextBuilder.loadTrustMaterial(trustStore, TrustAllStrategy.INSTANCE);
}
SSLContext sslContext = sslContextBuilder.build();
SSLConnectionSocketFactory sslContextFactory = new SSLConnectionSocketFactory(sslContext);
return HttpClients.custom().setSSLSocketFactory(sslContextFactory).build();
}
public static RestTemplate restTemplateWithMtls()
{
ClientHttpRequestFactory requestFactory = null;
try {
requestFactory = new HttpComponentsClientHttpRequestFactory(httpClientWithMtls());
} catch (Exception e) {
e.printStackTrace();
}
return new RestTemplate(requestFactory);
}
}

View File

@@ -13,6 +13,7 @@ import static org.springframework.http.MediaType.MULTIPART_FORM_DATA;
import java.util.Map; import java.util.Map;
import org.alfresco.transform.base.MtlsTestUtils;
import org.springframework.core.io.ClassPathResource; import org.springframework.core.io.ClassPathResource;
import org.springframework.core.io.Resource; import org.springframework.core.io.Resource;
import org.springframework.http.HttpEntity; import org.springframework.http.HttpEntity;
@@ -27,8 +28,8 @@ import org.springframework.web.client.RestTemplate;
*/ */
public class HttpClient public class HttpClient
{ {
private static final RestTemplate REST_TEMPLATE = new RestTemplate(); private static final RestTemplate REST_TEMPLATE = MtlsTestUtils.isMtlsEnabled() ? MtlsTestUtils.restTemplateWithMtls() : new RestTemplate();
public static ResponseEntity<Resource> sendTRequest( public static ResponseEntity<Resource> sendTRequest(
final String engineUrl, final String sourceFile, final String engineUrl, final String sourceFile,
final String sourceMimetype, final String targetMimetype, final String targetExtension) final String sourceMimetype, final String targetMimetype, final String targetExtension)

View File

@@ -21,6 +21,7 @@ import java.nio.file.Files;
import java.nio.file.Paths; import java.nio.file.Paths;
import java.nio.file.StandardCopyOption; import java.nio.file.StandardCopyOption;
import org.alfresco.transform.base.MtlsTestUtils;
import org.apache.http.HttpEntity; import org.apache.http.HttpEntity;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.methods.HttpGet; import org.apache.http.client.methods.HttpGet;
@@ -56,7 +57,7 @@ public class SfsClient
((Logger) LoggerFactory.getLogger("org.apache.http.wire")).setAdditive(false); ((Logger) LoggerFactory.getLogger("org.apache.http.wire")).setAdditive(false);
} }
private static final String SFS_BASE_URL = "http://localhost:8099"; private static final String SFS_BASE_URL = MtlsTestUtils.isMtlsEnabled() ? "https://localhost:8099" : "http://localhost:8099";
public static String uploadFile(final String fileToUploadName) throws Exception public static String uploadFile(final String fileToUploadName) throws Exception
{ {
@@ -75,7 +76,7 @@ public class SfsClient
.addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY)) .addPart("file", new FileBody(file, ContentType.DEFAULT_BINARY))
.build()); .build());
try (CloseableHttpClient client = HttpClients.createDefault()) try (CloseableHttpClient client = MtlsTestUtils.isMtlsEnabled() ? MtlsTestUtils.httpClientWithMtls() : HttpClients.createDefault())
{ {
final HttpResponse response = client.execute(post); final HttpResponse response = client.execute(post);
int status = response.getStatusLine().getStatusCode(); int status = response.getStatusLine().getStatusCode();
@@ -134,7 +135,8 @@ public class SfsClient
sfsBaseUrl+"/alfresco/api/-default-/private/sfs/versions/1/file/{0}", sfsBaseUrl+"/alfresco/api/-default-/private/sfs/versions/1/file/{0}",
uuid)); uuid));
try (CloseableHttpClient client = HttpClients.createDefault()) try (CloseableHttpClient client = MtlsTestUtils.isMtlsEnabled() ?
MtlsTestUtils.httpClientWithMtls() : HttpClients.createDefault())
{ {
final HttpResponse response = client.execute(head); final HttpResponse response = client.execute(head);
final int status = response.getStatusLine().getStatusCode(); final int status = response.getStatusLine().getStatusCode();