Search in sources :

Example 6 with DeviceUser

use of org.eclipse.hono.service.auth.DeviceUser in project hono by eclipse.

the class AbstractVertxBasedMqttProtocolAdapterTest method testConnectionsLimitExceeded.

/**
 * Verifies that the connection is rejected due to the limit exceeded.
 */
@Test
public void testConnectionsLimitExceeded() {
    // GIVEN an adapter requiring devices to authenticate endpoint
    properties.setAuthenticationRequired(true);
    givenAnAdapter(properties);
    // WHEN a device tries to establish a connection
    when(authHandler.authenticateDevice(any(MqttConnectContext.class))).thenReturn(Future.succeededFuture(new DeviceUser(Constants.DEFAULT_TENANT, "4711")));
    when(resourceLimitChecks.isConnectionLimitReached(any(TenantObject.class), any(SpanContext.class))).thenReturn(Future.succeededFuture(Boolean.TRUE));
    final MqttEndpoint endpoint = getMqttEndpointAuthenticated();
    adapter.handleEndpointConnection(endpoint);
    // THEN the adapter has tried to authenticate the device
    verify(authHandler).authenticateDevice(any(MqttConnectContext.class));
    // THEN the connection request is rejected
    verify(endpoint).reject(MqttConnectReturnCode.CONNECTION_REFUSED_NOT_AUTHORIZED);
    verify(metrics).reportConnectionAttempt(ConnectionAttemptOutcome.TENANT_CONNECTIONS_EXCEEDED, Constants.DEFAULT_TENANT, "BUMLUX_CIPHER");
}
Also used : TenantObject(org.eclipse.hono.util.TenantObject) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) SpanContext(io.opentracing.SpanContext) MqttEndpoint(io.vertx.mqtt.MqttEndpoint) Test(org.junit.jupiter.api.Test)

Example 7 with DeviceUser

use of org.eclipse.hono.service.auth.DeviceUser in project hono by eclipse.

the class CredentialsApiAuthProvider method authenticate.

@Override
public final void authenticate(final T deviceCredentials, final SpanContext spanContext, final Handler<AsyncResult<DeviceUser>> resultHandler) {
    Objects.requireNonNull(deviceCredentials);
    Objects.requireNonNull(resultHandler);
    final Span currentSpan = TracingHelper.buildServerChildSpan(tracer, spanContext, "authenticate device", getClass().getSimpleName()).withTag(MessageHelper.APP_PROPERTY_TENANT_ID, deviceCredentials.getTenantId()).withTag(TracingHelper.TAG_AUTH_ID.getKey(), deviceCredentials.getAuthId()).start();
    getCredentialsForDevice(deviceCredentials, currentSpan.context()).recover(t -> Future.failedFuture(mapNotFoundToBadCredentialsException(t))).compose(credentialsOnRecord -> validateCredentials(deviceCredentials, credentialsOnRecord, currentSpan.context())).map(device -> new DeviceUser(device.getTenantId(), device.getDeviceId())).onComplete(authAttempt -> {
        if (authAttempt.succeeded()) {
            currentSpan.log("successfully authenticated device");
        } else {
            currentSpan.log("authentication of device failed");
            TracingHelper.logError(currentSpan, authAttempt.cause());
        }
        currentSpan.finish();
        resultHandler.handle(authAttempt);
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) LoggerFactory(org.slf4j.LoggerFactory) ServerErrorException(org.eclipse.hono.client.ServerErrorException) ClientErrorException(org.eclipse.hono.client.ClientErrorException) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) MessageHelper(org.eclipse.hono.util.MessageHelper) Future(io.vertx.core.Future) Device(org.eclipse.hono.auth.Device) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) User(io.vertx.ext.auth.User) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) AsyncResult(io.vertx.core.AsyncResult) Handler(io.vertx.core.Handler) CredentialsObject(org.eclipse.hono.util.CredentialsObject) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) Span(io.opentracing.Span)

Example 8 with DeviceUser

use of org.eclipse.hono.service.auth.DeviceUser in project hono by eclipse.

the class LoraProtocolAdapterTest method newHttpContext.

private HttpContext newHttpContext() {
    final LoraMetaData metaData = new LoraMetaData();
    metaData.setFunctionPort(TEST_FUNCTION_PORT);
    final HttpServerRequest request = mock(HttpServerRequest.class);
    when(request.uri()).thenReturn("/lora");
    final RoutingContext context = mock(RoutingContext.class);
    when(context.getBody()).thenReturn(Buffer.buffer());
    when(context.user()).thenReturn(new DeviceUser(TEST_TENANT_ID, TEST_GATEWAY_ID));
    when(context.request()).thenReturn(request);
    when(context.response()).thenReturn(mock(HttpServerResponse.class));
    when(context.get(LoraConstants.APP_PROPERTY_ORIG_LORA_PROVIDER)).thenReturn(TEST_PROVIDER);
    when(context.get(LoraConstants.APP_PROPERTY_META_DATA)).thenReturn(metaData);
    final Span parentSpan = mock(Span.class);
    when(parentSpan.context()).thenReturn(mock(SpanContext.class));
    when(context.get(TracingHandler.CURRENT_SPAN)).thenReturn(parentSpan);
    return HttpContext.from(context);
}
Also used : RoutingContext(io.vertx.ext.web.RoutingContext) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) SpanContext(io.opentracing.SpanContext) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) Span(io.opentracing.Span)

Example 9 with DeviceUser

use of org.eclipse.hono.service.auth.DeviceUser in project hono by eclipse.

the class AbstractVertxBasedHttpProtocolAdapterTest method testUploadCommandResponseFailsForOtherDevice.

/**
 * Verifies that an authenticated device that is not a gateway fails to
 * upload a command response for another device.
 */
@Test
public void testUploadCommandResponseFailsForOtherDevice() {
    givenAnAdapter(properties);
    final Buffer payload = Buffer.buffer("some payload");
    final HttpContext ctx = newHttpContext(payload, "application/text", mock(HttpServerRequest.class), mock(HttpServerResponse.class));
    final TenantObject to = TenantObject.from("tenant", true);
    // Given an adapter that is enabled for a device's tenant
    to.addAdapter(new Adapter(ADAPTER_TYPE).setEnabled(Boolean.TRUE));
    when(tenantClient.get(eq("tenant"), (SpanContext) any())).thenReturn(Future.succeededFuture(to));
    // which is connected to a Credentials service that has credentials on record for device 9999
    when(ctx.getAuthenticatedDevice()).thenReturn(new DeviceUser("tenant", "9999"));
    // but for which no registration information is available
    when(registrationClient.assertRegistration(anyString(), anyString(), any(), any())).thenReturn(Future.failedFuture(new ClientErrorException(HttpURLConnection.HTTP_NOT_FOUND, "cannot publish data for device of other tenant")));
    adapter.uploadCommandResponseMessage(ctx, "tenant", "device", CMD_REQ_ID, 200);
    // Then the device gets a 404
    assertContextFailedWithClientError(ctx, HttpURLConnection.HTTP_NOT_FOUND);
    // and the response has not been reported as forwarded
    verify(metrics).reportCommand(eq(Direction.RESPONSE), eq("tenant"), eq(to), eq(ProcessingOutcome.UNPROCESSABLE), eq(payload.length()), any());
}
Also used : Buffer(io.vertx.core.buffer.Buffer) TenantObject(org.eclipse.hono.util.TenantObject) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) HttpServerRequest(io.vertx.core.http.HttpServerRequest) HttpServerResponse(io.vertx.core.http.HttpServerResponse) HttpContext(org.eclipse.hono.service.http.HttpContext) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Adapter(org.eclipse.hono.util.Adapter) Test(org.junit.jupiter.api.Test)

Example 10 with DeviceUser

use of org.eclipse.hono.service.auth.DeviceUser in project hono by eclipse.

the class ChainAuthHandlerTest method testPreCredentialsValidationHandlerGetsInvoked.

/**
 * Verifies that the PreCredentialsValidationHandler given for the ChainAuthHandler is invoked
 * when authenticating.
 *
 * @param ctx The vert.x test context.
 */
@SuppressWarnings({ "unchecked", "rawtypes" })
@Test
public void testPreCredentialsValidationHandlerGetsInvoked(final VertxTestContext ctx) {
    final Checkpoint preCredValidationHandlerInvokedCheckpoint = ctx.checkpoint();
    final Checkpoint testPassedCheckpoint = ctx.checkpoint();
    final TestExecutionContext context = new TestExecutionContext();
    final JsonObject parsedCredentials = new JsonObject().put("someKey", "someValue");
    final AbstractDeviceCredentials deviceCredentials = mock(AbstractDeviceCredentials.class);
    final DeviceUser deviceUser = new DeviceUser("tenant", "device");
    // prepare nestedAuthHandler1
    final AuthHandler<TestExecutionContext> nestedAuthHandler1 = newMockedAuthHandler();
    when(nestedAuthHandler1.parseCredentials(any(TestExecutionContext.class))).thenReturn(Future.failedFuture("parseCredentials failed"));
    // prepare nestedAuthHandler2
    final AuthHandler<TestExecutionContext> nestedAuthHandler2 = newMockedAuthHandler();
    when(nestedAuthHandler2.parseCredentials(any(TestExecutionContext.class))).thenReturn(Future.succeededFuture(parsedCredentials));
    // prepare authProvider
    final DeviceCredentialsAuthProvider<?> provider = mock(DeviceCredentialsAuthProvider.class);
    doReturn(deviceCredentials).when(provider).getCredentials(any(JsonObject.class));
    doAnswer(invocation -> {
        final Handler handler = invocation.getArgument(2);
        handler.handle(Future.succeededFuture(deviceUser));
        return null;
    }).when(provider).authenticate(any(), any(), any());
    doReturn(provider).when(nestedAuthHandler2).getAuthProvider(eq(context));
    // prepare PreCredentialsValidationHandler
    final PreCredentialsValidationHandler<TestExecutionContext> preCredValidationHandler = mock(PreCredentialsValidationHandler.class);
    doAnswer(invocation -> {
        preCredValidationHandlerInvokedCheckpoint.flag();
        return Future.succeededFuture();
    }).when(preCredValidationHandler).handle(eq(deviceCredentials), eq(context));
    // GIVEN an chain auth handler where the 2nd contained auth handler successfully parses credentials
    // and returns an auth provider that successfully authenticates
    final ChainAuthHandler<TestExecutionContext> chainAuthHandler = new ChainAuthHandler<>(preCredValidationHandler);
    chainAuthHandler.append(nestedAuthHandler1);
    chainAuthHandler.append(nestedAuthHandler2);
    // WHEN a device gets authenticated
    chainAuthHandler.authenticateDevice(context).onComplete(ctx.succeeding(user -> {
        // THEN the returned user is the one return by the auth provider
        ctx.verify(() -> {
            assertThat(user).isEqualTo(deviceUser);
        });
        testPassedCheckpoint.flag();
    }));
}
Also used : ArgumentMatchers.any(org.mockito.ArgumentMatchers.any) VertxTestContext(io.vertx.junit5.VertxTestContext) ArgumentMatchers.eq(org.mockito.ArgumentMatchers.eq) Mockito.when(org.mockito.Mockito.when) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) Test(org.junit.jupiter.api.Test) MapBasedExecutionContext(org.eclipse.hono.util.MapBasedExecutionContext) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Mockito.doAnswer(org.mockito.Mockito.doAnswer) JsonObject(io.vertx.core.json.JsonObject) Checkpoint(io.vertx.junit5.Checkpoint) NoopSpan(io.opentracing.noop.NoopSpan) Handler(io.vertx.core.Handler) Mockito.doReturn(org.mockito.Mockito.doReturn) Mockito.mock(org.mockito.Mockito.mock) Checkpoint(io.vertx.junit5.Checkpoint) DeviceUser(org.eclipse.hono.service.auth.DeviceUser) JsonObject(io.vertx.core.json.JsonObject) Handler(io.vertx.core.Handler) Test(org.junit.jupiter.api.Test)

Aggregations

DeviceUser (org.eclipse.hono.service.auth.DeviceUser)14 Test (org.junit.jupiter.api.Test)11 MqttEndpoint (io.vertx.mqtt.MqttEndpoint)7 Handler (io.vertx.core.Handler)5 JsonObject (io.vertx.core.json.JsonObject)5 SpanContext (io.opentracing.SpanContext)4 Future (io.vertx.core.Future)4 ClientErrorException (org.eclipse.hono.client.ClientErrorException)4 TenantObject (org.eclipse.hono.util.TenantObject)4 HttpServerRequest (io.vertx.core.http.HttpServerRequest)3 HttpServerResponse (io.vertx.core.http.HttpServerResponse)3 Truth.assertThat (com.google.common.truth.Truth.assertThat)2 Span (io.opentracing.Span)2 NoopSpan (io.opentracing.noop.NoopSpan)2 Promise (io.vertx.core.Promise)2 RoutingContext (io.vertx.ext.web.RoutingContext)2 Checkpoint (io.vertx.junit5.Checkpoint)2 VertxExtension (io.vertx.junit5.VertxExtension)2 VertxTestContext (io.vertx.junit5.VertxTestContext)2 HttpURLConnection (java.net.HttpURLConnection)2