Search in sources :

Example 1 with CredentialKey

use of org.eclipse.hono.deviceregistry.service.credentials.CredentialKey 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)

Example 2 with CredentialKey

use of org.eclipse.hono.deviceregistry.service.credentials.CredentialKey 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)

Aggregations

Span (io.opentracing.Span)2 Future (io.vertx.core.Future)2 Objects (java.util.Objects)2 Collectors (java.util.stream.Collectors)2 CredentialKey (org.eclipse.hono.deviceregistry.service.credentials.CredentialKey)2 SpanContext (io.opentracing.SpanContext)1 Tracer (io.opentracing.Tracer)1 Json (io.vertx.core.json.Json)1 JsonArray (io.vertx.core.json.JsonArray)1 JsonObject (io.vertx.core.json.JsonObject)1 JDBCClient (io.vertx.ext.jdbc.JDBCClient)1 ResultSet (io.vertx.ext.sql.ResultSet)1 HttpURLConnection (java.net.HttpURLConnection)1 Instant (java.time.Instant)1 OffsetDateTime (java.time.OffsetDateTime)1 DateTimeFormatter (java.time.format.DateTimeFormatter)1 Collection (java.util.Collection)1 List (java.util.List)1 Map (java.util.Map)1 Optional (java.util.Optional)1