Autopsy  4.15.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.Objects;
28 import org.apache.commons.lang3.StringUtils;
29 import org.sleuthkit.datamodel.SleuthkitCase;
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  public 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  String insertClause = " INTO persona_accounts (persona_id, account_id, justification, confidence_id, date_added, examiner_id ) "
140  + "VALUES ( "
141  + persona.getId() + ", "
142  + account.getId() + ", "
143  + "'" + ((StringUtils.isBlank(justification) ? "" : SleuthkitCase.escapeSingleQuotes(justification))) + "', "
144  + confidence.getLevelId() + ", "
145  + timeStampMillis.toString() + ", "
146  + currentExaminer.getId()
147  + ")";
148 
149  getCRInstance().executeInsertSQL(insertClause);
150 
151  String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
152  + "WHERE persona_id = " + persona.getId()
153  + " AND account_type_id = " + account.getAccountType().getAccountTypeId()
154  + " AND account_unique_identifier = '" + account.getIdentifier() + "'";
155  PersonaAccountsQueryCallback queryCallback = new PersonaAccountsQueryCallback();
156  getCRInstance().executeSelectSQL(queryClause, queryCallback);
157 
158  Collection<PersonaAccount> accounts = queryCallback.getPersonaAccountsList();
159  if (accounts.size() != 1) {
160  throw new CentralRepoException("Account add query failed");
161  }
162 
163  return accounts.iterator().next();
164  }
165 
169  private static class PersonaAccountsQueryCallback implements CentralRepositoryDbQueryCallback {
170 
171  Collection<PersonaAccount> personaAccountsList = new ArrayList<>();
172 
173  @Override
174  public void process(ResultSet rs) throws CentralRepoException, SQLException {
175 
176  while (rs.next()) {
177  // examiner that created the persona/account association
178  CentralRepoExaminer paExaminer = new CentralRepoExaminer(
179  rs.getInt("pa_examiner_id"),
180  rs.getString("pa_examiner_login_name"));
181 
182  // examiner that created the persona
183  CentralRepoExaminer personaExaminer = new CentralRepoExaminer(
184  rs.getInt("persona_examiner_id"),
185  rs.getString("persona_examiner_login_name"));
186 
187  // create persona
188  Persona.PersonaStatus status = Persona.PersonaStatus.fromId(rs.getInt("status_id"));
189  Persona persona = new Persona(
190  rs.getInt("persona_id"),
191  rs.getString("uuid"),
192  rs.getString("name"),
193  rs.getString("comment"),
194  Long.parseLong(rs.getString("created_date")),
195  Long.parseLong(rs.getString("modified_date")),
196  status,
197  personaExaminer
198  );
199 
200  // create account
201  CentralRepoAccount.CentralRepoAccountType crAccountType = getCRInstance().getAccountTypeByName(rs.getString("type_name"));
203  rs.getInt("account_id"),
204  crAccountType,
205  rs.getString("account_unique_identifier"));
206 
207  // create persona account
208  PersonaAccount personaAccount = new PersonaAccount(rs.getLong("persona_accounts_id"), persona, account,
209  rs.getString("justification"),
210  Persona.Confidence.fromId(rs.getInt("confidence_id")),
211  Long.parseLong(rs.getString("date_added")),
212  paExaminer);
213 
214  personaAccountsList.add(personaAccount);
215  }
216  }
217 
218  Collection<PersonaAccount> getPersonaAccountsList() {
219  return Collections.unmodifiableCollection(personaAccountsList);
220  }
221  };
222 
223  // Query clause to select from persona_accounts table to create PersonaAccount(s)
224  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,"
225  + " personas.id as persona_id, personas.uuid, personas.name, personas.comment, personas.created_date, personas.modified_date, personas.status_id, "
226  + " 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, "
227  + " accounts.id as account_id, account_type_id, account_unique_identifier,"
228  + " account_types.type_name as type_name "
229  + " FROM persona_accounts as persona_accounts "
230  + " JOIN personas as personas on persona_accounts.persona_id = personas.id "
231  + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
232  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id "
233  + " JOIN examiners as pa_examiner ON pa_examiner.id = persona_accounts.examiner_id "
234  + " JOIN examiners as persona_examiner ON persona_examiner.id = personas.examiner_id ";
235 
246  static Collection<PersonaAccount> getPersonaAccountsForPersona(long personaId) throws CentralRepoException {
247  String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
248  + " WHERE persona_accounts.persona_id = " + personaId;
249 
251  getCRInstance().executeSelectSQL(queryClause, queryCallback);
252 
253  return queryCallback.getPersonaAccountsList();
254  }
255 
266  public static Collection<PersonaAccount> getPersonaAccountsForAccount(long accountId) throws CentralRepoException {
267  String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
268  + " WHERE persona_accounts.account_id = " + accountId
269  + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId();
270 
272  getCRInstance().executeSelectSQL(queryClause, queryCallback);
273  return queryCallback.getPersonaAccountsList();
274  }
275 
288  public static Collection<PersonaAccount> getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring) throws CentralRepoException {
289  String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
290  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + accountIdentifierSubstring + "%')"
291  + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId();
292 
294  getCRInstance().executeSelectSQL(queryClause, queryCallback);
295  return queryCallback.getPersonaAccountsList();
296 
297  }
298 
309  public static Collection<PersonaAccount> getPersonaAccountsForAccount(Account account) throws CentralRepoException {
310  String queryClause = PERSONA_ACCOUNTS_QUERY_CLAUSE
311  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER('%" + account.getTypeSpecificID() + "%')"
312  + " AND type_name = '" + account.getAccountType().getTypeName() + "' "
313  + " AND personas.status_id != " + Persona.PersonaStatus.DELETED.getStatusId();
314 
316  getCRInstance().executeSelectSQL(queryClause, queryCallback);
317  return queryCallback.getPersonaAccountsList();
318  }
319 
328  static void removePersonaAccount(long id) throws CentralRepoException {
329  String deleteClause = " DELETE FROM persona_accounts WHERE id = " + id;
330  getCRInstance().executeDeleteSQL(deleteClause);
331  }
332 
341  static void modifyPersonaAccount(long id, Persona.Confidence confidence, String justification) throws CentralRepoException {
342  String updateClause = "UPDATE persona_accounts SET confidence_id = " + confidence.getLevelId() + ", justification = '" + justification + "' WHERE id = " + id;
343  getCRInstance().executeUpdateSQL(updateClause);
344  }
345 
350  private static class AccountsForPersonaQueryCallback implements CentralRepositoryDbQueryCallback {
351 
352  Collection<CentralRepoAccount> accountsList = new ArrayList<>();
353 
354  @Override
355  public void process(ResultSet rs) throws CentralRepoException, SQLException {
356 
357  while (rs.next()) {
358 
359  // create account
360  CentralRepoAccount.CentralRepoAccountType crAccountType = getCRInstance().getAccountTypeByName(rs.getString("type_name"));
362  rs.getInt("account_id"),
363  crAccountType,
364  rs.getString("account_unique_identifier"));
365 
366  accountsList.add(account);
367  }
368  }
369 
370  Collection<CentralRepoAccount> getAccountsList() {
371  return Collections.unmodifiableCollection(accountsList);
372  }
373  };
374 
386  static Collection<CentralRepoAccount> getAccountsForPersona(long personaId) throws CentralRepoException {
387  String queryClause = "SELECT account_id, "
388  + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
389  + " account_types.type_name as type_name "
390  + " FROM persona_accounts "
391  + " JOIN accounts as accounts on persona_accounts.account_id = accounts.id "
392  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id "
393  + " WHERE persona_accounts.persona_id = " + personaId;
394 
395  AccountsForPersonaQueryCallback queryCallback = new AccountsForPersonaQueryCallback();
396  getCRInstance().executeSelectSQL(queryClause, queryCallback);
397 
398  return queryCallback.getAccountsList();
399  }
400 
409  private static CentralRepository getCRInstance() throws CentralRepoException {
411 
412  if (instance == null) {
413  throw new CentralRepoException("Failed to get instance of CentralRespository, CR was null");
414  }
415 
416  return instance;
417  }
418 }
void executeSelectSQL(String sql, CentralRepositoryDbQueryCallback queryCallback)
static Collection< PersonaAccount > getPersonaAccountsForAccount(long accountId)
static Collection< PersonaAccount > getPersonaAccountsForIdentifierLike(String accountIdentifierSubstring)
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-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.