Skip to content

Commit

Permalink
WIP
Browse files Browse the repository at this point in the history
  • Loading branch information
jkunle committed Jan 27, 2025
1 parent 8019cbb commit 4f9db8d
Show file tree
Hide file tree
Showing 11 changed files with 145 additions and 6 deletions.
3 changes: 2 additions & 1 deletion build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -102,7 +102,8 @@ subprojects {

soap "com.sun.xml.messaging.saaj:saaj-impl:1.5.3",
"org.apache.cxf:cxf-rt-frontend-jaxws:3.4.5",
"org.apache.cxf:cxf-rt-transports-http:3.4.5"
"org.apache.cxf:cxf-rt-transports-http-hc:3.4.5",
"org.apache.httpcomponents:httpasyncclient:4.1.4"

powertools "software.amazon.lambda:powertools-logging:${dependencyVersions.powertools_version}",
"software.amazon.lambda:powertools-metrics:${dependencyVersions.powertools_version}",
Expand Down
2 changes: 2 additions & 0 deletions lib/build.gradle
Original file line number Diff line number Diff line change
Expand Up @@ -12,6 +12,8 @@ repositories {
}

dependencies {
implementation configurations.otel

implementation configurations.cri_common_lib,
configurations.aws,
configurations.dynamodb,
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,22 @@
package uk.gov.di.ipv.cri.kbv.api.security;

import org.apache.cxf.Bus;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory;

import java.net.http.HttpClient;

public class CXFConfigurator {
private final TelemetryHttpClientFactory telemetryHttpClientFactory;

public CXFConfigurator(TelemetryHttpClientFactory telemetryHttpClientFactory) {
this.telemetryHttpClientFactory = telemetryHttpClientFactory;
}

public void configure(Bus bus) {
HttpClient instrumentedHttpClient = telemetryHttpClientFactory.createHttpClient();

bus.setExtension(
new CustomAsyncHttpConduitFactory(instrumentedHttpClient, bus),
AsyncHTTPConduitFactory.class);
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,29 @@
package uk.gov.di.ipv.cri.kbv.api.security;

import org.apache.cxf.Bus;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduitFactory;
import org.apache.cxf.ws.addressing.EndpointReferenceType;

import java.io.IOException;
import java.net.http.HttpClient;

public class CustomAsyncHttpConduitFactory extends AsyncHTTPConduitFactory {
private final HttpClient httpClient;

public CustomAsyncHttpConduitFactory(HttpClient httpClient, Bus bus) {
super(bus);
this.httpClient = httpClient;
}

@Override
public AsyncHTTPConduit createConduit(
Bus bus, EndpointInfo endpointInfo, EndpointReferenceType target) throws IOException {
return new AsyncHTTPConduit(bus, endpointInfo, target, this) {
protected HttpClient getHttpClient() {
return httpClient;
}
};
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.experian.uk.schema.experian.identityiq.services.webservice.IdentityIQWebService;
import com.experian.uk.schema.experian.identityiq.services.webservice.IdentityIQWebServiceSoap;
import org.apache.cxf.BusFactory;
import uk.gov.di.ipv.cri.common.library.service.ConfigurationService;
import uk.gov.di.ipv.cri.kbv.api.exception.InvalidSoapTokenException;

Expand All @@ -13,18 +14,23 @@ public class KBVClientFactory {
private final HeaderHandlerResolver headerHandlerResolver;
private final IdentityIQWebService identityIQWebService;
private final ConfigurationService configurationService;
private final CXFConfigurator cxfConfigurator;

public KBVClientFactory(
IdentityIQWebService identityIQWebService,
HeaderHandlerResolver headerHandlerResolver,
ConfigurationService configurationService) {
ConfigurationService configurationService,
CXFConfigurator cxfConfigurator) {
this.identityIQWebService = identityIQWebService;
this.headerHandlerResolver = headerHandlerResolver;
this.configurationService = configurationService;
this.cxfConfigurator = cxfConfigurator;
}

public IdentityIQWebServiceSoap createClient() {
try {

cxfConfigurator.configure(BusFactory.getDefaultBus());
identityIQWebService.setHandlerResolver(headerHandlerResolver);

IdentityIQWebServiceSoap identityIQWebServiceSoap =
Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.experian.uk.wasp.TokenService;
import com.experian.uk.wasp.TokenServiceSoap;
import org.apache.cxf.BusFactory;
import org.apache.logging.log4j.LogManager;
import org.apache.logging.log4j.Logger;
import software.amazon.cloudwatchlogs.emf.model.Unit;
Expand All @@ -21,22 +22,26 @@ public class SoapToken {
private final boolean checkIp;
private final ConfigurationService configurationService;
private final MetricsService metricsService = new MetricsService(new EventProbe());
private final CXFConfigurator cxfConfigurator;

public SoapToken(
String application,
boolean checkIp,
TokenService tokenService,
ConfigurationService configurationService) {
ConfigurationService configurationService,
CXFConfigurator cxfConfigurator) {
this.application = application;
this.checkIp = checkIp;
this.tokenService = tokenService;
this.configurationService = configurationService;
this.cxfConfigurator = cxfConfigurator;
}

public String getToken() {
long startTime = System.nanoTime();

try {
cxfConfigurator.configure(BusFactory.getDefaultBus());
TokenServiceSoap tokenServiceSoap = tokenService.getTokenServiceSoap();

BindingProvider bindingProvider = (BindingProvider) tokenServiceSoap;
Expand Down
Original file line number Diff line number Diff line change
@@ -0,0 +1,15 @@
package uk.gov.di.ipv.cri.kbv.api.security;

import io.opentelemetry.api.GlobalOpenTelemetry;
import io.opentelemetry.instrumentation.httpclient.JavaHttpClientTelemetry;

import java.net.http.HttpClient;

public class TelemetryHttpClientFactory {
public HttpClient createHttpClient() {
return JavaHttpClientTelemetry.builder(GlobalOpenTelemetry.get())
.build()
.newHttpClient(
HttpClient.newBuilder().version(HttpClient.Version.HTTP_1_1).build());
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -16,10 +16,12 @@
import uk.gov.di.ipv.cri.kbv.api.gateway.KBVGateway;
import uk.gov.di.ipv.cri.kbv.api.gateway.KBVGatewayFactory;
import uk.gov.di.ipv.cri.kbv.api.gateway.KeyStoreLoader;
import uk.gov.di.ipv.cri.kbv.api.security.CXFConfigurator;
import uk.gov.di.ipv.cri.kbv.api.security.HeaderHandler;
import uk.gov.di.ipv.cri.kbv.api.security.HeaderHandlerResolver;
import uk.gov.di.ipv.cri.kbv.api.security.KBVClientFactory;
import uk.gov.di.ipv.cri.kbv.api.security.SoapToken;
import uk.gov.di.ipv.cri.kbv.api.security.TelemetryHttpClientFactory;

import java.time.Clock;

Expand Down Expand Up @@ -90,11 +92,20 @@ KBVGateway getKbvGateway(KeyStoreLoader keyStoreLoader, KBVClientFactory kbvClie

private KBVClientFactory getKbvClientFactory() {
TokenService tokenService = new TokenService();
SoapToken soapToken = new SoapToken(APPLICATION, true, tokenService, configurationService);
SoapToken soapToken =
new SoapToken(
APPLICATION,
true,
tokenService,
configurationService,
new CXFConfigurator(new TelemetryHttpClientFactory()));
HeaderHandler headerHandler = new HeaderHandler(soapToken);
HeaderHandlerResolver headerResolver = new HeaderHandlerResolver(headerHandler);

return new KBVClientFactory(
new IdentityIQWebService(), headerResolver, getConfigurationService());
new IdentityIQWebService(),
headerResolver,
getConfigurationService(),
new CXFConfigurator(new TelemetryHttpClientFactory()));
}
}
Original file line number Diff line number Diff line change
@@ -0,0 +1,33 @@
package uk.gov.di.ipv.cri.kbv.api.security;

import org.apache.cxf.Bus;
import org.apache.cxf.service.model.EndpointInfo;
import org.apache.cxf.transport.http.asyncclient.AsyncHTTPConduit;
import org.apache.cxf.ws.addressing.EndpointReferenceType;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.junit.jupiter.MockitoExtension;

import java.io.IOException;
import java.net.http.HttpClient;

import static org.junit.jupiter.api.Assertions.assertNotNull;
import static org.mockito.Mockito.mock;

@ExtendWith(MockitoExtension.class)
class CustomAsyncHttpConduitFactoryTest {
@Test
void shouldReturnAsyncHTTPConduitWithCustomHttpClient() throws IOException {
HttpClient mockHttpClient = mock(HttpClient.class);
Bus mockBus = mock(Bus.class);
EndpointInfo mockEndpointInfo = mock(EndpointInfo.class);
EndpointReferenceType mockTarget = mock(EndpointReferenceType.class);

CustomAsyncHttpConduitFactory factory =
new CustomAsyncHttpConduitFactory(mockHttpClient, mockBus);

AsyncHTTPConduit conduit = factory.createConduit(mockBus, mockEndpointInfo, mockTarget);

assertNotNull(conduit, "The AsyncHTTPConduit should not be null");
}
}
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.experian.uk.schema.experian.identityiq.services.webservice.IdentityIQWebService;
import com.experian.uk.schema.experian.identityiq.services.webservice.IdentityIQWebServiceSoap;
import org.apache.cxf.BusFactory;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
import org.mockito.InjectMocks;
Expand All @@ -16,6 +17,7 @@

import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -27,6 +29,7 @@ class KBVClientFactoryTest {
@Mock private HeaderHandlerResolver headerHandlerResolver;
@Mock private ConfigurationService configurationService;
@Mock private IdentityIQWebServiceSoap identityIQWebServiceSoap;
@Mock private CXFConfigurator cxfConfigurator;

@InjectMocks private KBVClientFactory kbvClientFactory;

Expand All @@ -42,11 +45,13 @@ void shouldCreateClientSuccessfully() {
when(configurationService.getSecretValue("experian/iiq-webservice"))
.thenReturn("http://test-endpoint");

doNothing().when(cxfConfigurator).configure(BusFactory.getDefaultBus());
IdentityIQWebServiceSoap result = kbvClientFactory.createClient();

verify(identityIQWebService).setHandlerResolver(headerHandlerResolver);
verify(identityIQWebService).getIdentityIQWebServiceSoap();
verify(configurationService).getSecretValue("experian/iiq-webservice");
verify(cxfConfigurator).configure(BusFactory.getDefaultBus());
assertEquals(identityIQWebServiceSoap, result);
}

Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -2,6 +2,7 @@

import com.experian.uk.wasp.TokenService;
import com.experian.uk.wasp.TokenServiceSoap;
import org.apache.cxf.BusFactory;
import org.junit.jupiter.api.BeforeEach;
import org.junit.jupiter.api.Test;
import org.junit.jupiter.api.extension.ExtendWith;
Expand All @@ -17,6 +18,7 @@
import static org.junit.jupiter.api.Assertions.assertEquals;
import static org.junit.jupiter.api.Assertions.assertThrows;
import static org.junit.jupiter.api.Assertions.assertTrue;
import static org.mockito.Mockito.doNothing;
import static org.mockito.Mockito.mock;
import static org.mockito.Mockito.verify;
import static org.mockito.Mockito.when;
Expand All @@ -27,6 +29,7 @@ class SoapTokenTest {
@Mock private TokenService tokenServiceMock;
@Mock private ConfigurationService configurationServiceMock;
@Mock private TokenServiceSoap tokenServiceSoapMock;
@Mock private CXFConfigurator cxfConfiguratorMock;
private SoapToken soapToken;
private final String application = "testApplication";
private Boolean checkIp = true;
Expand All @@ -39,11 +42,18 @@ void setUp() {
tokenServiceSoapMock =
mock(TokenServiceSoap.class, withSettings().extraInterfaces(BindingProvider.class));

soapToken = new SoapToken(application, checkIp, tokenServiceMock, configurationServiceMock);
soapToken =
new SoapToken(
application,
checkIp,
tokenServiceMock,
configurationServiceMock,
cxfConfiguratorMock);

when(configurationServiceMock.getSecretValue("experian/iiq-wasp-service"))
.thenReturn(endpointUrl);
when(tokenServiceMock.getTokenServiceSoap()).thenReturn(tokenServiceSoapMock);
doNothing().when(cxfConfiguratorMock).configure(BusFactory.getDefaultBus());
}

@Test
Expand Down

0 comments on commit 4f9db8d

Please sign in to comment.