diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java index c803f2913a8853c2d988dcfe42cc1298860bfc4e..434cd8bd38f4cc71d48fe04a6bce229dd6e9c2a4 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccount.java @@ -378,6 +378,16 @@ public boolean setCreationTime(Long creationTime) { } + /** + * Get the dirty flag. Indicates whether the account has any changes that need + * to be updated in the database. + * + * @return True if the object is dirty, false otherwise. + */ + boolean isDirty() { + return isDirty; + } + /** * Reset the dirty flag. Indicates that the account has been updated in the * database. @@ -399,26 +409,7 @@ public void addAttributes(Set<OsAccountAttribute> osAccountAttributes) throws Ts osAccountAttributes.addAll(osAccountAttributes); } - - /** - * Updates the account in the database. This must be called after calling - * any of the 'set' methods. - * - * @return OsAccount Updated account. - * - * @throws TskCoreException If there is an error in updating the account. - */ - public OsAccount update() throws TskCoreException { - - if (this.isDirty) { - OsAccount updatedAccount = sleuthkitCase.getOsAccountManager().updateAccount(this); - updatedAccount.resetDirty(); - return updatedAccount; - } else { - return this; - } - } - + /** * Get the account Object Id that is unique within the scope of the case. * diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java index 2788d080f299bb43dc7b2e014835c337e442f7b2..a523fdced8f3d902b42d212a0d9f865765632ea2 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccountManager.java @@ -642,6 +642,11 @@ void addOsAccountAttributes(OsAccount account, Set<OsAccountAttribute> accountAt */ OsAccount updateAccount(OsAccount osAccount) throws TskCoreException { + // do nothing if the account is not dirty. + if (!osAccount.isDirty()) { + return osAccount; + } + db.acquireSingleUserCaseWriteLock(); try(CaseDbConnection connection = db.getConnection()) { String updateSQL = "UPDATE tsk_os_accounts SET " @@ -673,6 +678,8 @@ OsAccount updateAccount(OsAccount osAccount) throws TskCoreException { preparedStatement.setLong(9, osAccount.getId()); connection.executeUpdate(preparedStatement); + + osAccount.resetDirty(); return osAccount; } catch (SQLException ex) { diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealm.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealm.java index f34b0a019b15b0ea23bf9eaf0f0f2ede2581663f..bb289747796fb96fa894737aa1cc6456ff85abd3 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealm.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealm.java @@ -18,7 +18,6 @@ */ package org.sleuthkit.datamodel; -import com.google.common.base.Strings; import java.util.Objects; import java.util.Optional; @@ -43,17 +42,20 @@ public final class OsAccountRealm { private final ScopeConfidence scopeConfidence; // confidence in realm scope. private boolean isDirty = false; // indicates that some member value has changed since construction and it should be updated in the database. + + /** * Creates OsAccountRealm. * - * @param id Row Id. - * @param realmName Realm name, may be null. - * @param realmAddr Unique numeric address for realm, may be null only if realm name is not null. - * @param signature Either the address or the name. - * @param host Host if the realm is host scoped. - * @param scopeConfidence Scope confidence. + * @param id Row Id. + * @param realmName Realm name, may be null. + * @param realmAddr Unique numeric address for realm, may be null only + * if realm name is not null. + * @param signature Either the address or the name. + * @param host Host if the realm is host scoped. + * @param scopeConfidence Scope confidence. */ OsAccountRealm(long id, String realmName, String realmAddr, String signature, Host host, ScopeConfidence scopeConfidence) { this.id = id; @@ -265,14 +267,33 @@ public boolean setRealmAddr(String addr) { return false; } - + + /** + * Get the dirty flag. Indicates whether the realm has any changes that need + * to be updated in the database. + * + * @return True if the object is dirty, false otherwise. + */ + boolean isDirty() { + return isDirty; + } + /** - * Updates the realm signature. + * Reset the dirty flag. Indicates that the realm has been updated in the + * database. + */ + void resetDirty() { + this.isDirty = false; + } + + /** + * Updates the signature with realm address or realm name. */ private void updateSignature() { signature = OsAccountRealmManager.makeRealmSignature(realmAddr, realmName, host); } + // /** // * Set the realm scope host if it is not already set. // * diff --git a/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealmManager.java b/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealmManager.java index 73b310fefc412acf7a1bcf7a860234d453378cc4..f9499bb41f313e473483f3706ae6bce97af22d03 100644 --- a/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealmManager.java +++ b/bindings/java/src/org/sleuthkit/datamodel/OsAccountRealmManager.java @@ -183,32 +183,40 @@ Optional<OsAccountRealm> getWindowsRealm(String accountSid, String realmName, Ho } /** - * Updates the realm name and name type for the the specified realm id. + * Updates the specified realm in the database. * - * @param realmId Row id of realm to update. - * @param realmName Realm name. - * @param nameType Name type. + * @param realm Realm to update. + * + * @return OsAccountRealm Updated realm. * - * @return OsAccountRealm * @throws TskCoreException */ - OsAccountRealm updateRealmName(long realmId, String realmName, OsAccountRealm.ScopeConfidence nameType) throws TskCoreException { + OsAccountRealm updateRealm(OsAccountRealm realm) throws TskCoreException { + + // do nothing if the realm is not dirty. + if (!realm.isDirty()) { + return realm; + } db.acquireSingleUserCaseWriteLock(); try (CaseDbConnection connection = db.getConnection()) { - String updateSQL = "UPDATE tsk_os_account_realms SET realm_name = ?, scope_confidence = ? WHERE id = ?"; + // We only alow realm addr, name and signature to be updated at this time. + String updateSQL = "UPDATE tsk_os_account_realms SET realm_name = ?, realm_addr = ?, realm_signature = ? WHERE id = ?"; PreparedStatement preparedStatement = connection.getPreparedStatement(updateSQL, Statement.NO_GENERATED_KEYS); preparedStatement.clearParameters(); - preparedStatement.setString(1, realmName); - preparedStatement.setInt(2, nameType.getId()); - preparedStatement.setLong(3, realmId); + preparedStatement.setString(1, realm.getRealmName().orElse(null)); + preparedStatement.setString(2, realm.getRealmAddr().orElse(null)); + preparedStatement.setString(3, realm.getSignature()); + + preparedStatement.setLong(4, realm.getId()); connection.executeUpdate(preparedStatement); - - return getRealm(realmId, connection ); + + realm.resetDirty(); + return realm; } catch (SQLException ex) { - throw new TskCoreException(String.format("Error updating realm with name = %s, id = %d", realmName, realmId), ex); + throw new TskCoreException(String.format("Error updating realm with id = %d, name = %s, addr = %s", realm.getId(), realm.getRealmName().orElse("Null"), realm.getRealmAddr().orElse("Null") ), ex); } finally { db.releaseSingleUserCaseWriteLock(); } diff --git a/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java b/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java index ba80566e42a5daa790023f24556430121f57fb03..a27f34feef6925ed3a51bc9e2164690a6da1d0f4 100644 --- a/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java +++ b/bindings/java/test/org/sleuthkit/datamodel/OsAccountTest.java @@ -170,7 +170,8 @@ public void osAccountRealmTests() throws TskCoreException { assertEquals(localRealm2.getScopeHost().orElse(null).getName().equalsIgnoreCase(hostName2), true); // update the a realm name on a existing realm. - OsAccountRealm updatedRealm2 = caseDB.getOsAccountRealmManager().updateRealmName(localRealm2.getId(), realmName2, OsAccountRealm.ScopeConfidence.KNOWN); + localRealm2.setRealmName(realmName2); + OsAccountRealm updatedRealm2 = caseDB.getOsAccountRealmManager().updateRealm(localRealm2); assertEquals(updatedRealm2.getRealmAddr().orElse("").equalsIgnoreCase(realmAddr2), true ); assertEquals(updatedRealm2.getRealmName().orElse("").equalsIgnoreCase(realmName2), true ); @@ -238,7 +239,7 @@ public void basicOsAccountTests() throws TskCoreException { osAccount1.setIsAdmin(true); - osAccount1 = osAccount1.update(); + osAccount1 = caseDB.getOsAccountManager().updateAccount(osAccount1); assertEquals(osAccount1.getCreationTime().orElse(null), creationTime1); assertEquals(osAccount1.getFullName().orElse(null).equalsIgnoreCase(fullName1), true );