Autopsy  4.15.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 org.apache.commons.lang3.StringUtils;
28 import org.sleuthkit.datamodel.SleuthkitCase;
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 insertClause = " INTO persona_metadata (persona_id, name, value, justification, confidence_id, date_added, examiner_id ) "
116  + "VALUES ( "
117  + personaId + ", "
118  + "'" + name + "', "
119  + "'" + value + "', "
120  + "'" + ((StringUtils.isBlank(justification) ? "" : SleuthkitCase.escapeSingleQuotes(justification))) + "', "
121  + confidence.getLevelId() + ", "
122  + timeStampMillis.toString() + ", "
123  + examiner.getId()
124  + ")";
125 
126  getCRInstance().executeInsertSQL(insertClause);
127 
128  String queryClause = SELECT_QUERY_BASE
129  + "WHERE pmd.persona_id = " + personaId
130  + " AND pmd.name = '" + name + "'"
131  + " AND pmd.value = '" + value + "'"
132  + " AND pmd.date_added = " + timeStampMillis
133  + " AND pmd.examiner_id = " + examiner.getId();
134 
135  PersonaMetadataQueryCallback queryCallback = new PersonaMetadataQueryCallback();
136  getCRInstance().executeSelectSQL(queryClause, queryCallback);
137 
138  Collection<PersonaMetadata> metadata = queryCallback.getMetadataList();
139  if (metadata.size() != 1) {
140  throw new CentralRepoException("Metadata add query failed");
141  }
142 
143  return metadata.iterator().next();
144  }
145 
153  static void removePersonaMetadata(PersonaMetadata metadata) throws CentralRepoException {
154  String deleteClause = " DELETE FROM persona_metadata WHERE id = " + metadata.getId();
155  getCRInstance().executeDeleteSQL(deleteClause);
156  }
157 
165  static void modifyPersonaMetadata(PersonaMetadata metadata, Persona.Confidence confidence, String justification) throws CentralRepoException {
166  CentralRepository cr = CentralRepository.getInstance();
167 
168  if (cr == null) {
169  throw new CentralRepoException("Failed to modify persona metadata, Central Repo is not enabled");
170  }
171 
172  String updateClause = "UPDATE persona_metadata SET confidence_id = " + confidence.getLevelId() + ", justification = '" + justification + "' WHERE id = " + metadata.id;
173  cr.executeUpdateSQL(updateClause);
174  }
175 
179  private static class PersonaMetadataQueryCallback implements CentralRepositoryDbQueryCallback {
180 
181  Collection<PersonaMetadata> personaMetadataList = new ArrayList<>();
182 
183  @Override
184  public void process(ResultSet rs) throws SQLException {
185 
186  while (rs.next()) {
188  rs.getInt("examiner_id"),
189  rs.getString("login_name"));
190 
191  PersonaMetadata metaData = new PersonaMetadata(
192  rs.getLong("id"),
193  rs.getLong("persona_id"),
194  rs.getString("name"),
195  rs.getString("value"),
196  rs.getString("justification"),
197  Persona.Confidence.fromId(rs.getInt("confidence_id")),
198  Long.parseLong(rs.getString("date_added")),
199  examiner);
200 
201  personaMetadataList.add(metaData);
202  }
203  }
204 
205  Collection<PersonaMetadata> getMetadataList() {
206  return Collections.unmodifiableCollection(personaMetadataList);
207  }
208  };
209 
218  static Collection<PersonaMetadata> getPersonaMetadata(long personaId) throws CentralRepoException {
219  String queryClause = SELECT_QUERY_BASE + "WHERE pmd.persona_id = " + personaId;
220 
221  PersonaMetadataQueryCallback queryCallback = new PersonaMetadataQueryCallback();
222  getCRInstance().executeSelectSQL(queryClause, queryCallback);
223 
224  return queryCallback.getMetadataList();
225 
226  }
227 
236  private static CentralRepository getCRInstance() throws CentralRepoException {
238 
239  if(instance == null) {
240  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
241  }
242 
243  return instance;
244  }
245 }
void executeSelectSQL(String sql, CentralRepositoryDbQueryCallback queryCallback)
PersonaMetadata(long id, long personaId, String name, String value, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)
CentralRepoExaminer getOrInsertExaminer(String examinerLoginName)

Copyright © 2012-2020 Basis Technology. Generated on: Mon Jul 6 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.