7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest/*
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * The contents of this file are subject to the terms of the Common Development and
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * Distribution License (the License). You may not use this file except in compliance with the
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * License.
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * You can obtain a copy of the License at legal/CDDLv1.0.txt. See the License for the
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * specific language governing permission and limitations under the License.
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * When distributing Covered Software, include this CDDL Header Notice in each file and include
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * the License file at legal/CDDLv1.0.txt. If applicable, add the following below the CDDL
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * Header, with the fields enclosed by brackets [] replaced by your own identifying
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * information: "Portions copyright [year] [name of copyright owner]".
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * Copyright 2015 ForgeRock AS.
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest */
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrestpackage com.sun.identity.log.service;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Joosteimport static org.forgerock.openam.utils.StringUtils.isEmpty;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Joosteimport org.forgerock.audit.events.AccessAuditEventBuilder.ResponseStatus;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrestimport java.util.regex.Matcher;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrestimport java.util.regex.Pattern;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest/**
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * Attempts to parse an agent log message to extract useful information.
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * @since 13.0.0
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest */
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrestfinal class AgentLogParser {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest private enum Extractor {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste WEB_AGENT("^user\\s+(\\S+)\\s*was\\s*(\\S+)\\s*access to\\s*(\\S+)$", 3, 1, 2),
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste JAVA_AGENT("^access to\\s*(\\S+)\\s+(\\S+)\\s*for user\\s*(\\S+)$", 1, 3, 2);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest final Pattern pattern;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest final int resourceIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest final int subjectIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest final int statusIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest Extractor(String pattern, int resourceIndex, int subjectIndex, int statusIndex) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.pattern = Pattern.compile(pattern, Pattern.CASE_INSENSITIVE);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.resourceIndex = resourceIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.subjectIndex = subjectIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.statusIndex = statusIndex;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest Matcher newMatcher(String message) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return pattern.matcher(message);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest /**
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * Given the log message, attempts to parse and extract known parts.
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * @param message
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * the log message
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest *
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest * @return the log extracts, null if parsing fails
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest */
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest LogExtracts tryParse(String message) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest for (Extractor extractor : Extractor.values()) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest Matcher matcher = extractor.newMatcher(message);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest if (matcher.matches()) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return extract(extractor, matcher);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return null;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest private LogExtracts extract(Extractor extractor, Matcher matcher) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest String resourceUrl = matcher.group(extractor.resourceIndex);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest String subjectId = matcher.group(extractor.subjectIndex);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest String status = matcher.group(extractor.statusIndex);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return new LogExtracts(resourceUrl, subjectId, status);
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest final static class LogExtracts {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest private final String resourceUrl;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest private final String subjectId;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste private final String statusCode;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste private LogExtracts(String resourceUrl, String subjectId, String statusCode) {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.resourceUrl = resourceUrl;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest this.subjectId = subjectId;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste this.statusCode = statusCode;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest String getResourceUrl() {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return resourceUrl;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest String getSubjectId() {
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest return subjectId;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste String getStatusCode() {
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste return statusCode;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste }
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste ResponseStatus getStatus() {
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste if (isEmpty(statusCode)) {
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste return null;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste }
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste // statusCode can be "allowed"
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste if (statusCode.length() == 7) {
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste return ResponseStatus.SUCCESSFUL;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste }
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste // or "denied"
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste if (statusCode.length() == 6) {
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste return ResponseStatus.FAILED;
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste }
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste
8acf5a373074b7db10b49aa33b35f8a541cabfd1Jaco Jooste return null;
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest }
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest
7ad2fbd2d39159e30fdde02d014626b643758033Andrew Forrest}