Search in sources :

Example 6 with SelfSignedCertificate

use of io.vertx.core.net.SelfSignedCertificate in project hono by eclipse.

the class AmqpUploadTestBase method testUploadMessagesUsingSaslExternal.

/**
 * Verifies that a number of messages uploaded to the AMQP adapter using client certificate
 * based authentication can be successfully consumed via the AMQP Messaging Network.
 *
 * @param senderQos The delivery semantics used by the device for uploading messages.
 * @throws InterruptedException if test execution is interrupted.
 */
@ParameterizedTest(name = IntegrationTestSupport.PARAMETERIZED_TEST_NAME_PATTERN)
@MethodSource("senderQoSTypes")
public void testUploadMessagesUsingSaslExternal(final ProtonQoS senderQos) throws InterruptedException {
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final SelfSignedCertificate deviceCert = SelfSignedCertificate.create(deviceId + ".iot.eclipse.org");
    final VertxTestContext setup = new VertxTestContext();
    helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
        final var tenant = Tenants.createTenantForTrustAnchor(cert);
        prepareTenantConfig(tenant);
        return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
    }).compose(ok -> connectToAdapter(deviceCert)).compose(con -> createProducer(null, senderQos)).onComplete(setup.succeeding(s -> {
        setup.verify(() -> {
            final UnsignedLong maxMessageSize = s.getRemoteMaxMessageSize();
            assertWithMessage("max-message-size included in adapter's attach frame").that(maxMessageSize).isNotNull();
            assertWithMessage("max-message-size").that(maxMessageSize.longValue()).isGreaterThan(0);
        });
        sender = s;
        setup.completeNow();
    }));
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    assertWithMessage("adapter connection failure occurred").that(setup.failed()).isFalse();
    testUploadMessages(tenantId, senderQos);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) AmqpErrorException(org.eclipse.hono.util.AmqpErrorException) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) Rejected(org.apache.qpid.proton.amqp.messaging.Rejected) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) Function(java.util.function.Function) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Symbol(org.apache.qpid.proton.amqp.Symbol) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) Message(org.apache.qpid.proton.message.Message) Binary(org.apache.qpid.proton.amqp.Binary) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) MethodSource(org.junit.jupiter.params.provider.MethodSource) Data(org.apache.qpid.proton.amqp.messaging.Data) Device(org.eclipse.hono.service.management.device.Device) MessageContext(org.eclipse.hono.application.client.MessageContext) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) LinkError(org.apache.qpid.proton.amqp.transport.LinkError) Promise(io.vertx.core.Promise) ServerErrorException(org.eclipse.hono.client.ServerErrorException) DownstreamMessageAssertions(org.eclipse.hono.tests.DownstreamMessageAssertions) ProtonHelper(io.vertx.proton.ProtonHelper) ProtonQoS(io.vertx.proton.ProtonQoS) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) ErrorCondition(org.apache.qpid.proton.amqp.transport.ErrorCondition) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) Buffer(io.vertx.core.buffer.Buffer) Assertions(org.junit.jupiter.api.Assertions) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) ProtonSender(io.vertx.proton.ProtonSender) Handler(io.vertx.core.Handler) Collections(java.util.Collections) Accepted(org.apache.qpid.proton.amqp.messaging.Accepted) QoS(org.eclipse.hono.util.QoS) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) UnsignedLong(org.apache.qpid.proton.amqp.UnsignedLong) VertxTestContext(io.vertx.junit5.VertxTestContext) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) MethodSource(org.junit.jupiter.params.provider.MethodSource)

Example 7 with SelfSignedCertificate

use of io.vertx.core.net.SelfSignedCertificate in project hono by eclipse.

the class DeviceCertificateValidatorTest method testValidateSucceedsForTrustAnchorBasedOnPublicKey.

/**
 * Verifies that the validator succeeds to verify a certificate chain
 * using a trust anchor that has been created with a name and public key
 * instead of a certificate.
 *
 * @param vertx The vert.x instance to use.
 * @param ctx The vert.x test context.
 */
@Test
void testValidateSucceedsForTrustAnchorBasedOnPublicKey(final Vertx vertx, final VertxTestContext ctx) {
    final SelfSignedCertificate deviceCert = SelfSignedCertificate.create("iot.eclipse.org");
    VertxTools.getCertificate(vertx, deviceCert.certificatePath()).compose(cert -> {
        final TrustAnchor ca = new TrustAnchor(cert.getSubjectX500Principal(), cert.getPublicKey(), null);
        return validator.validate(List.of(cert), ca);
    }).onComplete(ctx.succeedingThenComplete());
}
Also used : Test(org.junit.jupiter.api.Test) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) List(java.util.List) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Vertx(io.vertx.core.Vertx) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) VertxTools(org.eclipse.hono.test.VertxTools) VertxExtension(io.vertx.junit5.VertxExtension) TrustAnchor(java.security.cert.TrustAnchor) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) TrustAnchor(java.security.cert.TrustAnchor) Test(org.junit.jupiter.api.Test)

Example 8 with SelfSignedCertificate

use of io.vertx.core.net.SelfSignedCertificate in project hono by eclipse.

the class MqttPublishTestBase method testUploadMessagesUsingClientCertificate.

/**
 * Verifies that a number of messages published by a device authenticating with a client certificate can be
 * successfully consumed via the AMQP Messaging Network.
 *
 * @param ctx The test context.
 * @throws InterruptedException if the test fails.
 */
@Test
public void testUploadMessagesUsingClientCertificate(final VertxTestContext ctx) throws InterruptedException {
    final SelfSignedCertificate deviceCert = SelfSignedCertificate.create(UUID.randomUUID().toString());
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final VertxTestContext setup = new VertxTestContext();
    helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
        final var tenant = Tenants.createTenantForTrustAnchor(cert);
        return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
    }).onComplete(setup.succeedingThenComplete());
    assertThat(setup.awaitCompletion(5, TimeUnit.SECONDS)).isTrue();
    if (setup.failed()) {
        ctx.failNow(setup.causeOfFailure());
        return;
    }
    doTestUploadMessages(ctx, tenantId, deviceId, connectToAdapter(deviceCert), false);
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) MqttQoS(io.netty.handler.codec.mqtt.MqttQoS) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) MqttPublishMessage(io.vertx.mqtt.messages.MqttPublishMessage) HashMap(java.util.HashMap) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) ServiceInvocationException(org.eclipse.hono.client.ServiceInvocationException) AtomicReference(java.util.concurrent.atomic.AtomicReference) Function(java.util.function.Function) ServiceConfigProperties(org.eclipse.hono.config.ServiceConfigProperties) Tenant(org.eclipse.hono.service.management.tenant.Tenant) CompositeFuture(io.vertx.core.CompositeFuture) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) AtomicInteger(java.util.concurrent.atomic.AtomicInteger) Map(java.util.Map) JsonObject(io.vertx.core.json.JsonObject) AsyncResult(io.vertx.core.AsyncResult) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) MqttConnAckMessage(io.vertx.mqtt.messages.MqttConnAckMessage) Device(org.eclipse.hono.service.management.device.Device) QueryStringEncoder(io.netty.handler.codec.http.QueryStringEncoder) MessageContext(org.eclipse.hono.application.client.MessageContext) Promise(io.vertx.core.Promise) Set(java.util.Set) ServerErrorException(org.eclipse.hono.client.ServerErrorException) DownstreamMessageAssertions(org.eclipse.hono.tests.DownstreamMessageAssertions) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) CountDownLatch(java.util.concurrent.CountDownLatch) AtomicLong(java.util.concurrent.atomic.AtomicLong) List(java.util.List) Buffer(io.vertx.core.buffer.Buffer) Optional(java.util.Optional) MessageConsumer(org.eclipse.hono.application.client.MessageConsumer) Handler(io.vertx.core.Handler) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) VertxTestContext(io.vertx.junit5.VertxTestContext) Test(org.junit.jupiter.api.Test)

Example 9 with SelfSignedCertificate

use of io.vertx.core.net.SelfSignedCertificate in project hono by eclipse.

the class CredentialsApiTests method createCertificate.

private X509Certificate createCertificate() throws CertificateException, FileNotFoundException {
    final SelfSignedCertificate ssc = SelfSignedCertificate.create(UUID.randomUUID().toString());
    final CertificateFactory factory = CertificateFactory.getInstance("X.509");
    return (X509Certificate) factory.generateCertificate(new FileInputStream(ssc.certificatePath()));
}
Also used : SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) CertificateFactory(java.security.cert.CertificateFactory) X509Certificate(java.security.cert.X509Certificate) FileInputStream(java.io.FileInputStream)

Example 10 with SelfSignedCertificate

use of io.vertx.core.net.SelfSignedCertificate in project hono by eclipse.

the class AmqpConnectionIT method testConnectFailsForNonMatchingTrustAnchor.

/**
 * Verifies that the adapter fails to authenticate a device if the device's client certificate's signature cannot be
 * validated using the trust anchor that is registered for the tenant that the device belongs to.
 *
 * @param ctx The test context.
 * @throws GeneralSecurityException if the tenant's trust anchor cannot be generated
 */
@Test
public void testConnectFailsForNonMatchingTrustAnchor(final VertxTestContext ctx) throws GeneralSecurityException {
    final String tenantId = helper.getRandomTenantId();
    final String deviceId = helper.getRandomDeviceId(tenantId);
    final KeyPair keyPair = helper.newEcKeyPair();
    final SelfSignedCertificate deviceCert = SelfSignedCertificate.create(UUID.randomUUID().toString());
    // GIVEN a tenant configured with a trust anchor
    helper.getCertificate(deviceCert.certificatePath()).compose(cert -> {
        final Tenant tenant = Tenants.createTenantForTrustAnchor(cert.getSubjectX500Principal(), keyPair.getPublic());
        return helper.registry.addDeviceForTenant(tenantId, tenant, deviceId, cert);
    }).compose(ok -> {
        // using the trust anchor registered for the device's tenant
        return connectToAdapter(deviceCert);
    }).onComplete(ctx.failing(t -> {
        // THEN the connection is not established
        ctx.verify(() -> assertThat(t).isInstanceOf(SaslException.class));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) KeyPair(java.security.KeyPair) CsvSource(org.junit.jupiter.params.provider.CsvSource) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) ClientErrorException(org.eclipse.hono.client.ClientErrorException) SaslException(javax.security.sasl.SaslException) Supplier(java.util.function.Supplier) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) GeneralSecurityException(java.security.GeneralSecurityException) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) ValueSource(org.junit.jupiter.params.provider.ValueSource) SSLHandshakeException(javax.net.ssl.SSLHandshakeException) Promise(io.vertx.core.Promise) RegistrationConstants(org.eclipse.hono.util.RegistrationConstants) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest) AuthenticationException(javax.security.sasl.AuthenticationException) KeyPair(java.security.KeyPair) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test) ParameterizedTest(org.junit.jupiter.params.ParameterizedTest)

Aggregations

SelfSignedCertificate (io.vertx.core.net.SelfSignedCertificate)17 VertxTestContext (io.vertx.junit5.VertxTestContext)10 Tenant (org.eclipse.hono.service.management.tenant.Tenant)10 Test (org.junit.jupiter.api.Test)10 Truth.assertThat (com.google.common.truth.Truth.assertThat)9 Future (io.vertx.core.Future)9 Promise (io.vertx.core.Promise)9 HttpURLConnection (java.net.HttpURLConnection)9 TimeUnit (java.util.concurrent.TimeUnit)9 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)9 Tenants (org.eclipse.hono.tests.Tenants)9 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)9 JsonObject (io.vertx.core.json.JsonObject)8 Timeout (io.vertx.junit5.Timeout)8 VertxExtension (io.vertx.junit5.VertxExtension)8 UUID (java.util.UUID)8 Constants (org.eclipse.hono.util.Constants)8 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)8 ParameterizedTest (org.junit.jupiter.params.ParameterizedTest)8 GeneralSecurityException (java.security.GeneralSecurityException)7