Search in sources :

Example 31 with Tenant

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

the class TenantManagementIT method testAddTenantSucceedsForCaSharedWithinTrustAnchorGroup.

/**
 * Verifies that the service successfully creates a tenant containing a CA that is also used by
 * an existing tenant that belongs to the same trust anchor group.
 *
 * @param context The Vert.x test context.
 */
@Test
public void testAddTenantSucceedsForCaSharedWithinTrustAnchorGroup(final VertxTestContext context) {
    assumeTrue(IntegrationTestSupport.isTrustAnchorGroupsSupported(), "device registry does not support trust anchor groups");
    final PublicKey publicKey = TenantApiTests.getRandomPublicKey();
    final TrustedCertificateAuthority trustAnchor = Tenants.createTrustAnchor("test-ca", "CN=test-dn", publicKey.getEncoded(), publicKey.getAlgorithm(), Instant.now(), Instant.now().plus(365, ChronoUnit.DAYS));
    final Tenant tenant = new Tenant().setTrustAnchorGroup("test-group").setTrustedCertificateAuthorities(List.of(trustAnchor));
    final String tenantId = getHelper().getRandomTenantId();
    getHelper().registry.addTenant(tenantId, tenant).onFailure(context::failNow).compose(ok -> getHelper().registry.addTenant(getHelper().getRandomTenantId(), tenant)).onComplete(context.succeedingThenComplete());
}
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) PublicKey(java.security.PublicKey) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Test(org.junit.jupiter.api.Test)

Example 32 with Tenant

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

the class TenantManagementIT method testUpdateTenantPreventsEmptyCaArray.

/**
 * Verifies that setting an empty list of trusted CAs on multiple tenants does not result in a unique key
 * violation.
 *
 * @param context The Vert.x test context.
 */
@Test
public void testUpdateTenantPreventsEmptyCaArray(final VertxTestContext context) {
    final String tenantId = getHelper().getRandomTenantId();
    final var tenantTwoId = getHelper().getRandomTenantId();
    final PublicKey publicKey = TenantApiTests.getRandomPublicKey();
    final TrustedCertificateAuthority trustAnchor1 = Tenants.createTrustAnchor("test-ca", "CN=test-dn-1", publicKey.getEncoded(), publicKey.getAlgorithm(), Instant.now(), Instant.now().plus(365, ChronoUnit.DAYS));
    final TrustedCertificateAuthority trustAnchor2 = Tenants.createTrustAnchor(null, "CN=test-dn-2", publicKey.getEncoded(), publicKey.getAlgorithm(), Instant.now(), Instant.now().plus(365, ChronoUnit.DAYS));
    getHelper().registry.addTenant(tenantId, new Tenant().setTrustedCertificateAuthorities(List.of(trustAnchor1))).compose(ok -> getHelper().registry.addTenant(tenantTwoId, new Tenant().setTrustedCertificateAuthorities(List.of(trustAnchor2)))).compose(ok -> getHelper().registry.updateTenant(tenantId, new Tenant().setTrustedCertificateAuthorities(List.of()), HttpURLConnection.HTTP_NO_CONTENT)).compose(ok -> getHelper().registry.updateTenant(tenantTwoId, new Tenant().setTrustedCertificateAuthorities(List.of()), HttpURLConnection.HTTP_NO_CONTENT)).onComplete(context.succeedingThenComplete());
}
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) PublicKey(java.security.PublicKey) TrustedCertificateAuthority(org.eclipse.hono.service.management.tenant.TrustedCertificateAuthority) Test(org.junit.jupiter.api.Test)

Example 33 with Tenant

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

the class MongoDbBasedTenantServiceTest method testUpdateTenantFailsForDuplicateAlias.

/**
 * Verifies that a tenant cannot be updated with an alias that is already in use by another tenant.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testUpdateTenantFailsForDuplicateAlias(final VertxTestContext ctx) {
    final var tenantSpec = new Tenant().setAlias("the-alias");
    addTenant("tenant", tenantSpec).compose(ok -> getTenantManagementService().createTenant(Optional.of("other-tenant"), new Tenant(), NoopSpan.INSTANCE)).compose(ok -> getTenantManagementService().updateTenant("other-tenant", tenantSpec, Optional.empty(), NoopSpan.INSTANCE)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> Assertions.assertServiceInvocationException(t, HttpURLConnection.HTTP_CONFLICT));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) BeforeEach(org.junit.jupiter.api.BeforeEach) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TenantService(org.eclipse.hono.service.tenant.TenantService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) MongoDbBasedTenantsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedTenantsConfigProperties) AfterEach(org.junit.jupiter.api.AfterEach) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) MongoDbBasedTenantDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedTenantDao) Tenant(org.eclipse.hono.service.management.tenant.Tenant) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) Test(org.junit.jupiter.api.Test)

Example 34 with Tenant

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

the class MongoDbBasedTenantServiceTest method testGetTenantByAliasSucceedsForExistingTenant.

/**
 * Verifies that a tenant can be looked up by its alias using the {@link TenantManagementService} API.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetTenantByAliasSucceedsForExistingTenant(final VertxTestContext ctx) {
    final Tenant tenantSpec = new Tenant().setAlias("the-alias");
    // GIVEN a tenant that has been added via the Management API
    addTenant("tenant", tenantSpec).compose(ok -> {
        ctx.verify(() -> {
            assertEquals(HttpURLConnection.HTTP_CREATED, ok.getStatus());
        });
        // WHEN retrieving the tenant by alias using the Tenant API
        return getTenantService().get("the-alias", NoopSpan.INSTANCE);
    }).onComplete(ctx.succeeding(tenantResult -> {
        ctx.verify(() -> {
            // THEN the tenant is found
            assertThat(tenantResult.isOk()).isTrue();
            // and the response can be cached
            assertThat(tenantResult.getCacheDirective()).isNotNull();
            assertThat(tenantResult.getCacheDirective().isCachingAllowed()).isTrue();
            assertThat(tenantResult.getPayload().getString(RegistryManagementConstants.FIELD_PAYLOAD_TENANT_ID)).isEqualTo("tenant");
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) VertxTestContext(io.vertx.junit5.VertxTestContext) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) BeforeEach(org.junit.jupiter.api.BeforeEach) LoggerFactory(org.slf4j.LoggerFactory) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) AfterAll(org.junit.jupiter.api.AfterAll) TestInstance(org.junit.jupiter.api.TestInstance) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) BeforeAll(org.junit.jupiter.api.BeforeAll) TenantManagementService(org.eclipse.hono.service.management.tenant.TenantManagementService) TenantService(org.eclipse.hono.service.tenant.TenantService) Assertions.assertEquals(org.junit.jupiter.api.Assertions.assertEquals) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Promise(io.vertx.core.Promise) Vertx(io.vertx.core.Vertx) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) TestInfo(org.junit.jupiter.api.TestInfo) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) MongoDbBasedTenantsConfigProperties(org.eclipse.hono.deviceregistry.mongodb.config.MongoDbBasedTenantsConfigProperties) AfterEach(org.junit.jupiter.api.AfterEach) Optional(java.util.Optional) Assertions(org.eclipse.hono.deviceregistry.util.Assertions) NoopSpan(io.opentracing.noop.NoopSpan) MongoDbBasedTenantDao(org.eclipse.hono.deviceregistry.mongodb.model.MongoDbBasedTenantDao) Tenant(org.eclipse.hono.service.management.tenant.Tenant) AbstractTenantServiceTest(org.eclipse.hono.service.tenant.AbstractTenantServiceTest) Test(org.junit.jupiter.api.Test)

Example 35 with Tenant

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

the class MongoDbBasedTenantDao method create.

/**
 * {@inheritDoc}
 */
@Override
public Future<String> create(final TenantDto tenantConfig, final SpanContext tracingContext) {
    Objects.requireNonNull(tenantConfig);
    final Span span = tracer.buildSpan("create Tenant").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantConfig.getTenantId()).start();
    final JsonObject newTenantDtoJson = JsonObject.mapFrom(tenantConfig);
    if (LOG.isTraceEnabled()) {
        LOG.trace("creating tenant:{}{}", System.lineSeparator(), newTenantDtoJson.encodePrettily());
    }
    return validateTrustAnchors(tenantConfig, span).compose(ok -> mongoClient.insert(collectionName, newTenantDtoJson)).map(tenantObjectIdResult -> {
        LOG.debug("successfully created tenant [tenant-id: {}, version: {}]", tenantConfig.getTenantId(), tenantConfig.getVersion());
        span.log("successfully created tenant");
        return tenantConfig.getVersion();
    }).recover(error -> {
        if (MongoDbBasedDao.isDuplicateKeyError(error)) {
            TracingHelper.logError(span, "tenant already exists");
            return Future.failedFuture(new ClientErrorException(tenantConfig.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "tenant already exists"));
        } else {
            TracingHelper.logError(span, "error creating tenant", error);
            return mapError(error);
        }
    }).onComplete(r -> span.finish());
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X500Principal(javax.security.auth.x500.X500Principal) Filter(org.eclipse.hono.service.management.Filter) LoggerFactory(org.slf4j.LoggerFactory) AtomicBoolean(java.util.concurrent.atomic.AtomicBoolean) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Constants(org.eclipse.hono.util.Constants) Tenant(org.eclipse.hono.service.management.tenant.Tenant) ArrayList(java.util.ArrayList) Status(io.vertx.ext.healthchecks.Status) Sort(org.eclipse.hono.service.management.Sort) HealthCheckHandler(io.vertx.ext.healthchecks.HealthCheckHandler) HealthCheckProvider(org.eclipse.hono.service.HealthCheckProvider) SearchResult(org.eclipse.hono.service.management.SearchResult) References(io.opentracing.References) JsonObject(io.vertx.core.json.JsonObject) TracingHelper(org.eclipse.hono.tracing.TracingHelper) TenantDto(org.eclipse.hono.service.management.tenant.TenantDto) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Promise(io.vertx.core.Promise) TenantWithId(org.eclipse.hono.service.management.tenant.TenantWithId) MongoClient(io.vertx.ext.mongo.MongoClient) UUID(java.util.UUID) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) IndexOptions(io.vertx.ext.mongo.IndexOptions) Optional(java.util.Optional) Span(io.opentracing.Span) FindOptions(io.vertx.ext.mongo.FindOptions) UpdateOptions(io.vertx.ext.mongo.UpdateOptions) MongoDbDocumentBuilder(org.eclipse.hono.deviceregistry.mongodb.utils.MongoDbDocumentBuilder) JsonObject(io.vertx.core.json.JsonObject) ClientErrorException(org.eclipse.hono.client.ClientErrorException) Span(io.opentracing.Span)

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