Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PersonaMetadata.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2020 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.centralrepository.datamodel;
20 
21 import java.sql.ResultSet;
22 import java.sql.SQLException;
23 import java.time.Instant;
24 import java.util.ArrayList;
25 import java.util.Collection;
26 import java.util.Collections;
27 import java.util.List;
28 import org.apache.commons.lang3.StringUtils;
29 
37 public class PersonaMetadata {
38 
39  private static final String SELECT_QUERY_BASE
40  = "SELECT pmd.id, pmd.persona_id, pmd.name, pmd.value, pmd.justification, pmd.confidence_id, pmd.date_added, pmd.examiner_id, e.login_name, e.display_name "
41  + "FROM persona_metadata as pmd "
42  + "INNER JOIN examiners as e ON e.id = pmd.examiner_id ";
43 
44  private final long id;
45  private final long personaId;
46  private final String name;
47  private final String value;
48  private final String justification;
49  private final Persona.Confidence confidence;
50  private final long dateAdded;
52 
53  public long getId() {
54  return id;
55  }
56 
57  public long getPersonaId() {
58  return personaId;
59  }
60 
61  public String getName() {
62  return name;
63  }
64 
65  public String getValue() {
66  return value;
67  }
68 
69  public String getJustification() {
70  return justification;
71  }
72 
74  return confidence;
75  }
76 
77  public long getDateAdded() {
78  return dateAdded;
79  }
80 
82  return examiner;
83  }
84 
85  public PersonaMetadata(long id, long personaId, String name, String value, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner) {
86  this.id = id;
87  this.personaId = personaId;
88  this.name = name;
89  this.value = value;
90  this.justification = justification;
91  this.confidence = confidence;
92  this.dateAdded = dateAdded;
93  this.examiner = examiner;
94  }
95 
108  static PersonaMetadata addPersonaMetadata(long personaId, String name, String value, String justification, Persona.Confidence confidence) throws CentralRepoException {
109 
110  CentralRepoExaminer examiner = getCRInstance().getOrInsertExaminer(System.getProperty("user.name"));
111 
112  Instant instant = Instant.now();
113  Long timeStampMillis = instant.toEpochMilli();
114 
115  String insertSQL = "INSERT INTO persona_metadata (persona_id, name, value, justification, confidence_id, date_added, examiner_id ) "
116  + "VALUES ( ?, ?, ?, ?, ?, ?, ?)";
117 
118  List<Object> params = new ArrayList<>();
119  params.add(personaId);
120  params.add(name);
121  params.add(value);
122  params.add(StringUtils.isBlank(justification) ? "" : justification);
123  params.add(confidence.getLevelId());
124  params.add(timeStampMillis);
125  params.add(examiner.getId());
126 
127  getCRInstance().executeCommand(insertSQL, params);
128 
129  String queryClause = SELECT_QUERY_BASE
130  + "WHERE pmd.persona_id = ?"
131  + " AND pmd.name = ?"
132  + " AND pmd.value = ?"
133  + " AND pmd.date_added = ?"
134  + " AND pmd.examiner_id = ?";
135 
136  List<Object> queryParams = new ArrayList<>();
137  queryParams.add(personaId);
138  queryParams.add(name);
139  queryParams.add(value);
140  queryParams.add(timeStampMillis);
141  queryParams.add(examiner.getId());
142 
143  PersonaMetadataQueryCallback queryCallback = new PersonaMetadataQueryCallback();
144  getCRInstance().executeQuery(queryClause, queryParams, queryCallback);
145 
146  Collection<PersonaMetadata> metadata = queryCallback.getMetadataList();
147  if (metadata.size() != 1) {
148  throw new CentralRepoException("Metadata add query failed");
149  }
150 
151  return metadata.iterator().next();
152  }
153 
162  static void removePersonaMetadata(PersonaMetadata metadata) throws CentralRepoException {
163  String deleteSql = " DELETE FROM persona_metadata WHERE id = ?";
164 
165  List<Object> params = new ArrayList<>();
166  params.add(metadata.getId());
167 
168  getCRInstance().executeCommand(deleteSql, params);
169  }
170 
179  static void modifyPersonaMetadata(PersonaMetadata metadata, Persona.Confidence confidence, String justification) throws CentralRepoException {
180  CentralRepository cr = CentralRepository.getInstance();
181 
182  if (cr == null) {
183  throw new CentralRepoException("Failed to modify persona metadata, Central Repo is not enabled");
184  }
185 
186  String updateSql = "UPDATE persona_metadata SET confidence_id = ?, justification = ? WHERE id = ?";
187 
188  List<Object> params = new ArrayList<>();
189  params.add(confidence.getLevelId());
190  params.add(StringUtils.isBlank(justification) ? "" : justification);
191  params.add(metadata.id);
192 
193  getCRInstance().executeCommand(updateSql, params);
194  }
195 
199  private static class PersonaMetadataQueryCallback implements CentralRepositoryDbQueryCallback {
200 
201  Collection<PersonaMetadata> personaMetadataList = new ArrayList<>();
202 
203  @Override
204  public void process(ResultSet rs) throws SQLException {
205 
206  while (rs.next()) {
208  rs.getInt("examiner_id"),
209  rs.getString("login_name"));
210 
211  PersonaMetadata metaData = new PersonaMetadata(
212  rs.getLong("id"),
213  rs.getLong("persona_id"),
214  rs.getString("name"),
215  rs.getString("value"),
216  rs.getString("justification"),
217  Persona.Confidence.fromId(rs.getInt("confidence_id")),
218  Long.parseLong(rs.getString("date_added")),
219  examiner);
220 
221  personaMetadataList.add(metaData);
222  }
223  }
224 
225  Collection<PersonaMetadata> getMetadataList() {
226  return Collections.unmodifiableCollection(personaMetadataList);
227  }
228  };
229 
238  static Collection<PersonaMetadata> getPersonaMetadata(long personaId) throws CentralRepoException {
239  String queryClause = SELECT_QUERY_BASE
240  + "WHERE pmd.persona_id = ?";
241 
242  List<Object> params = new ArrayList<>();
243  params.add(personaId);
244 
245  PersonaMetadataQueryCallback queryCallback = new PersonaMetadataQueryCallback();
246  getCRInstance().executeQuery(queryClause, params, queryCallback);
247 
248  return queryCallback.getMetadataList();
249 
250  }
251 
260  private static CentralRepository getCRInstance() throws CentralRepoException {
262 
263  if (instance == null) {
264  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
265  }
266 
267  return instance;
268  }
269 }
PersonaMetadata(long id, long personaId, String name, String value, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
CentralRepoExaminer getOrInsertExaminer(String examinerLoginName)

Copyright © 2012-2021 Basis Technology. Generated on: Tue Jan 19 2021
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.