refactor properties, use config bean (#5)

This commit is contained in:
Anatoly Karlov 2022-07-01 14:27:51 +07:00 committed by GitHub
parent fc68854413
commit 11676b57e8
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
13 changed files with 67 additions and 66 deletions

19
pom.xml
View File

@ -20,14 +20,9 @@
<server.port>8022</server.port>
<management.port>8023</management.port>
<exposed.ports>${server.port} ${management.port}</exposed.ports>
<jackson-version>2.12.5</jackson-version>
<spring-version>2.5.3</spring-version>
<javax-annotation-api-version>1.3.2</javax-annotation-api-version>
<jaxb-version>2.3.1</jaxb-version>
</properties>
<dependencies>
<!--vality-->
<dependency>
<groupId>io.micrometer</groupId>
@ -54,11 +49,11 @@
<dependency>
<groupId>dev.vality.geck</groupId>
<artifactId>serializer</artifactId>
<version>0.0.1</version>
</dependency>
<dependency>
<groupId>dev.vality</groupId>
<artifactId>damsel</artifactId>
<version>1.566-03bbf48</version>
</dependency>
<!--spring-->
@ -145,23 +140,19 @@
<dependency>
<groupId>javax.servlet</groupId>
<artifactId>javax.servlet-api</artifactId>
<version>4.0.1</version>
</dependency>
<dependency>
<groupId>javax.annotation</groupId>
<artifactId>javax.annotation-api</artifactId>
<version>${javax-annotation-api-version}</version>
</dependency>
<dependency>
<groupId>javax.validation</groupId>
<artifactId>validation-api</artifactId>
<version>2.0.1.Final</version>
<scope>provided</scope>
</dependency>
<dependency>
<groupId>javax.xml.bind</groupId>
<artifactId>jaxb-api</artifactId>
<version>${jaxb-version}</version>
</dependency>
<dependency>
<groupId>org.bouncycastle</groupId>
@ -181,10 +172,16 @@
<version>0.9.1</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>com.github.tomakehurst</groupId>
<artifactId>wiremock-jre8-standalone</artifactId>
<version>2.33.2</version>
<scope>test</scope>
</dependency>
<dependency>
<groupId>org.springframework.cloud</groupId>
<artifactId>spring-cloud-contract-wiremock</artifactId>
<version>3.0.3</version>
<version>3.1.1</version>
<scope>test</scope>
</dependency>
</dependencies>

View File

@ -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));

View File

@ -11,7 +11,7 @@ public class ThriftGatewayConfiguration {
@Bean
@ConditionalOnMissingBean(TProtocolFactory.class)
TProtocolFactory thriftProtocolFactory() {
public TProtocolFactory thriftProtocolFactory() {
return new TBinaryProtocol.Factory();
}

View File

@ -16,11 +16,11 @@ import java.util.Map;
public class WachterProperties {
private String serviceHeader;
private Map<String, Services> services;
private Map<String, Service> services;
@Getter
@Setter
public static class Services {
public static class Service {
private String name;
private String url;

View File

@ -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);
}

View File

@ -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) {

View File

@ -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;
}

View File

@ -64,5 +64,4 @@ public class BouncerContextFactory {
.setDeployment(deployment)
.setNow(Instant.now().toString());
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -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();
}
}

View File

@ -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));
}
}

View File

@ -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));
}
}