From 61f2112bccef2cef1b430ff6b11a148dce17bf93 Mon Sep 17 00:00:00 2001 From: "U-BASIS\\dgrove" Date: Wed, 9 Jan 2019 00:14:53 -0500 Subject: [PATCH] Persist CR data on hash setters. --- .../datamodel/AbstractSqlEamDb.java | 69 ++++++++++++++++++- .../datamodel/CorrelationDataSource.java | 42 +++++++---- .../centralrepository/datamodel/EamDb.java | 21 ++++++ .../datamodel/SqliteEamDb.java | 45 ++++++++++++ .../eventlisteners/IngestEventsListener.java | 1 - 5 files changed, 161 insertions(+), 17 deletions(-) diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java index 1373431099..fc1ac13e3c 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/AbstractSqlEamDb.java @@ -826,7 +826,7 @@ abstract class AbstractSqlEamDb implements EamDb { } /** - * Updates an existing data source in the database + * Updates an existing data source in the database. * * @param eamDataSource The data source to update */ @@ -862,6 +862,73 @@ abstract class AbstractSqlEamDb implements EamDb { EamDbUtil.closeConnection(conn); } } + + /** + * Updates the MD5 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws EamDbException { + updateDataSourceStringValue(eamDataSource, "md5", eamDataSource.getMd5()); + } + + /** + * Updates the SHA-1 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws EamDbException { + updateDataSourceStringValue(eamDataSource, "sha1", eamDataSource.getSha1()); + } + + /** + * Updates the SHA-256 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws EamDbException { + updateDataSourceStringValue(eamDataSource, "sha256", eamDataSource.getSha256()); + } + + /** + * Updates the specified value in an existing data source in the database. + * + * @param eamDataSource The data source to update + * @param column The name of the column to be updated + * @param value The value to assign to the specified column + */ + private void updateDataSourceStringValue(CorrelationDataSource eamDataSource, String column, String value) throws EamDbException { + if (eamDataSource == null) { + throw new EamDbException("Correlation data source is null"); + } + + Connection conn = connect(); + + PreparedStatement preparedStatement = null; + String sql = "UPDATE data_sources " + + "SET " + column + "=? " + + "WHERE id=?"; + + try { + preparedStatement = conn.prepareStatement(sql); + + preparedStatement.setString(1, value); + preparedStatement.setInt(2, eamDataSource.getID()); + + preparedStatement.executeUpdate(); + //update the case in the cache + dataSourceCacheByDsObjectId.put(getDataSourceByDSObjectIdCacheKey(eamDataSource.getCaseID(), eamDataSource.getDataSourceObjectID()), eamDataSource); + dataSourceCacheById.put(getDataSourceByIdCacheKey(eamDataSource.getCaseID(), eamDataSource.getID()), eamDataSource); + } catch (SQLException ex) { + throw new EamDbException("Error updating data source.", ex); // NON-NLS + } finally { + EamDbUtil.closeStatement(preparedStatement); + EamDbUtil.closeConnection(conn); + } + } /** * Inserts new Artifact(s) into the database. Should add associated Case and diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CorrelationDataSource.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CorrelationDataSource.java index c634f10639..3548d70192 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CorrelationDataSource.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/CorrelationDataSource.java @@ -19,6 +19,7 @@ package org.sleuthkit.autopsy.centralrepository.datamodel; import java.io.Serializable; +import org.openide.util.Exceptions; import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException; import org.sleuthkit.datamodel.Content; @@ -229,15 +230,20 @@ public class CorrelationDataSource implements Serializable { } /** - * Set the MD5 hash value. - * - * Note: This does not add the hash value to Central Repository. A method, - * such as EamDb.updateDataSource(), can be called to persist hash values. + * Set the MD5 hash value and persist to the Central Repository if available. * * @param md5Hash The MD5 hash value. + * + * @exception EamDbException If there's an issue updating the Central + * Repository. */ - public void setMd5(String md5Hash) { + public void setMd5(String md5Hash) throws EamDbException { this.md5Hash = md5Hash; + + boolean useCR = EamDb.isEnabled(); + if (useCR) { + EamDb.getInstance().updateDataSourceMd5Hash(this); + } } /** @@ -248,15 +254,18 @@ public class CorrelationDataSource implements Serializable { } /** - * Set the SHA-1 hash value. - * - * Note: This does not add the hash value to Central Repository. A method, - * such as EamDb.updateDataSource(), can be called to persist hash values. + * Set the SHA-1 hash value and persist to the Central Repository if + * available. * * @param sha1Hash The SHA-1 hash value. */ - public void setSha1(String sha1Hash) { + public void setSha1(String sha1Hash) throws EamDbException { this.sha1Hash = sha1Hash; + + boolean useCR = EamDb.isEnabled(); + if (useCR) { + EamDb.getInstance().updateDataSourceSha1Hash(this); + } } /** @@ -267,14 +276,17 @@ public class CorrelationDataSource implements Serializable { } /** - * Set the SHA-256 hash value. - * - * Note: This does not add the hash value to Central Repository. A method, - * such as EamDb.updateDataSource(), can be called to persist hash values. + * Set the SHA-256 hash value and persist to the Central Repository if + * available. * * @param sha256Hash The SHA-256 hash value. */ - public void setSha256(String sha256Hash) { + public void setSha256(String sha256Hash) throws EamDbException { this.sha256Hash = sha256Hash; + + boolean useCR = EamDb.isEnabled(); + if (useCR) { + EamDb.getInstance().updateDataSourceSha256Hash(this); + } } } diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/EamDb.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/EamDb.java index a9e7685d14..89b54022cf 100755 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/EamDb.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/EamDb.java @@ -211,6 +211,27 @@ public interface EamDb { * @param eamDataSource The data source to update */ void updateDataSource(CorrelationDataSource eamDataSource) throws EamDbException; + + /** + * Updates the MD5 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws EamDbException; + + /** + * Updates the SHA-1 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws EamDbException; + + /** + * Updates the SHA-256 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws EamDbException; /** * Retrieves Data Source details based on data source device ID diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/SqliteEamDb.java b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/SqliteEamDb.java index f82a3bdc26..d2ad38b5a0 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/SqliteEamDb.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/datamodel/SqliteEamDb.java @@ -448,6 +448,51 @@ final class SqliteEamDb extends AbstractSqlEamDb { releaseExclusiveLock(); } } + + /** + * Updates the MD5 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceMd5Hash(CorrelationDataSource eamDataSource) throws EamDbException { + try { + acquireExclusiveLock(); + super.updateDataSourceMd5Hash(eamDataSource); + } finally { + releaseExclusiveLock(); + } + } + + /** + * Updates the SHA-1 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceSha1Hash(CorrelationDataSource eamDataSource) throws EamDbException { + try { + acquireExclusiveLock(); + super.updateDataSourceSha1Hash(eamDataSource); + } finally { + releaseExclusiveLock(); + } + } + + /** + * Updates the SHA-256 hash value in an existing data source in the database. + * + * @param eamDataSource The data source to update + */ + @Override + public void updateDataSourceSha256Hash(CorrelationDataSource eamDataSource) throws EamDbException { + try { + acquireExclusiveLock(); + super.updateDataSourceSha256Hash(eamDataSource); + } finally { + releaseExclusiveLock(); + } + } /** * Inserts new Artifact(s) into the database. Should add associated Case and diff --git a/Core/src/org/sleuthkit/autopsy/centralrepository/eventlisteners/IngestEventsListener.java b/Core/src/org/sleuthkit/autopsy/centralrepository/eventlisteners/IngestEventsListener.java index a7da840f95..3aba207cd9 100644 --- a/Core/src/org/sleuthkit/autopsy/centralrepository/eventlisteners/IngestEventsListener.java +++ b/Core/src/org/sleuthkit/autopsy/centralrepository/eventlisteners/IngestEventsListener.java @@ -401,7 +401,6 @@ public class IngestEventsListener { correlationDataSource.setMd5(imageMd5Hash); correlationDataSource.setSha1(imageSha1Hash); correlationDataSource.setSha256(imageSha256Hash); - dbManager.updateDataSource(correlationDataSource); } } }