Search in sources :

Example 6 with TraceTokenManager

use of io.airlift.tracetoken.TraceTokenManager in project airlift by airlift.

the class TestTraceTokenRequestFilter method testBasic.

@Test
public void testBasic() {
    TraceTokenManager manager = new TraceTokenManager();
    manager.registerRequestToken("testBasic");
    TraceTokenRequestFilter filter = new TraceTokenRequestFilter(manager);
    Request original = prepareGet().setUri(URI.create("http://example.com")).build();
    Request filtered = filter.filterRequest(original);
    assertNotSame(filter, original);
    assertEquals(filtered.getUri(), original.getUri());
    assertEquals(original.getHeaders().size(), 0);
    assertEquals(filtered.getHeaders().size(), 1);
    assertEquals(filtered.getHeaders().get(TRACETOKEN_HEADER), ImmutableList.of("testBasic"));
}
Also used : TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Test(org.testng.annotations.Test)

Example 7 with TraceTokenManager

use of io.airlift.tracetoken.TraceTokenManager in project airlift by airlift.

the class TestDelimitedRequestLog method testWriteLog.

@Test
public void testWriteLog() throws Exception {
    Request request = mock(Request.class);
    Response response = mock(Response.class);
    Principal principal = mock(Principal.class);
    long timeToFirstByte = 456;
    long timeToLastByte = 3453;
    long now = System.currentTimeMillis();
    long timestamp = now - timeToLastByte;
    String user = "martin";
    String agent = "HttpClient 4.0";
    String referrer = "http://www.google.com";
    String ip = "4.4.4.4";
    String protocol = "protocol";
    String method = "GET";
    long requestSize = 5432;
    String requestContentType = "request/type";
    long responseSize = 32311;
    int responseCode = 200;
    String responseContentType = "response/type";
    HttpURI uri = new HttpURI("http://www.example.com/aaa+bbb/ccc?param=hello%20there&other=true");
    long beginToDispatchMillis = 333;
    long firstToLastContentTimeInMillis = 444;
    long beginToEndMillis = 555;
    DoubleSummaryStatistics stats = new DoubleSummaryStatistics();
    stats.accept(1);
    stats.accept(3);
    DoubleSummaryStats responseContentInterarrivalStats = new DoubleSummaryStats(stats);
    TraceTokenManager tokenManager = new TraceTokenManager();
    InMemoryEventClient eventClient = new InMemoryEventClient();
    MockCurrentTimeMillisProvider currentTimeMillisProvider = new MockCurrentTimeMillisProvider(timestamp + timeToLastByte);
    DelimitedRequestLog logger = new DelimitedRequestLog(file.getAbsolutePath(), 1, 256, Long.MAX_VALUE, tokenManager, eventClient, currentTimeMillisProvider, false);
    when(principal.getName()).thenReturn(user);
    when(request.getTimeStamp()).thenReturn(timestamp);
    when(request.getHeader("User-Agent")).thenReturn(agent);
    when(request.getHeader("Referer")).thenReturn(referrer);
    when(request.getRemoteAddr()).thenReturn("9.9.9.9");
    when(request.getHeaders("X-FORWARDED-FOR")).thenReturn(Collections.enumeration(ImmutableList.of("1.1.1.1, 2.2.2.2", "3.3.3.3, " + ip)));
    when(request.getProtocol()).thenReturn("unknown");
    when(request.getHeader("X-FORWARDED-PROTO")).thenReturn(protocol);
    when(request.getAttribute(TimingFilter.FIRST_BYTE_TIME)).thenReturn(timestamp + timeToFirstByte);
    when(request.getRequestURI()).thenReturn(uri.toString());
    when(request.getUserPrincipal()).thenReturn(principal);
    when(request.getMethod()).thenReturn(method);
    when(request.getContentRead()).thenReturn(requestSize);
    when(request.getHttpVersion()).thenReturn(HTTP_2);
    when(request.getHeader("Content-Type")).thenReturn(requestContentType);
    when(response.getStatus()).thenReturn(responseCode);
    when(response.getContentCount()).thenReturn(responseSize);
    when(response.getHeader("Content-Type")).thenReturn(responseContentType);
    tokenManager.createAndRegisterNewRequestToken();
    logger.log(request, response, beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, responseContentInterarrivalStats);
    logger.stop();
    List<Object> events = eventClient.getEvents();
    assertEquals(events.size(), 1);
    HttpRequestEvent event = (HttpRequestEvent) events.get(0);
    assertEquals(event.getTimeStamp().toEpochMilli(), timestamp);
    assertEquals(event.getClientAddress(), ip);
    assertEquals(event.getProtocol(), protocol);
    assertEquals(event.getMethod(), method);
    assertEquals(event.getRequestUri(), uri.toString());
    assertEquals(event.getUser(), user);
    assertEquals(event.getAgent(), agent);
    assertEquals(event.getReferrer(), referrer);
    assertEquals(event.getRequestSize(), requestSize);
    assertEquals(event.getRequestContentType(), requestContentType);
    assertEquals(event.getResponseSize(), responseSize);
    assertEquals(event.getResponseCode(), responseCode);
    assertEquals(event.getResponseContentType(), responseContentType);
    assertEquals(event.getTimeToFirstByte(), (Long) timeToFirstByte);
    assertEquals(event.getTimeToLastByte(), timeToLastByte);
    assertEquals(event.getTraceToken(), tokenManager.getCurrentRequestToken());
    assertEquals(event.getBeginToDispatchMillis(), beginToDispatchMillis);
    assertEquals(event.getFirstToLastContentTimeInMillis(), firstToLastContentTimeInMillis);
    assertEquals(event.getResponseContentInterarrivalStats(), responseContentInterarrivalStats);
    String actual = asCharSource(file, UTF_8).read();
    String expected = String.format("%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\t%s\n", ISO_FORMATTER.format(Instant.ofEpochMilli(timestamp)), ip, method, uri, user, agent, responseCode, requestSize, responseSize, event.getTimeToLastByte(), tokenManager.getCurrentRequestToken(), HTTP_2.toString(), beginToDispatchMillis, beginToEndMillis, firstToLastContentTimeInMillis, format("%.2f, %.2f, %.2f, %d", stats.getMin(), stats.getAverage(), stats.getMax(), stats.getCount()));
    assertEquals(actual, expected);
}
Also used : InMemoryEventClient(io.airlift.event.client.InMemoryEventClient) Request(org.eclipse.jetty.server.Request) DoubleSummaryStatistics(java.util.DoubleSummaryStatistics) HttpURI(org.eclipse.jetty.http.HttpURI) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) Response(org.eclipse.jetty.server.Response) Principal(java.security.Principal) Test(org.testng.annotations.Test)

Example 8 with TraceTokenManager

use of io.airlift.tracetoken.TraceTokenManager in project airlift by airlift.

the class TestJettyMultipleCerts method test.

@Test
public void test() throws Exception {
    HttpServlet servlet = new HttpServlet() {

        @Override
        protected void service(HttpServletRequest request, HttpServletResponse response) throws IOException {
            response.setStatus(200);
            response.getOutputStream().write((request.getServerName() + " OK").getBytes(UTF_8));
        }
    };
    HttpServerConfig config = new HttpServerConfig().setLogEnabled(false).setHttpEnabled(false).setHttpPort(0).setHttpsEnabled(true);
    HttpsConfig httpsConfig = new HttpsConfig().setHttpsPort(0).setKeystorePath(getResource("multiple-certs/server.p12").getPath()).setKeystorePassword("airlift");
    HashLoginServiceProvider loginServiceProvider = new HashLoginServiceProvider(config);
    NodeInfo nodeInfo = new NodeInfo(new NodeConfig().setEnvironment("test").setNodeInternalAddress("localhost"));
    HttpServerInfo httpServerInfo = new HttpServerInfo(config, Optional.of(httpsConfig), nodeInfo);
    HttpServerProvider serverProvider = new HttpServerProvider(httpServerInfo, nodeInfo, config, Optional.of(httpsConfig), servlet, ImmutableSet.of(new DummyFilter()), ImmutableSet.of(), ImmutableSet.of(), ClientCertificate.NONE, new RequestStats(), new NullEventClient(), Optional.empty());
    serverProvider.setTheAdminServlet(new DummyServlet());
    serverProvider.setLoginService(loginServiceProvider.get());
    serverProvider.setTokenManager(new TraceTokenManager());
    try (Closer closer = Closer.create()) {
        HttpServer server = serverProvider.get();
        closer.register(() -> {
            try {
                server.stop();
            } catch (Exception ignore) {
            }
        });
        server.start();
        JettyHttpClient client = new JettyHttpClient(new HttpClientConfig().setTrustStorePath(getResource("multiple-certs/server.p12").getPath()).setTrustStorePassword("airlift"));
        closer.register(client);
        int httpsPort = httpServerInfo.getHttpsUri().getPort();
        tryHost(client, HostAndPort.fromParts("localhost", httpsPort));
        for (String name : List.of("127.0.0.1", "::1")) {
            assertThatThrownBy(() -> tryHost(client, HostAndPort.fromParts(name, httpsPort))).hasMessageStartingWith(name + " Failed communicating with server").hasRootCauseMessage("No subject alternative names present");
        }
        // 
        for (String name : List.of("single1", "single2", "single3", "single4")) {
            if (doesDomainResolveToLocalhost(name)) {
                tryHost(client, HostAndPort.fromParts(name, httpsPort));
            }
        }
    }
}
Also used : Closer(com.google.common.io.Closer) HttpClientConfig(io.airlift.http.client.HttpClientConfig) NullEventClient(io.airlift.event.client.NullEventClient) HttpServlet(javax.servlet.http.HttpServlet) HttpServletResponse(javax.servlet.http.HttpServletResponse) TraceTokenManager(io.airlift.tracetoken.TraceTokenManager) IOException(java.io.IOException) UnknownHostException(java.net.UnknownHostException) HttpServletRequest(javax.servlet.http.HttpServletRequest) NodeInfo(io.airlift.node.NodeInfo) JettyHttpClient(io.airlift.http.client.jetty.JettyHttpClient) NodeConfig(io.airlift.node.NodeConfig) Test(org.testng.annotations.Test)

Aggregations

TraceTokenManager (io.airlift.tracetoken.TraceTokenManager)8 Test (org.testng.annotations.Test)5 NullEventClient (io.airlift.event.client.NullEventClient)4 InMemoryEventClient (io.airlift.event.client.InMemoryEventClient)2 DoubleSummaryStatistics (java.util.DoubleSummaryStatistics)2 Request (org.eclipse.jetty.server.Request)2 Response (org.eclipse.jetty.server.Response)2 Closer (com.google.common.io.Closer)1 HttpClientConfig (io.airlift.http.client.HttpClientConfig)1 JettyHttpClient (io.airlift.http.client.jetty.JettyHttpClient)1 NodeConfig (io.airlift.node.NodeConfig)1 NodeInfo (io.airlift.node.NodeInfo)1 IOException (java.io.IOException)1 UnknownHostException (java.net.UnknownHostException)1 Principal (java.security.Principal)1 HttpServlet (javax.servlet.http.HttpServlet)1 HttpServletRequest (javax.servlet.http.HttpServletRequest)1 HttpServletResponse (javax.servlet.http.HttpServletResponse)1 HttpURI (org.eclipse.jetty.http.HttpURI)1