diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java b/backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java new file mode 100644 index 0000000..1a35303 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/pojo/holdingIdentity.java @@ -0,0 +1,6 @@ +package djmil.cordacheckers.pojo; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public record holdingIdentity(String x500Name, String shortHash) { } diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java b/backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java new file mode 100644 index 0000000..207227a --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/pojo/virtualNodes.java @@ -0,0 +1,6 @@ +package djmil.cordacheckers.pojo; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public record virtualNodes(holdingIdentity holdingIdentity) { } diff --git a/backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java b/backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java new file mode 100644 index 0000000..9662886 --- /dev/null +++ b/backend/src/main/java/djmil/cordacheckers/pojo/virtualnode.java @@ -0,0 +1,8 @@ +package djmil.cordacheckers.pojo; + +import java.util.List; + +import com.fasterxml.jackson.annotation.JsonIgnoreProperties; + +@JsonIgnoreProperties(ignoreUnknown = true) +public record virtualnode(List virtualNodes) { } diff --git a/backend/src/main/resources/application.properties b/backend/src/main/resources/application.properties index 8b13789..585b43b 100644 --- a/backend/src/main/resources/application.properties +++ b/backend/src/main/resources/application.properties @@ -1 +1,3 @@ +trust.store=classpath:keystore/truststore.p12 +trust.store.password=test123 \ No newline at end of file diff --git a/backend/src/main/resources/keystore/truststore.p12 b/backend/src/main/resources/keystore/truststore.p12 new file mode 100644 index 0000000..b7d4ef4 Binary files /dev/null and b/backend/src/main/resources/keystore/truststore.p12 differ diff --git a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java index f434f23..ff59269 100644 --- a/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java +++ b/backend/src/test/java/djmil/cordacheckers/CordacheckersApplicationTests.java @@ -1,5 +1,6 @@ package djmil.cordacheckers; +import java.io.IOException; import java.security.GeneralSecurityException; import javax.net.ssl.SSLContext; @@ -14,9 +15,10 @@ import org.apache.hc.client5.http.ssl.SSLConnectionSocketFactory; import org.apache.hc.core5.http.config.Registry; import org.apache.hc.core5.http.config.RegistryBuilder; import org.apache.hc.core5.ssl.SSLContexts; -import org.apache.hc.core5.ssl.TrustStrategy; import org.junit.jupiter.api.Test; +import org.springframework.beans.factory.annotation.Value; import org.springframework.boot.test.context.SpringBootTest; +import org.springframework.core.io.Resource; import org.springframework.http.HttpEntity; import org.springframework.http.HttpHeaders; import org.springframework.http.HttpMethod; @@ -25,22 +27,29 @@ import org.springframework.http.ResponseEntity; import org.springframework.http.client.HttpComponentsClientHttpRequestFactory; import org.springframework.web.client.RestTemplate; +import djmil.cordacheckers.pojo.holdingIdentity; +import djmil.cordacheckers.pojo.virtualnode; + import static org.assertj.core.api.Assertions.assertThat; @SpringBootTest class CordacheckersApplicationTests { + @Value("${trust.store}") + private Resource trustStore; + + @Value("${trust.store.password}") + private String trustStorePassword; + @Test void contextLoads() { } @Test - void givenAcceptingAllCertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException { - - final TrustStrategy acceptingTrustStrategy = (cert, authType) -> true; + void givenAcceptOnlyCACertificates_whenHttpsUrlIsConsumed_thenOk() throws GeneralSecurityException, IOException { final SSLContext sslContext = SSLContexts.custom() - .loadTrustMaterial(null, acceptingTrustStrategy) + .loadTrustMaterial(trustStore.getURL(), trustStorePassword.toCharArray()) .build(); final SSLConnectionSocketFactory sslsf = new SSLConnectionSocketFactory(sslContext, NoopHostnameVerifier.INSTANCE); final Registry socketFactoryRegistry = RegistryBuilder. create() @@ -54,26 +63,34 @@ class CordacheckersApplicationTests { .setConnectionManager(connectionManager) .build(); - - // create headers - HttpHeaders headers = new HttpHeaders(); - headers.setBasicAuth("admin", "admin"); - - // String authStr = "username:password"; - // String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes()); - // headers.add("Authorization", "Basic " + base64Creds); - - // create request - final HttpEntity request = new HttpEntity<>(headers); - - final HttpComponentsClientHttpRequestFactory requestFactory = new HttpComponentsClientHttpRequestFactory(httpClient); - final ResponseEntity response = new RestTemplate(requestFactory) - .exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, String.class); + //requestFactory.setReadTimeout(readTimeout); + //requestFactory.setConnectTimeout(connectTimeout); + + + // Request authorization header + HttpHeaders headers = new HttpHeaders(); + headers.setBasicAuth("admin", "admin"); + + // String authStr = "username:password"; + // String base64Creds = Base64.getEncoder().encodeToString(authStr.getBytes()); + // headers.add("Authorization", "Basic " + base64Creds); + + // Request + final HttpEntity request = new HttpEntity<>(headers); + + final ResponseEntity response = new RestTemplate(requestFactory) + .exchange("https://localhost:8888/api/v1/virtualnode", HttpMethod.GET, request, virtualnode.class); assertThat(response.getStatusCode()).isEqualTo(HttpStatus.OK); + assertThat(response.hasBody()); - System.out.println("UNSWER: " + response.getBody()); + virtualnode vNode = response.getBody(); + assertThat(vNode != null); + assertThat(vNode.virtualNodes().size() == 5); + + holdingIdentity identity = vNode.virtualNodes().get(0).holdingIdentity(); + assertThat(identity.x500Name().contains("NotaryRep1")); } }