Search in sources :

Example 51 with ResourceIdentifier

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

the class HonoMessagingMessageFilterTest method testVerifyDetectsMissingContentType.

@Test
public void testVerifyDetectsMissingContentType() {
    // GIVEN a valid telemetry message without content type
    final Message msg = givenAMessageHavingProperties(MY_DEVICE, MY_TENANT, null, new byte[] { 0x00 });
    // WHEN receiving the message via a link with matching 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)

Example 52 with ResourceIdentifier

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

the class HonoMessagingMessageFilterTest method assertMessageAnnotationsContainProperties.

private void assertMessageAnnotationsContainProperties(final Message msg, final String tenantId, final String deviceId) {
    assertNotNull(msg.getMessageAnnotations());
    assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(MessageHelper.APP_PROPERTY_TENANT_ID)), is(tenantId));
    assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(MessageHelper.APP_PROPERTY_DEVICE_ID)), is(deviceId));
    final ResourceIdentifier expectedResourceIdentifier = getResourceIdentifier(MY_TENANT, MY_DEVICE);
    assertThat(msg.getMessageAnnotations().getValue().get(Symbol.valueOf(APP_PROPERTY_RESOURCE)), is(expectedResourceIdentifier.toString()));
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier)

Example 53 with ResourceIdentifier

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

the class HonoMessagingMessageFilterTest method testVerifyDetectsMissingRegistrationAssertion.

/**
 * Verifies that the filter rejects messages lacking a registration assertion
 * property.
 */
@Test
public void testVerifyDetectsMissingRegistrationAssertion() {
    // GIVEN a valid telemetry message without registration assertion
    final Message msg = givenAMessageHavingProperties(MY_DEVICE, MY_TENANT, null, CONTENT_TYPE_OCTET_STREAM, new byte[] { 0x00 });
    // WHEN receiving the message via a link with matching 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)

Example 54 with ResourceIdentifier

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

the class SenderFactoryImplTest method testNewSenderIsClosedOnRemoteDetachOrClose.

@SuppressWarnings({ "unchecked", "rawtypes" })
private void testNewSenderIsClosedOnRemoteDetachOrClose(final TestContext ctx, final BiConsumer<ProtonSender, ArgumentCaptor<Handler>> handlerCaptor) {
    // GIVEN a sender created by the factory
    final Async senderCreation = ctx.async();
    final ProtonSender sender = mock(ProtonSender.class);
    when(sender.open()).then(answer -> {
        senderCreation.complete();
        return sender;
    });
    final ProtonConnection con = mock(ProtonConnection.class);
    final ProtonSession session = mock(ProtonSession.class);
    when(session.createSender(anyString())).thenReturn(sender);
    final ResourceIdentifier address = ResourceIdentifier.from(TelemetryConstants.TELEMETRY_ENDPOINT, Constants.DEFAULT_TENANT, null);
    final Handler<String> closeHook = mock(Handler.class);
    final SenderFactoryImpl factory = new SenderFactoryImpl();
    final ArgumentCaptor<Handler> captor = ArgumentCaptor.forClass(Handler.class);
    factory.newSender(con, session, address, ProtonQoS.AT_LEAST_ONCE, drain -> {
    }, closeHook);
    handlerCaptor.accept(sender, captor);
    // WHEN the peer detaches from the sender
    captor.getValue().handle(Future.succeededFuture(sender));
    // THEN the sender gets closed
    verify(sender).close();
    // and the close hook is called
    verify(closeHook).handle(any());
}
Also used : ProtonSender(io.vertx.proton.ProtonSender) ProtonConnection(io.vertx.proton.ProtonConnection) ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) ProtonSession(io.vertx.proton.ProtonSession) Async(io.vertx.ext.unit.Async) Handler(io.vertx.core.Handler)

Example 55 with ResourceIdentifier

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

the class SimpleAuthenticationServer method handleSenderOpen.

/**
 * Handles a request from a client to establish a link for receiving messages from this server.
 *
 * @param con the connection to the client.
 * @param sender the sender created for the link.
 */
@Override
protected void handleSenderOpen(final ProtonConnection con, final ProtonSender sender) {
    final Source remoteSource = sender.getRemoteSource();
    LOG.debug("client [{}] wants to open a link for receiving messages [address: {}]", con.getRemoteContainer(), remoteSource);
    try {
        final ResourceIdentifier targetResource = getResourceIdentifier(remoteSource.getAddress());
        final AmqpEndpoint endpoint = getEndpoint(targetResource);
        if (endpoint == null) {
            LOG.debug("no endpoint registered for node [{}]", targetResource);
            con.setCondition(ProtonHelper.condition(AmqpError.NOT_FOUND, "no such node")).close();
        } else {
            HonoUser user = Constants.getClientPrincipal(con);
            if (Constants.SUBJECT_ANONYMOUS.equals(user.getName())) {
                con.setCondition(ProtonHelper.condition(AmqpError.UNAUTHORIZED_ACCESS, "client must authenticate using SASL")).close();
            } else {
                Constants.copyProperties(con, sender);
                sender.setSource(sender.getRemoteSource());
                endpoint.onLinkAttach(con, sender, targetResource);
                vertx.setTimer(5000, closeCon -> {
                    if (!con.isDisconnected()) {
                        LOG.debug("connection with client [{}] timed out after 5 seconds, closing connection", con.getRemoteContainer());
                        con.setCondition(ProtonHelper.condition("hono: inactivity", "client must retrieve token within 5 secs after opening connection")).close();
                    }
                });
            }
        }
    } catch (final IllegalArgumentException e) {
        LOG.debug("client has provided invalid resource identifier as source address", e);
        con.setCondition(ProtonHelper.condition(AmqpError.INVALID_FIELD, "malformed source address")).close();
    }
}
Also used : ResourceIdentifier(org.eclipse.hono.util.ResourceIdentifier) HonoUser(org.eclipse.hono.auth.HonoUser) AmqpEndpoint(org.eclipse.hono.service.amqp.AmqpEndpoint) Source(org.apache.qpid.proton.amqp.transport.Source)

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