Autopsy  4.19.3
Graphical digital forensics platform for The Sleuth Kit and other tools.
CentralRepoAccount.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.util.ArrayList;
24 import java.util.Collection;
25 import java.util.Collections;
26 import java.util.List;
27 import java.util.Objects;
28 import org.apache.commons.lang.StringUtils;
29 import org.sleuthkit.datamodel.Account;
30 import org.sleuthkit.datamodel.InvalidAccountIDException;
31 
35 public final class CentralRepoAccount {
36 
37  // primary key in the Accounts table in CR database
38  private final long accountId;
39 
41 
42  // type specific unique account identifier
43  // Stores what is in the DB which should have been normalized before insertion.
44  private final String typeSpecificIdentifier;
45 
50  public static final class CentralRepoAccountType {
51 
52  // id is the primary key in the account_types table
53  private final int accountTypeId;
54  private final Account.Type acctType;
55  private final int correlationTypeId;
56 
57  CentralRepoAccountType(int acctTypeID, Account.Type acctType, int correlationTypeId) {
58  this.acctType = acctType;
59  this.correlationTypeId = correlationTypeId;
60  this.accountTypeId = acctTypeID;
61  }
62 
66  public Account.Type getAcctType() {
67  return acctType;
68  }
69 
70  public int getCorrelationTypeId() {
71  return this.correlationTypeId;
72  }
73 
74  public int getAccountTypeId() {
75  return this.accountTypeId;
76  }
77 
78  @Override
79  public int hashCode() {
80  int hash = 5;
81  hash = 29 * hash + this.accountTypeId;
82  hash = 29 * hash + Objects.hashCode(this.acctType);
83  hash = 29 * hash + this.correlationTypeId;
84  return hash;
85  }
86 
87  @Override
88  public boolean equals(Object obj) {
89  if (this == obj) {
90  return true;
91  }
92  if (obj == null) {
93  return false;
94  }
95  if (getClass() != obj.getClass()) {
96  return false;
97  }
99  if (this.accountTypeId != other.getAccountTypeId()) {
100  return false;
101  }
102  if (this.correlationTypeId != other.getCorrelationTypeId()) {
103  return false;
104  }
105  return Objects.equals(this.acctType, other.getAcctType());
106  }
107 
108  }
109 
110  CentralRepoAccount(long accountId, CentralRepoAccountType accountType, String typeSpecificIdentifier) {
111  this.accountId = accountId;
112  this.accountType = accountType;
113  this.typeSpecificIdentifier = typeSpecificIdentifier;
114  }
115 
124  public String getIdentifier() {
125  return this.typeSpecificIdentifier;
126  }
127 
134  return this.accountType;
135  }
136 
142  public long getId() {
143  return this.accountId;
144  }
145 
146  @Override
147  public int hashCode() {
148  int hash = 5;
149  hash = 43 * hash + (int) (this.accountId ^ (this.accountId >>> 32));
150  hash = 43 * hash + (this.accountType != null ? this.accountType.hashCode() : 0);
151  hash = 43 * hash + (this.typeSpecificIdentifier != null ? this.typeSpecificIdentifier.hashCode() : 0);
152  return hash;
153  }
154 
155  @Override
156  public boolean equals(Object obj) {
157  if (this == obj) {
158  return true;
159  }
160  if (obj == null) {
161  return false;
162  }
163  if (getClass() != obj.getClass()) {
164  return false;
165  }
166  final CentralRepoAccount other = (CentralRepoAccount) obj;
167  if (this.accountId != other.getId()) {
168  return false;
169  }
170  if (!Objects.equals(this.typeSpecificIdentifier, other.getIdentifier())) {
171  return false;
172  }
173  return Objects.equals(this.accountType, other.getAccountType());
174  }
175 
179  private static class AccountsQueryCallback implements CentralRepositoryDbQueryCallback {
180 
181  Collection<CentralRepoAccount> accountsList = new ArrayList<>();
182 
183  @Override
184  public void process(ResultSet rs) throws CentralRepoException, SQLException {
185 
186  while (rs.next()) {
187 
188  // create account
189  Account.Type acctType = new Account.Type(rs.getString("type_name"), rs.getString("display_name"));
190  CentralRepoAccountType crAccountType = new CentralRepoAccountType(rs.getInt("account_type_id"), acctType, rs.getInt("correlation_type_id"));
191 
193  rs.getInt("account_id"),
194  crAccountType,
195  rs.getString("account_unique_identifier"));
196 
197  accountsList.add(account);
198  }
199  }
200 
201  Collection<CentralRepoAccount> getAccountsList() {
202  return Collections.unmodifiableCollection(accountsList);
203  }
204  };
205 
206  private static final String ACCOUNTS_QUERY_CLAUSE
207  = "SELECT accounts.id as account_id, "
208  + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
209  + " account_types.id as account_type_id, "
210  + " account_types.type_name as type_name, account_types.display_name as display_name, account_types.correlation_type_id as correlation_type_id "
211  + " FROM accounts "
212  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id ";
213 
226  public static Collection<CentralRepoAccount> getAccountsWithIdentifierLike(String accountIdentifierSubstring) throws CentralRepoException {
227 
228  String queryClause = ACCOUNTS_QUERY_CLAUSE
229  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER(?)";
230 
231  List<Object> params = new ArrayList<>();
232  params.add("%" + accountIdentifierSubstring + "%");
233 
234  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
235  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
236 
237  return queryCallback.getAccountsList();
238  }
239 
251  public static Collection<CentralRepoAccount> getAccountsWithIdentifier(String accountIdentifier) throws InvalidAccountIDException, CentralRepoException {
252 
253  String normalizedAccountIdentifier = normalizeAccountIdentifier(accountIdentifier);
254  String queryClause = ACCOUNTS_QUERY_CLAUSE
255  + " WHERE LOWER(accounts.account_unique_identifier) = LOWER(?)";
256 
257  List<Object> params = new ArrayList<>();
258  params.add(normalizedAccountIdentifier);
259 
260  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
261  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
262 
263  return queryCallback.getAccountsList();
264  }
265 
275  public static Collection<CentralRepoAccount> getAllAccounts() throws CentralRepoException {
276 
277  String queryClause = ACCOUNTS_QUERY_CLAUSE;
278 
279  List<Object> params = new ArrayList<>(); // empty param list
280 
281  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
282  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
283 
284  return queryCallback.getAccountsList();
285  }
286 
296  private static String normalizeAccountIdentifier(String accountIdentifier) throws InvalidAccountIDException {
297  if (StringUtils.isEmpty(accountIdentifier)) {
298  throw new InvalidAccountIDException("Account id is null or empty.");
299  }
300 
301  String normalizedAccountIdentifier;
302  try {
303  if (CorrelationAttributeNormalizer.isValidPhoneNumber(accountIdentifier)) {
304  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizePhone(accountIdentifier);
305  } else if (CorrelationAttributeNormalizer.isValidEmailAddress(accountIdentifier)) {
306  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizeEmail(accountIdentifier);
307  } else {
308  normalizedAccountIdentifier = accountIdentifier.toLowerCase().trim();
309  }
311  throw new InvalidAccountIDException("Failed to normalize the account idenitier " + accountIdentifier, ex);
312  }
313  return normalizedAccountIdentifier;
314  }
315 
325  public static String normalizeAccountIdentifier(CentralRepoAccountType crAccountType, String accountIdentifier) throws InvalidAccountIDException {
326 
327  if (StringUtils.isBlank(accountIdentifier)) {
328  throw new InvalidAccountIDException("Account identifier is null or empty.");
329  }
330 
331  String normalizedAccountIdentifier;
332  try {
333  if (crAccountType.getAcctType().equals(Account.Type.PHONE)) {
334  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizePhone(accountIdentifier);
335  } else if (crAccountType.getAcctType().equals(Account.Type.EMAIL)) {
336  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizeEmail(accountIdentifier);
337  } else {
338  // convert to lowercase
339  normalizedAccountIdentifier = accountIdentifier.toLowerCase();
340  }
342  throw new InvalidAccountIDException(String.format("Account id normaization failed, invalid account identifier %s", accountIdentifier), ex);
343  }
344 
345  return normalizedAccountIdentifier;
346  }
347 }
static String normalizeAccountIdentifier(CentralRepoAccountType crAccountType, String accountIdentifier)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
static Collection< CentralRepoAccount > getAccountsWithIdentifier(String accountIdentifier)
static Collection< CentralRepoAccount > getAccountsWithIdentifierLike(String accountIdentifierSubstring)

Copyright © 2012-2022 Basis Technology. Generated on: Tue Jun 27 2023
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.