diff --git a/pom.xml b/pom.xml index b85dfb3..3b8a47d 100644 --- a/pom.xml +++ b/pom.xml @@ -20,14 +20,9 @@ 8022 8023 ${server.port} ${management.port} - 2.12.5 - 2.5.3 - 1.3.2 - 2.3.1 - io.micrometer @@ -54,11 +49,11 @@ dev.vality.geck serializer - 0.0.1 dev.vality damsel + 1.566-03bbf48 @@ -145,23 +140,19 @@ javax.servlet javax.servlet-api - 4.0.1 javax.annotation javax.annotation-api - ${javax-annotation-api-version} javax.validation validation-api - 2.0.1.Final provided javax.xml.bind jaxb-api - ${jaxb-version} org.bouncycastle @@ -181,10 +172,16 @@ 0.9.1 test + + com.github.tomakehurst + wiremock-jre8-standalone + 2.33.2 + test + org.springframework.cloud spring-cloud-contract-wiremock - 3.0.3 + 3.1.1 test diff --git a/src/main/java/dev/vality/wachter/client/WachterClient.java b/src/main/java/dev/vality/wachter/client/WachterClient.java index 9e2c69c..6ec508e 100644 --- a/src/main/java/dev/vality/wachter/client/WachterClient.java +++ b/src/main/java/dev/vality/wachter/client/WachterClient.java @@ -2,6 +2,7 @@ package dev.vality.wachter.client; import dev.vality.wachter.config.properties.WachterProperties; import lombok.RequiredArgsConstructor; +import lombok.SneakyThrows; import org.apache.http.HttpResponse; import org.apache.http.client.HttpClient; import org.apache.http.client.methods.HttpPost; @@ -10,7 +11,6 @@ import org.apache.http.util.EntityUtils; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; import java.util.Enumeration; @Service @@ -19,9 +19,8 @@ public class WachterClient { private final HttpClient httpclient; - public byte[] send(HttpServletRequest request, - byte[] contentData, - WachterProperties.Services service) throws IOException { + @SneakyThrows + public byte[] send(HttpServletRequest request, byte[] contentData, WachterProperties.Service service) { HttpPost httppost = new HttpPost(service.getUrl()); setHeader(request, httppost); httppost.setEntity(new ByteArrayEntity(contentData)); diff --git a/src/main/java/dev/vality/wachter/config/ThriftGatewayConfiguration.java b/src/main/java/dev/vality/wachter/config/ThriftGatewayConfiguration.java index 8fd8b22..895d3c9 100644 --- a/src/main/java/dev/vality/wachter/config/ThriftGatewayConfiguration.java +++ b/src/main/java/dev/vality/wachter/config/ThriftGatewayConfiguration.java @@ -11,7 +11,7 @@ public class ThriftGatewayConfiguration { @Bean @ConditionalOnMissingBean(TProtocolFactory.class) - TProtocolFactory thriftProtocolFactory() { + public TProtocolFactory thriftProtocolFactory() { return new TBinaryProtocol.Factory(); } diff --git a/src/main/java/dev/vality/wachter/config/properties/WachterProperties.java b/src/main/java/dev/vality/wachter/config/properties/WachterProperties.java index fbbbff7..6361cb3 100644 --- a/src/main/java/dev/vality/wachter/config/properties/WachterProperties.java +++ b/src/main/java/dev/vality/wachter/config/properties/WachterProperties.java @@ -16,11 +16,11 @@ import java.util.Map; public class WachterProperties { private String serviceHeader; - private Map services; + private Map services; @Getter @Setter - public static class Services { + public static class Service { private String name; private String url; diff --git a/src/main/java/dev/vality/wachter/controller/WachterController.java b/src/main/java/dev/vality/wachter/controller/WachterController.java index 63aa163..e423415 100644 --- a/src/main/java/dev/vality/wachter/controller/WachterController.java +++ b/src/main/java/dev/vality/wachter/controller/WachterController.java @@ -2,13 +2,11 @@ package dev.vality.wachter.controller; import dev.vality.wachter.service.WachterService; import lombok.RequiredArgsConstructor; -import org.apache.thrift.TException; import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RestController; import javax.servlet.http.HttpServletRequest; -import java.io.IOException; @RestController @@ -19,7 +17,7 @@ public class WachterController { private final WachterService wachterService; @PostMapping("/wachter") - public byte[] getRequest(HttpServletRequest request) throws IOException, TException { + public byte[] getRequest(HttpServletRequest request) { return wachterService.process(request); } diff --git a/src/main/java/dev/vality/wachter/mapper/ServiceMapper.java b/src/main/java/dev/vality/wachter/mapper/ServiceMapper.java index 1757f31..1f5b8b6 100644 --- a/src/main/java/dev/vality/wachter/mapper/ServiceMapper.java +++ b/src/main/java/dev/vality/wachter/mapper/ServiceMapper.java @@ -13,12 +13,12 @@ public class ServiceMapper { private final WachterProperties wachterProperties; - public WachterProperties.Services getService(HttpServletRequest request) { + public WachterProperties.Service getService(HttpServletRequest request) { if (request.getHeader(wachterProperties.getServiceHeader()) == null) { throw new WachterException( String.format("Header \"%s\" must be set", wachterProperties.getServiceHeader())); } - WachterProperties.Services service = wachterProperties.getServices() + WachterProperties.Service service = wachterProperties.getServices() .get(request.getHeader(wachterProperties.getServiceHeader())); if (service == null) { diff --git a/src/main/java/dev/vality/wachter/security/AccessData.java b/src/main/java/dev/vality/wachter/security/AccessData.java index 4b0dd24..bda528e 100644 --- a/src/main/java/dev/vality/wachter/security/AccessData.java +++ b/src/main/java/dev/vality/wachter/security/AccessData.java @@ -14,6 +14,6 @@ public class AccessData { private final String tokenId; private final String userId; private final String userEmail; - private final WachterProperties.Services service; + private final WachterProperties.Service service; } diff --git a/src/main/java/dev/vality/wachter/security/BouncerContextFactory.java b/src/main/java/dev/vality/wachter/security/BouncerContextFactory.java index 7475b8a..6698615 100644 --- a/src/main/java/dev/vality/wachter/security/BouncerContextFactory.java +++ b/src/main/java/dev/vality/wachter/security/BouncerContextFactory.java @@ -64,5 +64,4 @@ public class BouncerContextFactory { .setDeployment(deployment) .setNow(Instant.now().toString()); } - } diff --git a/src/main/java/dev/vality/wachter/service/KeycloakService.java b/src/main/java/dev/vality/wachter/service/KeycloakService.java index e1def26..4c6a20e 100644 --- a/src/main/java/dev/vality/wachter/service/KeycloakService.java +++ b/src/main/java/dev/vality/wachter/service/KeycloakService.java @@ -9,14 +9,17 @@ import org.springframework.stereotype.Service; public class KeycloakService { public String getPartyId() { - return ((KeycloakPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName(); + return ((KeycloakPrincipal) SecurityContextHolder.getContext() + .getAuthentication() + .getPrincipal()) + .getName(); } public AccessToken getAccessToken() { - KeycloakPrincipal keycloakPrincipal = (KeycloakPrincipal) SecurityContextHolder.getContext() + return ((KeycloakPrincipal) SecurityContextHolder.getContext() .getAuthentication() - .getPrincipal(); - - return keycloakPrincipal.getKeycloakSecurityContext().getToken(); + .getPrincipal()) + .getKeycloakSecurityContext() + .getToken(); } } diff --git a/src/main/java/dev/vality/wachter/service/MethodNameReaderService.java b/src/main/java/dev/vality/wachter/service/MethodNameReaderService.java new file mode 100644 index 0000000..14b63d7 --- /dev/null +++ b/src/main/java/dev/vality/wachter/service/MethodNameReaderService.java @@ -0,0 +1,28 @@ +package dev.vality.wachter.service; + +import lombok.RequiredArgsConstructor; +import org.apache.thrift.TException; +import org.apache.thrift.protocol.TMessage; +import org.apache.thrift.protocol.TProtocol; +import org.apache.thrift.protocol.TProtocolFactory; +import org.apache.thrift.transport.TMemoryInputTransport; +import org.apache.thrift.transport.TTransportException; +import org.springframework.stereotype.Service; + +@Service +@RequiredArgsConstructor +public class MethodNameReaderService { + + private final TProtocolFactory thriftProtocolFactory; + + public String getMethodName(byte[] thriftBody) throws TException { + TProtocol protocol = createProtocol(thriftBody); + TMessage message = protocol.readMessageBegin(); + protocol.readMessageEnd(); + return message.name; + } + + private TProtocol createProtocol(byte[] thriftBody) throws TTransportException { + return thriftProtocolFactory.getProtocol(new TMemoryInputTransport(thriftBody)); + } +} diff --git a/src/main/java/dev/vality/wachter/service/WachterService.java b/src/main/java/dev/vality/wachter/service/WachterService.java index 4bdada1..ba3a122 100644 --- a/src/main/java/dev/vality/wachter/service/WachterService.java +++ b/src/main/java/dev/vality/wachter/service/WachterService.java @@ -1,19 +1,16 @@ package dev.vality.wachter.service; import dev.vality.wachter.client.WachterClient; +import dev.vality.wachter.mapper.ServiceMapper; import dev.vality.wachter.security.AccessData; import dev.vality.wachter.security.AccessService; -import dev.vality.wachter.mapper.ServiceMapper; import lombok.RequiredArgsConstructor; -import org.apache.thrift.TException; +import lombok.SneakyThrows; import org.apache.tomcat.util.http.fileupload.IOUtils; import org.springframework.stereotype.Service; import javax.servlet.http.HttpServletRequest; import java.io.ByteArrayOutputStream; -import java.io.IOException; - -import static dev.vality.wachter.utils.MethodNameReader.getMethodName; @RequiredArgsConstructor @Service @@ -23,11 +20,12 @@ public class WachterService { private final AccessService accessService; private final WachterClient wachterClient; private final ServiceMapper serviceMapper; + private final MethodNameReaderService methodNameReaderService; - - public byte[] process(HttpServletRequest request) throws IOException, TException { + @SneakyThrows + public byte[] process(HttpServletRequest request) { byte[] contentData = getContentData(request); - var methodName = getMethodName(contentData); + var methodName = methodNameReaderService.getMethodName(contentData); var partyID = keycloakService.getPartyId(); var token = keycloakService.getAccessToken(); var service = serviceMapper.getService(request); @@ -43,10 +41,10 @@ public class WachterService { return wachterClient.send(request, contentData, service); } - private byte[] getContentData(HttpServletRequest request) throws IOException { + @SneakyThrows + private byte[] getContentData(HttpServletRequest request) { ByteArrayOutputStream baos = new ByteArrayOutputStream(); IOUtils.copy(request.getInputStream(), baos); return baos.toByteArray(); } - } diff --git a/src/main/java/dev/vality/wachter/utils/MethodNameReader.java b/src/main/java/dev/vality/wachter/utils/MethodNameReader.java deleted file mode 100644 index c96371a..0000000 --- a/src/main/java/dev/vality/wachter/utils/MethodNameReader.java +++ /dev/null @@ -1,23 +0,0 @@ -package dev.vality.wachter.utils; - -import org.apache.thrift.TException; -import org.apache.thrift.protocol.TBinaryProtocol; -import org.apache.thrift.protocol.TMessage; -import org.apache.thrift.protocol.TProtocol; -import org.apache.thrift.transport.TMemoryInputTransport; -import org.apache.thrift.transport.TTransportException; - -public class MethodNameReader { - - public static String getMethodName(byte[] thriftBody) throws TException { - TProtocol protocol = createProtocol(thriftBody); - TMessage message = protocol.readMessageBegin(); - protocol.readMessageEnd(); - return message.name; - } - - private static TProtocol createProtocol(byte[] thriftBody) throws TTransportException { - return new TBinaryProtocol.Factory().getProtocol(new TMemoryInputTransport(thriftBody)); - } - -} diff --git a/src/test/java/dev/vality/wachter/util/MethodNameReaderTest.java b/src/test/java/dev/vality/wachter/util/MethodNameReaderTest.java index f731004..ad9e998 100644 --- a/src/test/java/dev/vality/wachter/util/MethodNameReaderTest.java +++ b/src/test/java/dev/vality/wachter/util/MethodNameReaderTest.java @@ -1,7 +1,7 @@ package dev.vality.wachter.util; +import dev.vality.wachter.service.MethodNameReaderService; import dev.vality.wachter.testutil.TMessageUtil; -import dev.vality.wachter.utils.MethodNameReader; import org.apache.thrift.TException; import org.apache.thrift.protocol.TProtocolFactory; import org.junit.jupiter.api.Test; @@ -16,10 +16,12 @@ class MethodNameReaderTest { @Autowired private TProtocolFactory protocolFactory; + @Autowired + private MethodNameReaderService methodNameReaderService; + @Test void readMethodName() throws TException { byte[] message = TMessageUtil.createTMessage(protocolFactory); - assertEquals("methodName", MethodNameReader.getMethodName(message)); + assertEquals("methodName", methodNameReaderService.getMethodName(message)); } - }