Search in sources :

Example 61 with Tenant

use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.

the class TenantApiTests method testGetTenantByCaFailsIfNotAuthorized.

/**
 * Verifies that a request to retrieve information for a tenant by the
 * subject DN of the trusted certificate authority fails with a
 * <em>403 Forbidden</em> status if the client is not authorized to retrieve
 * information for the tenant.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetTenantByCaFailsIfNotAuthorized(final VertxTestContext ctx) {
    final String tenantId = getHelper().getRandomTenantId();
    final X500Principal subjectDn = new X500Principal("CN=ca-http,OU=Hono,O=Eclipse");
    final PublicKey publicKey = getRandomPublicKey();
    final Tenant tenant = Tenants.createTenantForTrustAnchor(subjectDn, publicKey);
    getHelper().registry.addTenant(tenantId, tenant).compose(r -> getRestrictedClient().get(subjectDn, NoopSpan.INSTANCE.context())).onComplete(ctx.failing(t -> {
        assertErrorCode(t, HttpURLConnection.HTTP_FORBIDDEN);
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) KeyPair(java.security.KeyPair) X500Principal(javax.security.auth.x500.X500Principal) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions(org.assertj.core.api.Assertions) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) KeyPairGenerator(java.security.KeyPairGenerator) ResourceLimits(org.eclipse.hono.util.ResourceLimits) PublicKey(java.security.PublicKey) TenantClient(org.eclipse.hono.client.registry.TenantClient) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) PeriodMode(org.eclipse.hono.util.ResourceLimitsPeriod.PeriodMode) ResourceLimitsPeriod(org.eclipse.hono.util.ResourceLimitsPeriod) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) DataVolume(org.eclipse.hono.util.DataVolume) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoopSpan(io.opentracing.noop.NoopSpan) TrustAnchor(java.security.cert.TrustAnchor) Tenant(org.eclipse.hono.service.management.tenant.Tenant) PublicKey(java.security.PublicKey) X500Principal(javax.security.auth.x500.X500Principal) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 62 with Tenant

use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.

the class TenantApiTests method testGetTenant.

/**
 * Verifies that an existing tenant can be retrieved.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetTenant(final VertxTestContext ctx) {
    final JsonObject defaults = new JsonObject().put("ttl", 30);
    final Map<String, Object> httpAdapterExtensions = Map.of("deployment", Map.of("maxInstances", 4));
    final ResourceLimits resourceLimits = new ResourceLimits().setMaxConnections(100000).setMaxTtl(30L).setDataVolume(new DataVolume(Instant.parse("2019-07-27T14:30:00Z"), new ResourceLimitsPeriod(PeriodMode.days).setNoOfDays(30), 2147483648L));
    final String tenantId = getHelper().getRandomTenantId();
    final Tenant tenant = new Tenant();
    tenant.setEnabled(true);
    tenant.setResourceLimits(resourceLimits);
    tenant.setDefaults(defaults.getMap());
    tenant.putExtension("customer", "ACME Inc.");
    tenant.addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(true).setDeviceAuthenticationRequired(false)).addAdapterConfig(new Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(true).setDeviceAuthenticationRequired(true).setExtensions(httpAdapterExtensions));
    // expected tenant object
    final TenantObject expectedTenantObject = TenantObject.from(tenantId, true).setDefaults(defaults).setResourceLimits(resourceLimits).addAdapter(new org.eclipse.hono.util.Adapter(Constants.PROTOCOL_ADAPTER_TYPE_MQTT).setEnabled(Boolean.TRUE).setDeviceAuthenticationRequired(Boolean.FALSE)).addAdapter(new org.eclipse.hono.util.Adapter(Constants.PROTOCOL_ADAPTER_TYPE_HTTP).setEnabled(Boolean.TRUE).setDeviceAuthenticationRequired(Boolean.TRUE).setExtensions(httpAdapterExtensions));
    getHelper().registry.addTenant(tenantId, tenant).compose(ok -> getAdminClient().get(tenantId, NoopSpan.INSTANCE.context())).onComplete(ctx.succeeding(tenantObject -> {
        ctx.verify(() -> {
            assertThat(tenantObject.getDefaults()).isEqualTo(expectedTenantObject.getDefaults());
            Assertions.assertThat(tenantObject.getAdapters()).usingRecursiveFieldByFieldElementComparator().containsAll(expectedTenantObject.getAdapters());
            assertThat(tenantObject.getResourceLimits().getMaxConnections()).isEqualTo(expectedTenantObject.getResourceLimits().getMaxConnections());
            assertThat(tenantObject.getResourceLimits().getMaxTtl()).isEqualTo(expectedTenantObject.getResourceLimits().getMaxTtl());
            assertThat(tenantObject.getResourceLimits().getDataVolume().getMaxBytes()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getMaxBytes());
            assertThat(tenantObject.getResourceLimits().getDataVolume().getEffectiveSince()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getEffectiveSince());
            assertThat(tenantObject.getResourceLimits().getDataVolume().getPeriod().getMode()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getPeriod().getMode());
            assertThat(tenantObject.getResourceLimits().getDataVolume().getPeriod().getNoOfDays()).isEqualTo(expectedTenantObject.getResourceLimits().getDataVolume().getPeriod().getNoOfDays());
            final JsonObject extensions = tenantObject.getProperty("ext", JsonObject.class);
            assertThat(extensions.getString("customer")).isEqualTo("ACME Inc.");
            // implicitly added by DeviceRegistryHttpClient
            assertThat(extensions.getString(TenantConstants.FIELD_EXT_MESSAGING_TYPE)).isEqualTo(IntegrationTestSupport.getConfiguredMessagingType().name());
        });
        ctx.completeNow();
    }));
}
Also used : DataVolume(org.eclipse.hono.util.DataVolume) HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) KeyPair(java.security.KeyPair) X500Principal(javax.security.auth.x500.X500Principal) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions(org.assertj.core.api.Assertions) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) KeyPairGenerator(java.security.KeyPairGenerator) ResourceLimits(org.eclipse.hono.util.ResourceLimits) PublicKey(java.security.PublicKey) TenantClient(org.eclipse.hono.client.registry.TenantClient) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) PeriodMode(org.eclipse.hono.util.ResourceLimitsPeriod.PeriodMode) ResourceLimitsPeriod(org.eclipse.hono.util.ResourceLimitsPeriod) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) DataVolume(org.eclipse.hono.util.DataVolume) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoopSpan(io.opentracing.noop.NoopSpan) TrustAnchor(java.security.cert.TrustAnchor) JsonObject(io.vertx.core.json.JsonObject) Adapter(org.eclipse.hono.util.Adapter) TenantObject(org.eclipse.hono.util.TenantObject) Tenant(org.eclipse.hono.service.management.tenant.Tenant) ResourceLimitsPeriod(org.eclipse.hono.util.ResourceLimitsPeriod) ResourceLimits(org.eclipse.hono.util.ResourceLimits) JsonObject(io.vertx.core.json.JsonObject) TenantObject(org.eclipse.hono.util.TenantObject) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 63 with Tenant

use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.

the class TenantApiTests method testGetTenantByCa.

/**
 * Verifies that an existing tenant can be retrieved by a trusted CA's subject DN.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetTenantByCa(final VertxTestContext ctx) {
    final String tenantId = getHelper().getRandomTenantId();
    final X500Principal subjectDn = new X500Principal("CN=ca, OU=Hono, O=Eclipse");
    final PublicKey publicKey = getRandomPublicKey();
    final Tenant tenant = Tenants.createTenantForTrustAnchor(subjectDn, publicKey);
    getHelper().registry.addTenant(tenantId, tenant).compose(r -> getAdminClient().get(subjectDn, NoopSpan.INSTANCE.context())).onComplete(ctx.succeeding(tenantObject -> {
        ctx.verify(() -> {
            assertThat(tenantObject.getTenantId()).isEqualTo(tenantId);
            assertThat(tenantObject.getTrustAnchors()).hasSize(1);
            final TrustAnchor trustAnchor = tenantObject.getTrustAnchors().iterator().next();
            assertThat(trustAnchor.getCA()).isEqualTo(subjectDn);
            assertThat(trustAnchor.getCAPublicKey()).isEqualTo(publicKey);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) KeyPair(java.security.KeyPair) X500Principal(javax.security.auth.x500.X500Principal) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions(org.assertj.core.api.Assertions) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) KeyPairGenerator(java.security.KeyPairGenerator) ResourceLimits(org.eclipse.hono.util.ResourceLimits) PublicKey(java.security.PublicKey) TenantClient(org.eclipse.hono.client.registry.TenantClient) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) PeriodMode(org.eclipse.hono.util.ResourceLimitsPeriod.PeriodMode) ResourceLimitsPeriod(org.eclipse.hono.util.ResourceLimitsPeriod) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) DataVolume(org.eclipse.hono.util.DataVolume) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoopSpan(io.opentracing.noop.NoopSpan) TrustAnchor(java.security.cert.TrustAnchor) Tenant(org.eclipse.hono.service.management.tenant.Tenant) PublicKey(java.security.PublicKey) X500Principal(javax.security.auth.x500.X500Principal) TrustAnchor(java.security.cert.TrustAnchor) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 64 with Tenant

use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.

the class TenantApiTests method testGetTenantByAlias.

/**
 * Verifies that an existing tenant can be retrieved by alias.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetTenantByAlias(final VertxTestContext ctx) {
    assumeTrue(IntegrationTestSupport.isTenantAliasSupported(), "device registry does not support tenant aliases");
    final String tenantId = getHelper().getRandomTenantId();
    getHelper().registry.addTenant(tenantId, new Tenant().setAlias("test-alias")).onFailure(ctx::failNow).compose(response -> getAdminClient().get("test-alias", NoopSpan.INSTANCE.context())).onComplete(ctx.succeeding(tenantObject -> {
        ctx.verify(() -> assertThat(tenantObject.getTenantId()).isEqualTo(tenantId));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) KeyPair(java.security.KeyPair) X500Principal(javax.security.auth.x500.X500Principal) TenantConstants(org.eclipse.hono.util.TenantConstants) 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) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) Assertions(org.assertj.core.api.Assertions) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) KeyPairGenerator(java.security.KeyPairGenerator) ResourceLimits(org.eclipse.hono.util.ResourceLimits) PublicKey(java.security.PublicKey) TenantClient(org.eclipse.hono.client.registry.TenantClient) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) PeriodMode(org.eclipse.hono.util.ResourceLimitsPeriod.PeriodMode) ResourceLimitsPeriod(org.eclipse.hono.util.ResourceLimitsPeriod) TenantObject(org.eclipse.hono.util.TenantObject) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) Adapter(org.eclipse.hono.util.Adapter) DataVolume(org.eclipse.hono.util.DataVolume) NoSuchAlgorithmException(java.security.NoSuchAlgorithmException) NoopSpan(io.opentracing.noop.NoopSpan) TrustAnchor(java.security.cert.TrustAnchor) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Test(org.junit.jupiter.api.Test)

Example 65 with Tenant

use of org.eclipse.hono.service.management.tenant.Tenant in project hono by eclipse.

the class TenantManagementIT method testAddTenantFailsForUnknownProperties.

/**
 * Verifies that a request to register a tenant that contains unsupported properties
 * fails with a 400 status.
 *
 * @param context The Vert.x test context.
 */
@Test
public void testAddTenantFailsForUnknownProperties(final VertxTestContext context) {
    final JsonObject requestBody = JsonObject.mapFrom(new Tenant());
    requestBody.put("unexpected", "property");
    final String tenantId = getHelper().getRandomTenantId();
    getHelper().registry.addTenant(tenantId, requestBody, "application/json", HttpURLConnection.HTTP_BAD_REQUEST).onComplete(context.succeeding(response -> {
        context.verify(() -> IntegrationTestSupport.assertErrorPayload(response));
        context.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) Assertions.assertNotNull(org.junit.jupiter.api.Assertions.assertNotNull) Arrays(java.util.Arrays) TenantConstants(org.eclipse.hono.util.TenantConstants) LoggerFactory(org.slf4j.LoggerFactory) MultiMap(io.vertx.core.MultiMap) AtomicReference(java.util.concurrent.atomic.AtomicReference) Constants(org.eclipse.hono.util.Constants) Nested(org.junit.jupiter.api.Nested) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) CompositeFuture(io.vertx.core.CompositeFuture) Matcher(java.util.regex.Matcher) Assertions.assertFalse(org.junit.jupiter.api.Assertions.assertFalse) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) SearchResult(org.eclipse.hono.service.management.SearchResult) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Map(java.util.Map) Assumptions.assumeTrue(org.junit.jupiter.api.Assumptions.assumeTrue) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) TypeReference(com.fasterxml.jackson.core.type.TypeReference) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) EnabledIf(org.junit.jupiter.api.condition.EnabledIf) Device(org.eclipse.hono.service.management.device.Device) RegistrationLimits(org.eclipse.hono.service.management.tenant.RegistrationLimits) ResourceLimits(org.eclipse.hono.util.ResourceLimits) Logger(org.slf4j.Logger) JacksonCodec(io.vertx.core.json.jackson.JacksonCodec) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) HttpHeaders(io.vertx.core.http.HttpHeaders) PublicKey(java.security.PublicKey) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) VertxExtension(io.vertx.junit5.VertxExtension) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) JsonArray(io.vertx.core.json.JsonArray) List(java.util.List) Adapter(org.eclipse.hono.util.Adapter) ChronoUnit(java.time.temporal.ChronoUnit) Assertions.assertTrue(org.junit.jupiter.api.Assertions.assertTrue) Optional(java.util.Optional) Pattern(java.util.regex.Pattern) Tenant(org.eclipse.hono.service.management.tenant.Tenant) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test)

Aggregations

Tenant (org.eclipse.hono.service.management.tenant.Tenant)165 Test (org.junit.jupiter.api.Test)138 VertxTestContext (io.vertx.junit5.VertxTestContext)137 HttpURLConnection (java.net.HttpURLConnection)122 Truth.assertThat (com.google.common.truth.Truth.assertThat)113 TimeUnit (java.util.concurrent.TimeUnit)109 JsonObject (io.vertx.core.json.JsonObject)108 Future (io.vertx.core.Future)107 Timeout (io.vertx.junit5.Timeout)99 IntegrationTestSupport (org.eclipse.hono.tests.IntegrationTestSupport)98 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)97 Constants (org.eclipse.hono.util.Constants)95 Tenants (org.eclipse.hono.tests.Tenants)92 Optional (java.util.Optional)91 Promise (io.vertx.core.Promise)86 Device (org.eclipse.hono.service.management.device.Device)80 Adapter (org.eclipse.hono.util.Adapter)78 VertxExtension (io.vertx.junit5.VertxExtension)77 ExtendWith (org.junit.jupiter.api.extension.ExtendWith)77 Logger (org.slf4j.Logger)74