blob: 5370c9f96fea05fa330acd4a10543032c5e00b97 [file] [log] [blame]
Michael Hanlca740d72015-06-16 10:04:58 +02001package de.ids_mannheim.korap.auditing;
2
3import com.fasterxml.jackson.annotation.JsonIgnore;
4import com.fasterxml.jackson.databind.JsonNode;
margaretha894a7d72017-11-08 19:24:20 +01005
6import de.ids_mannheim.korap.exceptions.KustvaktException;
Michael Hanlca740d72015-06-16 10:04:58 +02007import de.ids_mannheim.korap.utils.JsonUtils;
8import de.ids_mannheim.korap.utils.TimeUtils;
9import lombok.Getter;
10import lombok.Setter;
11
12import java.util.Arrays;
Michael Hanl0f6ffd72015-08-27 19:23:15 +020013import java.util.Date;
Michael Hanlca740d72015-06-16 10:04:58 +020014
15/**
16 * @author hanl
17 * <p/>
Michael Hanl8abaf9e2016-05-23 16:46:35 +020018 * Record holder for auditing requests. Holds the data until
19 * it can be persisted to a database
Michael Hanlca740d72015-06-16 10:04:58 +020020 */
21@Getter
22@Setter
23public class AuditRecord {
24
Michael Hanl0f6ffd72015-08-27 19:23:15 +020025 // todo: handle via status codes
Michael Hanlca740d72015-06-16 10:04:58 +020026 @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 Hanl8abaf9e2016-05-23 16:46:35 +020049
50 private AuditRecord () {
Michael Hanlca740d72015-06-16 10:04:58 +020051 this.timestamp = TimeUtils.getNow().getMillis();
52 }
53
Michael Hanl8abaf9e2016-05-23 16:46:35 +020054
55 public AuditRecord (CATEGORY category) {
Michael Hanlca740d72015-06-16 10:04:58 +020056 this();
57 this.category = category;
58 }
59
Michael Hanl8abaf9e2016-05-23 16:46:35 +020060
61 public AuditRecord (CATEGORY cat, Object userID, Integer status) {
Michael Hanlca740d72015-06-16 10:04:58 +020062 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 Hanl8abaf9e2016-05-23 16:46:35 +020070 }
71 else {
Michael Hanlca740d72015-06-16 10:04:58 +020072 this.loc = clientInfoToString("null", "null");
73 userid = "-1";
74 }
75 }
76
Michael Hanl8abaf9e2016-05-23 16:46:35 +020077
78 public static AuditRecord serviceRecord (Object user, Integer status,
79 String ... args) {
Michael Hanlca740d72015-06-16 10:04:58 +020080 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 Hanl8abaf9e2016-05-23 16:46:35 +020087
88 public static AuditRecord dbRecord (Object user, Integer status,
89 String ... args) {
Michael Hanlca740d72015-06-16 10:04:58 +020090 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 Hanl8abaf9e2016-05-23 16:46:35 +020097
margaretha894a7d72017-11-08 19:24:20 +010098 public AuditRecord fromJson (String json) throws KustvaktException {
Michael Hanlca740d72015-06-16 10:04:58 +020099 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 Hanl8abaf9e2016-05-23 16:46:35 +0200110
111 private String clientInfoToString (String IP, String userAgent) {
Michael Hanlca740d72015-06-16 10:04:58 +0200112 return userAgent + "@" + IP;
113 }
114
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200115
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200116 // fixme: add id, useragent
Michael Hanlca740d72015-06-16 10:04:58 +0200117 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200118 public String toString () {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200119 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 Hanlca740d72015-06-16 10:04:58 +0200129 }
130
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200131
Michael Hanlca740d72015-06-16 10:04:58 +0200132 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200133 public boolean equals (Object o) {
Michael Hanlca740d72015-06-16 10:04:58 +0200134 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 Hanl8abaf9e2016-05-23 16:46:35 +0200147 if (field_1 != null ? !field_1.equals(that.field_1)
148 : that.field_1 != null)
Michael Hanlca740d72015-06-16 10:04:58 +0200149 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 Hanl8abaf9e2016-05-23 16:46:35 +0200154 if (timestamp != null ? !timestamp.equals(that.timestamp)
155 : that.timestamp != null)
Michael Hanlca740d72015-06-16 10:04:58 +0200156 return false;
157
158 return true;
159 }
160
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200161
Michael Hanlca740d72015-06-16 10:04:58 +0200162 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200163 public int hashCode () {
Michael Hanlca740d72015-06-16 10:04:58 +0200164 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}