Search in sources :

Example 56 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class FileBasedAuthenticationServiceTest method testVerifyPlainAddsAuthoritiesForOperations.

@Test
public void testVerifyPlainAddsAuthoritiesForOperations(final TestContext ctx) {
    final ResourceIdentifier registration = ResourceIdentifier.fromString("registration/tenant");
    authService.verifyPlain(null, "hono-client@HONO", "secret", ctx.asyncAssertSuccess(res -> {
        assertTrue(res.getAuthorities().isAuthorized(registration, "assert"));
        assertTrue(res.getAuthorities().isAuthorized(registration, "add"));
    }));
}
Also used : CoreMatchers.is(org.hamcrest.CoreMatchers.is) AuthTokenHelper(org.eclipse.hono.service.auth.AuthTokenHelper) TestContext(io.vertx.ext.unit.TestContext) BeforeClass(org.junit.BeforeClass) RunWith(org.junit.runner.RunWith) ClassPathResource(org.springframework.core.io.ClassPathResource) IOException(java.io.IOException) Test(org.junit.Test) Mockito.when(org.mockito.Mockito.when) VertxUnitRunner(io.vertx.ext.unit.junit.VertxUnitRunner) Matchers.anyString(org.mockito.Matchers.anyString) Matchers.any(org.mockito.Matchers.any) Duration(java.time.Duration) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Assert(org.junit.Assert) Authorities(org.eclipse.hono.auth.Authorities) Mockito.mock(org.mockito.Mockito.mock) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Test(org.junit.Test)

Example 57 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class ForwardingDownstreamAdapter method onClientAttach.

@Override
public final void onClientAttach(final UpstreamReceiver client, final Handler<AsyncResult<Void>> resultHandler) {
    if (!running) {
        throw new IllegalStateException("adapter must be started first");
    }
    Objects.requireNonNull(client);
    Objects.requireNonNull(resultHandler);
    ProtonSender sender = activeSenders.get(client);
    if (sender != null && sender.isOpen()) {
        logger.info("reusing existing downstream sender [con: {}, link: {}]", client.getConnectionId(), client.getLinkId());
        resultHandler.handle(Future.succeededFuture());
    } else {
        removeSender(client);
        // register the result handler to be failed if the connection to the downstream container fails during
        // the attempt to create a downstream sender
        clientAttachHandlers.add(resultHandler);
        Future<Void> tracker = Future.future();
        tracker.setHandler(attempt -> {
            if (attempt.succeeded()) {
                logger.info("created downstream sender [con: {}, link: {}]", client.getConnectionId(), client.getLinkId());
            } else {
                logger.warn("can't create downstream sender [con: {}, link: {}]: {}", client.getConnectionId(), client.getLinkId(), attempt.cause().getMessage());
            }
            clientAttachHandlers.remove(resultHandler);
            resultHandler.handle(attempt);
        });
        final ResourceIdentifier targetAddress = ResourceIdentifier.fromString(client.getTargetAddress());
        createSender(targetAddress, replenishedSender -> handleFlow(replenishedSender, client), closeHook -> {
            removeSender(client);
            closeReceiver(client);
        }).compose(createdSender -> {
            addSender(client, createdSender);
            tracker.complete();
        }, tracker);
    }
}
Also used : ProtonConnection(io.vertx.proton.ProtonConnection) ProtonDelivery(io.vertx.proton.ProtonDelivery) LoggerFactory(org.slf4j.LoggerFactory) Autowired(org.springframework.beans.factory.annotation.Autowired) HashMap(java.util.HashMap) Constants(org.eclipse.hono.util.Constants) ArrayList(java.util.ArrayList) ConnectionFactory(org.eclipse.hono.connection.ConnectionFactory) ProtonClientOptions(io.vertx.proton.ProtonClientOptions) Map(java.util.Map) Qualifier(org.springframework.beans.factory.annotation.Qualifier) Message(org.apache.qpid.proton.message.Message) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) AsyncResult(io.vertx.core.AsyncResult) Logger(org.slf4j.Logger) Iterator(java.util.Iterator) Vertx(io.vertx.core.Vertx) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Future(io.vertx.core.Future) Objects(java.util.Objects) List(java.util.List) Component(org.springframework.stereotype.Component) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) ProtonSender(io.vertx.proton.ProtonSender) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 58 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class MessageForwardingEndpoint method forwardMessage.

final void forwardMessage(final UpstreamReceiver link, final ProtonDelivery delivery, final Message msg) {
    final ResourceIdentifier messageAddress = ResourceIdentifier.fromString(getAnnotation(msg, MessageHelper.APP_PROPERTY_RESOURCE, String.class));
    final String token = MessageHelper.getAndRemoveRegistrationAssertion(msg);
    if (assertRegistration(token, messageAddress)) {
        downstreamAdapter.processMessage(link, delivery, msg);
    } else {
        logger.debug("failed to validate device registration status");
        rejectMessage(delivery, ProtonHelper.condition(AmqpError.PRECONDITION_FAILED, "device non-existent/disabled"), link);
    }
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 59 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class EventEndpointTest method testOnLinkAttachDisconnectsClientsUsingWrongQos.

/**
 * Verifies that the endpoint rejects a client's attempt to create a link using <em>AT_MOST_ONCE</em>
 * delivery mode.
 */
@Test
public void testOnLinkAttachDisconnectsClientsUsingWrongQos() {
    ProtonConnection con = mock(ProtonConnection.class);
    ProtonReceiver receiver = mock(ProtonReceiver.class);
    when(receiver.getRemoteQoS()).thenReturn(ProtonQoS.AT_MOST_ONCE);
    ResourceIdentifier targetAddress = ResourceIdentifier.from("event", "tenant", null);
    endpoint.onLinkAttach(con, receiver, targetAddress);
    ArgumentCaptor<ErrorCondition> errorCondition = ArgumentCaptor.forClass(ErrorCondition.class);
    verify(receiver).setCondition(errorCondition.capture());
    assertThat(errorCondition.getValue(), is(ErrorConditions.ERROR_UNSUPPORTED_DELIVERY_MODE));
    verify(receiver).close();
}
Also used : ProtonReceiver(io.vertx.proton.ProtonReceiver) ProtonConnection(io.vertx.proton.ProtonConnection) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) Test(org.junit.Test)

Example 60 with ResourceIdentifier

use of org.eclipse.hono.util.ResourceIdentifier in project hono by eclipse.

the class HonoMessagingMessageFilterTest method testVerifyDetectsMissingDeviceId.

@Test
public void testVerifyDetectsMissingDeviceId() {
    // GIVEN a valid telemetry message without device id
    final Message msg = givenAMessageHavingProperties(null);
    // WHEN receiving the message via a link with mismatching tenant
    final ResourceIdentifier linkTarget = getResourceIdentifier(MY_TENANT);
    // THEN message validation fails
    assertFalse(HonoMessagingMessageFilter.verify(linkTarget, msg));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) Message(org.apache.qpid.proton.message.Message) Test(org.junit.Test)

Aggregations

ResourceIdentifier (org.eclipse.hono.util.ResourceIdentifier)82 Message (org.apache.qpid.proton.message.Message)30 Future (io.vertx.core.Future)24 HttpURLConnection (java.net.HttpURLConnection)22 MessageHelper (org.eclipse.hono.util.MessageHelper)22 ClientErrorException (org.eclipse.hono.client.ClientErrorException)20 Test (org.junit.Test)20 Test (org.junit.jupiter.api.Test)19 Handler (io.vertx.core.Handler)18 Map (java.util.Map)18 Span (io.opentracing.Span)17 Buffer (io.vertx.core.buffer.Buffer)17 SpanContext (io.opentracing.SpanContext)16 Constants (org.eclipse.hono.util.Constants)16 Promise (io.vertx.core.Promise)15 Objects (java.util.Objects)14 AsyncResult (io.vertx.core.AsyncResult)13 Vertx (io.vertx.core.Vertx)13 ProtonConnection (io.vertx.proton.ProtonConnection)13 ProtonReceiver (io.vertx.proton.ProtonReceiver)13