Commit c97a2788 authored by Georg Mittendorfer's avatar Georg Mittendorfer

Log user agent.

parent ef8a3fae
Pipeline #46910576 passed with stage
in 3 minutes and 23 seconds
......@@ -71,6 +71,10 @@ public abstract class IriCommand {
@JsonIgnore
private String ip;
@Null
@JsonIgnore
private String userAgent;
@NotBlank
@Size(max = 32)
private String command;
......
......@@ -90,9 +90,10 @@ public class IriApiHandler {
public Mono<ResponseEntity<String>> exchangeWithIri(@Valid @RequestBody IriCommand command, ServerHttpRequest request) {
String ip = session.getClientIp(request);
String sslId = session.getClientSslId(request);
command.setSessionId(StringUtils.isBlank(sslId) ? ip : sslId);
String sslId = StringUtils.abbreviate(session.getClientSslId(request), 64);
command.setSessionId(StringUtils.defaultIfBlank(sslId, ip));
command.setIp(ip);
command.setUserAgent(StringUtils.abbreviate(session.getUserAgent(request), 64));
Mono<ResponseEntity<String>> responseEntityMono = executeCommand(command)
.onErrorResume(retryPredicate(), throwable -> retry(throwable, command))
......@@ -161,14 +162,27 @@ public class IriApiHandler {
}
private void logCall(String nodeName, IriCommand command) {
commandLogger.info("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{}] #ID [{}]",
"REQ", command.getIp(), nodeName, command.getCommand(), command.toString(), command.getSessionId());
commandLogger.info("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{}] #ID [{}] #UA [{}]",
"REQ",
command.getIp(),
nodeName,
command.getCommand(),
command.toString(),
command.getSessionId(),
command.getUserAgent());
}
private void logResponseIfError(String nodeName, IriCommand command, ResponseEntity<String> responseEntity) {
if (responseEntity.getStatusCode().isError()) {
commandLogger.warn("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{} => {}] #ID [{}]",
"ERR", command.getIp(), nodeName, command.getCommand(), responseEntity.getStatusCode(), responseEntity.getBody(), command.getSessionId());
commandLogger.warn("#A [{}] #IP [{}] #N [{}] #C [{}] #D [{} => {}] #ID [{}] #UA [{}]",
"ERR",
command.getIp(),
nodeName,
command.getCommand(),
responseEntity.getStatusCode(),
responseEntity.getBody(),
command.getSessionId(),
command.getUserAgent());
}
}
......
......@@ -23,6 +23,7 @@ import org.apache.commons.lang3.StringUtils;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.core.env.Environment;
import org.springframework.http.HttpHeaders;
import org.springframework.http.server.reactive.ServerHttpRequest;
import org.springframework.http.server.reactive.SslInfo;
......@@ -75,6 +76,10 @@ public class ClientSessionExtractor {
return proxyEnabled ? getProxyClientSslSessionId(request) : sslId(request.getSslInfo());
}
public String getUserAgent(ServerHttpRequest request) {
return request.getHeaders().getFirst(HttpHeaders.USER_AGENT);
}
private String getProxyClientSslSessionId(ServerHttpRequest request) {
return remoteSslIdHeader != null && containsHeaderAndIsTrusted(request, remoteSslIdHeader)
? request.getHeaders().getFirst(remoteSslIdHeader)
......
......@@ -175,6 +175,19 @@ public class ClientSessionExtractorTest {
assertThat(session.getClientIp(serverRequest)).isEqualTo("bar");
}
@Test
public void whenGetUserAgentThenReturnUserAgentHeader() {
when(headers.getFirst(HttpHeaders.USER_AGENT)).thenReturn("foo");
ClientSessionExtractor session = new ClientSessionExtractor(env);
assertThat(session.getUserAgent(serverRequest)).isEqualTo("foo");
}
@Test
public void givenNoUserAgentWhenGetUserAgentThenReturnNull() {
ClientSessionExtractor session = new ClientSessionExtractor(env);
assertThat(session.getUserAgent(serverRequest)).isNull();
}
private void disableProxyMode() {
when(env.getProperty("piri.proxy.mode.enabled", Boolean.class, false)).thenReturn(false);
......
Markdown is supported
0% or
You are about to add 0 people to the discussion. Proceed with caution.
Finish editing this message first!
Please register or to comment