Search in sources :

Example 1 with AuthHandler

use of org.eclipse.hono.adapter.auth.device.AuthHandler in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testUnauthenticatedConnectionMetrics.

/**
 * Verifies the connection metrics for unauthenticated connections.
 * <p>
 * This test should check if the metrics receive a call to increment and decrement when a connection is being
 * established and then closed.
 */
@Test
public void testUnauthenticatedConnectionMetrics() {
    properties.setAuthenticationRequired(false);
    givenAnAdapter(properties);
    final MqttEndpoint endpoint = mockEndpoint();
    adapter.handleEndpointConnection(endpoint);
    // THEN the adapter does not try to authenticate the device
    verify(authHandler, never()).authenticateDevice(any(MqttConnectContext.class));
    // and increments the number of unauthenticated connections
    verify(metrics).incrementUnauthenticatedConnections();
    // and when the device closes the connection
    final ArgumentCaptor<Handler<Void>> closeHandlerCaptor = VertxMockSupport.argumentCaptorHandler();
    verify(endpoint, times(2)).closeHandler(closeHandlerCaptor.capture());
    closeHandlerCaptor.getValue().handle(null);
    // the number of unauthenticated connections is decremented again
    verify(metrics).decrementUnauthenticatedConnections();
}
Also used : MqttEndpoint(io.vertx.mqtt.MqttEndpoint) AuthHandler(org.eclipse.hono.adapter.auth.device.AuthHandler) Handler(io.vertx.core.Handler) Test(org.junit.jupiter.api.Test)

Example 2 with AuthHandler

use of org.eclipse.hono.adapter.auth.device.AuthHandler in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapter method doStart.

@Override
protected final void doStart(final Promise<Void> startPromise) {
    registerDeviceAndTenantChangeNotificationConsumers();
    log.info("limiting size of inbound message payload to {} bytes", getConfig().getMaxPayloadSize());
    if (!getConfig().isAuthenticationRequired()) {
        log.warn("authentication of devices turned off");
    }
    final ConnectionLimitManager connectionLimitManager = Optional.ofNullable(getConnectionLimitManager()).orElseGet(this::createConnectionLimitManager);
    setConnectionLimitManager(connectionLimitManager);
    checkPortConfiguration().compose(ok -> CompositeFuture.all(bindSecureMqttServer(), bindInsecureMqttServer())).compose(ok -> {
        if (authHandler == null) {
            authHandler = createAuthHandler();
        }
        return Future.succeededFuture((Void) null);
    }).onComplete(startPromise);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) LifecycleChange(org.eclipse.hono.notification.deviceregistry.LifecycleChange) ZonedDateTime(java.time.ZonedDateTime) DeviceChangeNotification(org.eclipse.hono.notification.deviceregistry.DeviceChangeNotification) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) Tags(io.opentracing.tag.Tags) ProcessingOutcome(org.eclipse.hono.service.metric.MetricsTags.ProcessingOutcome) EndpointType(org.eclipse.hono.service.metric.MetricsTags.EndpointType) DeviceCredentials(org.eclipse.hono.adapter.auth.device.DeviceCredentials) Map(java.util.Map) Pair(org.eclipse.hono.util.Pair) AuthorizationException(org.eclipse.hono.adapter.AuthorizationException) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Fields(io.opentracing.log.Fields) JsonObject(io.vertx.core.json.JsonObject) MqttConnectionException(io.vertx.mqtt.MqttConnectionException) ZoneOffset(java.time.ZoneOffset) TracingHelper(org.eclipse.hono.tracing.TracingHelper) AllDevicesOfTenantDeletedNotification(org.eclipse.hono.notification.deviceregistry.AllDevicesOfTenantDeletedNotification) AuthHandler(org.eclipse.hono.adapter.auth.device.AuthHandler) Futures(org.eclipse.hono.util.Futures) TenantServiceBasedX509Authentication(org.eclipse.hono.adapter.auth.device.TenantServiceBasedX509Authentication) Predicate(java.util.function.Predicate) ConcurrentHashMap(java.util.concurrent.ConcurrentHashMap) CommandContext(org.eclipse.hono.client.command.CommandContext) MetricsTags(org.eclipse.hono.service.metric.MetricsTags) Set(java.util.Set) RegistrationAssertion(org.eclipse.hono.util.RegistrationAssertion) MessageHelper(org.eclipse.hono.util.MessageHelper) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) Objects(java.util.Objects) List(java.util.List) TenantTraceSamplingHelper(org.eclipse.hono.tracing.TenantTraceSamplingHelper) Buffer(io.vertx.core.buffer.Buffer) CommandConsumer(org.eclipse.hono.client.command.CommandConsumer) Optional(java.util.Optional) Span(io.opentracing.Span) NotificationEventBusSupport(org.eclipse.hono.notification.NotificationEventBusSupport) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) MqttConnectReturnCode(io.netty.handler.codec.mqtt.MqttConnectReturnCode) ConnectionLimitManager(org.eclipse.hono.adapter.limiting.ConnectionLimitManager) Command(org.eclipse.hono.client.command.Command) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) HashMap(java.util.HashMap) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Deque(java.util.Deque) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) OptionalInt(java.util.OptionalInt) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) MqttServer(io.vertx.mqtt.MqttServer) HashSet(java.util.HashSet) CompositeFuture(io.vertx.core.CompositeFuture) SSLSession(javax.net.ssl.SSLSession) MqttTopicSubscription(io.vertx.mqtt.MqttTopicSubscription) AdapterConnectionsExceededException(org.eclipse.hono.adapter.AdapterConnectionsExceededException) LinkedList(java.util.LinkedList) CommandConstants(org.eclipse.hono.util.CommandConstants) TenantChangeNotification(org.eclipse.hono.notification.deviceregistry.TenantChangeNotification) ChainAuthHandler(org.eclipse.hono.adapter.auth.device.ChainAuthHandler) UsernamePasswordAuthProvider(org.eclipse.hono.adapter.auth.device.UsernamePasswordAuthProvider) CredentialsApiAuthProvider(org.eclipse.hono.adapter.auth.device.CredentialsApiAuthProvider) AbstractProtocolAdapterBase(org.eclipse.hono.adapter.AbstractProtocolAdapterBase) Direction(org.eclipse.hono.service.metric.MetricsTags.Direction) MqttServerOptions(io.vertx.mqtt.MqttServerOptions) Promise(io.vertx.core.Promise) MqttSubscribeMessage(io.vertx.mqtt.messages.MqttSubscribeMessage) ServerErrorException(org.eclipse.hono.client.ServerErrorException) Sample(io.micrometer.core.instrument.Timer.Sample) CommandResponse(org.eclipse.hono.client.command.CommandResponse) TenantObject(org.eclipse.hono.util.TenantObject) SpanContext(io.opentracing.SpanContext) ErrorHandlingMode(org.eclipse.hono.adapter.mqtt.MqttContext.ErrorHandlingMode) MqttUnsubscribeMessage(io.vertx.mqtt.messages.MqttUnsubscribeMessage) ChronoUnit(java.time.temporal.ChronoUnit) ConnectionAttemptOutcome(org.eclipse.hono.service.metric.MetricsTags.ConnectionAttemptOutcome) MemoryBasedConnectionLimitStrategy(org.eclipse.hono.adapter.limiting.MemoryBasedConnectionLimitStrategy) X509AuthProvider(org.eclipse.hono.adapter.auth.device.X509AuthProvider) DateTimeFormatter(java.time.format.DateTimeFormatter) ArrayDeque(java.util.ArrayDeque) Handler(io.vertx.core.Handler) DefaultConnectionLimitManager(org.eclipse.hono.adapter.limiting.DefaultConnectionLimitManager) ConnectionLimitManager(org.eclipse.hono.adapter.limiting.ConnectionLimitManager) DefaultConnectionLimitManager(org.eclipse.hono.adapter.limiting.DefaultConnectionLimitManager)

Aggregations

Handler (io.vertx.core.Handler)2 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)2 AuthHandler (org.eclipse.hono.adapter.auth.device.AuthHandler)2 Sample (io.micrometer.core.instrument.Timer.Sample)1 MqttConnectReturnCode (io.netty.handler.codec.mqtt.MqttConnectReturnCode)1 MqttQoS (io.netty.handler.codec.mqtt.MqttQoS)1 Span (io.opentracing.Span)1 SpanContext (io.opentracing.SpanContext)1 Fields (io.opentracing.log.Fields)1 Tags (io.opentracing.tag.Tags)1 CompositeFuture (io.vertx.core.CompositeFuture)1 Future (io.vertx.core.Future)1 Promise (io.vertx.core.Promise)1 Buffer (io.vertx.core.buffer.Buffer)1 JsonObject (io.vertx.core.json.JsonObject)1 MqttConnectionException (io.vertx.mqtt.MqttConnectionException)1 MqttServer (io.vertx.mqtt.MqttServer)1 MqttServerOptions (io.vertx.mqtt.MqttServerOptions)1 MqttTopicSubscription (io.vertx.mqtt.MqttTopicSubscription)1 MqttPublishMessage (io.vertx.mqtt.messages.MqttPublishMessage)1