blob: c2e07918b21249b506ebb6fbb839a67ed7f18e0d [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;
5import de.ids_mannheim.korap.utils.JsonUtils;
6import de.ids_mannheim.korap.utils.TimeUtils;
7import lombok.Getter;
8import lombok.Setter;
9
10import java.util.Arrays;
Michael Hanl0f6ffd72015-08-27 19:23:15 +020011import java.util.Date;
Michael Hanlca740d72015-06-16 10:04:58 +020012
13/**
14 * @author hanl
15 * <p/>
Michael Hanl8abaf9e2016-05-23 16:46:35 +020016 * Record holder for auditing requests. Holds the data until
17 * it can be persisted to a database
Michael Hanlca740d72015-06-16 10:04:58 +020018 */
19@Getter
20@Setter
21public class AuditRecord {
22
Michael Hanl0f6ffd72015-08-27 19:23:15 +020023 // todo: handle via status codes
Michael Hanlca740d72015-06-16 10:04:58 +020024 @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 Hanl8abaf9e2016-05-23 16:46:35 +020047
48 private AuditRecord () {
Michael Hanlca740d72015-06-16 10:04:58 +020049 this.timestamp = TimeUtils.getNow().getMillis();
50 }
51
Michael Hanl8abaf9e2016-05-23 16:46:35 +020052
53 public AuditRecord (CATEGORY category) {
Michael Hanlca740d72015-06-16 10:04:58 +020054 this();
55 this.category = category;
56 }
57
Michael Hanl8abaf9e2016-05-23 16:46:35 +020058
59 public AuditRecord (CATEGORY cat, Object userID, Integer status) {
Michael Hanlca740d72015-06-16 10:04:58 +020060 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 Hanl8abaf9e2016-05-23 16:46:35 +020068 }
69 else {
Michael Hanlca740d72015-06-16 10:04:58 +020070 this.loc = clientInfoToString("null", "null");
71 userid = "-1";
72 }
73 }
74
Michael Hanl8abaf9e2016-05-23 16:46:35 +020075
76 public static AuditRecord serviceRecord (Object user, Integer status,
77 String ... args) {
Michael Hanlca740d72015-06-16 10:04:58 +020078 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 Hanl8abaf9e2016-05-23 16:46:35 +020085
86 public static AuditRecord dbRecord (Object user, Integer status,
87 String ... args) {
Michael Hanlca740d72015-06-16 10:04:58 +020088 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 Hanl8abaf9e2016-05-23 16:46:35 +020095
96 public AuditRecord fromJson (String json) {
Michael Hanlca740d72015-06-16 10:04:58 +020097 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 Hanl8abaf9e2016-05-23 16:46:35 +0200108
109 private String clientInfoToString (String IP, String userAgent) {
Michael Hanlca740d72015-06-16 10:04:58 +0200110 return userAgent + "@" + IP;
111 }
112
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200113
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200114 // fixme: add id, useragent
Michael Hanlca740d72015-06-16 10:04:58 +0200115 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200116 public String toString () {
Michael Hanl0f6ffd72015-08-27 19:23:15 +0200117 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 Hanlca740d72015-06-16 10:04:58 +0200127 }
128
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200129
Michael Hanlca740d72015-06-16 10:04:58 +0200130 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200131 public boolean equals (Object o) {
Michael Hanlca740d72015-06-16 10:04:58 +0200132 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 Hanl8abaf9e2016-05-23 16:46:35 +0200145 if (field_1 != null ? !field_1.equals(that.field_1)
146 : that.field_1 != null)
Michael Hanlca740d72015-06-16 10:04:58 +0200147 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 Hanl8abaf9e2016-05-23 16:46:35 +0200152 if (timestamp != null ? !timestamp.equals(that.timestamp)
153 : that.timestamp != null)
Michael Hanlca740d72015-06-16 10:04:58 +0200154 return false;
155
156 return true;
157 }
158
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200159
Michael Hanlca740d72015-06-16 10:04:58 +0200160 @Override
Michael Hanl8abaf9e2016-05-23 16:46:35 +0200161 public int hashCode () {
Michael Hanlca740d72015-06-16 10:04:58 +0200162 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}