use of io.opentracing.SpanContext in project hono by eclipse.
the class CredentialsApiAuthProvider method authenticate.
@Override
public final void authenticate(final T deviceCredentials, final SpanContext spanContext, final Handler<AsyncResult<DeviceUser>> resultHandler) {
Objects.requireNonNull(deviceCredentials);
Objects.requireNonNull(resultHandler);
final Span currentSpan = TracingHelper.buildServerChildSpan(tracer, spanContext, "authenticate device", getClass().getSimpleName()).withTag(MessageHelper.APP_PROPERTY_TENANT_ID, deviceCredentials.getTenantId()).withTag(TracingHelper.TAG_AUTH_ID.getKey(), deviceCredentials.getAuthId()).start();
getCredentialsForDevice(deviceCredentials, currentSpan.context()).recover(t -> Future.failedFuture(mapNotFoundToBadCredentialsException(t))).compose(credentialsOnRecord -> validateCredentials(deviceCredentials, credentialsOnRecord, currentSpan.context())).map(device -> new DeviceUser(device.getTenantId(), device.getDeviceId())).onComplete(authAttempt -> {
if (authAttempt.succeeded()) {
currentSpan.log("successfully authenticated device");
} else {
currentSpan.log("authentication of device failed");
TracingHelper.logError(currentSpan, authAttempt.cause());
}
currentSpan.finish();
resultHandler.handle(authAttempt);
});
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class CredentialsApiAuthProvider method validateCredentials.
/**
* Verifies that the credentials provided by a device during the authentication
* process match the credentials on record for that device.
*
* @param deviceCredentials The credentials provided by the device.
* @param credentialsOnRecord The credentials to match against.
* @param spanContext The OpenTracing context to use for tracking the operation.
* @return A future that is succeeded with the authenticated device if the
* credentials have been validated successfully. Otherwise, the
* future is failed with a {@link ServiceInvocationException}.
*/
private Future<Device> validateCredentials(final T deviceCredentials, final CredentialsObject credentialsOnRecord, final SpanContext spanContext) {
final Span currentSpan = TracingHelper.buildServerChildSpan(tracer, spanContext, "validate credentials", getClass().getSimpleName()).withTag(MessageHelper.APP_PROPERTY_TENANT_ID, deviceCredentials.getTenantId()).withTag(TracingHelper.TAG_AUTH_ID.getKey(), deviceCredentials.getAuthId()).withTag(TracingHelper.TAG_CREDENTIALS_TYPE.getKey(), deviceCredentials.getType()).start();
final Promise<Device> result = Promise.promise();
if (!deviceCredentials.getAuthId().equals(credentialsOnRecord.getAuthId())) {
currentSpan.log(String.format("Credentials service returned wrong credentials-on-record [auth-id: %s]", credentialsOnRecord.getAuthId()));
result.fail(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR));
} else if (!deviceCredentials.getType().equals(credentialsOnRecord.getType())) {
currentSpan.log(String.format("Credentials service returned wrong credentials-on-record [type: %s]", credentialsOnRecord.getType()));
result.fail(new ServerErrorException(HttpURLConnection.HTTP_INTERNAL_ERROR));
} else if (!credentialsOnRecord.isEnabled()) {
currentSpan.log("credentials-on-record are disabled");
result.fail(new ClientErrorException(HttpURLConnection.HTTP_UNAUTHORIZED));
} else {
doValidateCredentials(deviceCredentials, credentialsOnRecord).onComplete(result);
}
return result.future().map(device -> {
currentSpan.log("validation of credentials succeeded");
currentSpan.finish();
return device;
}).recover(t -> {
currentSpan.log("validation of credentials failed");
TracingHelper.logError(currentSpan, t);
currentSpan.finish();
return Future.failedFuture(t);
});
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class AbstractProtocolAdapterBase method getRegistrationAssertion.
@Override
public final Future<RegistrationAssertion> getRegistrationAssertion(final String tenantId, final String deviceId, final Device authenticatedDevice, final SpanContext context) {
Objects.requireNonNull(tenantId);
Objects.requireNonNull(deviceId);
final Future<String> gatewayId = getGatewayId(tenantId, deviceId, authenticatedDevice);
return gatewayId.compose(gwId -> getRegistrationClient().assertRegistration(tenantId, deviceId, gwId, context)).onSuccess(assertion -> {
// the updateLastGateway invocation shouldn't delay or possibly fail the surrounding operation
// so don't wait for the outcome here
updateLastGateway(assertion, tenantId, deviceId, authenticatedDevice, context).onFailure(t -> {
log.warn("failed to update last gateway [tenantId: {}, deviceId: {}]", tenantId, deviceId, t);
});
}).recover(error -> {
final int errorCode = ServiceInvocationException.extractStatusCode(error);
if (errorCode == HttpURLConnection.HTTP_NOT_FOUND) {
return Future.failedFuture(new DeviceDisabledOrNotRegisteredException(tenantId, errorCode));
} else if (errorCode == HttpURLConnection.HTTP_FORBIDDEN) {
return Future.failedFuture(new GatewayDisabledOrNotRegisteredException(tenantId, errorCode));
} else {
return Future.failedFuture(error);
}
});
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class AbstractMessageSenderConnectionEventProducer method sendNotificationEvent.
private Future<Void> sendNotificationEvent(final Context context, final Device authenticatedDevice, final String protocolAdapter, final String remoteId, final String cause, final JsonObject data, final SpanContext spanContext) {
if (authenticatedDevice == null) {
// we only handle authenticated devices
return Future.succeededFuture();
}
final String tenantId = authenticatedDevice.getTenantId();
final String deviceId = authenticatedDevice.getDeviceId();
return context.getTenantClient().get(tenantId, spanContext).compose(tenant -> {
final JsonObject payload = new JsonObject();
payload.put("cause", cause);
payload.put("remote-id", remoteId);
payload.put("source", protocolAdapter);
if (data != null) {
payload.put("data", data);
}
return Optional.ofNullable(context.getMessageSenderClient()).map(client -> client.sendEvent(tenant, new RegistrationAssertion(deviceId), EventConstants.EVENT_CONNECTION_NOTIFICATION_CONTENT_TYPE, payload.toBuffer(), null, spanContext)).orElseGet(Future::succeededFuture);
});
}
use of io.opentracing.SpanContext in project hono by eclipse.
the class KafkaHeadersInjectExtractAdapterTest method testJaegerTracerCanUseAdapter.
/**
* Verifies that the Jaeger tracer implementation can successfully use the adapters to inject and extract
* a SpanContext.
*/
@Test
public void testJaegerTracerCanUseAdapter() {
final Configuration config = new Configuration("test");
final Tracer tracer = config.getTracer();
final Span span = tracer.buildSpan("do").start();
final List<KafkaHeader> headers = new ArrayList<>();
final KafkaHeadersInjectAdapter injectAdapter = new KafkaHeadersInjectAdapter(headers);
tracer.inject(span.context(), Format.Builtin.TEXT_MAP, injectAdapter);
final SpanContext context = tracer.extract(Format.Builtin.TEXT_MAP, new KafkaHeadersExtractAdapter(headers));
assertThat(context.toSpanId()).isEqualTo(span.context().toSpanId());
}
Aggregations