Autopsy  4.5.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SqliteEamDb.java
Go to the documentation of this file.
1 /*
2  * Central Repository
3  *
4  * Copyright 2015-2017 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.io.File;
22 import java.sql.Connection;
23 import java.sql.SQLException;
24 import java.sql.Statement;
25 import java.util.Arrays;
26 import java.util.List;
27 import java.util.Set;
28 import java.util.concurrent.locks.ReentrantReadWriteLock;
29 import java.util.logging.Level;
30 import org.apache.commons.dbcp2.BasicDataSource;
32 import org.sleuthkit.datamodel.TskData;
35 
41 public class SqliteEamDb extends AbstractSqlEamDb {
42 
43  private final static Logger LOGGER = Logger.getLogger(SqliteEamDb.class.getName());
44 
45  private static SqliteEamDb instance;
46 
47  private BasicDataSource connectionPool = null;
48 
50 
51  // While the Sqlite database should only be used for single users, it is still
52  // possible for multiple threads to attempt to write to the database simultaneously.
53  private final ReentrantReadWriteLock rwLock = new ReentrantReadWriteLock(true);
54 
62  public synchronized static SqliteEamDb getInstance() throws EamDbException {
63  if (instance == null) {
64  instance = new SqliteEamDb();
65  }
66 
67  return instance;
68  }
69 
75  private SqliteEamDb() throws EamDbException {
76  dbSettings = new SqliteEamDbSettings();
78  }
79 
80  @Override
81  public void shutdownConnections() throws EamDbException {
82  try {
83  synchronized(this) {
84  if (null != connectionPool) {
85  connectionPool.close();
86  connectionPool = null; // force it to be re-created on next connect()
87  }
88  }
89  } catch (SQLException ex) {
90  throw new EamDbException("Failed to close existing database connections.", ex); // NON-NLS
91  }
92  }
93 
94  @Override
95  public void updateSettings() {
96  synchronized (this) {
97  dbSettings.loadSettings();
99  }
100  }
101 
102  @Override
103  public void saveSettings() {
104  synchronized (this) {
105  dbSettings.saveSettings();
106  }
107  }
108 
109  @Override
110  public void reset() throws EamDbException {
111  try{
113 
114  Connection conn = connect();
115 
116  try {
117 
118  Statement dropContent = conn.createStatement();
119  dropContent.executeUpdate("DELETE FROM organizations");
120  dropContent.executeUpdate("DELETE FROM cases");
121  dropContent.executeUpdate("DELETE FROM data_sources");
122  dropContent.executeUpdate("DELETE FROM reference_sets");
123  dropContent.executeUpdate("DELETE FROM artifact_types");
124  dropContent.executeUpdate("DELETE FROM db_info");
125 
126  String instancesTemplate = "DELETE FROM %s_instances";
127  String referencesTemplate = "DELETE FROM global_files";
129  dropContent.executeUpdate(String.format(instancesTemplate, type.getDbTableName()));
130  // FUTURE: support other reference types
131  if (type.getId() == CorrelationAttribute.FILES_TYPE_ID) {
132  dropContent.executeUpdate(String.format(referencesTemplate, type.getDbTableName()));
133  }
134  }
135 
136  dropContent.executeUpdate("VACUUM");
137  } catch (SQLException ex) {
138  LOGGER.log(Level.WARNING, "Failed to reset database.", ex);
139  } finally {
141  }
142 
143  dbSettings.insertDefaultDatabaseContent();
144  } finally {
146  }
147  }
148 
153  private void setupConnectionPool() throws EamDbException {
154 
155  if (dbSettings.dbFileExists() == false) {
156  throw new EamDbException("Central repository database missing");
157  }
158 
159  connectionPool = new BasicDataSource();
160  connectionPool.setDriverClassName(dbSettings.getDriver());
161  connectionPool.setUrl(dbSettings.getConnectionURL());
162 
163  // tweak pool configuration
164  connectionPool.setInitialSize(50);
165  connectionPool.setMaxTotal(-1);
166  connectionPool.setMaxIdle(-1);
167  connectionPool.setMaxWaitMillis(1000);
168  connectionPool.setValidationQuery(dbSettings.getValidationQuery());
169  connectionPool.setConnectionInitSqls(Arrays.asList("PRAGMA foreign_keys = ON"));
170  }
171 
179  @Override
180  protected Connection connect() throws EamDbException {
181  synchronized (this) {
182  if (!EamDb.isEnabled()) {
183  throw new EamDbException("Central Repository module is not enabled"); // NON-NLS
184  }
185 
186  if (connectionPool == null) {
188  }
189 
190  try {
191  return connectionPool.getConnection();
192  } catch (SQLException ex) {
193  throw new EamDbException("Error getting connection from connection pool.", ex); // NON-NLS
194  }
195  }
196  }
197 
198  @Override
199  protected String getConflictClause() {
200  // For sqlite, our conflict clause is part of the table schema
201  return "";
202  }
203 
204 
213  @Override
214  public void newDbInfo(String name, String value) throws EamDbException {
215  try{
217  super.newDbInfo(name, value);
218  } finally {
220  }
221  }
222 
232  @Override
233  public String getDbInfo(String name) throws EamDbException {
234  try{
236  return super.getDbInfo(name);
237  } finally {
239  }
240  }
241 
250  @Override
251  public void updateDbInfo(String name, String value) throws EamDbException {
252  try{
254  super.updateDbInfo(name, value);
255  } finally {
257  }
258  }
259 
265  @Override
266  public CorrelationCase newCase(Case autopsyCase) throws EamDbException {
267  try{
269  return super.newCase(autopsyCase);
270  } finally {
272  }
273  }
274 
282  @Override
284  try{
286  return super.newCase(eamCase);
287  } finally {
289  }
290  }
291 
297  @Override
298  public void updateCase(CorrelationCase eamCase) throws EamDbException {
299  try{
301  super.updateCase(eamCase);
302  } finally {
304  }
305  }
306 
314  @Override
315  public CorrelationCase getCaseByUUID(String caseUUID) throws EamDbException {
316  try{
318  return super.getCaseByUUID(caseUUID);
319  } finally {
321  }
322  }
323 
329  @Override
330  public List<CorrelationCase> getCases() throws EamDbException {
331  try{
333  return super.getCases();
334  } finally {
336  }
337  }
338 
344  @Override
345  public void newDataSource(CorrelationDataSource eamDataSource) throws EamDbException {
346  try{
348  super.newDataSource(eamDataSource);
349  } finally {
351  }
352  }
353 
362  @Override
363  public CorrelationDataSource getDataSource(CorrelationCase correlationCase, String dataSourceDeviceId) throws EamDbException {
364  try{
366  return super.getDataSource(correlationCase, dataSourceDeviceId);
367  } finally {
369  }
370  }
371 
377  @Override
378  public List<CorrelationDataSource> getDataSources() throws EamDbException {
379  try{
381  return super.getDataSources();
382  } finally {
384  }
385  }
386 
393  @Override
394  public void addArtifact(CorrelationAttribute eamArtifact) throws EamDbException {
395  try{
397  super.addArtifact(eamArtifact);
398  } finally {
400  }
401  }
402 
412  @Override
413  public List<CorrelationAttributeInstance> getArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value) throws EamDbException {
414  try{
416  return super.getArtifactInstancesByTypeValue(aType, value);
417  } finally {
419  }
420  }
421 
433  @Override
434  public List<CorrelationAttributeInstance> getArtifactInstancesByPath(CorrelationAttribute.Type aType, String filePath) throws EamDbException {
435  try{
437  return super.getArtifactInstancesByPath(aType, filePath);
438  } finally {
440  }
441  }
442 
454  @Override
456  try{
458  return super.getCountArtifactInstancesByTypeValue(aType, value);
459  } finally {
461  }
462  }
463 
464  @Override
466  try{
468  return super.getFrequencyPercentage(corAttr);
469  } finally {
471  }
472  }
473 
485  @Override
487  try{
489  return super.getCountUniqueCaseDataSourceTuplesHavingTypeValue(aType, value);
490  } finally {
492  }
493  }
494 
495 
496  @Override
498  try{
500  return super.getCountUniqueDataSources();
501  } finally {
503  }
504  }
505 
517  @Override
518  public Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID) throws EamDbException {
519  try{
521  return super.getCountArtifactInstancesByCaseDataSource(caseUUID, dataSourceID);
522  } finally {
524  }
525  }
526 
531  @Override
532  public void bulkInsertArtifacts() throws EamDbException {
533  try{
535  super.bulkInsertArtifacts();
536  } finally {
538  }
539  }
540 
544  @Override
545  public void bulkInsertCases(List<CorrelationCase> cases) throws EamDbException {
546  try{
548  super.bulkInsertCases(cases);
549  } finally {
551  }
552  }
553 
564  @Override
565  public void setArtifactInstanceKnownStatus(CorrelationAttribute eamArtifact, TskData.FileKnown knownStatus) throws EamDbException {
566  try{
568  super.setArtifactInstanceKnownStatus(eamArtifact, knownStatus);
569  } finally {
571  }
572  }
573 
583  @Override
584  public List<CorrelationAttributeInstance> getArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value) throws EamDbException {
585  try{
587  return super.getArtifactInstancesKnownBad(aType, value);
588  } finally {
590  }
591  }
592 
601  @Override
603  try{
605  return super.getCountArtifactInstancesKnownBad(aType, value);
606  } finally {
608  }
609  }
610 
623  @Override
625  try{
627  return super.getListCasesHavingArtifactInstancesKnownBad(aType, value);
628  } finally {
630  }
631  }
632 
638  @Override
639  public void deleteReferenceSet(int referenceSetID) throws EamDbException{
640  try{
642  super.deleteReferenceSet(referenceSetID);
643  } finally {
645  }
646  }
647 
655  @Override
656  public boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID) throws EamDbException {
657  try{
659  return super.isValueInReferenceSet(value, referenceSetID, correlationTypeID);
660  } finally {
662  }
663  }
664 
673  @Override
674  public boolean referenceSetExists(String referenceSetName, String version) throws EamDbException {
675  try{
677  return super.referenceSetExists(referenceSetName, version);
678  } finally {
680  }
681  }
682 
691  @Override
692  public boolean isArtifactKnownBadByReference(CorrelationAttribute.Type aType, String value) throws EamDbException {
693  try{
695  return super.isArtifactKnownBadByReference(aType, value);
696  } finally {
698  }
699  }
700 
710  @Override
711  public long newOrganization(EamOrganization eamOrg) throws EamDbException {
712  try{
714  return super.newOrganization(eamOrg);
715  } finally {
717  }
718  }
719 
727  @Override
728  public List<EamOrganization> getOrganizations() throws EamDbException {
729  try{
731  return super.getOrganizations();
732  } finally {
734  }
735  }
736 
746  @Override
748  try{
750  return super.getOrganizationByID(orgID);
751  } finally {
753  }
754  }
755 
756  @Override
757  public void updateOrganization(EamOrganization updatedOrganization) throws EamDbException {
758  try{
760  super.updateOrganization(updatedOrganization);
761  } finally {
763  }
764  }
765 
766  @Override
767  public void deleteOrganization(EamOrganization organizationToDelete) throws EamDbException {
768  try{
770  super.deleteOrganization(organizationToDelete);
771  } finally {
773  }
774  }
784  @Override
785  public int newReferenceSet(EamGlobalSet eamGlobalSet) throws EamDbException {
786  try{
788  return super.newReferenceSet(eamGlobalSet);
789  } finally {
791  }
792  }
793 
803  @Override
804  public EamGlobalSet getReferenceSetByID(int referenceSetID) throws EamDbException {
805  try{
807  return super.getReferenceSetByID(referenceSetID);
808  } finally {
810  }
811  }
812 
822  @Override
823  public List<EamGlobalSet> getAllReferenceSets(CorrelationAttribute.Type correlationType) throws EamDbException {
824  try{
826  return super.getAllReferenceSets(correlationType);
827  } finally {
829  }
830  }
831 
841  @Override
842  public void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttribute.Type correlationType) throws EamDbException {
843  try{
845  super.addReferenceInstance(eamGlobalFileInstance, correlationType);
846  } finally {
848  }
849  }
850 
856  @Override
857  public void bulkInsertReferenceTypeEntries(Set<EamGlobalFileInstance> globalInstances, CorrelationAttribute.Type contentType) throws EamDbException {
858  try{
860  super.bulkInsertReferenceTypeEntries(globalInstances, contentType);
861  } finally {
863  }
864  }
865 
876  @Override
877  public List<EamGlobalFileInstance> getReferenceInstancesByTypeValue(CorrelationAttribute.Type aType, String aValue) throws EamDbException {
878  try{
880  return super.getReferenceInstancesByTypeValue(aType, aValue);
881  } finally {
883  }
884  }
885 
895  @Override
897  try{
899  return super.newCorrelationType(newType);
900  } finally {
902  }
903  }
904 
914  @Override
916  try{
918  return super.getDefinedCorrelationTypes();
919  } finally {
921  }
922  }
923 
933  @Override
935  try{
937  return super.getEnabledCorrelationTypes();
938  } finally {
940  }
941  }
942 
952  @Override
954  try{
956  return super.getSupportedCorrelationTypes();
957  } finally {
959  }
960  }
961 
969  @Override
971  try{
973  super.updateCorrelationType(aType);
974  } finally {
976  }
977  }
978 
988  @Override
990  try{
992  return super.getCorrelationTypeById(typeId);
993  } finally {
995  }
996  }
997 
1002  @Override
1003  public void upgradeSchema() throws EamDbException, SQLException {
1004  try{
1006  super.upgradeSchema();
1007  } finally {
1009  }
1010  }
1011 
1020  @Override
1022  // Multiple users are not supported for SQLite
1023  return null;
1024  }
1025 
1031  private void acquireExclusiveLock() {
1032  rwLock.writeLock().lock();
1033  }
1034 
1040  private void releaseExclusiveLock() {
1041  rwLock.writeLock().unlock();
1042  }
1043 
1049  private void acquireSharedLock() {
1050  rwLock.readLock().lock();
1051  }
1052 
1058  private void releaseSharedLock() {
1059  rwLock.readLock().unlock();
1060  }
1061 
1062 }
List< CorrelationAttribute.Type > getSupportedCorrelationTypes()
void deleteOrganization(EamOrganization organizationToDelete)
Long getCountUniqueCaseDataSourceTuplesHavingTypeValue(CorrelationAttribute.Type aType, String value)
void updateOrganization(EamOrganization updatedOrganization)
void updateCorrelationType(CorrelationAttribute.Type aType)
CorrelationCase newCase(CorrelationCase eamCase)
CorrelationAttribute.Type getCorrelationTypeById(int typeId)
Long getCountArtifactInstancesByCaseDataSource(String caseUUID, String dataSourceID)
Long getCountArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
List< CorrelationAttributeInstance > getArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
void setArtifactInstanceKnownStatus(CorrelationAttribute eamArtifact, TskData.FileKnown knownStatus)
void addReferenceInstance(EamGlobalFileInstance eamGlobalFileInstance, CorrelationAttribute.Type correlationType)
List< CorrelationAttributeInstance > getArtifactInstancesByTypeValue(CorrelationAttribute.Type aType, String value)
List< EamGlobalFileInstance > getReferenceInstancesByTypeValue(CorrelationAttribute.Type aType, String aValue)
CorrelationDataSource getDataSource(CorrelationCase correlationCase, String dataSourceDeviceId)
boolean isValueInReferenceSet(String value, int referenceSetID, int correlationTypeID)
void addArtifact(CorrelationAttribute eamArtifact)
void newDataSource(CorrelationDataSource eamDataSource)
List< CorrelationAttribute.Type > getEnabledCorrelationTypes()
boolean referenceSetExists(String referenceSetName, String version)
void bulkInsertReferenceTypeEntries(Set< EamGlobalFileInstance > globalInstances, CorrelationAttribute.Type contentType)
List< String > getListCasesHavingArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
boolean isArtifactKnownBadByReference(CorrelationAttribute.Type aType, String value)
Long getCountArtifactInstancesKnownBad(CorrelationAttribute.Type aType, String value)
List< EamGlobalSet > getAllReferenceSets(CorrelationAttribute.Type correlationType)
List< CorrelationAttributeInstance > getArtifactInstancesByPath(CorrelationAttribute.Type aType, String filePath)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
List< CorrelationAttribute.Type > getDefinedCorrelationTypes()
int newCorrelationType(CorrelationAttribute.Type newType)

Copyright © 2012-2016 Basis Technology. Generated on: Tue Feb 20 2018
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.