aboutsummaryrefslogtreecommitdiffstats
path: root/jdisc_http_service/src/main/java/com/yahoo/jdisc/http/server/jetty/AccessLogRequestLog.java
blob: 32790534f863b9e35920f48d8d7176f13560cef4 (plain) (blame)
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
// Copyright 2017 Yahoo Holdings. Licensed under the terms of the Apache 2.0 license. See LICENSE in the project root.
package com.yahoo.jdisc.http.server.jetty;

import com.google.common.base.Objects;
import com.yahoo.container.logging.AccessLog;
import com.yahoo.container.logging.AccessLogEntry;
import com.yahoo.jdisc.http.ServerConfig;
import com.yahoo.jdisc.http.servlet.ServletRequest;
import org.eclipse.jetty.server.Request;
import org.eclipse.jetty.server.RequestLog;
import org.eclipse.jetty.server.Response;
import org.eclipse.jetty.util.component.AbstractLifeCycle;

import javax.servlet.http.HttpServletRequest;
import java.security.Principal;
import java.security.cert.X509Certificate;
import java.util.List;
import java.util.Optional;
import java.util.OptionalInt;
import java.util.UUID;
import java.util.logging.Level;
import java.util.logging.Logger;

import static com.yahoo.jdisc.http.core.HttpServletRequestUtils.getConnectorLocalPort;

/**
 * This class is a bridge between Jetty's {@link org.eclipse.jetty.server.handler.RequestLogHandler}
 * and our own configurable access logging in different formats provided by {@link AccessLog}.
 *
 * @author Oyvind Bakksjo
 * @author bjorncs
 */
class AccessLogRequestLog extends AbstractLifeCycle implements RequestLog {

    private static final Logger logger = Logger.getLogger(AccessLogRequestLog.class.getName());

    // HTTP headers that are logged as extra key-value-pairs in access log entries
    private static final List<String> LOGGED_REQUEST_HEADERS = List.of("Vespa-Client-Version");

    private final AccessLog accessLog;
    private final List<String> remoteAddressHeaders;
    private final List<String> remotePortHeaders;

    AccessLogRequestLog(AccessLog accessLog, ServerConfig.AccessLog config) {
        this.accessLog = accessLog;
        this.remoteAddressHeaders = config.remoteAddressHeaders();
        this.remotePortHeaders = config.remotePortHeaders();
    }

    @Override
    public void log(Request request, Response response) {
        try {
            AccessLogEntry accessLogEntry = Optional.ofNullable(request.getAttribute(JDiscHttpServlet.ATTRIBUTE_NAME_ACCESS_LOG_ENTRY))
                    .map(AccessLogEntry.class::cast)
                    .orElseGet(AccessLogEntry::new);

            accessLogEntry.setRawPath(request.getRequestURI());
            String queryString = request.getQueryString();
            if (queryString != null) {
                accessLogEntry.setRawQuery(queryString);
            }

            accessLogEntry.setUserAgent(request.getHeader("User-Agent"));
            accessLogEntry.setHttpMethod(request.getMethod());
            accessLogEntry.setHostString(request.getHeader("Host"));
            accessLogEntry.setReferer(request.getHeader("Referer"));

            String peerAddress = request.getRemoteAddr();
            accessLogEntry.setIpV4Address(peerAddress);
            accessLogEntry.setPeerAddress(peerAddress);
            String remoteAddress = getRemoteAddress(request);
            if (!Objects.equal(remoteAddress, peerAddress)) {
                accessLogEntry.setRemoteAddress(remoteAddress);
            }

            int peerPort = request.getRemotePort();
            accessLogEntry.setPeerPort(peerPort);
            int remotePort = getRemotePort(request);
            if (remotePort != peerPort) {
                accessLogEntry.setRemotePort(remotePort);
            }
            accessLogEntry.setHttpVersion(request.getProtocol());
            accessLogEntry.setScheme(request.getScheme());
            accessLogEntry.setLocalPort(getConnectorLocalPort(request));
            Principal principal = (Principal) request.getAttribute(ServletRequest.JDISC_REQUEST_PRINCIPAL);
            if (principal != null) {
                accessLogEntry.setUserPrincipal(principal);
            }
            X509Certificate[] clientCert = (X509Certificate[]) request.getAttribute(ServletRequest.SERVLET_REQUEST_X509CERT);
            if (clientCert != null && clientCert.length > 0) {
                accessLogEntry.setSslPrincipal(clientCert[0].getSubjectX500Principal());
            }
            String sslSessionId = (String) request.getAttribute(ServletRequest.SERVLET_REQUEST_SSL_SESSION_ID);
            if (sslSessionId != null) {
                accessLogEntry.addKeyValue("ssl-session-id", sslSessionId);
            }
            String cipherSuite = (String) request.getAttribute(ServletRequest.SERVLET_REQUEST_CIPHER_SUITE);
            if (cipherSuite != null) {
                accessLogEntry.addKeyValue("cipher-suite", cipherSuite);
            }
            String requestFilterId = (String) request.getAttribute(ServletRequest.JDISC_REQUEST_CHAIN);
            if (requestFilterId != null) {
                accessLogEntry.addKeyValue("request-chain", requestFilterId);
            }
            String responseFilterId = (String) request.getAttribute(ServletRequest.JDISC_RESPONSE_CHAIN);
            if (responseFilterId != null) {
                accessLogEntry.addKeyValue("response-chain", responseFilterId);
            }

            long startTime = request.getTimeStamp();
            long endTime = System.currentTimeMillis();
            accessLogEntry.setTimeStamp(startTime);
            accessLogEntry.setDurationBetweenRequestResponse(endTime - startTime);
            accessLogEntry.setReturnedContentSize(response.getHttpChannel().getBytesWritten());
            accessLogEntry.setStatusCode(response.getCommittedMetaData().getStatus());

            LOGGED_REQUEST_HEADERS.forEach(header -> {
                String value = request.getHeader(header);
                if (value != null) {
                    accessLogEntry.addKeyValue(header, value);
                }
            });

            UUID connectionId = (UUID) request.getAttribute(JettyConnectionLogger.CONNECTION_ID_REQUEST_ATTRIBUTE);
            if (connectionId != null) {
                accessLogEntry.setConnectionId(connectionId.toString());
            }

            accessLog.log(accessLogEntry);
        } catch (Exception e) {
            // Catching any exceptions here as it is unclear how Jetty handles exceptions from a RequestLog.
            logger.log(Level.SEVERE, "Failed to log access log entry: " + e.getMessage(), e);
        }
    }

    private String getRemoteAddress(HttpServletRequest request) {
        for (String header : remoteAddressHeaders) {
            String value = request.getHeader(header);
            if (value != null) return value;
        }
        return request.getRemoteAddr();
    }

    private int getRemotePort(HttpServletRequest request) {
        for (String header : remotePortHeaders) {
            String value = request.getHeader(header);
            if (value != null) {
                OptionalInt maybePort = parsePort(value);
                if (maybePort.isPresent()) return maybePort.getAsInt();
            }
        }
        return request.getRemotePort();
    }

    private static OptionalInt parsePort(String port) {
        try {
            return OptionalInt.of(Integer.parseInt(port));
        } catch (IllegalArgumentException e) {
            return OptionalInt.empty();
        }
    }

}