All Downloads are FREE. Search and download functionalities are using the official Maven repository.

base.jee.api.cassandra.RecentLogEntriesByIP Maven / Gradle / Ivy

/*
 This is free and unencumbered software released into the public domain.

 Anyone is free to copy, modify, publish, use, compile, sell, or
 distribute this software, either in source code form or as a compiled
 binary, for any purpose, commercial or non-commercial, and by any
 means.

 In jurisdictions that recognize copyright laws, the author or authors
 of this software dedicate any and all copyright interest in the
 software to the public domain. We make this dedication for the benefit
 of the public at large and to the detriment of our heirs and
 successors. We intend this dedication to be an overt act of
 relinquishment in perpetuity of all present and future rights to this
 software under copyright law.

 THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND,
 EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF
 MERCHANTABILITY, FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT.
 IN NO EVENT SHALL THE AUTHORS BE LIABLE FOR ANY CLAIM, DAMAGES OR
 OTHER LIABILITY, WHETHER IN AN ACTION OF CONTRACT, TORT OR OTHERWISE,
 ARISING FROM, OUT OF OR IN CONNECTION WITH THE SOFTWARE OR THE USE OR
 OTHER DEALINGS IN THE SOFTWARE.
 */
package base.jee.api.cassandra;

import base.Query;
import base.jee.Constants;
import base.jee.api.cassandra.util.Log;
import base.jee.api.model.AuditLogEntry;
import base.security.PermissionException;
import base.security.User;
import base.text.TagsToArray;
import base.uuid.TimeUUIDToDate;
import com.datastax.driver.core.PreparedStatement;
import com.datastax.driver.core.ResultSet;
import com.datastax.driver.core.Row;
import com.datastax.driver.core.Session;

import java.io.IOException;
import java.util.Calendar;
import java.util.Date;
import java.util.GregorianCalendar;
import java.util.LinkedList;
import java.util.List;
import java.util.Map;

import static base.jee.api.cassandra.util.Log.log;

public class RecentLogEntriesByIP extends Query {

	private CassandraAPI api;
	private User user;
	private String ip;
	private long limit;
	private boolean debug;

	public RecentLogEntriesByIP(CassandraAPI api, User user, String ip, boolean debug, long limit) {
		this.api = api;
		this.user = user;
		this.ip = ip;
		this.limit = limit;
		this.debug = debug;

		if(api == null) {
			throw new IllegalArgumentException("Invalid parameter: api");
		}
		if(user == null || !user.isAuthenticated()) {
			throw new IllegalArgumentException("Requires an authenticated user.");
		}
		if(ip == null || ip.length() == 0) {
			throw new IllegalArgumentException("Please specify a valid IP address.");
		}
		if(limit == 0) {
			throw new IllegalArgumentException("Please specify a limit");
		}
		if(limit > 20000) {
			throw new IllegalArgumentException("Limit must be less than 20,000.");
		}
	}

	public RecentLogEntriesByIP() {
	}

	@Override
	public Query newWithParameters(Map parameters) {
		return new RecentLogEntriesByIP(
				(CassandraAPI)parameters.get("api"),
				(User)parameters.get("user"),
				(String)parameters.get("ip"),
				parameters.get("debug") != null && ((String)parameters.get("debug")).equalsIgnoreCase("true"),
				Integer.parseInt((String)parameters.get("limit")));
	}

	public List execute() throws IOException {
		List results = new LinkedList<>();
		Session s = api.getCassandraSession();

		if(!user.hasRole(Constants.AUDIT_ROLE)) {
			log(s, "WARN", user, "Permission denied invoking: " + RecentLogEntries.class.getSimpleName() + " " + getJsonParameters());
			throw new PermissionException(this.getClass().getSimpleName(), user, "You do not have permission to access audit information.", Constants.AUDIT_ROLE);
		}

		// Exclude particular IP's from appearing in the general log entry search results.
		String[] hiddenIP = TagsToArray.tagsToArray(api.getSettingsCache().get("log.hidden.ip", ""));
		Date date = new Date();
		Calendar c = new GregorianCalendar();
		c.setTime(date);
		int dayCount = 0;

		ResultSet rs;
		PreparedStatement p = s.prepare(
				"select uuid,level,person_uuid,first_name,last_name,message,ip "+
				"from audit_event " +
				"where yyyymmdd = ?" +
				"order by uuid desc " +
				"limit " + limit);

		while(results.size() < limit && dayCount < 20) {
			rs = s.execute(p.bind(Log.YYYYMMDD.format(date)));

			for(Row r : rs) {
				boolean skip = false;

				for(String ip : hiddenIP) {
					if(ip.equals(r.getString(6))) {
						skip = true;
						break;
					}
				}
				if(!debug && r.getString(1).equalsIgnoreCase("debug")) {
					skip = true;
				}
				if(!ip.equals(r.getString(6))) {
					skip = true;
				}

				if(!skip) {
					results.add(new AuditLogEntry(r.getUUID(0), new Date(TimeUUIDToDate.getTimeFromUUID(r.getUUID(0))), r.getString(1), r.getUUID(2), r.getString(3), r.getString(4), r.getString(5), r.getString(6)));
				}
			}

			c.add(Calendar.DAY_OF_YEAR, -1);
			date = c.getTime();
			dayCount++;
		}

		return results;
	}

	@Override
	public String getJsonParameters() {
		return "{" +
				"}";
	}

}




© 2015 - 2024 Weber Informatics LLC | Privacy Policy