use of org.bf2.srs.fleetmanager.common.operation.auditing.AuditingConstants.KEY_ERROR_MESSAGE in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class AuditingAuthenticationMechanism method authenticate.
@Override
public Uni<SecurityIdentity> authenticate(RoutingContext context, IdentityProviderManager identityProviderManager) {
BiConsumer<RoutingContext, Throwable> failureHandler = context.get(QuarkusHttpUser.AUTH_FAILURE_HANDLER);
BiConsumer<RoutingContext, Throwable> auditWrapper = (ctx, ex) -> {
// this sends the http response
failureHandler.accept(ctx, ex);
// if it was an error response log it
if (ctx.response().getStatusCode() >= 400) {
var event = new AuditingEvent();
event.setEventId("authentication_failure");
event.addData(KEY_REQUEST_SOURCE_IP, ctx.request().remoteAddress());
event.addData(KEY_REQUEST_FORWARDED_FOR, ctx.request().getHeader(HEADER_X_FORWARDED_FOR));
event.addData(KEY_REQUEST_METHOD, ctx.request().method().name());
event.addData(KEY_REQUEST_PATH, ctx.request().path());
event.addData(KEY_RESPONSE_CODE, ctx.response().getStatusCode());
event.setSuccessful(false);
if (ex != null) {
event.addData(KEY_ERROR_MESSAGE, ex.getMessage());
}
// Request Context does not exist at this point
AuditingServiceImpl.recordEventNoContext(event);
}
};
context.put(QuarkusHttpUser.AUTH_FAILURE_HANDLER, auditWrapper);
Timer.Sample sample = timerService.start();
return oidcAuthenticationMechanism.authenticate(context, identityProviderManager).onItemOrFailure().invoke((securityIdentity, throwable) -> {
timerService.record(AUTH_TIMER, AUTH_TIMER_DESCRIPTION, throwable == null ? null : List.of(Tag.of(TAG_STATUS_CODE_FAMILY, "4xx")), sample);
});
}
use of org.bf2.srs.fleetmanager.common.operation.auditing.AuditingConstants.KEY_ERROR_MESSAGE in project srs-fleet-manager by bf2fc6cc711aee1a0c2a.
the class AuditingInterceptor method intercept.
@AroundInvoke
public Object intercept(InvocationContext context) throws Exception {
Audited annotation = context.getMethod().getAnnotation(Audited.class);
if (annotation.extractParameters().length % 2 != 0)
throw new IllegalStateException("Field @Audited.extractParameters on method '" + context.getMethod().getName() + "' must contain an even number of elements.");
var event = new AuditingEvent();
if (securityIdentity != null && !securityIdentity.isAnonymous()) {
event.addData(KEY_PRINCIPAL_ID, securityIdentity.getPrincipal().getName());
}
// Event ID
var eventId = annotation.eventId();
if (eventId.isBlank()) {
eventId = EVENT_ID_METHOD_CALL_PREFIX + context.getMethod().getName();
}
event.setEventId(eventId);
event.addData(KEY_CLASS, context.getTarget().getClass().getCanonicalName());
// Event Description
var eventDescription = annotation.eventDescription();
if (!eventDescription.isBlank()) {
event.setEventDescription(eventDescription);
}
// Parameter extraction via annotation
var annotationParams = annotation.extractParameters();
if (annotationParams.length > 0) {
for (int i = 0; i <= annotationParams.length - 2; i += 2) {
var key = annotationParams[i + 1];
var value = context.getParameters()[Integer.parseInt(annotationParams[i])];
event.addData(key, value);
}
}
// Parameter extraction via extractors
for (Object param : context.getParameters()) {
if (param != null) {
var extractor = PARAMETER_EXTRACTORS.get(param.getClass());
if (extractor != null) {
extractor.accept(param, event);
}
}
}
try {
var result = context.proceed();
event.setSuccessful(true);
if (result != null) {
// Return value extraction via annotation
if (!annotation.extractResult().isBlank()) {
var key = annotation.extractResult();
event.addData(key, result);
}
// Return value extraction via extractors
var extractor = PARAMETER_EXTRACTORS.get(result.getClass());
if (extractor != null) {
extractor.accept(result, event);
}
}
return result;
} catch (Exception ex) {
event.setSuccessful(false);
var message = ex.getClass().getCanonicalName() + (ex.getMessage() != null ? ": " + ex.getMessage() : "");
event.addData(KEY_ERROR_MESSAGE, shorten(message, 120));
throw ex;
} finally {
auditing.recordEvent(event);
}
}
Aggregations