Search in sources :

Example 11 with Metric

use of com.yahoo.athenz.common.metrics.Metric in project athenz by yahoo.

the class MetricsTest method testMetricInterface.

@Test
public void testMetricInterface() {
    Metric metric = new Metric() {

        @Override
        public void increment(String metric) {
        }

        @Override
        public void increment(String metric, String requestDomainName) {
        }

        @Override
        public void increment(String metric, String requestDomainName, int count) {
        }

        @Override
        public void increment(String metric, String requestDomainName, String principalDomainName, String httpMethod, int httpStatus, String apiName) {
        }

        @Override
        public Object startTiming(String metric, String requestDomainName) {
            return null;
        }

        @Override
        public void stopTiming(Object timerMetric) {
        }

        @Override
        public void stopTiming(Object timerMetric, String requestDomainName, String principalDomainName, String httpMethod, int httpStatus, String apiName) {
        }

        @Override
        public void flush() {
        }

        @Override
        public void quit() {
        }
    };
    metric.increment("metric1");
    metric.increment("metric1", "athenz", 3);
    metric.increment("metric1", "athenz", "sports");
    metric.increment("metric1", "athenz", "sports", 3);
    metric.increment("apiRquestsMetric", "athenz", "sports", "POST", 200, "caller");
    String[] attributes = new String[] { "tag1", "value1", "tag2", "value2", "tag3", "value3" };
    metric.increment("metric1", attributes);
    assertNull(metric.startTiming("metric1", "athenz", "sports"));
    assertNull(metric.startTiming("apiRquestsMetric", "athenz", "sports", "POST", "caller"));
    metric.stopTiming("metric1", "athenz", "sports");
    metric.stopTiming("apiRquestsMetric", "athenz", "sports", "POST", 200, "caller");
    metric.flush();
    metric.quit();
}
Also used : Metric(com.yahoo.athenz.common.metrics.Metric) Test(org.testng.annotations.Test)

Example 12 with Metric

use of com.yahoo.athenz.common.metrics.Metric in project athenz by yahoo.

the class JettyConnectionLoggerTest method testStartStopConnectionFailedHandshake.

@Test
public void testStartStopConnectionFailedHandshake() throws Exception {
    // First start 2 connections
    ConnectionLog connectionLog = Mockito.mock(ConnectionLog.class);
    Metric metric = Mockito.mock(Metric.class);
    JettyConnectionLogger jettyConnectionLogger = new JettyConnectionLogger(connectionLog, metric);
    MockedConnection failedMockedConnection = getMockConnection();
    SslConnection mockConnection2 = failedMockedConnection.sslConnection;
    // Now simulate handshake failure for mockConnection2
    SslHandshakeListener.Event event = Mockito.mock(SslHandshakeListener.Event.class);
    when(event.getSSLEngine()).thenReturn(failedMockedConnection.sslEngine);
    SSLHandshakeException sslHandshakeException = new SSLHandshakeException("no cipher suites in common");
    ArgumentCaptor<ConnectionLogEntry> connectionLogEntryArgumentCaptor = ArgumentCaptor.forClass(ConnectionLogEntry.class);
    ArgumentCaptor<String[]> metricArgumentCaptor = ArgumentCaptor.forClass(String[].class);
    when(mockConnection2.getEndPoint().isOpen()).thenReturn(false);
    jettyConnectionLogger.handshakeFailed(event, sslHandshakeException);
    Mockito.verify(connectionLog, Mockito.times(1)).log(connectionLogEntryArgumentCaptor.capture());
    Mockito.verify(metric, Mockito.times(1)).increment(Mockito.eq(CONNECTION_LOGGER_METRIC_DEFAULT_NAME), metricArgumentCaptor.capture());
    assertEquals("no cipher suites in common", connectionLogEntryArgumentCaptor.getValue().sslHandshakeFailureMessage().get());
    assertFalse(connectionLogEntryArgumentCaptor.getValue().sslHandshakeFailureCause().isPresent());
    List<String[]> allMetricValues = metricArgumentCaptor.getAllValues();
    assertEquals(2, allMetricValues.size());
    assertEquals("failureType", allMetricValues.get(0));
    assertEquals("INCOMPATIBLE_CLIENT_CIPHER_SUITES", allMetricValues.get(1));
}
Also used : SslHandshakeListener(org.eclipse.jetty.io.ssl.SslHandshakeListener) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) SslConnection(org.eclipse.jetty.io.ssl.SslConnection) Metric(com.yahoo.athenz.common.metrics.Metric) Test(org.testng.annotations.Test)

Example 13 with Metric

use of com.yahoo.athenz.common.metrics.Metric in project athenz by yahoo.

the class JettyConnectionLoggerTest method testFailedHandshakeInnerCause.

@Test
public void testFailedHandshakeInnerCause() throws Exception {
    // First start q connection
    ConnectionLog connectionLog = Mockito.mock(ConnectionLog.class);
    Metric metric = Mockito.mock(Metric.class);
    JettyConnectionLogger jettyConnectionLogger = new JettyConnectionLogger(connectionLog, metric);
    MockedConnection failedMockedConnection = getMockConnection();
    SslConnection mockConnection = failedMockedConnection.sslConnection;
    // Now simulate handshake failure for mockConnection
    SslHandshakeListener.Event event = Mockito.mock(SslHandshakeListener.Event.class);
    when(event.getSSLEngine()).thenReturn(failedMockedConnection.sslEngine);
    SSLHandshakeException sslHandshakeException = new SSLHandshakeException(GENERAL_SSL_ERROR);
    SSLHandshakeException innerCause1 = new SSLHandshakeException(GENERAL_SSL_ERROR);
    SSLHandshakeException innerCause2 = new SSLHandshakeException(GENERAL_SSL_ERROR);
    SSLHandshakeException innerCause3 = new SSLHandshakeException("Last cause (most specific reason)");
    innerCause2.initCause(innerCause3);
    innerCause1.initCause(innerCause2);
    sslHandshakeException.initCause(innerCause1);
    ArgumentCaptor<ConnectionLogEntry> connectionLogEntryArgumentCaptor = ArgumentCaptor.forClass(ConnectionLogEntry.class);
    when(mockConnection.getEndPoint().isOpen()).thenReturn(false);
    jettyConnectionLogger.handshakeFailed(event, sslHandshakeException);
    Mockito.verify(connectionLog, Mockito.times(1)).log(connectionLogEntryArgumentCaptor.capture());
    assertEquals(GENERAL_SSL_ERROR, connectionLogEntryArgumentCaptor.getValue().sslHandshakeFailureMessage().get());
    assertEquals("Last cause (most specific reason)", connectionLogEntryArgumentCaptor.getValue().sslHandshakeFailureCause().get());
}
Also used : SslConnection(org.eclipse.jetty.io.ssl.SslConnection) SslHandshakeListener(org.eclipse.jetty.io.ssl.SslHandshakeListener) Metric(com.yahoo.athenz.common.metrics.Metric) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Test(org.testng.annotations.Test)

Example 14 with Metric

use of com.yahoo.athenz.common.metrics.Metric in project athenz by yahoo.

the class JettyConnectionLoggerFactory method create.

public JettyConnectionLogger create() {
    ConnectionLog connectionLog = getSslConnectionLog();
    Metric metric = getMetric();
    return new JettyConnectionLogger(connectionLog, metric);
}
Also used : Metric(com.yahoo.athenz.common.metrics.Metric)

Example 15 with Metric

use of com.yahoo.athenz.common.metrics.Metric in project athenz by yahoo.

the class RsrcCtxWrapperTest method testAuthorize.

@Test
public void testAuthorize() {
    HttpServletRequest reqMock = Mockito.mock(HttpServletRequest.class);
    HttpServletResponse resMock = Mockito.mock(HttpServletResponse.class);
    AuthorityList authListMock = new AuthorityList();
    Authorizer authorizerMock = Mockito.mock(Authorizer.class);
    Authority authMock = Mockito.mock(Authority.class);
    Metric metricMock = Mockito.mock(Metric.class);
    Object timerMetricMock = Mockito.mock(Object.class);
    Principal prin = Mockito.mock(Principal.class);
    Mockito.when(authMock.getHeader()).thenReturn("testheader");
    Mockito.when(reqMock.getHeader("testheader")).thenReturn("testcred");
    Mockito.when(authMock.getCredSource()).thenReturn(com.yahoo.athenz.auth.Authority.CredSource.HEADER);
    Mockito.when(authMock.authenticate(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(prin);
    Mockito.when(reqMock.getRemoteAddr()).thenReturn("1.1.1.1");
    Mockito.when(reqMock.getMethod()).thenReturn("POST");
    authListMock.add(authMock);
    // force true access right
    Mockito.when(authorizerMock.access(Mockito.any(), Mockito.any(), Mockito.any(), Mockito.any())).thenReturn(true);
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
    wrapper.authorize("add-domain", "test", "test");
    // after authorize success, principal should be set
    assertEquals(wrapper.principal(), prin);
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) Authority(com.yahoo.athenz.auth.Authority) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) Authorizer(com.yahoo.athenz.auth.Authorizer) HttpServletResponse(javax.servlet.http.HttpServletResponse) Metric(com.yahoo.athenz.common.metrics.Metric) AuthorityList(com.yahoo.athenz.common.server.rest.Http.AuthorityList) SimplePrincipal(com.yahoo.athenz.auth.impl.SimplePrincipal) Principal(com.yahoo.athenz.auth.Principal) Test(org.testng.annotations.Test)

Aggregations

Metric (com.yahoo.athenz.common.metrics.Metric)21 Test (org.testng.annotations.Test)19 Authorizer (com.yahoo.athenz.auth.Authorizer)11 AuthorityList (com.yahoo.athenz.common.server.rest.Http.AuthorityList)11 HttpServletRequest (javax.servlet.http.HttpServletRequest)11 HttpServletResponse (javax.servlet.http.HttpServletResponse)11 Authority (com.yahoo.athenz.auth.Authority)7 PrincipalAuthority (com.yahoo.athenz.auth.impl.PrincipalAuthority)7 SimplePrincipal (com.yahoo.athenz.auth.impl.SimplePrincipal)6 Principal (com.yahoo.athenz.auth.Principal)4 SSLHandshakeException (javax.net.ssl.SSLHandshakeException)2 SslConnection (org.eclipse.jetty.io.ssl.SslConnection)2 SslHandshakeListener (org.eclipse.jetty.io.ssl.SslHandshakeListener)2 DomainChangeMessage (com.yahoo.athenz.common.messaging.DomainChangeMessage)1 MetricFactory (com.yahoo.athenz.common.metrics.MetricFactory)1 Notification (com.yahoo.athenz.common.server.notification.Notification)1 NotificationMetric (com.yahoo.athenz.common.server.notification.NotificationMetric)1 NotificationToMetricConverter (com.yahoo.athenz.common.server.notification.NotificationToMetricConverter)1