Autopsy  4.16.0
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  private final String typeSpecificIdentifier;
44 
49  public static final class CentralRepoAccountType {
50 
51  // id is the primary key in the account_types table
52  private final int accountTypeId;
53  private final Account.Type acctType;
54  private final int correlationTypeId;
55 
56  CentralRepoAccountType(int acctTypeID, Account.Type acctType, int correlationTypeId) {
57  this.acctType = acctType;
58  this.correlationTypeId = correlationTypeId;
59  this.accountTypeId = acctTypeID;
60  }
61 
65  public Account.Type getAcctType() {
66  return acctType;
67  }
68 
69  public int getCorrelationTypeId() {
70  return this.correlationTypeId;
71  }
72 
73  public int getAccountTypeId() {
74  return this.accountTypeId;
75  }
76 
77  @Override
78  public int hashCode() {
79  int hash = 5;
80  hash = 29 * hash + this.accountTypeId;
81  hash = 29 * hash + Objects.hashCode(this.acctType);
82  hash = 29 * hash + this.correlationTypeId;
83  return hash;
84  }
85 
86  @Override
87  public boolean equals(Object obj) {
88  if (this == obj) {
89  return true;
90  }
91  if (obj == null) {
92  return false;
93  }
94  if (getClass() != obj.getClass()) {
95  return false;
96  }
98  if (this.accountTypeId != other.getAccountTypeId()) {
99  return false;
100  }
101  if (this.correlationTypeId != other.getCorrelationTypeId()) {
102  return false;
103  }
104  return Objects.equals(this.acctType, other.getAcctType());
105  }
106 
107  }
108 
109  public CentralRepoAccount(long accountId, CentralRepoAccountType accountType, String typeSpecificIdentifier) {
110  this.accountId = accountId;
111  this.accountType = accountType;
112  this.typeSpecificIdentifier = typeSpecificIdentifier;
113  }
114 
121  public String getIdentifier() {
122  return this.typeSpecificIdentifier;
123  }
124 
131  return this.accountType;
132  }
133 
139  public long getId() {
140  return this.accountId;
141  }
142 
143  @Override
144  public int hashCode() {
145  int hash = 5;
146  hash = 43 * hash + (int) (this.accountId ^ (this.accountId >>> 32));
147  hash = 43 * hash + (this.accountType != null ? this.accountType.hashCode() : 0);
148  hash = 43 * hash + (this.typeSpecificIdentifier != null ? this.typeSpecificIdentifier.hashCode() : 0);
149  return hash;
150  }
151 
152  @Override
153  public boolean equals(Object obj) {
154  if (this == obj) {
155  return true;
156  }
157  if (obj == null) {
158  return false;
159  }
160  if (getClass() != obj.getClass()) {
161  return false;
162  }
163  final CentralRepoAccount other = (CentralRepoAccount) obj;
164  if (this.accountId != other.getId()) {
165  return false;
166  }
167  if (!Objects.equals(this.typeSpecificIdentifier, other.getIdentifier())) {
168  return false;
169  }
170  return Objects.equals(this.accountType, other.getAccountType());
171  }
172 
176  private static class AccountsQueryCallback implements CentralRepositoryDbQueryCallback {
177 
178  Collection<CentralRepoAccount> accountsList = new ArrayList<>();
179 
180  @Override
181  public void process(ResultSet rs) throws CentralRepoException, SQLException {
182 
183  while (rs.next()) {
184 
185  // create account
186  Account.Type acctType = new Account.Type(rs.getString("type_name"), rs.getString("display_name"));
187  CentralRepoAccountType crAccountType = new CentralRepoAccountType(rs.getInt("account_type_id"), acctType, rs.getInt("correlation_type_id"));
188 
190  rs.getInt("account_id"),
191  crAccountType,
192  rs.getString("account_unique_identifier"));
193 
194  accountsList.add(account);
195  }
196  }
197 
198  Collection<CentralRepoAccount> getAccountsList() {
199  return Collections.unmodifiableCollection(accountsList);
200  }
201  };
202 
203  private static final String ACCOUNTS_QUERY_CLAUSE
204  = "SELECT accounts.id as account_id, "
205  + " accounts.account_type_id as account_type_id, accounts.account_unique_identifier as account_unique_identifier,"
206  + " account_types.id as account_type_id, "
207  + " account_types.type_name as type_name, account_types.display_name as display_name, account_types.correlation_type_id as correlation_type_id "
208  + " FROM accounts "
209  + " JOIN account_types as account_types on accounts.account_type_id = account_types.id ";
210 
223  public static Collection<CentralRepoAccount> getAccountsWithIdentifierLike(String accountIdentifierSubstring) throws CentralRepoException {
224 
225  String queryClause = ACCOUNTS_QUERY_CLAUSE
226  + " WHERE LOWER(accounts.account_unique_identifier) LIKE LOWER(?)";
227 
228  List<Object> params = new ArrayList<>();
229  params.add("%" + accountIdentifierSubstring + "%");
230 
231  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
232  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
233 
234  return queryCallback.getAccountsList();
235  }
236 
248  public static Collection<CentralRepoAccount> getAccountsWithIdentifier(String accountIdentifier) throws InvalidAccountIDException, CentralRepoException {
249 
250  String normalizedAccountIdentifier = normalizeAccountIdentifier(accountIdentifier);
251  String queryClause = ACCOUNTS_QUERY_CLAUSE
252  + " WHERE LOWER(accounts.account_unique_identifier) = LOWER(?)";
253 
254  List<Object> params = new ArrayList<>();
255  params.add(normalizedAccountIdentifier);
256 
257  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
258  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
259 
260  return queryCallback.getAccountsList();
261  }
262 
272  public static Collection<CentralRepoAccount> getAllAccounts() throws CentralRepoException {
273 
274  String queryClause = ACCOUNTS_QUERY_CLAUSE;
275 
276  List<Object> params = new ArrayList<>(); // empty param list
277 
278  AccountsQueryCallback queryCallback = new AccountsQueryCallback();
279  CentralRepository.getInstance().executeQuery(queryClause, params, queryCallback);
280 
281  return queryCallback.getAccountsList();
282  }
283 
293  private static String normalizeAccountIdentifier(String accountIdentifier) throws InvalidAccountIDException {
294  if (StringUtils.isEmpty(accountIdentifier)) {
295  throw new InvalidAccountIDException("Account id is null or empty.");
296  }
297 
298  String normalizedAccountIdentifier;
299  try {
300  if (CorrelationAttributeNormalizer.isValidPhoneNumber(accountIdentifier)) {
301  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizePhone(accountIdentifier);
302  } else if (CorrelationAttributeNormalizer.isValidEmailAddress(accountIdentifier)) {
303  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizeEmail(accountIdentifier);
304  } else {
305  normalizedAccountIdentifier = accountIdentifier.toLowerCase().trim();
306  }
308  throw new InvalidAccountIDException("Failed to normalize the account idenitier " + accountIdentifier, ex);
309  }
310  return normalizedAccountIdentifier;
311  }
312 
322  public static String normalizeAccountIdentifier(CentralRepoAccountType crAccountType, String accountIdentifier) throws InvalidAccountIDException {
323 
324  if (StringUtils.isBlank(accountIdentifier)) {
325  throw new InvalidAccountIDException("Account identifier is null or empty.");
326  }
327 
328  String normalizedAccountIdentifier;
329  try {
330  if (crAccountType.getAcctType().equals(Account.Type.PHONE)) {
331  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizePhone(accountIdentifier);
332  } else if (crAccountType.getAcctType().equals(Account.Type.EMAIL)) {
333  normalizedAccountIdentifier = CorrelationAttributeNormalizer.normalizeEmail(accountIdentifier);
334  } else {
335  // convert to lowercase
336  normalizedAccountIdentifier = accountIdentifier.toLowerCase();
337  }
339  throw new InvalidAccountIDException("Invalid account identifier", ex);
340  }
341 
342  return normalizedAccountIdentifier;
343  }
344 }
static String normalizeAccountIdentifier(CentralRepoAccountType crAccountType, String accountIdentifier)
void executeQuery(String sql, List< Object > params, CentralRepositoryDbQueryCallback queryCallback)
CentralRepoAccount(long accountId, CentralRepoAccountType accountType, String typeSpecificIdentifier)
static Collection< CentralRepoAccount > getAccountsWithIdentifier(String accountIdentifier)
static Collection< CentralRepoAccount > getAccountsWithIdentifierLike(String accountIdentifierSubstring)

Copyright © 2012-2020 Basis Technology. Generated on: Tue Sep 22 2020
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.