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);
}
}
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);
}
}
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());
}
}
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());
}
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());
}
Aggregations