Search in sources :

Example 6 with Metric

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

the class RsrcCtxWrapperTest method testAuthorizeMtlsRestricted.

@Test
public void testAuthorizeMtlsRestricted() {
    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(prin.getMtlsRestricted()).thenReturn(true);
    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");
    try {
        wrapper.authorize("add-domain", "test", "test");
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getMessage(), "ResourceException (403): {code: 403, message: \"mTLS Restricted\"}");
        assertEquals(ex.getCode(), 403);
    }
}
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)

Example 7 with Metric

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

the class RsrcCtxWrapperTest method testRsrcCtxWrapperSimpleAssertionMtlsRestricted.

@Test
public void testRsrcCtxWrapperSimpleAssertionMtlsRestricted() {
    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(prin.getMtlsRestricted()).thenReturn(true);
    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);
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
    assertNotNull(wrapper.context());
    // default principal should be null
    assertNull(wrapper.principal());
    assertEquals(wrapper.request(), reqMock);
    assertEquals(wrapper.response(), resMock);
    assertEquals(wrapper.getApiName(), "apiname");
    assertEquals(wrapper.getHttpMethod(), "POST");
    try {
        wrapper.authenticate();
        fail();
    } catch (ResourceException ex) {
        assertEquals(ex.getMessage(), "ResourceException (401): {code: 401, message: \"certificate is mTLS restricted\"}");
        assertEquals(ex.getCode(), 401);
    }
}
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)

Example 8 with Metric

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

the class RsrcCtxWrapperTest method testAuthenticateException.

@Test
public void testAuthenticateException() {
    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);
    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())).thenThrow(new com.yahoo.athenz.common.server.rest.ResourceException(403));
    Mockito.when(reqMock.getRemoteAddr()).thenReturn("1.1.1.1");
    Mockito.when(reqMock.getMethod()).thenReturn("POST");
    authListMock.add(authMock);
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
    try {
        wrapper.authenticate();
    } catch (ResourceException ex) {
        assertEquals(403, ex.getCode());
    }
}
Also used : Authority(com.yahoo.athenz.auth.Authority) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthorityList(com.yahoo.athenz.common.server.rest.Http.AuthorityList) HttpServletRequest(javax.servlet.http.HttpServletRequest) Authorizer(com.yahoo.athenz.auth.Authorizer) Metric(com.yahoo.athenz.common.metrics.Metric) Test(org.testng.annotations.Test)

Example 9 with Metric

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

the class RsrcCtxWrapperTest method testLogPrincipalRoleName.

@Test
public void testLogPrincipalRoleName() {
    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);
    SimplePrincipal principal = (SimplePrincipal) SimplePrincipal.create("hockey", "kings", "v=S1,d=hockey;n=kings;s=sig", 0, new PrincipalAuthority());
    principal.setRolePrincipalName("athenz.role");
    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(principal);
    Mockito.when(reqMock.getRemoteAddr()).thenReturn("1.1.1.1");
    Mockito.when(reqMock.getMethod()).thenReturn("POST");
    authListMock.add(authMock);
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(reqMock, resMock, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
    wrapper.authenticate();
    assertEquals("athenz.role", wrapper.logPrincipal());
    assertEquals("hockey", wrapper.getPrincipalDomain());
}
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) PrincipalAuthority(com.yahoo.athenz.auth.impl.PrincipalAuthority) Test(org.testng.annotations.Test)

Example 10 with Metric

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

the class MetricNotificationServiceTest method testNotify.

@Test
public void testNotify() {
    Metric metric = Mockito.mock(Metric.class);
    MetricNotificationService metricNotificationService = new MetricNotificationService(metric);
    String[] attributesList1 = new String[] { "key1", "attribute11", "key2", "attribute12", "key3", "attribute13" };
    String[] attributesList2 = new String[] { "key1", "attribute21", "key2", "attribute22", "key3", "attribute23" };
    List<String[]> attributes = new ArrayList<>();
    attributes.add(attributesList1);
    attributes.add(attributesList2);
    NotificationToMetricConverter notificationToMetricConverter = Mockito.mock(NotificationToMetricConverter.class);
    Mockito.when(notificationToMetricConverter.getNotificationAsMetrics(Mockito.any(), Mockito.any())).thenReturn(new NotificationMetric(attributes));
    Notification notification = new Notification();
    notification.setNotificationToMetricConverter(notificationToMetricConverter);
    boolean notify = metricNotificationService.notify(notification);
    assertTrue(notify);
    ArgumentCaptor<String> captorMetric = ArgumentCaptor.forClass(String.class);
    ArgumentCaptor<String[]> captorAttributes = ArgumentCaptor.forClass(String[].class);
    Mockito.verify(metric, Mockito.times(2)).increment(captorMetric.capture(), captorAttributes.capture());
    assertEquals(2, captorMetric.getAllValues().size());
    assertEquals("athenz_notification", captorMetric.getAllValues().get(0));
    assertEquals("athenz_notification", captorMetric.getAllValues().get(1));
    // Mockito captures all varargs arguments in a single array
    assertEquals(12, captorAttributes.getAllValues().size());
    List<String> expectedAttributes = new ArrayList<String>(Arrays.asList("key1", "attribute11", "key2", "attribute12", "key3", "attribute13", "key1", "attribute21", "key2", "attribute22", "key3", "attribute23"));
    assertEquals(expectedAttributes, captorAttributes.getAllValues());
}
Also used : NotificationMetric(com.yahoo.athenz.common.server.notification.NotificationMetric) Notification(com.yahoo.athenz.common.server.notification.Notification) NotificationToMetricConverter(com.yahoo.athenz.common.server.notification.NotificationToMetricConverter) NotificationMetric(com.yahoo.athenz.common.server.notification.NotificationMetric) Metric(com.yahoo.athenz.common.metrics.Metric) 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