Skip to content

Commit

Permalink
M2K Kfunc: create httpclient with sslcontext to accept selfsigned certs
Browse files Browse the repository at this point in the history
  • Loading branch information
gabriel-farache authored and masayag committed Mar 22, 2024
1 parent f70c237 commit a2f8cfd
Show file tree
Hide file tree
Showing 4 changed files with 51 additions and 13 deletions.
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,9 @@
import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Date;

@ApplicationScoped
Expand Down Expand Up @@ -58,6 +61,11 @@ public CloudEvent<EventGenerator.EventPOJO> saveTransformation(FunInput input) {
return EventGenerator.createErrorEvent(input.workflowCallerId, String.format("Cannot get transformation output of transformation %s" +
" in workspace %s for project %s for repo %s; error: %s",
input.transformId, input.workspaceId, input.projectId, input.gitRepo, e), SOURCE);
} catch (NoSuchAlgorithmException | KeyStoreException | KeyManagementException e) {
log.error("Error while creating httpclient to get transformation output", e);
return EventGenerator.createErrorEvent(input.workflowCallerId, String.format("Cannot create client to get transformation output of transformation %s" +
" in workspace %s for project %s for repo %s; error: %s",
input.transformId, input.workspaceId, input.projectId, input.gitRepo, e), SOURCE);
}

try {
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -4,8 +4,11 @@

import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;

public interface Move2KubeService {

public Path getTransformationOutput(String workspaceId, String projectId, String transformationId) throws IllegalArgumentException, IOException, ApiException;
public Path getTransformationOutput(String workspaceId, String projectId, String transformationId) throws IllegalArgumentException, IOException, ApiException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException;
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,8 +2,6 @@

import dev.parodos.move2kube.ApiClient;
import dev.parodos.move2kube.ApiException;
import dev.parodos.move2kube.api.PlanApi;
import dev.parodos.move2kube.api.ProjectInputsApi;
import dev.parodos.move2kube.api.ProjectOutputsApi;
import dev.parodos.move2kube.api.ProjectsApi;
import dev.parodos.move2kube.client.model.Project;
Expand All @@ -12,6 +10,14 @@
import jakarta.inject.Inject;
import org.apache.commons.compress.archivers.zip.ZipArchiveEntry;
import org.apache.commons.compress.archivers.zip.ZipFile;
import org.apache.hc.client5.http.impl.classic.CloseableHttpClient;
import org.apache.hc.client5.http.impl.classic.HttpClientBuilder;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManager;
import org.apache.hc.client5.http.impl.io.PoolingHttpClientConnectionManagerBuilder;
import org.apache.hc.client5.http.ssl.NoopHostnameVerifier;
import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactoryBuilder;
import org.apache.hc.core5.ssl.SSLContextBuilder;
import org.apache.hc.core5.ssl.TrustStrategy;
import org.eclipse.microprofile.config.inject.ConfigProperty;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
Expand All @@ -21,6 +27,9 @@
import java.io.FileOutputStream;
import java.io.IOException;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.Collections;

@ApplicationScoped
Expand All @@ -34,10 +43,11 @@ public class Move2KubeServiceImpl implements Move2KubeService {
String move2kubeApi;

@Override
public Path getTransformationOutput(String workspaceId, String projectId, String transformationId) throws IllegalArgumentException, IOException, ApiException {
public Path getTransformationOutput(String workspaceId, String projectId, String transformationId) throws IllegalArgumentException, IOException, ApiException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
Path outputPath = folderCreatorService.createMove2KubeTransformationFolder(String.format("move2kube-transform-%s", transformationId));
ApiClient client = new ApiClient();
ApiClient client = new ApiClient(createHttpClientAcceptingSelfSignedCerts());
client.setBasePath(move2kubeApi);

ProjectOutputsApi output = new ProjectOutputsApi(client);

waitForTransformationToBeDone(workspaceId, projectId, transformationId, client);
Expand Down Expand Up @@ -94,4 +104,18 @@ public static void extractZipFile(File zipFile, Path extractPath) throws IOExcep
}
}
}

private static CloseableHttpClient createHttpClientAcceptingSelfSignedCerts() throws NoSuchAlgorithmException, KeyManagementException, KeyStoreException {
PoolingHttpClientConnectionManager connectionManager = PoolingHttpClientConnectionManagerBuilder.create()
.setSSLSocketFactory(SSLConnectionSocketFactoryBuilder.create()
.setSslContext(new SSLContextBuilder().loadTrustMaterial(null, (TrustStrategy) (x509Certificates, s) -> true).build()
)
.setHostnameVerifier(NoopHostnameVerifier.INSTANCE)
.build())
.build();
return HttpClientBuilder
.create()
.setConnectionManager(connectionManager)
.build();
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -26,6 +26,9 @@
import java.net.URL;
import java.nio.file.Files;
import java.nio.file.Path;
import java.security.KeyManagementException;
import java.security.KeyStoreException;
import java.security.NoSuchAlgorithmException;
import java.util.UUID;

import static org.hamcrest.Matchers.containsString;
Expand Down Expand Up @@ -94,7 +97,7 @@ public void tearDown() throws IOException {
}

@Test
public void testSaveTransformationIsWorking() throws GitAPIException, IOException, ApiException, URISyntaxException {
public void testSaveTransformationIsWorking() throws GitAPIException, IOException, ApiException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -144,7 +147,7 @@ public void testSaveTransformationIsWorking() throws GitAPIException, IOExceptio
}

@Test
public void testSaveTransformationIsFailingWhenRetrievingTransformationOutput() throws IOException, ApiException, GitAPIException {
public void testSaveTransformationIsFailingWhenRetrievingTransformationOutput() throws IOException, ApiException, GitAPIException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -185,7 +188,7 @@ public void testSaveTransformationIsFailingWhenRetrievingTransformationOutput()
}

@Test
public void testSaveTransformationGitCloneFail() throws GitAPIException, IOException, ApiException {
public void testSaveTransformationGitCloneFail() throws GitAPIException, IOException, ApiException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -227,7 +230,7 @@ public void testSaveTransformationGitCloneFail() throws GitAPIException, IOExcep
}

@Test
public void testSaveTransformationBranchExists() throws GitAPIException, IOException, ApiException, URISyntaxException {
public void testSaveTransformationBranchExists() throws GitAPIException, IOException, ApiException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -270,7 +273,7 @@ public void testSaveTransformationBranchExists() throws GitAPIException, IOExcep
}

@Test
public void testSaveTransformationCreateBranchFail() throws GitAPIException, IOException, ApiException, URISyntaxException {
public void testSaveTransformationCreateBranchFail() throws GitAPIException, IOException, ApiException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -314,7 +317,7 @@ public void testSaveTransformationCreateBranchFail() throws GitAPIException, IOE
}

@Test
public void testSaveTransformationCommitChangesFail() throws GitAPIException, IOException, ApiException, URISyntaxException {
public void testSaveTransformationCommitChangesFail() throws GitAPIException, IOException, ApiException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -361,7 +364,7 @@ public void testSaveTransformationCommitChangesFail() throws GitAPIException, IO
}

@Test
public void testSaveTransformationGitPushFails() throws GitAPIException, IOException, ApiException, URISyntaxException {
public void testSaveTransformationGitPushFails() throws GitAPIException, IOException, ApiException, URISyntaxException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
UUID transformId = UUID.randomUUID();
transformOutputPath = Files.createTempDirectory(String.format("move2kube-transform-TEST-%s", transformId));
Expand Down Expand Up @@ -408,7 +411,7 @@ public void testSaveTransformationGitPushFails() throws GitAPIException, IOExcep
}

@Test
public void testSaveTransformationMissingInput() throws GitAPIException, IOException, ApiException {
public void testSaveTransformationMissingInput() throws GitAPIException, IOException, ApiException, NoSuchAlgorithmException, KeyStoreException, KeyManagementException {
UUID workflowCallerId = UUID.randomUUID();
RestAssured.given().contentType("application/json")
.header("ce-specversion", "1.0")
Expand Down

0 comments on commit a2f8cfd

Please sign in to comment.