| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 1 | package de.ids_mannheim.korap.auditing; |
| 2 | |
| 3 | import com.fasterxml.jackson.annotation.JsonIgnore; |
| 4 | import com.fasterxml.jackson.databind.JsonNode; |
| 5 | import de.ids_mannheim.korap.utils.JsonUtils; |
| 6 | import de.ids_mannheim.korap.utils.TimeUtils; |
| 7 | import lombok.Getter; |
| 8 | import lombok.Setter; |
| 9 | |
| 10 | import java.util.Arrays; |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 11 | import java.util.Date; |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 12 | |
| 13 | /** |
| 14 | * @author hanl |
| 15 | * <p/> |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 16 | * Record holder for auditing requests. Holds the data until |
| 17 | * it can be persisted to a database |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 18 | */ |
| 19 | @Getter |
| 20 | @Setter |
| 21 | public class AuditRecord { |
| 22 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 23 | // todo: handle via status codes |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 24 | @Deprecated |
| 25 | public enum Operation { |
| 26 | GET, INSERT, UPDATE, DELETE, CREATE |
| 27 | } |
| 28 | |
| 29 | public enum CATEGORY { |
| 30 | SECURITY, DATABASE, RESOURCE, QUERY, SERVICE |
| 31 | } |
| 32 | |
| 33 | @JsonIgnore |
| 34 | private Integer id; |
| 35 | //security access describes changes in user authorities and access control permissions of resources |
| 36 | private String userid; |
| 37 | private String target; |
| 38 | |
| 39 | //fixme: replace with more specific error codes |
| 40 | private CATEGORY category; |
| 41 | private String loc; |
| 42 | private Long timestamp; |
| 43 | private Integer status = -1; |
| 44 | private String args; |
| 45 | private String field_1 = "None"; |
| 46 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 47 | |
| 48 | private AuditRecord () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 49 | this.timestamp = TimeUtils.getNow().getMillis(); |
| 50 | } |
| 51 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 52 | |
| 53 | public AuditRecord (CATEGORY category) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 54 | this(); |
| 55 | this.category = category; |
| 56 | } |
| 57 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 58 | |
| 59 | public AuditRecord (CATEGORY cat, Object userID, Integer status) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 60 | this(cat); |
| 61 | this.status = status; |
| 62 | if (userID != null) { |
| 63 | //todo: client info! |
| 64 | // this.loc = clientInfoToString(user.getTokenContext().getHostAddress(), |
| 65 | // user.getTokenContext().getUserAgent()); |
| 66 | this.loc = clientInfoToString("null", "null"); |
| 67 | userid = String.valueOf(userID); |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 68 | } |
| 69 | else { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 70 | this.loc = clientInfoToString("null", "null"); |
| 71 | userid = "-1"; |
| 72 | } |
| 73 | } |
| 74 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 75 | |
| 76 | public static AuditRecord serviceRecord (Object user, Integer status, |
| 77 | String ... args) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 78 | AuditRecord r = new AuditRecord(CATEGORY.SERVICE); |
| 79 | r.setArgs(Arrays.asList(args).toString()); |
| 80 | r.setUserid(String.valueOf(user)); |
| 81 | r.setStatus(status); |
| 82 | return r; |
| 83 | } |
| 84 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 85 | |
| 86 | public static AuditRecord dbRecord (Object user, Integer status, |
| 87 | String ... args) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 88 | AuditRecord r = new AuditRecord(CATEGORY.DATABASE); |
| 89 | r.setArgs(Arrays.asList(args).toString()); |
| 90 | r.setUserid(String.valueOf(user)); |
| 91 | r.setStatus(status); |
| 92 | return r; |
| 93 | } |
| 94 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 95 | |
| 96 | public AuditRecord fromJson (String json) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 97 | JsonNode n = JsonUtils.readTree(json); |
| 98 | AuditRecord r = new AuditRecord(); |
| 99 | r.setCategory(CATEGORY.valueOf(n.path("category").asText())); |
| 100 | r.setTarget(n.path("target").asText()); |
| 101 | r.setField_1(n.path("field_1").asText()); |
| 102 | r.setUserid(n.path("account").asText()); |
| 103 | r.setStatus(n.path("status").asInt()); |
| 104 | r.setLoc(n.path("loc").asText()); |
| 105 | return r; |
| 106 | } |
| 107 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 108 | |
| 109 | private String clientInfoToString (String IP, String userAgent) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 110 | return userAgent + "@" + IP; |
| 111 | } |
| 112 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 113 | |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 114 | // fixme: add id, useragent |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 115 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 116 | public String toString () { |
| Michael Hanl | 0f6ffd7 | 2015-08-27 19:23:15 +0200 | [diff] [blame] | 117 | StringBuilder b = new StringBuilder(); |
| 118 | b.append(category.toString().toLowerCase() + " audit : ") |
| 119 | .append(userid + "@" + new Date(timestamp)).append("\n") |
| 120 | .append("Status " + status).append("; "); |
| 121 | |
| 122 | if (this.args != null) |
| 123 | b.append("Args " + field_1).append("; "); |
| 124 | if (this.loc != null) |
| 125 | b.append("Location " + loc).append("; "); |
| 126 | return b.toString(); |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 127 | } |
| 128 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 129 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 130 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 131 | public boolean equals (Object o) { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 132 | if (this == o) |
| 133 | return true; |
| 134 | if (o == null || getClass() != o.getClass()) |
| 135 | return false; |
| 136 | |
| 137 | AuditRecord that = (AuditRecord) o; |
| 138 | |
| 139 | if (userid != null ? !userid.equals(that.userid) : that.userid != null) |
| 140 | return false; |
| 141 | if (category != that.category) |
| 142 | return false; |
| 143 | if (status != null ? !status.equals(that.status) : that.status != null) |
| 144 | return false; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 145 | if (field_1 != null ? !field_1.equals(that.field_1) |
| 146 | : that.field_1 != null) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 147 | return false; |
| 148 | if (loc != null ? !loc.equals(that.loc) : that.loc != null) |
| 149 | return false; |
| 150 | if (target != null ? !target.equals(that.target) : that.target != null) |
| 151 | return false; |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 152 | if (timestamp != null ? !timestamp.equals(that.timestamp) |
| 153 | : that.timestamp != null) |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 154 | return false; |
| 155 | |
| 156 | return true; |
| 157 | } |
| 158 | |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 159 | |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 160 | @Override |
| Michael Hanl | 8abaf9e | 2016-05-23 16:46:35 +0200 | [diff] [blame^] | 161 | public int hashCode () { |
| Michael Hanl | ca740d7 | 2015-06-16 10:04:58 +0200 | [diff] [blame] | 162 | int result = userid != null ? userid.hashCode() : 0; |
| 163 | result = 31 * result + (target != null ? target.hashCode() : 0); |
| 164 | result = 31 * result + category.hashCode(); |
| 165 | result = 31 * result + (loc != null ? loc.hashCode() : 0); |
| 166 | result = 31 * result + (timestamp != null ? timestamp.hashCode() : 0); |
| 167 | result = 31 * result + (status != null ? status.hashCode() : 0); |
| 168 | result = 31 * result + (field_1 != null ? field_1.hashCode() : 0); |
| 169 | return result; |
| 170 | } |
| 171 | } |