Search in sources :

Example 6 with DomainChangeMessage

use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.

the class MockAthenzPulsarClient method getPulsarClient.

protected PulsarClientImpl getPulsarClient(String serviceUrl, ClientConfigurationData config) throws PulsarClientException {
    try {
        CompletableFuture asyncProducerResult = null;
        if (serviceUrl == null || serviceUrl.isEmpty()) {
            asyncProducerResult = FutureUtil.failedFuture(new PulsarClientException.InvalidConfigurationException("Producer configuration undefined"));
        }
        PulsarClientImpl pulsarClient = Mockito.mock(PulsarClientImpl.class);
        Producer producer = Mockito.mock(Producer.class);
        Consumer consumer = Mockito.mock(Consumer.class);
        MessageMetadata meta = new MessageMetadata();
        DomainChangeMessage roleChange = new DomainChangeMessage().setDomainName("domain").setObjectType(DomainChangeMessage.ObjectType.ROLE).setApiName("putRole").setObjectName("role1");
        MessageImpl<byte[]> msg = MessageImpl.create(meta, ByteBuffer.wrap(new ObjectMapper().writeValueAsBytes(roleChange)), Schema.BYTES, null);
        Mockito.when(consumer.receive(1, TimeUnit.SECONDS)).thenReturn(msg);
        CompletableFuture finalAsyncProducerResult = asyncProducerResult;
        Mockito.when(pulsarClient.createProducerAsync(any(ProducerConfigurationData.class), any(Schema.class))).thenAnswer(invocation -> {
            if (serviceUrl != null) {
                return ((ProducerConfigurationData) invocation.getArgument(0)).getTopicName() == null ? finalAsyncProducerResult : CompletableFuture.completedFuture(producer);
            }
            return finalAsyncProducerResult;
        });
        Mockito.when(pulsarClient.subscribeAsync(any(ConsumerConfigurationData.class), any(Schema.class), any())).thenReturn(CompletableFuture.completedFuture(consumer));
        return pulsarClient;
    } catch (Exception e) {
        fail();
    }
    return null;
}
Also used : DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) Schema(org.apache.pulsar.client.api.Schema) PulsarClientException(org.apache.pulsar.client.api.PulsarClientException) ProducerConfigurationData(org.apache.pulsar.client.impl.conf.ProducerConfigurationData) CompletableFuture(java.util.concurrent.CompletableFuture) MessageMetadata(org.apache.pulsar.common.api.proto.MessageMetadata) Producer(org.apache.pulsar.client.api.Producer) Consumer(org.apache.pulsar.client.api.Consumer) ConsumerConfigurationData(org.apache.pulsar.client.impl.conf.ConsumerConfigurationData) PulsarClientImpl(org.apache.pulsar.client.impl.PulsarClientImpl) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper)

Example 7 with DomainChangeMessage

use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.

the class ZMSImplTest method testPublisherException.

@Test
public void testPublisherException() {
    ZMSImpl zmsImpl = zmsTestInitializer.zmsInit();
    String apiName = "postTopLevelDomain";
    ResourceContext mockContext = Mockito.mock(ResourceContext.class);
    when(mockContext.getApiName()).thenReturn(apiName);
    when(mockContext.getDomainChangeMessages()).thenReturn(Collections.singletonList(new DomainChangeMessage()));
    MockDomainChangePublisher mockDomainChangePublisher = new MockDomainChangePublisher("domainChanges");
    mockDomainChangePublisher.setThrowPublishExceptions(true);
    zmsImpl.domainChangePublishers = new ArrayList<>();
    zmsImpl.domainChangePublishers.add(mockDomainChangePublisher);
    // make sure no exceptions are thrown since we should catch and log them
    zmsImpl.publishChangeMessage(mockContext, 200);
}
Also used : DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) MockDomainChangePublisher(com.yahoo.athenz.common.messaging.MockDomainChangePublisher)

Example 8 with DomainChangeMessage

use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.

the class ZMSImplTest method testPublishEvent.

@Test
public void testPublishEvent() {
    System.setProperty(ZMS_PROP_DOMAIN_CHANGE_PUBLISHER_FACTORY_CLASS, "com.yahoo.athenz.common.messaging.MockDomainChangePublisherFactory");
    System.setProperty(ZMS_PROP_DOMAIN_CHANGE_TOPIC_NAMES, "topic1");
    ZMSImpl zmsImpl = zmsTestInitializer.zmsInit();
    assertNotNull(zmsImpl.domainChangePublishers);
    List<String> topicNames = zmsImpl.domainChangePublishers.stream().map(publisher -> ((MockDomainChangePublisher) publisher).getTopicName()).collect(Collectors.toList());
    assertThat(topicNames, containsInAnyOrder("topic1"));
    ResourceContext mockContext = Mockito.mock(ResourceContext.class);
    when(mockContext.getApiName()).thenReturn("apiName");
    when(mockContext.getDomainChangeMessages()).thenReturn(Collections.singletonList(new DomainChangeMessage().setDomainName("domainName").setObjectName("objectName").setObjectType(DOMAIN).setApiName("apiName").setPublished(Instant.now().toEpochMilli()).setMessageId(java.util.UUID.randomUUID().toString())));
    zmsImpl.publishChangeMessage(mockContext, 200);
    // verify publish messages
    MockDomainChangePublisher.Recorder evtRecorder = getEventRecorder(zmsImpl);
    ArgumentCaptor<DomainChangeMessage> evtArgumentCaptor = ArgumentCaptor.forClass(DomainChangeMessage.class);
    verify(evtRecorder, Mockito.times(1)).record(evtArgumentCaptor.capture());
    DomainChangeMessage actual = evtArgumentCaptor.getValue();
    assertEquals(actual.getDomainName(), "domainName");
    assertEquals(actual.getApiName(), "apiName");
    assertEquals(actual.getObjectType(), DOMAIN);
    assertEquals(actual.getObjectName(), "objectName");
    System.clearProperty(ZMS_PROP_DOMAIN_CHANGE_PUBLISHER_FACTORY_CLASS);
    System.clearProperty(ZMS_PROP_DOMAIN_CHANGE_TOPIC_NAMES);
}
Also used : CoreMatchers(org.hamcrest.CoreMatchers) ArgumentMatchers(org.mockito.ArgumentMatchers) AccessStatus(com.yahoo.athenz.zms.ZMSImpl.AccessStatus) MockStatusCheckerNoException(com.yahoo.athenz.zms.status.MockStatusCheckerNoException) JOSEException(com.nimbusds.jose.JOSEException) JWSObject(com.nimbusds.jose.JWSObject) DeserializationFeature(com.fasterxml.jackson.databind.DeserializationFeature) ZMSConsts(com.yahoo.athenz.zms.ZMSConsts) com.yahoo.athenz.auth.impl(com.yahoo.athenz.auth.impl) RSAPublicKey(java.security.interfaces.RSAPublicKey) RSASSAVerifier(com.nimbusds.jose.crypto.RSASSAVerifier) Base64URL(com.nimbusds.jose.util.Base64URL) DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) SignUtils(com.yahoo.athenz.common.utils.SignUtils) Struct(com.yahoo.rdl.Struct) ParseException(java.text.ParseException) DateFormat(java.text.DateFormat) DomainMetaStore(com.yahoo.athenz.common.server.metastore.DomainMetaStore) Crypto(com.yahoo.athenz.auth.util.Crypto) AthenzObject(com.yahoo.athenz.zms.ZMSImpl.AthenzObject) NotificationToEmailConverterCommon(com.yahoo.athenz.common.server.notification.NotificationToEmailConverterCommon) Instant(java.time.Instant) DefaultAuditLogMsgBuilder(com.yahoo.athenz.common.server.log.impl.DefaultAuditLogMsgBuilder) MemberDueDays(com.yahoo.athenz.zms.config.MemberDueDays) Collectors(java.util.stream.Collectors) StandardCharsets(java.nio.charset.StandardCharsets) Response(javax.ws.rs.core.Response) PrivateKey(java.security.PrivateKey) Matchers.containsInAnyOrder(org.hamcrest.Matchers.containsInAnyOrder) Metric(com.yahoo.athenz.common.metrics.Metric) AthenzDomain(com.yahoo.athenz.zms.store.AthenzDomain) Schema(com.yahoo.rdl.Schema) AuditLogger(com.yahoo.athenz.common.server.log.AuditLogger) java.util(java.util) AthenzUtils(com.yahoo.athenz.auth.util.AthenzUtils) org.testng.annotations(org.testng.annotations) SimpleDateFormat(java.text.SimpleDateFormat) ZMSUtils(com.yahoo.athenz.zms.utils.ZMSUtils) AuthzDetailsEntity(com.yahoo.athenz.common.config.AuthzDetailsEntity) AuthzDetailsField(com.yahoo.athenz.common.config.AuthzDetailsField) ObjectStoreConnection(com.yahoo.athenz.zms.store.ObjectStoreConnection) Strings(com.google.common.base.Strings) HttpServletRequest(javax.servlet.http.HttpServletRequest) ArgumentCaptor(org.mockito.ArgumentCaptor) Assert(org.testng.Assert) PutRoleMembershipNotificationTask(com.yahoo.athenz.zms.notification.PutRoleMembershipNotificationTask) DynamicConfigBoolean(com.yahoo.athenz.common.server.util.config.dynamic.DynamicConfigBoolean) ObjectType(com.yahoo.athenz.common.messaging.DomainChangeMessage.ObjectType) MatcherAssert.assertThat(org.hamcrest.MatcherAssert.assertThat) AuditLogMsgBuilder(com.yahoo.athenz.common.server.log.AuditLogMsgBuilder) MockStatusCheckerThrowException(com.yahoo.athenz.zms.status.MockStatusCheckerThrowException) JWSVerifier(com.nimbusds.jose.JWSVerifier) ResourceUtils(com.yahoo.athenz.common.server.util.ResourceUtils) ObjectMapper(com.fasterxml.jackson.databind.ObjectMapper) HttpServletResponse(javax.servlet.http.HttpServletResponse) FileOutputStream(java.io.FileOutputStream) JsonProcessingException(com.fasterxml.jackson.core.JsonProcessingException) IOException(java.io.IOException) Authority(com.yahoo.athenz.auth.Authority) PrincipalToken(com.yahoo.athenz.auth.token.PrincipalToken) EntityTag(javax.ws.rs.core.EntityTag) File(java.io.File) TimeUnit(java.util.concurrent.TimeUnit) Notification(com.yahoo.athenz.common.server.notification.Notification) Mockito(org.mockito.Mockito) Timestamp(com.yahoo.rdl.Timestamp) Principal(com.yahoo.athenz.auth.Principal) MockDomainChangePublisher(com.yahoo.athenz.common.messaging.MockDomainChangePublisher) ServerPrivateKey(com.yahoo.athenz.auth.ServerPrivateKey) AuthzHelper(com.yahoo.athenz.common.server.util.AuthzHelper) DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) MockDomainChangePublisher(com.yahoo.athenz.common.messaging.MockDomainChangePublisher)

Example 9 with DomainChangeMessage

use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.

the class RsrcCtxWrapperTest method testDomainChangeMessageDisabled.

@Test
public void testDomainChangeMessageDisabled() {
    HttpServletRequest servletRequest = new MockHttpServletRequest();
    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
    AuthorityList authListMock = new AuthorityList();
    Authorizer authorizerMock = Mockito.mock(Authorizer.class);
    Object timerMetric = new Object();
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(servletRequest, servletResponse, authListMock, false, authorizerMock, timerMetric, "apiName", false);
    assertNull(wrapper.getDomainChangeMessages());
    // add domain msg
    wrapper.addDomainChangeMessage(new DomainChangeMessage().setDomainName("domain1Name").setObjectName("domain1Name1").setObjectType(DOMAIN));
    // add role msg for the same domain
    wrapper.addDomainChangeMessage(new DomainChangeMessage().setDomainName("domain1Name").setObjectName("domain1role").setObjectType(ROLE));
    assertNull(wrapper.getDomainChangeMessages());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) Authorizer(com.yahoo.athenz.auth.Authorizer) HttpServletResponse(javax.servlet.http.HttpServletResponse) AuthorityList(com.yahoo.athenz.common.server.rest.Http.AuthorityList) Test(org.testng.annotations.Test)

Example 10 with DomainChangeMessage

use of com.yahoo.athenz.common.messaging.DomainChangeMessage in project athenz by yahoo.

the class RsrcCtxWrapperTest method testDomainChanges.

@Test
public void testDomainChanges() {
    HttpServletRequest servletRequest = new MockHttpServletRequest();
    HttpServletResponse servletResponse = Mockito.mock(HttpServletResponse.class);
    AuthorityList authListMock = new AuthorityList();
    Authorizer authorizerMock = Mockito.mock(Authorizer.class);
    Metric metricMock = Mockito.mock(Metric.class);
    Object timerMetricMock = Mockito.mock(Object.class);
    RsrcCtxWrapper wrapper = new RsrcCtxWrapper(servletRequest, servletResponse, authListMock, false, authorizerMock, metricMock, timerMetricMock, "apiName");
    wrapper.addDomainChangeMessage(new DomainChangeMessage());
    assertNull(wrapper.getDomainChangeMessages());
}
Also used : HttpServletRequest(javax.servlet.http.HttpServletRequest) DomainChangeMessage(com.yahoo.athenz.common.messaging.DomainChangeMessage) 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) Test(org.testng.annotations.Test)

Aggregations

DomainChangeMessage (com.yahoo.athenz.common.messaging.DomainChangeMessage)11 Test (org.testng.annotations.Test)7 HttpServletRequest (javax.servlet.http.HttpServletRequest)4 HttpServletResponse (javax.servlet.http.HttpServletResponse)4 ObjectMapper (com.fasterxml.jackson.databind.ObjectMapper)3 Authorizer (com.yahoo.athenz.auth.Authorizer)3 TlsConfig (com.yahoo.athenz.common.messaging.pulsar.client.AthenzPulsarClient.TlsConfig)3 AuthorityList (com.yahoo.athenz.common.server.rest.Http.AuthorityList)3 AuthzDetailsEntity (com.yahoo.athenz.common.config.AuthzDetailsEntity)2 MockDomainChangePublisher (com.yahoo.athenz.common.messaging.MockDomainChangePublisher)2 Metric (com.yahoo.athenz.common.metrics.Metric)2 JsonProcessingException (com.fasterxml.jackson.core.JsonProcessingException)1 DeserializationFeature (com.fasterxml.jackson.databind.DeserializationFeature)1 Strings (com.google.common.base.Strings)1 JOSEException (com.nimbusds.jose.JOSEException)1 JWSObject (com.nimbusds.jose.JWSObject)1 JWSVerifier (com.nimbusds.jose.JWSVerifier)1 RSASSAVerifier (com.nimbusds.jose.crypto.RSASSAVerifier)1 Base64URL (com.nimbusds.jose.util.Base64URL)1 Authority (com.yahoo.athenz.auth.Authority)1