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());
}
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());
}
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();
}));
}
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();
}));
}
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());
}
Aggregations