ClearspaceSecurityAuditProvider.java 6.03 KB
Newer Older
1 2 3 4
/**
 * $Revision$
 * $Date$
 *
5
 * Copyright (C) 2005-2008 Jive Software. All rights reserved.
6
 *
7 8 9 10 11 12 13 14 15 16 17
 * Licensed under the Apache License, Version 2.0 (the "License");
 * you may not use this file except in compliance with the License.
 * You may obtain a copy of the License at
 *
 *     http://www.apache.org/licenses/LICENSE-2.0
 *
 * Unless required by applicable law or agreed to in writing, software
 * distributed under the License is distributed on an "AS IS" BASIS,
 * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 * See the License for the specific language governing permissions and
 * limitations under the License.
18 19 20
 */
package org.jivesoftware.openfire.clearspace;

21 22 23 24 25
import static org.jivesoftware.openfire.clearspace.ClearspaceManager.HttpType.POST;

import java.util.Date;
import java.util.List;

26 27 28
import org.dom4j.Document;
import org.dom4j.DocumentHelper;
import org.dom4j.Element;
29 30 31
import org.jivesoftware.openfire.security.EventNotFoundException;
import org.jivesoftware.openfire.security.SecurityAuditEvent;
import org.jivesoftware.openfire.security.SecurityAuditProvider;
32 33
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
34
import org.xmpp.packet.JID;
35 36 37 38 39 40 41 42 43 44

/**
 * The ClearspaceSecurityAuditProvider uses the AuditService web service inside of Clearspace
 * to send audit logs into Clearspace's own audit handler.  It also refers the admin to a URL
 * inside the Clearspace admin console where they can view the logs.
 *
 * @author Daniel Henninger
 */
public class ClearspaceSecurityAuditProvider implements SecurityAuditProvider {

45 46
	private static final Logger Log = LoggerFactory.getLogger(ClearspaceSecurityAuditProvider.class);

47 48
    protected static final String AUDIT_URL_PREFIX = "auditService/";

49 50 51 52 53 54 55 56 57 58 59 60
    /**
     * Generate a ClearspaceSecurityAuditProvider instance.
     */
    public ClearspaceSecurityAuditProvider() {
    }

    /**
     * The ClearspaceSecurityAuditProvider will log events into Clearspace via the AuditService
     * web service, provided by Clearspace.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#logEvent(String, String, String)
     */
    public void logEvent(String username, String summary, String details) {
61 62
        try {
            // Request to log event
63
            String path = AUDIT_URL_PREFIX + "audit";
64 65 66

            // Creates the XML with the data
            Document auditDoc =  DocumentHelper.createDocument();
67 68
            Element rootE = auditDoc.addElement("auditEvent");
            Element userE = rootE.addElement("username");
69 70
            // Un-escape username.
            username = JID.unescapeNode(username);
71 72
            // Encode potentially non-ASCII characters
            username = URLUTF8Encoder.encode(username);
73 74 75
            userE.addText(username);
            Element descE = rootE.addElement("description");
            if (summary != null) {
76 77 78 79
                descE.addText("[Openfire] "+summary);
            }
            else {
                descE.addText("[Openfire] No summary provided.");
80 81 82 83 84
            }
            Element detlE = rootE.addElement("details");
            if (details != null) {
                detlE.addText(details);
            }
85 86 87
            else {
                detlE.addText("No details provided.");
            }
88

89
            ClearspaceManager.getInstance().executeRequest(POST, path, auditDoc.asXML());
90 91 92 93 94
        }
        catch (Exception e) {
            // Error while setting properties?
            Log.error("Unable to send audit log via REST service to Clearspace:", e);
        }
95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116
    }

    /**
     * The ClearspaceSecurityAuditProvider does not retrieve audit entries from Clearspace.  Instead
     * it refers the admin to a URL where they can read the logs.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#getEvents(String, Integer, Integer, java.util.Date, java.util.Date)
     */
    public List<SecurityAuditEvent> getEvents(String username, Integer skipEvents, Integer numEvents, Date startTime, Date endTime) {
        // This is not used.
        return null;
    }

    /**
     * The ClearspaceSecurityAuditProvider does not retrieve audit entries from Clearspace.  Instead
     * it refers the admin to a URL where they can read the logs.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#getEvent(Integer)
     */
    public SecurityAuditEvent getEvent(Integer msgID) throws EventNotFoundException {
        // This is not used.
        return null;
    }

117 118 119 120 121 122 123 124 125 126
    /**
     * The ClearspaceSecurityAuditProvider does not retrieve audit entries from Clearspace.  Instead
     * it refers the admin to a URL where they can read the logs.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#getEventCount() 
     */
    public Integer getEventCount() {
        // This is not used.
        return null;
    }

127 128 129 130 131 132 133 134 135 136 137 138 139 140 141
    /**
     * The ClearspaceSecurityAuditProvider does not retrieve audit entries from Clearspace.  Instead
     * it refers the admin to a URL where they can read the logs.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#isWriteOnly()
     */
    public boolean isWriteOnly() {
        return true;
    }

    /**
     * The ClearspaceSecurityAuditProvider does not retrieve audit entries from Clearspace.  Instead
     * it refers the admin to a URL where they can read the logs.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#getAuditURL()
     */
    public String getAuditURL() {
142 143
        String url = ClearspaceManager.getInstance().getConnectionURI();
        if (url != null) {
144
            url += "admin/view-audit-log.jspa";
145 146 147 148 149
            return url;
        }
        else {
            return null;
        }
150 151
    }

152 153 154 155 156 157 158 159 160 161 162 163 164 165 166 167
    /**
     * Clearspace handles logging it's own user events.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#blockUserEvents()
     */
    public boolean blockUserEvents() {
        return true;
    }

    /**
     * Clearspace handles logging it's own group events.
     * @see org.jivesoftware.openfire.security.SecurityAuditProvider#blockGroupEvents()
     */
    public boolean blockGroupEvents() {
        return true;
    }

168
}