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

View File

@ -2,6 +2,7 @@ package dev.vality.wachter.client;
import dev.vality.wachter.config.properties.WachterProperties; import dev.vality.wachter.config.properties.WachterProperties;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import lombok.SneakyThrows;
import org.apache.http.HttpResponse; import org.apache.http.HttpResponse;
import org.apache.http.client.HttpClient; import org.apache.http.client.HttpClient;
import org.apache.http.client.methods.HttpPost; import org.apache.http.client.methods.HttpPost;
@ -10,7 +11,6 @@ import org.apache.http.util.EntityUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
import java.util.Enumeration; import java.util.Enumeration;
@Service @Service
@ -19,9 +19,8 @@ public class WachterClient {
private final HttpClient httpclient; private final HttpClient httpclient;
public byte[] send(HttpServletRequest request, @SneakyThrows
byte[] contentData, public byte[] send(HttpServletRequest request, byte[] contentData, WachterProperties.Service service) {
WachterProperties.Services service) throws IOException {
HttpPost httppost = new HttpPost(service.getUrl()); HttpPost httppost = new HttpPost(service.getUrl());
setHeader(request, httppost); setHeader(request, httppost);
httppost.setEntity(new ByteArrayEntity(contentData)); httppost.setEntity(new ByteArrayEntity(contentData));

View File

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

View File

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

View File

@ -2,13 +2,11 @@ package dev.vality.wachter.controller;
import dev.vality.wachter.service.WachterService; import dev.vality.wachter.service.WachterService;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException;
import org.springframework.web.bind.annotation.PostMapping; import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping; import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController; import org.springframework.web.bind.annotation.RestController;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.IOException;
@RestController @RestController
@ -19,7 +17,7 @@ public class WachterController {
private final WachterService wachterService; private final WachterService wachterService;
@PostMapping("/wachter") @PostMapping("/wachter")
public byte[] getRequest(HttpServletRequest request) throws IOException, TException { public byte[] getRequest(HttpServletRequest request) {
return wachterService.process(request); return wachterService.process(request);
} }

View File

@ -13,12 +13,12 @@ public class ServiceMapper {
private final WachterProperties wachterProperties; private final WachterProperties wachterProperties;
public WachterProperties.Services getService(HttpServletRequest request) { public WachterProperties.Service getService(HttpServletRequest request) {
if (request.getHeader(wachterProperties.getServiceHeader()) == null) { if (request.getHeader(wachterProperties.getServiceHeader()) == null) {
throw new WachterException( throw new WachterException(
String.format("Header \"%s\" must be set", wachterProperties.getServiceHeader())); String.format("Header \"%s\" must be set", wachterProperties.getServiceHeader()));
} }
WachterProperties.Services service = wachterProperties.getServices() WachterProperties.Service service = wachterProperties.getServices()
.get(request.getHeader(wachterProperties.getServiceHeader())); .get(request.getHeader(wachterProperties.getServiceHeader()));
if (service == null) { if (service == null) {

View File

@ -14,6 +14,6 @@ public class AccessData {
private final String tokenId; private final String tokenId;
private final String userId; private final String userId;
private final String userEmail; 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) .setDeployment(deployment)
.setNow(Instant.now().toString()); .setNow(Instant.now().toString());
} }
} }

View File

@ -9,14 +9,17 @@ import org.springframework.stereotype.Service;
public class KeycloakService { public class KeycloakService {
public String getPartyId() { public String getPartyId() {
return ((KeycloakPrincipal) SecurityContextHolder.getContext().getAuthentication().getPrincipal()).getName(); return ((KeycloakPrincipal) SecurityContextHolder.getContext()
.getAuthentication()
.getPrincipal())
.getName();
} }
public AccessToken getAccessToken() { public AccessToken getAccessToken() {
KeycloakPrincipal keycloakPrincipal = (KeycloakPrincipal) SecurityContextHolder.getContext() return ((KeycloakPrincipal) SecurityContextHolder.getContext()
.getAuthentication() .getAuthentication()
.getPrincipal(); .getPrincipal())
.getKeycloakSecurityContext()
return keycloakPrincipal.getKeycloakSecurityContext().getToken(); .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; package dev.vality.wachter.service;
import dev.vality.wachter.client.WachterClient; import dev.vality.wachter.client.WachterClient;
import dev.vality.wachter.mapper.ServiceMapper;
import dev.vality.wachter.security.AccessData; import dev.vality.wachter.security.AccessData;
import dev.vality.wachter.security.AccessService; import dev.vality.wachter.security.AccessService;
import dev.vality.wachter.mapper.ServiceMapper;
import lombok.RequiredArgsConstructor; import lombok.RequiredArgsConstructor;
import org.apache.thrift.TException; import lombok.SneakyThrows;
import org.apache.tomcat.util.http.fileupload.IOUtils; import org.apache.tomcat.util.http.fileupload.IOUtils;
import org.springframework.stereotype.Service; import org.springframework.stereotype.Service;
import javax.servlet.http.HttpServletRequest; import javax.servlet.http.HttpServletRequest;
import java.io.ByteArrayOutputStream; import java.io.ByteArrayOutputStream;
import java.io.IOException;
import static dev.vality.wachter.utils.MethodNameReader.getMethodName;
@RequiredArgsConstructor @RequiredArgsConstructor
@Service @Service
@ -23,11 +20,12 @@ public class WachterService {
private final AccessService accessService; private final AccessService accessService;
private final WachterClient wachterClient; private final WachterClient wachterClient;
private final ServiceMapper serviceMapper; private final ServiceMapper serviceMapper;
private final MethodNameReaderService methodNameReaderService;
@SneakyThrows
public byte[] process(HttpServletRequest request) throws IOException, TException { public byte[] process(HttpServletRequest request) {
byte[] contentData = getContentData(request); byte[] contentData = getContentData(request);
var methodName = getMethodName(contentData); var methodName = methodNameReaderService.getMethodName(contentData);
var partyID = keycloakService.getPartyId(); var partyID = keycloakService.getPartyId();
var token = keycloakService.getAccessToken(); var token = keycloakService.getAccessToken();
var service = serviceMapper.getService(request); var service = serviceMapper.getService(request);
@ -43,10 +41,10 @@ public class WachterService {
return wachterClient.send(request, contentData, service); return wachterClient.send(request, contentData, service);
} }
private byte[] getContentData(HttpServletRequest request) throws IOException { @SneakyThrows
private byte[] getContentData(HttpServletRequest request) {
ByteArrayOutputStream baos = new ByteArrayOutputStream(); ByteArrayOutputStream baos = new ByteArrayOutputStream();
IOUtils.copy(request.getInputStream(), baos); IOUtils.copy(request.getInputStream(), baos);
return baos.toByteArray(); 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; package dev.vality.wachter.util;
import dev.vality.wachter.service.MethodNameReaderService;
import dev.vality.wachter.testutil.TMessageUtil; import dev.vality.wachter.testutil.TMessageUtil;
import dev.vality.wachter.utils.MethodNameReader;
import org.apache.thrift.TException; import org.apache.thrift.TException;
import org.apache.thrift.protocol.TProtocolFactory; import org.apache.thrift.protocol.TProtocolFactory;
import org.junit.jupiter.api.Test; import org.junit.jupiter.api.Test;
@ -16,10 +16,12 @@ class MethodNameReaderTest {
@Autowired @Autowired
private TProtocolFactory protocolFactory; private TProtocolFactory protocolFactory;
@Autowired
private MethodNameReaderService methodNameReaderService;
@Test @Test
void readMethodName() throws TException { void readMethodName() throws TException {
byte[] message = TMessageUtil.createTMessage(protocolFactory); byte[] message = TMessageUtil.createTMessage(protocolFactory);
assertEquals("methodName", MethodNameReader.getMethodName(message)); assertEquals("methodName", methodNameReaderService.getMethodName(message));
} }
} }