use of org.eclipse.hono.deviceregistry.util.FieldLevelEncryption in project hono by eclipse.
the class MongoDbBasedCredentialsDao method update.
/**
* {@inheritDoc}
*/
@Override
public Future<String> update(final CredentialsDto credentials, final Optional<String> resourceVersion, final SpanContext tracingContext) {
Objects.requireNonNull(credentials);
Objects.requireNonNull(resourceVersion);
final Span span = tracer.buildSpan("update Credentials").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, credentials.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, credentials.getDeviceId()).start();
resourceVersion.ifPresent(v -> TracingHelper.TAG_RESOURCE_VERSION.set(span, v));
credentials.getCredentials().stream().forEach(cred -> cred.encryptFields(fieldLevelEncryption));
final JsonObject replaceCredentialsQuery = MongoDbDocumentBuilder.builder().withVersion(resourceVersion).withTenantId(credentials.getTenantId()).withDeviceId(credentials.getDeviceId()).document();
final var document = JsonObject.mapFrom(credentials);
if (LOG.isTraceEnabled()) {
LOG.trace("updating credentials of device [tenant: {}, device-id: {}, resource-version; {}]:{}{}", credentials.getTenantId(), credentials.getDeviceId(), resourceVersion.orElse(null), System.lineSeparator(), document.encodePrettily());
}
return mongoClient.findOneAndReplaceWithOptions(collectionName, replaceCredentialsQuery, document, new FindOptions(), new UpdateOptions().setReturningNewDocument(true)).compose(result -> {
if (result == null) {
return MongoDbBasedDao.checkForVersionMismatchAndFail(String.format("credentials [tenant-id: %s, device-id: %s]", credentials.getTenantId(), credentials.getDeviceId()), resourceVersion, getByDeviceId(credentials.getTenantId(), credentials.getDeviceId()));
} else {
LOG.debug("successfully updated credentials for device [tenant: {}, device-id: {}]", credentials.getTenantId(), credentials.getDeviceId());
span.log("successfully updated credentials");
if (LOG.isTraceEnabled()) {
LOG.trace("new document in DB:{}{}", System.lineSeparator(), result.encodePrettily());
}
return Future.succeededFuture(result.getString(CredentialsDto.FIELD_VERSION));
}
}).recover(error -> {
if (MongoDbBasedDao.isDuplicateKeyError(error)) {
return Future.failedFuture(new ClientErrorException(credentials.getTenantId(), HttpURLConnection.HTTP_CONFLICT, "credentials (type, auth-id) must be unique for device"));
} else {
return Future.failedFuture(error);
}
}).onFailure(error -> {
LOG.debug("error updating credentials", error);
TracingHelper.logError(span, "error updating credentials", error);
}).recover(this::mapError).onComplete(r -> span.finish());
}
use of org.eclipse.hono.deviceregistry.util.FieldLevelEncryption in project hono by eclipse.
the class MongoDbBasedCredentialsDao method getByAuthIdAndType.
/**
* {@inheritDoc}
*/
@Override
public Future<CredentialsDto> getByAuthIdAndType(final String tenantId, final String authId, final String type, final SpanContext tracingContext) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(authId);
Objects.requireNonNull(type);
final Span span = tracer.buildSpan("get Credentials by auth ID and type").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, tenantId).withTag(TracingHelper.TAG_AUTH_ID, authId).withTag(TracingHelper.TAG_CREDENTIALS_TYPE, type).start();
final JsonObject filter = MongoDbDocumentBuilder.builder().withTenantId(tenantId).withAuthId(authId).withType(type).document();
if (LOG.isTraceEnabled()) {
LOG.trace("retrieving credentials using filter:{}{}", System.lineSeparator(), filter.encodePrettily());
}
return mongoClient.findOne(collectionName, filter, PROJECTION_CREDS_BY_TYPE_AND_AUTH_ID).map(result -> {
if (result == null) {
throw new ClientErrorException(tenantId, HttpURLConnection.HTTP_NOT_FOUND, "no matching credentials on record");
} else {
if (LOG.isTraceEnabled()) {
LOG.trace("credentials data from collection:{}{}", System.lineSeparator(), result.encodePrettily());
}
final var dto = result.mapTo(CredentialsDto.class);
dto.getCredentials().stream().forEach(cred -> cred.decryptFields(fieldLevelEncryption));
return dto;
}
}).onFailure(t -> {
LOG.debug("error retrieving credentials by auth-id and type", t);
TracingHelper.logError(span, "error retrieving credentials by auth-id and type", t);
}).recover(this::mapError).onComplete(r -> span.finish());
}
use of org.eclipse.hono.deviceregistry.util.FieldLevelEncryption in project hono by eclipse.
the class MongoDbBasedCredentialsDao method create.
/**
* {@inheritDoc}
*/
@Override
public Future<String> create(final CredentialsDto credentials, final SpanContext tracingContext) {
Objects.requireNonNull(credentials);
final Span span = tracer.buildSpan("add Credentials").addReference(References.CHILD_OF, tracingContext).withTag(TracingHelper.TAG_TENANT_ID, credentials.getTenantId()).withTag(TracingHelper.TAG_DEVICE_ID, credentials.getDeviceId()).start();
credentials.getCredentials().stream().forEach(cred -> cred.encryptFields(fieldLevelEncryption));
final var document = JsonObject.mapFrom(credentials);
if (LOG.isTraceEnabled()) {
LOG.trace("creating credentials for device [tenant: {}, device-id: {}, resource-version; {}]:{}{}", credentials.getTenantId(), credentials.getDeviceId(), credentials.getVersion(), System.lineSeparator(), document.encodePrettily());
}
return mongoClient.insert(collectionName, document).map(added -> {
span.log("successfully added credentials");
LOG.debug("successfully added credentials for device [tenant: {}, device-id: {}, resource-version: {}]", credentials.getTenantId(), credentials.getDeviceId(), credentials.getVersion());
return credentials.getVersion();
}).onFailure(t -> {
LOG.debug("error adding credentials for device [tenant: {}, device-id: {}]", credentials.getTenantId(), credentials.getDeviceId(), t);
TracingHelper.logError(span, "error adding credentials", t);
}).recover(this::mapError).onComplete(r -> span.finish());
}
Aggregations