Search in sources :

Example 11 with CommonCredential

use of org.eclipse.hono.service.management.credentials.CommonCredential in project hono by eclipse.

the class TableAdapterStore method findCredentials.

/**
 * Find credentials for a device.
 *
 * @param key The credentials key to look for.
 * @param spanContext The span context.
 *
 * @return A future tracking the outcome of the operation.
 */
public Future<Optional<CredentialsReadResult>> findCredentials(final CredentialKey key, final SpanContext spanContext) {
    final Span span = TracingHelper.buildChildSpan(this.tracer, spanContext, "find credentials", getClass().getSimpleName()).withTag(TracingHelper.TAG_AUTH_ID, key.getAuthId()).withTag(TracingHelper.TAG_CREDENTIALS_TYPE, key.getType()).withTag(TracingHelper.TAG_TENANT_ID, key.getTenantId()).start();
    final var expanded = this.findCredentialsStatement.expand(params -> {
        params.put("tenant_id", key.getTenantId());
        params.put("type", key.getType());
        params.put("auth_id", key.getAuthId());
    });
    log.debug("findCredentials - statement: {}", expanded);
    return expanded.trace(this.tracer, span.context()).query(this.client).<Optional<CredentialsReadResult>>flatMap(r -> {
        final var entries = r.getRows(true);
        span.log(Map.of("event", "read result", "rows", entries.size()));
        final Set<String> deviceIds = entries.stream().map(o -> o.getString("device_id")).filter(Objects::nonNull).collect(Collectors.toSet());
        final int num = deviceIds.size();
        if (num <= 0) {
            return Future.succeededFuture(Optional.empty());
        } else if (num > 1) {
            TracingHelper.logError(span, "Found multiple entries for a single device");
            return Future.failedFuture(new IllegalStateException("Found multiple entries for a single device"));
        }
        // we know now that we have exactly one entry
        final String deviceId = deviceIds.iterator().next();
        final List<CommonCredential> credentials = entries.stream().map(o -> o.getString("data")).map(s -> Json.decodeValue(s, CommonCredential.class)).collect(Collectors.toList());
        return Future.succeededFuture(Optional.of(new CredentialsReadResult(deviceId, credentials, Optional.empty())));
    }).onComplete(x -> span.finish());
}
Also used : SQL(org.eclipse.hono.service.base.jdbc.store.SQL) Device(org.eclipse.hono.service.management.device.Device) Json(io.vertx.core.json.Json) Logger(org.slf4j.Logger) Tracer(io.opentracing.Tracer) Collection(java.util.Collection) LoggerFactory(org.slf4j.LoggerFactory) Set(java.util.Set) DeviceKey(org.eclipse.hono.deviceregistry.service.device.DeviceKey) CredentialKey(org.eclipse.hono.deviceregistry.service.credentials.CredentialKey) Collectors(java.util.stream.Collectors) Future(io.vertx.core.Future) Statement(org.eclipse.hono.service.base.jdbc.store.Statement) SpanContext(io.opentracing.SpanContext) Objects(java.util.Objects) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JDBCClient(io.vertx.ext.jdbc.JDBCClient) Map(java.util.Map) ResultSet(io.vertx.ext.sql.ResultSet) Optional(java.util.Optional) Span(io.opentracing.Span) TracingHelper(org.eclipse.hono.tracing.TracingHelper) StatementConfiguration(org.eclipse.hono.service.base.jdbc.store.StatementConfiguration) Set(java.util.Set) ResultSet(io.vertx.ext.sql.ResultSet) Objects(java.util.Objects) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) List(java.util.List) Span(io.opentracing.Span)

Example 12 with CommonCredential

use of org.eclipse.hono.service.management.credentials.CommonCredential in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsFailsForDisabledCredentials.

/**
 * Verifies that the service fails when the credentials set is disabled.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsFailsForDisabledCredentials(final VertxTestContext ctx) {
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final String authId = UUID.randomUUID().toString();
    final CommonCredential credential = getRandomHashedPasswordCredential(authId);
    credential.setEnabled(false);
    getHelper().registry.registerDevice(tenantId, deviceId).compose(ok -> getHelper().registry.addCredentials(tenantId, deviceId, Collections.singleton(credential))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, spanContext)).onComplete(ctx.failing(t -> {
        ctx.verify(() -> assertErrorCode(t, HttpURLConnection.HTTP_NOT_FOUND));
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) CertificateFactory(java.security.cert.CertificateFactory) LoggerFactory(org.slf4j.LoggerFactory) Credentials(org.eclipse.hono.service.management.credentials.Credentials) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) OptionalInt(java.util.OptionalInt) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Vertx(io.vertx.core.Vertx) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) FileNotFoundException(java.io.FileNotFoundException) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) AuthenticationConstants(org.eclipse.hono.util.AuthenticationConstants) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Checkpoint(io.vertx.junit5.Checkpoint) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) CredentialsObject(org.eclipse.hono.util.CredentialsObject) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CertificateEncodingException(java.security.cert.CertificateEncodingException) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Example 13 with CommonCredential

use of org.eclipse.hono.service.management.credentials.CommonCredential in project hono by eclipse.

the class CredentialsApiTests method testGetCredentialsByClientContext.

/**
 * Verifies that the service returns credentials for a given type, authentication ID and matching client context.
 *
 * @param ctx The vert.x test context.
 */
@Timeout(value = 5, timeUnit = TimeUnit.SECONDS)
@Test
public void testGetCredentialsByClientContext(final VertxTestContext ctx) {
    final String deviceId = getHelper().getRandomDeviceId(tenantId);
    final String authId = UUID.randomUUID().toString();
    final CommonCredential credentials = getRandomHashedPasswordCredential(authId).putExtension("client-id", "gateway-one");
    final JsonObject clientContext = new JsonObject().put("client-id", "gateway-one");
    getHelper().registry.registerDevice(tenantId, deviceId).compose(httpResponse -> getHelper().registry.addCredentials(tenantId, deviceId, List.of(credentials))).compose(ok -> getClient().get(tenantId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, authId, clientContext, spanContext)).onComplete(ctx.succeeding(result -> {
        ctx.verify(() -> {
            assertStandardProperties(result, deviceId, authId, CredentialsConstants.SECRETS_TYPE_HASHED_PASSWORD, 2);
        });
        ctx.completeNow();
    }));
}
Also used : HttpURLConnection(java.net.HttpURLConnection) X509Certificate(java.security.cert.X509Certificate) VertxTestContext(io.vertx.junit5.VertxTestContext) BeforeEach(org.junit.jupiter.api.BeforeEach) X500Principal(javax.security.auth.x500.X500Principal) DownstreamMessage(org.eclipse.hono.application.client.DownstreamMessage) CertificateFactory(java.security.cert.CertificateFactory) LoggerFactory(org.slf4j.LoggerFactory) Credentials(org.eclipse.hono.service.management.credentials.Credentials) SelfSignedCertificate(io.vertx.core.net.SelfSignedCertificate) OptionalInt(java.util.OptionalInt) Tenant(org.eclipse.hono.service.management.tenant.Tenant) Timeout(io.vertx.junit5.Timeout) IntegrationTestSupport(org.eclipse.hono.tests.IntegrationTestSupport) CredentialsClient(org.eclipse.hono.client.registry.CredentialsClient) JsonObject(io.vertx.core.json.JsonObject) Tenants(org.eclipse.hono.tests.Tenants) RegistryManagementConstants(org.eclipse.hono.util.RegistryManagementConstants) Device(org.eclipse.hono.service.management.device.Device) Logger(org.slf4j.Logger) Truth.assertWithMessage(com.google.common.truth.Truth.assertWithMessage) Vertx(io.vertx.core.Vertx) FileInputStream(java.io.FileInputStream) CertificateException(java.security.cert.CertificateException) UUID(java.util.UUID) Truth.assertThat(com.google.common.truth.Truth.assertThat) Instant(java.time.Instant) DeviceStatus(org.eclipse.hono.service.management.device.DeviceStatus) MessageHelper(org.eclipse.hono.util.MessageHelper) EventConstants(org.eclipse.hono.util.EventConstants) FileNotFoundException(java.io.FileNotFoundException) CredentialsConstants(org.eclipse.hono.util.CredentialsConstants) AuthenticationConstants(org.eclipse.hono.util.AuthenticationConstants) SpanContext(io.opentracing.SpanContext) TimeUnit(java.util.concurrent.TimeUnit) Test(org.junit.jupiter.api.Test) List(java.util.List) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) Checkpoint(io.vertx.junit5.Checkpoint) NoopSpan(io.opentracing.noop.NoopSpan) Collections(java.util.Collections) CredentialsObject(org.eclipse.hono.util.CredentialsObject) PasswordCredential(org.eclipse.hono.service.management.credentials.PasswordCredential) CertificateEncodingException(java.security.cert.CertificateEncodingException) CommonCredential(org.eclipse.hono.service.management.credentials.CommonCredential) JsonObject(io.vertx.core.json.JsonObject) Test(org.junit.jupiter.api.Test) Timeout(io.vertx.junit5.Timeout)

Aggregations

CommonCredential (org.eclipse.hono.service.management.credentials.CommonCredential)13 List (java.util.List)12 HttpURLConnection (java.net.HttpURLConnection)10 Truth.assertThat (com.google.common.truth.Truth.assertThat)9 JsonObject (io.vertx.core.json.JsonObject)9 VertxTestContext (io.vertx.junit5.VertxTestContext)9 UUID (java.util.UUID)9 CredentialsConstants (org.eclipse.hono.util.CredentialsConstants)9 Test (org.junit.jupiter.api.Test)9 Logger (org.slf4j.Logger)9 LoggerFactory (org.slf4j.LoggerFactory)9 Optional (java.util.Optional)8 RegistryManagementConstants (org.eclipse.hono.util.RegistryManagementConstants)8 BeforeEach (org.junit.jupiter.api.BeforeEach)8 Timeout (io.vertx.junit5.Timeout)7 Vertx (io.vertx.core.Vertx)6 Instant (java.time.Instant)6 Collections (java.util.Collections)6 Map (java.util.Map)6 PasswordCredential (org.eclipse.hono.service.management.credentials.PasswordCredential)6