Search in sources :

Example 1 with AuditException

use of org.forgerock.audit.AuditException in project OpenAM by OpenRock.

the class CsvAuditEventHandlerFactory method setFileRotationPolicies.

private void setFileRotationPolicies(CsvAuditEventHandlerConfiguration csvHandlerConfiguration, Map<String, Set<String>> attributes) throws AuditException {
    boolean enabled = getBooleanMapAttr(attributes, "rotationEnabled", true);
    csvHandlerConfiguration.getFileRotation().setRotationEnabled(enabled);
    long maxFileSize = getLongMapAttr(attributes, "rotationMaxFileSize", 100000000L, DEBUG);
    csvHandlerConfiguration.getFileRotation().setMaxFileSize(maxFileSize);
    String filePrefix = getMapAttr(attributes, "rotationFilePrefix", "");
    csvHandlerConfiguration.getFileRotation().setRotationFilePrefix(filePrefix);
    String fileSuffix = getMapAttr(attributes, "rotationFileSuffix", "-MM.dd.yy-kk.mm");
    csvHandlerConfiguration.getFileRotation().setRotationFileSuffix(fileSuffix);
    String interval = getMapAttr(attributes, "rotationInterval", "-1");
    try {
        Long intervalAsLong = Long.valueOf(interval);
        if (intervalAsLong <= 0) {
            //If interval is 0 or a negative value, then this indicates that the feature is disabled. Change
            //it to a value indicating disablement.
            interval = "disabled";
        } else {
            //If interval is a positive number, add seconds to the end as the time unit.
            interval = interval + " seconds";
        }
    } catch (NumberFormatException nfe) {
        throw new AuditException("Attribute 'rotationInterval' is invalid: " + interval);
    }
    csvHandlerConfiguration.getFileRotation().setRotationInterval(interval);
    List<String> times = new ArrayList<>();
    Set<String> rotationTimesAttribute = attributes.get("rotationTimes");
    if (rotationTimesAttribute != null && !rotationTimesAttribute.isEmpty()) {
        for (String rotationTime : rotationTimesAttribute) {
            times.add(rotationTime + " seconds");
        }
        csvHandlerConfiguration.getFileRotation().setRotationTimes(times);
    }
}
Also used : ArrayList(java.util.ArrayList) AuditException(org.forgerock.audit.AuditException)

Example 2 with AuditException

use of org.forgerock.audit.AuditException in project OpenAM by OpenRock.

the class SyslogAuditEventHandlerFactory method create.

@Override
public AuditEventHandler create(AuditEventHandlerConfiguration configuration) throws AuditException {
    Map<String, Set<String>> attributes = configuration.getAttributes();
    SyslogAuditEventHandlerConfiguration syslogHandlerConfiguration = new SyslogAuditEventHandlerConfiguration();
    syslogHandlerConfiguration.setEnabled(getBooleanMapAttr(attributes, "enabled", true));
    syslogHandlerConfiguration.setName(configuration.getHandlerName());
    syslogHandlerConfiguration.setTopics(attributes.get("topics"));
    syslogHandlerConfiguration.setHost(getMapAttr(attributes, "host"));
    setPort(syslogHandlerConfiguration, attributes);
    String transportProtocol = getMapAttr(attributes, "transportProtocol");
    try {
        syslogHandlerConfiguration.setProtocol(TransportProtocol.valueOf(transportProtocol));
    } catch (IllegalArgumentException iae) {
        throw new AuditException("Attribute 'transportProtocol' is invalid: " + transportProtocol);
    }
    setConnectTimeout(syslogHandlerConfiguration, attributes);
    EventBufferingConfiguration eventBufferingConfiguration = new EventBufferingConfiguration();
    eventBufferingConfiguration.setEnabled(getBooleanMapAttr(attributes, "bufferingEnabled", true));
    syslogHandlerConfiguration.setBufferingConfiguration(eventBufferingConfiguration);
    String facility = getMapAttr(attributes, "facility");
    try {
        syslogHandlerConfiguration.setFacility(Facility.valueOf(facility));
    } catch (IllegalArgumentException iae) {
        throw new AuditException("Attribute 'facility' is invalid: " + facility);
    }
    return new SyslogAuditEventHandler(syslogHandlerConfiguration, configuration.getEventTopicsMetaData(), new ProductInfoProviderImpl(), new SyslogLocalHostNameProvider());
}
Also used : Set(java.util.Set) SyslogAuditEventHandlerConfiguration(org.forgerock.audit.handlers.syslog.SyslogAuditEventHandlerConfiguration) AuditException(org.forgerock.audit.AuditException) EventBufferingConfiguration(org.forgerock.audit.handlers.syslog.SyslogAuditEventHandlerConfiguration.EventBufferingConfiguration) SyslogAuditEventHandler(org.forgerock.audit.handlers.syslog.SyslogAuditEventHandler)

Example 3 with AuditException

use of org.forgerock.audit.AuditException in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilter method beforeHandle.

@Override
protected int beforeHandle(Request request, Response response) {
    try {
        Representation representation = request.getEntity();
        // buffer in order to read from it during the event logging and later during authentication
        if (representation.isTransient()) {
            request.setEntity(new BufferingRepresentation(request.getEntity()));
        }
        auditAccessAttempt(request);
    } catch (AuditException e) {
        debug.error("Unable to publish {} audit event '{}' due to error: {} [{}]", ACCESS_TOPIC, EventName.AM_ACCESS_ATTEMPT, e.getMessage(), e);
    }
    return CONTINUE;
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation)

Example 4 with AuditException

use of org.forgerock.audit.AuditException in project OpenAM by OpenRock.

the class AbstractRestletAccessAuditFilter method auditAccessSuccess.

private void auditAccessSuccess(Request request, Response response) {
    String realm = getRealmFromRequest(request);
    if (auditEventPublisher.isAuditing(realm, ACCESS_TOPIC, EventName.AM_ACCESS_OUTCOME)) {
        long endTime = System.currentTimeMillis();
        long elapsedTime = endTime - request.getDate().getTime();
        final Representation entity = response.getEntity();
        AMAccessAuditEventBuilder builder = auditEventFactory.accessEvent(realm).timestamp(endTime).transactionId(AuditRequestContext.getTransactionIdValue()).eventName(EventName.AM_ACCESS_OUTCOME).component(component).userId(getUserIdForAccessOutcome(request, response)).trackingIds(getTrackingIdsForAccessOutcome(request, response));
        JsonValue detail = null;
        if (responseDetailCreator != null) {
            try {
                detail = responseDetailCreator.apply(entity);
            } catch (AuditException e) {
                debug.warning("An error occurred when fetching response body details for audit", e);
            }
        }
        if (detail == null) {
            builder.response(SUCCESSFUL, "", elapsedTime, MILLISECONDS);
        } else {
            builder.responseWithDetail(SUCCESSFUL, "", elapsedTime, MILLISECONDS, detail);
        }
        addHttpData(request, builder);
        auditEventPublisher.tryPublish(ACCESS_TOPIC, builder.toEvent());
    }
}
Also used : JsonValue(org.forgerock.json.JsonValue) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) AMAccessAuditEventBuilder(org.forgerock.openam.audit.AMAccessAuditEventBuilder)

Example 5 with AuditException

use of org.forgerock.audit.AuditException in project OpenAM by OpenRock.

the class RestletBodyAuditor method jsonAuditor.

/**
     * Create a body auditor for JSON bodies.
     * @param fields The fields that should be captured if they exist.
     * @return The auditor object.
     */
public static RestletBodyAuditor jsonAuditor(String... fields) {
    return new RestletBodyAuditor<JSONObject>(fields) {

        @Override
        public JsonValue apply(Representation representation) throws AuditException {
            try {
                boolean isBufferingRepresentation = (representation instanceof BufferingRepresentation);
                boolean isEmptyBufferingRepresentation = isBufferingRepresentation && ((BufferingRepresentation) representation).getWrappedRepresentation().isEmpty();
                if (isEmptyBufferingRepresentation || (!isBufferingRepresentation && representation.isEmpty())) {
                    return json(object());
                }
                return extractValues(new JsonRepresentation(representation).getJsonObject());
            } catch (IOException | JSONException e) {
                throw new AuditException("Could not parse body as JSON - wrong body auditor?", e);
            }
        }

        @Override
        Object getValue(String field, JSONObject object) throws AuditException {
            return object.opt(field);
        }
    };
}
Also used : BufferingRepresentation(org.restlet.representation.BufferingRepresentation) JSONObject(org.json.JSONObject) JSONException(org.json.JSONException) AuditException(org.forgerock.audit.AuditException) BufferingRepresentation(org.restlet.representation.BufferingRepresentation) Representation(org.restlet.representation.Representation) JsonRepresentation(org.restlet.ext.json.JsonRepresentation) JacksonRepresentation(org.restlet.ext.jackson.JacksonRepresentation) IOException(java.io.IOException) JsonRepresentation(org.restlet.ext.json.JsonRepresentation)

Aggregations

AuditException (org.forgerock.audit.AuditException)5 BufferingRepresentation (org.restlet.representation.BufferingRepresentation)3 Representation (org.restlet.representation.Representation)3 IOException (java.io.IOException)1 ArrayList (java.util.ArrayList)1 Set (java.util.Set)1 SyslogAuditEventHandler (org.forgerock.audit.handlers.syslog.SyslogAuditEventHandler)1 SyslogAuditEventHandlerConfiguration (org.forgerock.audit.handlers.syslog.SyslogAuditEventHandlerConfiguration)1 EventBufferingConfiguration (org.forgerock.audit.handlers.syslog.SyslogAuditEventHandlerConfiguration.EventBufferingConfiguration)1 JsonValue (org.forgerock.json.JsonValue)1 AMAccessAuditEventBuilder (org.forgerock.openam.audit.AMAccessAuditEventBuilder)1 JSONException (org.json.JSONException)1 JSONObject (org.json.JSONObject)1 JacksonRepresentation (org.restlet.ext.jackson.JacksonRepresentation)1 JsonRepresentation (org.restlet.ext.json.JsonRepresentation)1