Autopsy  4.17.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
PersonaAccount.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 java.util.Objects;
29 import org.apache.commons.lang3.StringUtils;
30 import org.sleuthkit.datamodel.Account;
31 
39 public class PersonaAccount {
40 
41  private final long id;
42  private final Persona persona;
43  private final CentralRepoAccount account;
44  private final String justification;
45  private final Persona.Confidence confidence;
46  private final long dateAdded;
48 
49  private PersonaAccount(long id, Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner) {
50  this.id = id;
51  this.persona = persona;
52  this.account = account;
53  this.justification = justification;
54  this.confidence = confidence;
55  this.dateAdded = dateAdded;
56  this.examiner = examiner;
57  }
58 
59  public long getId() {
60  return id;
61  }
62 
63  public Persona getPersona() {
64  return persona;
65  }
66 
68  return account;
69  }
70 
71  public String getJustification() {
72  return justification;
73  }
74 
76  return confidence;
77  }
78 
79  public long getDateAdded() {
80  return dateAdded;
81  }
82 
84  return examiner;
85  }
86 
87  @Override
88  public int hashCode() {
89  int hash = 5;
90  hash = 83 * hash + Objects.hashCode(this.persona);
91  hash = 83 * hash + Objects.hashCode(this.account);
92  hash = 83 * hash + (int) (this.dateAdded ^ (this.dateAdded >>> 32));
93  hash = 83 * hash + Objects.hashCode(this.examiner);
94  return hash;
95  }
96 
97  @Override
98  public boolean equals(Object obj) {
99  if (this == obj) {
100  return true;
101  }
102  if (obj == null) {
103  return false;
104  }
105  if (getClass() != obj.getClass()) {
106  return false;
107  }
108  final PersonaAccount other = (PersonaAccount) obj;
109  if (this.dateAdded != other.getDateAdded()) {
110  return false;
111  }
112  if (!Objects.equals(this.persona, other.getPersona())) {
113  return false;
114  }
115  if (!Objects.equals(this.account, other.getAccount())) {
116  return false;
117  }
118  return Objects.equals(this.examiner, other.getExaminer());
119  }
120 
134  static PersonaAccount addPersonaAccount(Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence) throws CentralRepoException {
135  CentralRepoExaminer currentExaminer = getCRInstance().getOrInsertExaminer(System.getProperty("user.name"));
136 
137  Instant instant = Instant.now();
138  Long timeStampMillis = instant.toEpochMilli();
139 
140  String insertSQL = "INSERT INTO persona_accounts (persona_id, account_id, justification, confidence_id, date_added, examiner_id ) "
141  + " VALUES ( ?, ?, ?, ?, ?, ?)";
142 
143  List<Object> params = new ArrayList<>();
144  params.add(persona.getId());
145  params.add(account.getId());
146  params.add(StringUtils.isBlank(justification) ? "" : justification);
147  params.add(confidence.getLevelId());
148  params.add(timeStampMillis);
149  params.add(currentExaminer.getId());
150 
151  getCRInstance().executeCommand(insertSQL, params);
152 
153  String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
154  + "WHERE persona_id = ? "
155  + " AND account_type_id = ?"
156  + " AND account_unique_identifier = ?";
157 
158  List<Object> queryParams = new ArrayList<>();
159  queryParams.add(persona.getId());
160  queryParams.add(account.getAccountType().getAccountTypeId());
161  queryParams.add(account.getIdentifier());
162 
163  PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback();
164  getCRInstance().executeQuery(querySQL, queryParams, queryCallback);
165 
166  Collection<PersonaAccount> accounts = queryCallback.getPersonaAccountsList();
167  if (accounts.size() != 1) {
168  throw new CentralRepoException("Account add query failed");
169  }
170 
171  return accounts.iterator().next();
172  }
173 
177  private static class PersonaAccountsQueryCallback implements CentralRepositoryDbQueryCallback {
178 
179  Collection<PersonaAccount> personaAccountsList = new ArrayList<>();
180 
181  @Override
182  public void process(ResultSet rs) throws CentralRepoException, SQLException {
183 
184  while (rs.next()) {
185  // examiner that created the persona/account association
186  CentralRepoExaminer paExaminer = new CentralRepoExaminer(
187  rs.getInt("pa_examiner_id"),
188  rs.getString("pa_examiner_login_name"));
189 
190  // examiner that created the persona
191  CentralRepoExaminer personaExaminer = new CentralRepoExaminer(
192  rs.getInt("persona_examiner_id"),
193  rs.getString("persona_examiner_login_name"));
194 
195  // create persona
196  Persona.PersonaStatus status = Persona.PersonaStatus.fromId(rs.getInt("status_id"));
197  Persona persona = new Persona(
198  rs.getInt("persona_id"),
199  rs.getString("uuid"),
200  rs.getString("name"),
201  rs.getString("comment"),
202  Long.parseLong(rs.getString("created_date")),
203  Long.parseLong(rs.getString("modified_date")),
204  status,
205  personaExaminer
206  );
207 
208  // create account
209  CentralRepoAccount.CentralRepoAccountType crAccountType = getCRInstance().getAccountTypeByName(rs.getString("type_name"));
211  rs.getInt("account_id"),
212  crAccountType,
213  rs.getString("account_unique_identifier"));
214 
215  // create persona account
216  PersonaAccount personaAccount = new PersonaAccount(rs.getLong("persona_accounts_id"), persona, account,
217  rs.getString("justification"),
218  Persona.Confidence.fromId(rs.getInt("confidence_id")),
219  Long.parseLong(rs.getString("date_added")),
220  paExaminer);
221 
222  personaAccountsList.add(personaAccount);
223  }
224  }
225 
226  Collection<PersonaAccount> getPersonaAccountsList() {
227  return Collections.unmodifiableCollection(personaAccountsList);
228  }
229  };
230 
231  // Query clause to select from persona_accounts table to create PersonaAccount(s)
232  private static final String PERSONA_ACCOUNTS_QUERY_CLAUSE = "SELECT persona_accounts.id as persona_accounts_id, justification, confidence_id, date_added, persona_accounts.examiner_id as pa_examiner_id, pa_examiner.login_name as pa_examiner_login_name, pa_examiner.display_name as pa_examiner_display_name,"
233  + " personas.id as persona_id, personas.uuid, personas.name, personas.comment, personas.created_date, personas.modified_date, personas.status_id, "
234  + " personas.examiner_id as persona_examiner_id, persona_examiner.login_name as persona_examiner_login_name, persona_examiner.display_name as persona_examiner_display_name, "
235  + " accounts.id as account_id, account_type_id, account_unique_identifier,"
236  + " account_types.type_name as type_name "
237  + " FROM persona_accounts as persona_accounts "
238  + " JOIN personas as personas on persona_accounts.persona_id = personas.id "
239  + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
240  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id "
241  + " JOIN examiners as pa_examiner ON pa_examiner.id = persona_accounts.examiner_id "
242  + " JOIN examiners as persona_examiner ON persona_examiner.id = personas.examiner_id ";
243 
254  static Collection<PersonaAccount> getPersonaAccountsForPersona(long personaId) throws CentralRepoException {
255  String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
256  + " WHERE persona_accounts.persona_id = ?";
257 
258  List<Object> queryParams = new ArrayList<>();
259  queryParams.add(personaId);
260 
262  getCRInstance().executeQuery(querySQL, queryParams, queryCallback);
263 
264  return queryCallback.getPersonaAccountsList();
265  }
266 
277  public static Collection<PersonaAccount> getPersonaAccountsForAccount(long accountId) throws CentralRepoException {
278  String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
279  + " WHERE persona_accounts.account_id = ?"
280  + " AND personas.status_id != ?";
281 
282  List<Object> queryParams = new ArrayList<>();
283  queryParams.add(accountId);
284  queryParams.add(Persona.PersonaStatus.DELETED.getStatusId());
285 
287  getCRInstance().executeQuery(querySQL, queryParams, queryCallback);
288  return queryCallback.getPersonaAccountsList();
289  }
290 
303  public static Collection<PersonaAccount> getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring) throws CentralRepoException {
304  String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
305  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER(?)"
306  + " AND personas.status_id != ?";
307 
308  List<Object> queryParams = new ArrayList<>();
309  queryParams.add("%" + accountIdentifierSubstring + "%"); // substring match
310  queryParams.add(Persona.PersonaStatus.DELETED.getStatusId());
311 
313  getCRInstance().executeQuery(querySQL, queryParams, queryCallback);
314  return queryCallback.getPersonaAccountsList();
315  }
316 
327  public static Collection<PersonaAccount> getPersonaAccountsForAccount(Account account) throws CentralRepoException {
328  String querySQL = PERSONA_ACCOUNTS_QUERY_CLAUSE
329  + " WHERE LOWER(accounts.account_unique_identifier) = LOWER(?)"
330  + " AND type_name = ?"
331  + " AND personas.status_id != ?";
332 
333  List<Object> queryParams = new ArrayList<>();
334  queryParams.add(account.getTypeSpecificID()); // substring match
335  queryParams.add(account.getAccountType().getTypeName());
336  queryParams.add(Persona.PersonaStatus.DELETED.getStatusId());
337 
339  getCRInstance().executeQuery(querySQL, queryParams, queryCallback);
340  return queryCallback.getPersonaAccountsList();
341  }
342 
351  static void removePersonaAccount(long id) throws CentralRepoException {
352  String deleteSQL = " DELETE FROM persona_accounts WHERE id = ?";
353  List<Object> params = new ArrayList<>();
354  params.add(id);
355 
356  getCRInstance().executeCommand(deleteSQL, params);
357  }
358 
367  static void modifyPersonaAccount(long id, Persona.Confidence confidence, String justification) throws CentralRepoException {
368  String updateSQL = "UPDATE persona_accounts SET confidence_id = ?, justification = ? WHERE id = ?";
369 
370  List<Object> params = new ArrayList<>();
371  params.add(confidence.getLevelId());
372  params.add(StringUtils.isBlank(justification) ? "" : justification);
373  params.add(id);
374 
375  getCRInstance().executeCommand(updateSQL, params);
376  }
377 
382  private static class AccountsForPersonaQueryCallback implements CentralRepositoryDbQueryCallback {
383 
384  Collection<CentralRepoAccount> accountsList = new ArrayList<>();
385 
386  @Override
387  public void process(ResultSet rs) throws CentralRepoException, SQLException {
388 
389  while (rs.next()) {
390 
391  // create account
392  CentralRepoAccount.CentralRepoAccountType crAccountType = getCRInstance().getAccountTypeByName(rs.getString("type_name"));
394  rs.getInt("account_id"),
395  crAccountType,
396  rs.getString("account_unique_identifier"));
397 
398  accountsList.add(account);
399  }
400  }
401 
402  Collection<CentralRepoAccount> getAccountsList() {
403  return Collections.unmodifiableCollection(accountsList);
404  }
405  };
406 
418  static Collection<CentralRepoAccount> getAccountsForPersona(long personaId) throws CentralRepoException {
419  String queryClause = "SELECT account_id, "
420  + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
421  + " account_types.type_name as type_name "
422  + " FROM persona_accounts "
423  + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
424  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id "
425  + " WHERE persona_accounts.persona_id = ?";
426 
427  List<Object> queryParams = new ArrayList<>();
428  queryParams.add(personaId);
429 
430  AccountsForPersonaQueryCallback queryCallback = new AccountsForPersonaQueryCallback();
431  getCRInstance().executeQuery(queryClause, queryParams, queryCallback);
432 
433  return queryCallback.getAccountsList();
434  }
435 
444  private static CentralRepository getCRInstance() throws CentralRepoException {
446 
447  if (instance == null) {
448  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
449  }
450 
451  return instance;
452  }
453 }
static Collection< PersonaAccount > getPersonaAccountsForAccount(long accountId)
static Collection< PersonaAccount > getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
CentralRepoExaminer getOrInsertExaminer(String examinerLoginName)
static Collection< PersonaAccount > getPersonaAccountsForAccount(Account account)
CentralRepoAccountType getAccountTypeByName(String accountTypeName)
PersonaAccount(long id, Persona persona, CentralRepoAccount account, String justification, Persona.Confidence confidence, long dateAdded, CentralRepoExaminer examiner)

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.