Autopsy  4.15.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PersonaAlias.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 
35 public class PersonaAlias {
36 
37  private static final String SELECT_QUERY_BASE =
38  "SELECT pa.id, pa.persona_id, pa.alias, pa.justification, pa.confidence_id, pa.date_added, pa.examiner_id, e.login_name, e.display_name "
39  + "FROM persona_alias as pa "
40  + "INNER JOIN examiners as e ON e.id = pa.examiner_id ";
41 
42  private final long id;
43  private final long personaId;
44  private final String alias;
45  private final String justification;
46  private final Persona.Confidence confidence;
47  private final long dateAdded;
49 
50  public long getId() {
51  return id;
52  }
53 
54  public long getPersonaId() {
55  return personaId;
56  }
57 
58  public String getAlias() {
59  return alias;
60  }
61 
62  public String getJustification() {
63  return justification;
64  }
65 
67  return confidence;
68  }
69 
70  public long getDateAadded() {
71  return dateAdded;
72  }
73 
75  return examiner;
76  }
77 
78  public PersonaAlias(long id, long personaId, String alias, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner) {
79  this.id = id;
80  this.personaId = personaId;
81  this.alias = alias;
82  this.justification = justification;
83  this.confidence = confidence;
84  this.dateAdded = dateAdded;
85  this.examiner = examiner;
86  }
87 
99  static PersonaAlias addPersonaAlias(Persona persona, String alias, String justification, Persona.Confidence confidence) throws CentralRepoException {
100 
101  CentralRepoExaminer examiner = getCRInstance().getOrInsertExaminer(System.getProperty("user.name"));
102 
103  Instant instant = Instant.now();
104  Long timeStampMillis = instant.toEpochMilli();
105 
106  String insertClause = " INTO persona_alias (persona_id, alias, justification, confidence_id, date_added, examiner_id ) "
107  + "VALUES ( "
108  + persona.getId() + ", "
109  + "'" + alias + "', "
110  + "'" + ((StringUtils.isBlank(justification) ? "" : SleuthkitCase.escapeSingleQuotes(justification))) + "', "
111  + confidence.getLevelId() + ", "
112  + timeStampMillis.toString() + ", "
113  + examiner.getId()
114  + ")";
115 
116  getCRInstance().executeInsertSQL(insertClause);
117 
118  String queryClause = SELECT_QUERY_BASE
119  + "WHERE pa.persona_id = " + persona.getId()
120  + " AND pa.alias = '" + alias + "'"
121  + " AND pa.date_added = " + timeStampMillis
122  + " AND pa.examiner_id = " + examiner.getId();
123 
124  PersonaAliasesQueryCallback queryCallback = new PersonaAliasesQueryCallback();
125  getCRInstance().executeSelectSQL(queryClause, queryCallback);
126 
127  Collection<PersonaAlias> aliases = queryCallback.getAliases();
128  if (aliases.size() != 1) {
129  throw new CentralRepoException("Alias add query failed");
130  }
131 
132  return aliases.iterator().next();
133  }
134 
142  static void removePersonaAlias(PersonaAlias alias) throws CentralRepoException {
143  String deleteClause = " DELETE FROM persona_alias WHERE id = " + alias.getId();
144  getCRInstance().executeDeleteSQL(deleteClause);
145  }
146 
154  static void modifyPersonaAlias(PersonaAlias alias, Persona.Confidence confidence, String justification) throws CentralRepoException {
155  CentralRepository cr = CentralRepository.getInstance();
156 
157  if (cr == null) {
158  throw new CentralRepoException("Failed to modify persona alias, Central Repo is not enabled");
159  }
160 
161  String updateClause = "UPDATE persona_alias SET confidence_id = " + confidence.getLevelId() + ", justification = '" + justification + "' WHERE id = " + alias.id;
162  cr.executeUpdateSQL(updateClause);
163  }
164 
165 
169  static class PersonaAliasesQueryCallback implements CentralRepositoryDbQueryCallback {
170 
171  private final Collection<PersonaAlias> personaAliases = new ArrayList<>();
172 
173  @Override
174  public void process(ResultSet rs) throws SQLException {
175 
176  while (rs.next()) {
177  CentralRepoExaminer examiner = new CentralRepoExaminer(
178  rs.getInt("examiner_id"),
179  rs.getString("login_name"));
180 
181  PersonaAlias alias = new PersonaAlias(
182  rs.getLong("id"),
183  rs.getLong("persona_id"),
184  rs.getString("alias"),
185  rs.getString("justification"),
186  Persona.Confidence.fromId(rs.getInt("confidence_id")),
187  Long.parseLong(rs.getString("date_added")),
188  examiner);
189 
190  personaAliases.add(alias);
191  }
192  }
193 
194  Collection<PersonaAlias> getAliases() {
195  return Collections.unmodifiableCollection(personaAliases);
196  }
197  };
198 
207  public static Collection<PersonaAlias> getPersonaAliases(long personaId) throws CentralRepoException {
208  String queryClause = SELECT_QUERY_BASE + "WHERE pa.persona_id = " + personaId;
209 
210  PersonaAliasesQueryCallback queryCallback = new PersonaAliasesQueryCallback();
211  getCRInstance().executeSelectSQL(queryClause, queryCallback);
212 
213  return queryCallback.getAliases();
214  }
215 
224  private static CentralRepository getCRInstance() throws CentralRepoException {
226 
227  if(instance == null) {
228  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
229  }
230 
231  return instance;
232  }
233 }
void executeSelectSQL(String sql, CentralRepositoryDbQueryCallback queryCallback)
CentralRepoExaminer getOrInsertExaminer(String examinerLoginName)
static Collection< PersonaAlias > getPersonaAliases(long personaId)
PersonaAlias(long id, long personaId, String alias, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)

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.