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