1
0
mirror of https://github.com/elisspace/autopsy.git synced 2026-09-01 08:53:51 +00:00

updated to enforce invariants

This commit is contained in:
Greg DiCristofaro
2020-03-12 16:17:26 -04:00
parent 487d680d41
commit 6ee3aeeecd
3 changed files with 71 additions and 24 deletions

View File

@@ -25,6 +25,7 @@ import java.sql.SQLException;
import java.util.logging.Level;
import org.openide.util.NbBundle;
import org.sleuthkit.autopsy.coordinationservice.CoordinationService;
import org.sleuthkit.autopsy.core.UserPreferences;
import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.autopsy.coreutils.ModuleSettings;
@@ -47,11 +48,30 @@ public class CentralRepoDbManager {
private static final Object dbChoiceLock = new Object();
private static final Object disabledDueToFailureLock = new Object();
/**
* Save the selected platform to the config file.
* This saves the currently selected database choice and clears any disabledDueToFailure flag.
* @param choice the choice to save.
* @return the newly saved choice
*/
public static CentralRepoDbChoice saveDbChoice(CentralRepoDbChoice choice) {
return saveDbChoice(choice, true);
}
/**
* This saves the currently selected database choice.
* @param choice the choice to save.
* @param clearDisabledDueToError whether or not to clear the 'disabledDueToFailure' settings key.
* @return the newly saved choice
*/
public static CentralRepoDbChoice saveDbChoice(CentralRepoDbChoice choice, boolean clearDisabledDueToError) {
synchronized(dbChoiceLock) {
// clear disabling due to a failure
if (clearDisabledDueToError)
setDisabledDueToFailure(false);
// change the settings
CentralRepoDbChoice newChoice = (choice == null) ? CentralRepoDbChoice.DISABLED : choice;
CentralRepoDbChoice oldChoice = savedChoice;
savedChoice = newChoice;
@@ -61,6 +81,22 @@ public class CentralRepoDbManager {
}
}
/**
* This method indicates whether or not 'PostgreSQL using multi-user settings' is a valid option.
* @return true if 'PostgreSQL using multi-user settings' is valid.
*/
public static boolean isPostgresMultiuserAllowed() {
// if multi user mode is not enabled, then this cannot be used
if (!UserPreferences.getIsMultiUserModeEnabled())
return false;
// also validate the connection as well
PostgresCentralRepoSettings multiUserSettings =
new PostgresCentralRepoSettings(PostgresSettingsLoader.MULTIUSER_SETTINGS_LOADER);
return multiUserSettings.testStatus() == DatabaseTestResult.TESTED_OK;
}
/**
@@ -77,13 +113,23 @@ public class CentralRepoDbManager {
}
}
/**
* disable the central repository and indicate through a flag that this was due to a failure during database setup.
* this is used when re-enabling multi-user as a flag to determine whether or not CR should be re-enabled.
* NOTE: cr is disabled while persisting database choice.
*/
public static void disableDueToFailure() {
CentralRepoDbUtil.setUseCentralRepo(false);
setDisabledDueToFailure(true);
}
/**
* set whether or not the repository has been disabled due to a database setup issue;
* this is used when re-enabling multi-user as a flag to determine whether or not CR should be re-enabled
*
* @param disabledDueToFailure whether or not the repository has been disabled due to a database setup issue
*/
public static void setDisabledDueToFailure(boolean disabledDueToFailure) {
private static void setDisabledDueToFailure(boolean disabledDueToFailure) {
synchronized(disabledDueToFailureLock) {
boolean oldValue = isDisabledDueToFailure();
ModuleSettings.setConfigSetting(CENTRAL_REPOSITORY_SETTINGS_KEY, DISABLED_DUE_TO_FAILURE_KEY, Boolean.toString(disabledDueToFailure));
@@ -243,7 +289,7 @@ public class CentralRepoDbManager {
} catch (CentralRepoException ex2) {
logger.log(Level.SEVERE, "Error shutting down central repo connection pool", ex2);
}
saveDbChoice(CentralRepoDbChoice.DISABLED);
saveDbChoice(CentralRepoDbChoice.DISABLED, false);
if (innerException == null) {
throw new CentralRepoException(message, desc);
} else {

View File

@@ -62,16 +62,11 @@ public class EamDbSettingsDialog extends JDialog {
private static final Logger logger = Logger.getLogger(EamDbSettingsDialog.class.getName());
private static final long serialVersionUID = 1L;
private static final DbChoiceRenderer DB_CHOICE_RENDERER = new DbChoiceRenderer();
private static boolean isDbChoiceSelectable(CentralRepoDbChoice item) {
return (item != CentralRepoDbChoice.POSTGRESQL_MULTIUSER || UserPreferences.getIsMultiUserModeEnabled());
}
/**
* handles displaying and rendering drop down menu for database choices in central repo
*/
private static class DbChoiceRenderer extends BasicComboBoxRenderer {
private class DbChoiceRenderer extends BasicComboBoxRenderer {
private static final long serialVersionUID = 1L;
public Component getListCellRendererComponent(JList list, Object value,
@@ -91,11 +86,18 @@ public class EamDbSettingsDialog extends JDialog {
private final Collection<JTextField> textBoxes;
private final TextBoxChangedListener textBoxChangedListener;
private final CentralRepoDbManager manager = new CentralRepoDbManager();
private final boolean isMultiUserSelectable = CentralRepoDbManager.isPostgresMultiuserAllowed();
private final DbChoiceRenderer DB_CHOICE_RENDERER = new DbChoiceRenderer();
public EamDbSettingsDialog() {
this(null);
}
private boolean isDbChoiceSelectable(CentralRepoDbChoice item) {
return (item != CentralRepoDbChoice.POSTGRESQL_MULTIUSER || isMultiUserSelectable);
}
/**
* Creates new form EamDbSettingsDialog
*/

View File

@@ -177,7 +177,7 @@ public final class GlobalSettingsPanel extends IngestModuleGlobalSettingsPanel i
private static void askForCentralRepoDbChoice(Component parent) {
// disable central repository until user makes choice
CentralRepoDbUtil.setUseCentralRepo(false);
CentralRepoDbManager.saveDbChoice(CentralRepoDbChoice.DISABLED);
CentralRepoDbManager.saveDbChoice(CentralRepoDbChoice.DISABLED, false);
Object[] options = {
"Use SQLite",
@@ -214,14 +214,11 @@ public final class GlobalSettingsPanel extends IngestModuleGlobalSettingsPanel i
SwingUtilities.invokeLater(() -> {
boolean successful = EamDbSettingsDialog.testStatusAndCreate(parent, new CentralRepoDbManager());
if (successful) {
updateDatabase(parent);
// clear any error if there was one
CentralRepoDbManager.setDisabledDueToFailure(false);
updateDatabase(parent);
}
else {
CentralRepoDbUtil.setUseCentralRepo(false);
// disable central repository due to error
CentralRepoDbManager.setDisabledDueToFailure(true);
CentralRepoDbManager.disableDueToFailure();
}
});
}
@@ -593,6 +590,16 @@ public final class GlobalSettingsPanel extends IngestModuleGlobalSettingsPanel i
private void cbUseCentralRepoActionPerformed(java.awt.event.ActionEvent evt) {//GEN-FIRST:event_cbUseCentralRepoActionPerformed
//if saved setting is disabled checkbox should be disabled already
store();
// if moving to using CR, multi-user mode is disabled and selection is multiuser settings, set to disabled
if (cbUseCentralRepo.isSelected() &&
!CentralRepoDbManager.isPostgresMultiuserAllowed() &&
CentralRepoDbManager.getSavedDbChoice() == CentralRepoDbChoice.POSTGRESQL_MULTIUSER) {
CentralRepoDbManager.saveDbChoice(CentralRepoDbChoice.DISABLED);
}
updateDatabase();
load();
this.ingestStateUpdated(Case.isCaseOpen());
@@ -638,14 +645,6 @@ public final class GlobalSettingsPanel extends IngestModuleGlobalSettingsPanel i
@Override
public void store() { // Click OK or Apply on Options Panel
CentralRepoDbUtil.setUseCentralRepo(cbUseCentralRepo.isSelected());
// if moving to using CR, multi-user mode is disabled and selection is multiuser settings, set to disabled
if (cbUseCentralRepo.isSelected() &&
!UserPreferences.getIsMultiUserModeEnabled() &&
CentralRepoDbManager.getSavedDbChoice() == CentralRepoDbChoice.POSTGRESQL_MULTIUSER) {
CentralRepoDbManager.saveDbChoice(CentralRepoDbChoice.DISABLED);
}
}
/**