Search in sources :

Example 1 with TenantKey

use of org.eclipse.hono.deviceregistry.service.tenant.TenantKey in project hono by eclipse.

the class AbstractCredentialsServiceTest method testGetCredentialsPreservesOriginalErrorStatus.

/**
 * Verifies that when the <em>processGet</em> method returns an error result, its status
 * is adopted for the <em>get</em> method result.
 *
 * @param ctx The vert.x test context.
 */
@Test
public void testGetCredentialsPreservesOriginalErrorStatus(final VertxTestContext ctx) {
    final AbstractCredentialsService credentialsService = new AbstractCredentialsService() {

        @Override
        protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
            return Future.succeededFuture(CredentialsResult.from(HttpURLConnection.HTTP_BAD_GATEWAY));
        }
    };
    final String tenantId = "tenant";
    final String type = CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD;
    final String authId = UUID.randomUUID().toString();
    final NoopSpan span = NoopSpan.INSTANCE;
    credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult -> {
        ctx.verify(() -> {
            assertThat(getCredentialsResult.getCacheDirective()).isNotNull();
            assertThat(getCredentialsResult.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
            assertThat(getCredentialsResult.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
        });
        // another test with auto-provisioning enabled
        credentialsService.setDeviceAndGatewayAutoProvisioner(getDeviceAndGatewayAutoProvisionerMock());
        credentialsService.get(tenantId, type, authId, span).onComplete(ctx.succeeding(getCredentialsResult2 -> {
            ctx.verify(() -> {
                assertThat(getCredentialsResult2.getCacheDirective()).isNotNull();
                assertThat(getCredentialsResult2.getCacheDirective()).isEqualTo(CacheDirective.noCacheDirective());
                assertThat(getCredentialsResult2.getStatus()).isEqualTo(HttpURLConnection.HTTP_BAD_GATEWAY);
            });
            ctx.completeNow();
        }));
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CacheDirective(org.eclipse.hono.util.CacheDirective) VertxTestContext(io.vertx.junit5.VertxTestContext) CredentialsManagementService(org.eclipse.hono.service.management.credentials.CredentialsManagementService) CredentialsResult(org.eclipse.hono.util.CredentialsResult) MessagingClientProvider(org.eclipse.hono.client.util.MessagingClientProvider) Vertx(io.vertx.core.Vertx) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) VertxExtension(io.vertx.junit5.VertxExtension) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Timeout(io.vertx.junit5.Timeout) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) DeviceManagementService(org.eclipse.hono.service.management.device.DeviceManagementService) DeviceAndGatewayAutoProvisioner(org.eclipse.hono.service.management.device.DeviceAndGatewayAutoProvisioner) ExtendWith(org.junit.jupiter.api.extension.ExtendWith) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) NoopSpan(io.opentracing.noop.NoopSpan) Mockito.mock(org.mockito.Mockito.mock) NoopSpan(io.opentracing.noop.NoopSpan) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) JsonObject(io.vertx.core.json.JsonObject) CredentialsResult(org.eclipse.hono.util.CredentialsResult) Span(io.opentracing.Span) NoopSpan(io.opentracing.noop.NoopSpan) Test(org.junit.jupiter.api.Test)

Example 2 with TenantKey

use of org.eclipse.hono.deviceregistry.service.tenant.TenantKey in project hono by eclipse.

the class CredentialsServiceImpl method processGet.

@Override
protected Future<CredentialsResult<JsonObject>> processGet(final TenantKey tenant, final CredentialKey key, final JsonObject clientContext, final Span span) {
    return this.store.findCredentials(key, span.context()).map(r -> {
        if (r.isEmpty()) {
            return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
        }
        final var result = r.get();
        final var secrets = result.getCredentials().stream().map(JsonObject::mapFrom).filter(filter(key.getType(), key.getAuthId())).filter(credential -> DeviceRegistryUtils.matchesWithClientContext(credential, clientContext)).flatMap(c -> c.getJsonArray(CredentialsConstants.FIELD_SECRETS).stream().filter(JsonObject.class::isInstance).map(JsonObject.class::cast)).filter(CredentialsServiceImpl::filterSecrets).collect(Collectors.toList());
        if (secrets.isEmpty()) {
            // nothing was left after filtering ... not found
            return CredentialsResult.from(HttpURLConnection.HTTP_NOT_FOUND);
        }
        final var payload = new JsonObject().put(Constants.JSON_FIELD_DEVICE_ID, result.getDeviceId()).put(CredentialsConstants.FIELD_TYPE, key.getType()).put(CredentialsConstants.FIELD_AUTH_ID, key.getAuthId()).put(CredentialsConstants.FIELD_SECRETS, new JsonArray(secrets));
        return CredentialsResult.from(HttpURLConnection.HTTP_OK, payload, getCacheDirective(key.getType(), config.getCredentialsTtl().toSeconds()));
    });
}
Also used : HttpURLConnection(java.net.HttpURLConnection) CredentialsResult(org.eclipse.hono.util.CredentialsResult) Predicate(java.util.function.Predicate) BiFunction(java.util.function.BiFunction) CredentialKey(org.eclipse.hono.deviceregistry.service.credentials.CredentialKey) Instant(java.time.Instant) Collectors(java.util.stream.Collectors) Constants(org.eclipse.hono.util.Constants) Future(io.vertx.core.Future) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) Objects(java.util.Objects) JsonArray(io.vertx.core.json.JsonArray) OffsetDateTime(java.time.OffsetDateTime) DateTimeFormatter(java.time.format.DateTimeFormatter) AbstractCredentialsService(org.eclipse.hono.deviceregistry.service.credentials.AbstractCredentialsService) TenantKey(org.eclipse.hono.deviceregistry.service.tenant.TenantKey) TableAdapterStore(org.eclipse.hono.service.base.jdbc.store.device.TableAdapterStore) Span(io.opentracing.Span) JsonObject(io.vertx.core.json.JsonObject) DeviceServiceProperties(org.eclipse.hono.deviceregistry.jdbc.config.DeviceServiceProperties) DeviceRegistryUtils(org.eclipse.hono.deviceregistry.util.DeviceRegistryUtils) JsonArray(io.vertx.core.json.JsonArray) JsonObject(io.vertx.core.json.JsonObject)

Aggregations

Span (io.opentracing.Span)2 Future (io.vertx.core.Future)2 JsonObject (io.vertx.core.json.JsonObject)2 HttpURLConnection (java.net.HttpURLConnection)2 TenantKey (org.eclipse.hono.deviceregistry.service.tenant.TenantKey)2 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)2 CredentialsResult (org.eclipse.hono.util.CredentialsResult)2 Truth.assertThat (com.google.common.truth.Truth.assertThat)1 NoopSpan (io.opentracing.noop.NoopSpan)1 Vertx (io.vertx.core.Vertx)1 JsonArray (io.vertx.core.json.JsonArray)1 Timeout (io.vertx.junit5.Timeout)1 VertxExtension (io.vertx.junit5.VertxExtension)1 VertxTestContext (io.vertx.junit5.VertxTestContext)1 Instant (java.time.Instant)1 OffsetDateTime (java.time.OffsetDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Objects (java.util.Objects)1 UUID (java.util.UUID)1 TimeUnit (java.util.concurrent.TimeUnit)1