Autopsy  4.19.3
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 java.util.List;
28 import org.apache.commons.lang3.StringUtils;
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 insertSQL = "INSERT INTO persona_alias (persona_id, alias, justification, confidence_id, date_added, examiner_id ) "
107  + " VALUES ( ?, ?, ?, ?, ?, ?)";
108 
109  List<Object> params = new ArrayList<>();
110  params.add(persona.getId());
111  params.add(alias);
112  params.add(StringUtils.isBlank(justification) ? "" : justification);
113  params.add(confidence.getLevelId());
114  params.add(timeStampMillis);
115  params.add(examiner.getId());
116 
117  getCRInstance().executeCommand(insertSQL, params);
118 
119  String queryClause = SELECT_QUERY_BASE
120  + "WHERE pa.persona_id = ?"
121  + " AND pa.alias = ?"
122  + " AND pa.date_added = ?"
123  + " AND pa.examiner_id = ?";
124 
125  List<Object> queryParams = new ArrayList<>();
126  queryParams.add(persona.getId());
127  queryParams.add(alias);
128  queryParams.add(timeStampMillis);
129  queryParams.add(examiner.getId());
130 
131  PersonaAliasesQueryCallback queryCallback = new PersonaAliasesQueryCallback();
132  getCRInstance().executeQuery(queryClause, queryParams, queryCallback);
133 
134  Collection<PersonaAlias> aliases = queryCallback.getAliases();
135  if (aliases.size() != 1) {
136  throw new CentralRepoException("Alias add query failed");
137  }
138 
139  return aliases.iterator().next();
140  }
141 
149  static void removePersonaAlias(PersonaAlias alias) throws CentralRepoException {
150  String deleteSQL = " DELETE FROM persona_alias WHERE id = ?";
151 
152  List<Object> params = new ArrayList<>();
153  params.add(alias.getId());
154 
155  getCRInstance().executeCommand(deleteSQL, params);
156  }
157 
165  static void modifyPersonaAlias(PersonaAlias alias, 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 alias, Central Repo is not enabled");
170  }
171 
172  String updateClause = "UPDATE persona_alias SET confidence_id = ?, justification = ? WHERE id = ?";
173 
174  List<Object> params = new ArrayList<>();
175  params.add(confidence.getLevelId());
176  params.add(StringUtils.isBlank(justification) ? "" : justification);
177  params.add(alias.getId());
178 
179  cr.executeCommand(updateClause, params);
180  }
181 
185  static class PersonaAliasesQueryCallback implements CentralRepositoryDbQueryCallback {
186 
187  private final Collection<PersonaAlias> personaAliases = new ArrayList<>();
188 
189  @Override
190  public void process(ResultSet rs) throws SQLException {
191 
192  while (rs.next()) {
193  CentralRepoExaminer examiner = new CentralRepoExaminer(
194  rs.getInt("examiner_id"),
195  rs.getString("login_name"));
196 
197  PersonaAlias alias = new PersonaAlias(
198  rs.getLong("id"),
199  rs.getLong("persona_id"),
200  rs.getString("alias"),
201  rs.getString("justification"),
202  Persona.Confidence.fromId(rs.getInt("confidence_id")),
203  Long.parseLong(rs.getString("date_added")),
204  examiner);
205 
206  personaAliases.add(alias);
207  }
208  }
209 
210  Collection<PersonaAlias> getAliases() {
211  return Collections.unmodifiableCollection(personaAliases);
212  }
213  };
214 
223  public static Collection<PersonaAlias> getPersonaAliases(long personaId) throws CentralRepoException {
224  String queryClause = SELECT_QUERY_BASE
225  + "WHERE pa.persona_id = ?";
226 
227  List<Object> params = new ArrayList<>();
228  params.add(personaId);
229 
230  PersonaAliasesQueryCallback queryCallback = new PersonaAliasesQueryCallback();
231  getCRInstance().executeQuery(queryClause, params, queryCallback);
232 
233  return queryCallback.getAliases();
234  }
235 
244  private static CentralRepository getCRInstance() throws CentralRepoException {
246 
247  if (instance == null) {
248  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
249  }
250 
251  return instance;
252  }
253 }
void executeQuery(String sql, List< Object > params, 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-2022 Basis Technology. Generated on: Mon Nov 28 2022
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.