Search in sources :

Example 1 with CredentialsChangeNotification

use of org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification in project hono by eclipse.

the class AbstractCredentialsManagementService method updateCredentials.

@Override
public final Future<OperationResult<Void>> updateCredentials(final String tenantId, final String deviceId, final List<CommonCredential> credentials, final Optional<String> resourceVersion, final Span span) {
    Objects.requireNonNull(tenantId);
    Objects.requireNonNull(deviceId);
    Objects.requireNonNull(credentials);
    Objects.requireNonNull(resourceVersion);
    Objects.requireNonNull(span);
    return this.tenantInformationService.getTenant(tenantId, span).compose(tenant -> tenant.checkCredentialsLimitExceeded(tenantId, credentials)).compose(ok -> verifyAndEncodePasswords(credentials)).compose(encodedCredentials -> processUpdateCredentials(DeviceKey.from(tenantId, deviceId), encodedCredentials, resourceVersion, span)).onSuccess(result -> NotificationEventBusSupport.sendNotification(vertx, new CredentialsChangeNotification(tenantId, deviceId, Instant.now()))).recover(t -> DeviceRegistryUtils.mapError(t, tenantId));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) HonoPasswordEncoder(org.eclipse.hono.auth.HonoPasswordEncoder) TenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.TenantInformationService) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) Strings(org.eclipse.hono.util.Strings) NoopTenantInformationService(org.eclipse.hono.deviceregistry.service.tenant.NoopTenantInformationService) Futures(org.eclipse.hono.util.Futures) Vertx(io.vertx.core.Vertx) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) Instant(java.time.Instant) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Optional(java.util.Optional) OperationResult(org.eclipse.hono.service.management.OperationResult) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) Collections(java.util.Collections) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification)

Example 2 with CredentialsChangeNotification

use of org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification in project hono by eclipse.

the class ProtonBasedCredentialsClientTest method testCredentialsChangeNotificationRemovesValueFromCache.

/**
 * Verifies that the client removes credentials of a device from the cache if it receives a notification about a
 * change in the credentials of that device.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testCredentialsChangeNotificationRemovesValueFromCache(final VertxTestContext ctx) {
    final String tenantId = "the-tenant-id";
    final String deviceId = "the-device-id";
    givenAClient(cache);
    final var notificationHandlerCaptor = getEventBusConsumerHandlerArgumentCaptor(CredentialsChangeNotification.TYPE);
    final Set<AnnotatedCacheKey<?>> expectedCacheRemovals = new HashSet<>();
    // GIVEN a client with a cache containing credentials of two tenants
    client.start().compose(v -> addResultToCache("other-tenant", deviceId, "other")).compose(v -> addResultToCache(tenantId, deviceId, "auth-id1")).map(expectedCacheRemovals::add).compose(v -> addResultToCache(tenantId, deviceId, "auth-id2")).map(expectedCacheRemovals::add).onComplete(ctx.succeeding(ok -> ctx.verify(() -> {
        // WHEN receiving a notification about a change on the credentials
        sendViaEventBusMock(new CredentialsChangeNotification(tenantId, deviceId, Instant.now()), notificationHandlerCaptor.getValue());
        // THEN the cache is invalidated for all credentials of the changed device and not for other devices
        ctx.verify(() -> verify(cache).invalidateAll(expectedCacheRemovals));
        ctx.completeNow();
    })));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) Tags(io.opentracing.tag.Tags) Timeout(io.vertx.junit5.Timeout) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.verifyNoMoreInteractions(org.mockito.Mockito.verifyNoMoreInteractions) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) NotificationType(org.eclipse.hono.notification.NotificationType) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsResult(org.eclipse.hono.util.CredentialsResult) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) Set(java.util.Set) ProtonQoS(io.vertx.proton.ProtonQoS) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) RequestResponseApiConstants(org.eclipse.hono.util.RequestResponseApiConstants) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Test(org.junit.jupiter.api.Test) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Span(io.opentracing.Span) ProtonSender(io.vertx.proton.ProtonSender) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) Mockito.mock(org.mockito.Mockito.mock) ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) AbstractNotification(org.eclipse.hono.notification.AbstractNotification) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.anyLong(org.mockito.ArgumentMatchers.anyLong) ProtonDelivery(io.vertx.proton.ProtonDelivery) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Cache(com.github.benmanes.caffeine.cache.Cache) Constants(org.eclipse.hono.util.Constants) ConcurrentMap(java.util.concurrent.ConcurrentMap) HashSet(java.util.HashSet) ArgumentCaptor(org.mockito.ArgumentCaptor) Message(org.apache.qpid.proton.message.Message) HonoConnection(org.eclipse.hono.client.HonoConnection) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Vertx(io.vertx.core.Vertx) Mockito.times(org.mockito.Mockito.times) ProtonHelper(io.vertx.proton.ProtonHelper) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Mockito.never(org.mockito.Mockito.never) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) SendMessageSampler(org.eclipse.hono.client.SendMessageSampler) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) AnnotatedCacheKey(org.eclipse.hono.client.util.AnnotatedCacheKey) HashSet(java.util.HashSet) Test(org.junit.jupiter.api.Test)

Example 3 with CredentialsChangeNotification

use of org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification in project hono by eclipse.

the class ProtonBasedNotificationReceiverTest method testThatCorrectHandlerIsInvoked.

/**
 * Verifies that the receiver decodes the notifications it receives and invokes the correct handler.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testThatCorrectHandlerIsInvoked(final VertxTestContext ctx) {
    final String tenantId = "my-tenant";
    final String deviceId = "my-device";
    final Instant creationTime = Instant.parse("2007-12-03T10:15:30Z");
    final TenantChangeNotification tenantChangeNotification = new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false);
    final Message tenantChangeNotificationMsg = ProtonHelper.message();
    MessageHelper.setJsonPayload(tenantChangeNotificationMsg, JsonObject.mapFrom(tenantChangeNotification));
    final DeviceChangeNotification deviceChangeNotification = new DeviceChangeNotification(LifecycleChange.CREATE, tenantId, deviceId, creationTime, false);
    final Message deviceChangeNotificationMsg = ProtonHelper.message();
    MessageHelper.setJsonPayload(deviceChangeNotificationMsg, JsonObject.mapFrom(deviceChangeNotification));
    final CredentialsChangeNotification credentialsChangeNotification = new CredentialsChangeNotification(tenantId, deviceId, creationTime);
    final Message credentialsChangeNotificationMsg = ProtonHelper.message();
    MessageHelper.setJsonPayload(credentialsChangeNotificationMsg, JsonObject.mapFrom(credentialsChangeNotification));
    final AllDevicesOfTenantDeletedNotification allDevicesOfTenantDeletedChangeNotification = new AllDevicesOfTenantDeletedNotification(tenantId, creationTime);
    final Message allDevicesOfTenantDeletedChangeNotificationMsg = ProtonHelper.message();
    MessageHelper.setJsonPayload(allDevicesOfTenantDeletedChangeNotificationMsg, JsonObject.mapFrom(allDevicesOfTenantDeletedChangeNotification));
    final Checkpoint handlerInvokedCheckpoint = ctx.checkpoint(4);
    client.registerConsumer(TenantChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(TenantChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    client.registerConsumer(DeviceChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(DeviceChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    client.registerConsumer(CredentialsChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(CredentialsChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    client.registerConsumer(AllDevicesOfTenantDeletedNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(AllDevicesOfTenantDeletedNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    // WHEN starting the client
    client.start().onComplete(ctx.succeeding(v -> ctx.verify(() -> {
        // THEN the receiver links got created
        final Map<String, ProtonMessageHandler> receiverMsgHandlersPerAddress = assertReceiverLinkCreated(connection);
        assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(TenantChangeNotification.TYPE));
        assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(DeviceChangeNotification.TYPE));
        assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(CredentialsChangeNotification.TYPE));
        assertThat(receiverMsgHandlersPerAddress).containsKey(NotificationAddressHelper.getAddress(AllDevicesOfTenantDeletedNotification.TYPE));
        final ProtonMessageHandler tenantChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(TenantChangeNotification.TYPE));
        final ProtonMessageHandler deviceChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(DeviceChangeNotification.TYPE));
        final ProtonMessageHandler credentialsChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(CredentialsChangeNotification.TYPE));
        final ProtonMessageHandler allDevicesOfTenantDeletedChangeReceiverMsgHandler = receiverMsgHandlersPerAddress.get(NotificationAddressHelper.getAddress(AllDevicesOfTenantDeletedNotification.TYPE));
        // and sending notifications on the links
        tenantChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), tenantChangeNotificationMsg);
        deviceChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), deviceChangeNotificationMsg);
        credentialsChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), credentialsChangeNotificationMsg);
        allDevicesOfTenantDeletedChangeReceiverMsgHandler.handle(mock(ProtonDelivery.class), allDevicesOfTenantDeletedChangeNotificationMsg);
    // causes the client to receive the notifications and the handlerInvokedCheckpoint to get flagged
    })));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ProtonReceiver(io.vertx.proton.ProtonReceiver) BeforeEach(org.junit.jupiter.api.BeforeEach) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ProtonDelivery(io.vertx.proton.ProtonDelivery) AmqpClientUnitTestHelper(org.eclipse.hono.client.amqp.test.AmqpClientUnitTestHelper) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) HashMap(java.util.HashMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Timeout(io.vertx.junit5.Timeout) ArgumentCaptor(org.mockito.ArgumentCaptor) EventBus(io.vertx.core.eventbus.EventBus) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Map(java.util.Map) Message(org.apache.qpid.proton.message.Message) TracingMockSupport(org.eclipse.hono.test.TracingMockSupport) JsonObject(io.vertx.core.json.JsonObject) HonoConnection(org.eclipse.hono.client.HonoConnection) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) RequestResponseClientConfigProperties(org.eclipse.hono.client.RequestResponseClientConfigProperties) Tracer(io.opentracing.Tracer) Mockito.atLeastOnce(org.mockito.Mockito.atLeastOnce) Vertx(io.vertx.core.Vertx) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) MessageHelper(org.eclipse.hono.util.MessageHelper) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) Mockito.verify(org.mockito.Mockito.verify) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) VertxMockSupport(org.eclipse.hono.test.VertxMockSupport) Checkpoint(io.vertx.junit5.Checkpoint) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) Mockito.mock(org.mockito.Mockito.mock) Checkpoint(io.vertx.junit5.Checkpoint) ProtonMessageHandler(io.vertx.proton.ProtonMessageHandler) Message(org.apache.qpid.proton.message.Message) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) ProtonDelivery(io.vertx.proton.ProtonDelivery) Instant(java.time.Instant) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) ArgumentMatchers.anyString(org.mockito.ArgumentMatchers.anyString) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Example 4 with CredentialsChangeNotification

use of org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification in project hono by eclipse.

the class KafkaBasedNotificationSenderTest method testProducerRecordForCredentialsNotification.

/**
 * Verifies that the expected Kafka record is created when publishing a {@link CredentialsChangeNotification}.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testProducerRecordForCredentialsNotification(final VertxTestContext ctx) {
    final CredentialsChangeNotification notification = new CredentialsChangeNotification(TENANT_ID, DEVICE_ID, CREATION_TIME);
    testProducerRecordForNotification(ctx, notification, DEVICE_ID);
}
Also used : CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) Test(org.junit.jupiter.api.Test)

Example 5 with CredentialsChangeNotification

use of org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification in project hono by eclipse.

the class KafkaBasedNotificationReceiverTest method testThatCorrectHandlerIsInvoked.

/**
 * Verifies that the receiver decodes the notifications it receives and invokes the correct handler.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testThatCorrectHandlerIsInvoked(final VertxTestContext ctx) {
    final String tenantId = "my-tenant";
    final String deviceId = "my-device";
    final Instant creationTime = Instant.parse("2007-12-03T10:15:30Z");
    mockConsumer.schedulePollTask(() -> {
        mockConsumer.addRecord(createKafkaRecord(new TenantChangeNotification(LifecycleChange.CREATE, tenantId, creationTime, false), 0L));
        mockConsumer.addRecord(createKafkaRecord(new DeviceChangeNotification(LifecycleChange.CREATE, tenantId, deviceId, creationTime, false), 0L));
        mockConsumer.addRecord(createKafkaRecord(new CredentialsChangeNotification(tenantId, deviceId, creationTime), 1L));
        mockConsumer.addRecord(createKafkaRecord(new AllDevicesOfTenantDeletedNotification(tenantId, creationTime), 2L));
    });
    final var receiver = createReceiver();
    final Checkpoint handlerInvokedCheckpoint = ctx.checkpoint(4);
    receiver.registerConsumer(TenantChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(TenantChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(DeviceChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(DeviceChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(CredentialsChangeNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(CredentialsChangeNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.registerConsumer(AllDevicesOfTenantDeletedNotification.TYPE, notification -> ctx.verify(() -> {
        assertThat(notification).isInstanceOf(AllDevicesOfTenantDeletedNotification.class);
        handlerInvokedCheckpoint.flag();
    }));
    receiver.start();
}
Also used : Checkpoint(io.vertx.junit5.Checkpoint) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) Instant(java.time.Instant) CredentialsChangeNotification(org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) Test(org.junit.jupiter.api.Test)

Aggregations

CredentialsChangeNotification (org.eclipse.hono.notification.deviceregistry.CredentialsChangeNotification)5 Instant (java.time.Instant)4 Test (org.junit.jupiter.api.Test)4 Future (io.vertx.core.Future)3 Vertx (io.vertx.core.Vertx)3 AllDevicesOfTenantDeletedNotification (org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification)3 DeviceChangeNotification (org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Span (io.opentracing.Span)2 Tracer (io.opentracing.Tracer)2 EventBus (io.vertx.core.eventbus.EventBus)2 JsonObject (io.vertx.core.json.JsonObject)2 Checkpoint (io.vertx.junit5.Checkpoint)2 Timeout (io.vertx.junit5.Timeout)2 VertxExtension (io.vertx.junit5.VertxExtension)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 ProtonDelivery (io.vertx.proton.ProtonDelivery)2 ProtonHelper (io.vertx.proton.ProtonHelper)2 ProtonMessageHandler (io.vertx.proton.ProtonMessageHandler)2 ProtonQoS (io.vertx.proton.ProtonQoS)2