Tidy up IngestJobSettings

This commit is contained in:
Richard Cordovano 2018-02-27 17:42:17 -05:00
parent 29c5ed1f0f
commit 4d594948e8

View File

@ -1,7 +1,7 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2011-2017 Basis Technology Corp.
* Copyright 2014-2018 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
@ -26,12 +26,14 @@ import java.nio.file.Files;
import java.nio.file.Path;
import java.nio.file.Paths;
import java.util.ArrayList;
import java.util.Collection;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
import java.util.Objects;
import java.util.logging.Level;
import java.util.stream.Collectors;
import org.openide.util.NbBundle;
import org.openide.util.io.NbObjectInputStream;
import org.openide.util.io.NbObjectOutputStream;
@ -43,113 +45,93 @@ import org.sleuthkit.autopsy.modules.interestingitems.FilesSet;
import org.sleuthkit.autopsy.modules.interestingitems.FilesSetsManager;
/**
* Encapsulates the ingest job settings for a particular execution context.
* Examples of execution contexts include the add data source wizard and the run
* ingest modules dialog. Different execution contexts may have different ingest
* job settings.
* The settings for an ingest job.
*/
public class IngestJobSettings {
private static final String ENABLED_MODULES_KEY = "Enabled_Ingest_Modules"; //NON-NLS
private static final String DISABLED_MODULES_KEY = "Disabled_Ingest_Modules"; //NON-NLS
private static final String LAST_FILE_INGEST_FILTER_KEY = "Last_File_Ingest_Filter"; //NON-NLS
private static final String ENABLED_MODULES_PROPERTY = "Enabled_Ingest_Modules"; //NON-NLS
private static final String DISABLED_MODULES_PROPERTY = "Disabled_Ingest_Modules"; //NON-NLS
private static final String LAST_FILE_INGEST_FILTER_PROPERTY = "Last_File_Ingest_Filter"; //NON-NLS
private static final String MODULE_SETTINGS_FOLDER = "IngestModuleSettings"; //NON-NLS
private static final String MODULE_SETTINGS_FOLDER_PATH = Paths.get(PlatformUtil.getUserConfigDirectory(), IngestJobSettings.MODULE_SETTINGS_FOLDER).toAbsolutePath().toString();
private static final String MODULE_SETTINGS_FILE_EXT = ".settings"; //NON-NLS
private static final Logger LOGGER = Logger.getLogger(IngestJobSettings.class.getName());
private FilesSet fileIngestFilter;
private String executionContext;
private static final CharSequence PYTHON_CLASS_PROXY_PREFIX = "org.python.proxies.".subSequence(0, "org.python.proxies.".length() - 1); //NON-NLS
private static final Logger logger = Logger.getLogger(IngestJobSettings.class.getName());
private final IngestType ingestType;
private String moduleSettingsFolderPath;
private static final CharSequence pythonModuleSettingsPrefixCS = "org.python.proxies.".subSequence(0, "org.python.proxies.".length() - 1); //NON-NLS
private final List<IngestModuleTemplate> moduleTemplates;
private final List<String> warnings;
private String executionContext;
private FilesSet fileIngestFilter;
private String moduleSettingsFolderPath;
/**
* Gets the last selected FileIngestFilter saved in settings which is
* represented by a FilesSet, if the last selected filter is null the
* default filter will be returned.
* Gets the path to the module settings folder for a given execution
* context.
*
* @return FilesSet which represents the FileIngestFilter
* Some examples of execution contexts include the Add Data Source wizard,
* the Run Ingest Modules dialog, and auto ingest. Different execution
* contexts may have different ingest job settings.
*
* @return The execution context identifier.
*
* @return The path to the module settings folder
*/
FilesSet getFileIngestFilter() {
if (fileIngestFilter == null) {
fileIngestFilter = FilesSetsManager.getDefaultFilter();
}
return fileIngestFilter;
static Path getSavedModuleSettingsFolder(String executionContext) {
return Paths.get(IngestJobSettings.MODULE_SETTINGS_FOLDER_PATH, executionContext);
}
/**
* Sets the FileIngestFilter which is currently being used by ingest.
* Loads the ingest job settings for a given execution context. If settings
* for the context have not been previously saved, default settings are
* used.
*
* @param fileIngestFilter the FilesSet which represents the
* FileIngestFilter
* Some examples of execution contexts include the Add Data Source wizard,
* the Run Ingest Modules dialog, and auto ingest. Different execution
* contexts may have different ingest job settings.
*
* @param executionContext The execution context identifier.
*/
void setFileIngestFilter(FilesSet fileIngestFilter) {
this.fileIngestFilter = fileIngestFilter;
public IngestJobSettings(final String executionContext) {
this(executionContext, IngestType.ALL_MODULES);
}
/**
* The type of ingest modules to run.
*/
public enum IngestType {
/**
* Run both data source level and file-level ingest modules.
*/
ALL_MODULES,
/**
* Run only data source level ingest modules.
*/
DATA_SOURCE_ONLY,
/**
* Run only file level ingest modules.
*/
FILES_ONLY
}
/**
* Constructs an ingest job settings object for a given execution context.
* Examples of execution contexts include the add data source wizard and the
* run ingest modules dialog. Different execution contexts may have
* different ingest job settings.
* Loads the ingest job settings for a given execution context. If settings
* for the context have not been previously saved, default settings are
* used.
*
* @param executionContext The ingest execution context identifier.
*/
public IngestJobSettings(String executionContext) {
this.executionContext = executionContext;
this.ingestType = IngestType.ALL_MODULES;
this.moduleTemplates = new ArrayList<>();
this.warnings = new ArrayList<>();
this.createSavedModuleSettingsFolder();
this.load();
}
/**
* Constructs an ingest job settings object for a given context. Examples of
* execution contexts include the add data source wizard and the run ingest
* modules dialog. Different execution contexts may have different ingest
* job settings.
* Some examples of execution contexts include the Add Data Source wizard,
* the Run Ingest Modules dialog, and auto ingest. Different execution
* contexts may have different ingest job settings.
*
* @param context The context identifier string.
* @param ingestType The type of modules ingest is running.
* @param executionContext The execution context identifier.
* @param ingestType Whether to run all ingest modules, data source
* level ingest modules only, or file level ingest
* modules only.
*/
public IngestJobSettings(String context, IngestType ingestType) {
public IngestJobSettings(String executionContext, IngestType ingestType) {
this.ingestType = ingestType;
if (this.ingestType.equals(IngestType.ALL_MODULES)) {
this.executionContext = context;
} else {
this.executionContext = context + "." + this.ingestType.name();
}
this.moduleTemplates = new ArrayList<>();
this.warnings = new ArrayList<>();
if (this.ingestType.equals(IngestType.ALL_MODULES)) {
this.executionContext = executionContext;
} else {
this.executionContext = executionContext + "." + this.ingestType.name();
}
this.createSavedModuleSettingsFolder();
this.load();
}
/**
* Gets the path to the module settings folder for these ingest job
* settings.
*
* @return The path to the ingest module settings folder.
*/
public Path getSavedModuleSettingsFolder() {
return Paths.get(IngestJobSettings.MODULE_SETTINGS_FOLDER_PATH, executionContext);
}
/**
* Saves these ingest job settings.
*/
@ -158,11 +140,13 @@ public class IngestJobSettings {
}
/**
* Saves the settings with a new context name removing the old profile
* folder
* Saves these ingest job settings for use in a different execution context.
*
* @param executionContext will be used to name the new folder for storing
* the settings
* Some examples of execution contexts include the Add Data Source wizard,
* the Run Ingest Modules dialog, and auto ingest. Different execution
* contexts may have different ingest job settings.
*
* @param executionContext The new execution context.
*/
void saveAs(String executionContext) {
this.executionContext = executionContext;
@ -171,20 +155,8 @@ public class IngestJobSettings {
}
/**
* Gets the ingest execution context identifier. Examples of execution
* contexts include the add data source wizard and the run ingest modules
* dialog. Different execution contexts may have different ingest job
* settings.
*
* @return The execution context identifier.
*/
String getExecutionContext() {
return this.executionContext;
}
/**
* Gets and clears any accumulated warnings associated with these ingest job
* settings.
* Gets and clears any accumulated warnings associated with the loading or
* saving of these ingest job settings.
*
* @return A list of warning messages, possibly empty.
*/
@ -195,7 +167,41 @@ public class IngestJobSettings {
}
/**
* Gets the ingest module templates part of these ingest job settings.
* Gets the execution context identifier.
*
* Some examples of execution contexts include the Add Data Source wizard,
* the Run Ingest Modules dialog, and auto ingest. Different execution
* contexts may have different ingest job settings.
*
* @return The execution context identifier.
*/
String getExecutionContext() {
return this.executionContext;
}
/**
* Gets the file ingest filter for the ingest job.
*
* @return FilesSet The filter as a files set.
*/
FilesSet getFileIngestFilter() {
if (fileIngestFilter == null) {
fileIngestFilter = FilesSetsManager.getDefaultFilter();
}
return fileIngestFilter;
}
/**
* Sets the file ingest filter for the ingest job.
*
* @param fileIngestFilter The filter as a files set.
*/
void setFileIngestFilter(FilesSet fileIngestFilter) {
this.fileIngestFilter = fileIngestFilter;
}
/**
* Gets the enabled ingest module templates for the ingest job.
*
* @return The list of ingest module templates.
*/
@ -204,8 +210,17 @@ public class IngestJobSettings {
}
/**
* Gets the enabled ingest module templates part of these ingest job
* settings.
* Sets the enabled ingest module templates for the ingest job.
*
* @param moduleTemplates The ingest module templates.
*/
void setIngestModuleTemplates(List<IngestModuleTemplate> moduleTemplates) {
this.moduleTemplates.clear();
this.moduleTemplates.addAll(moduleTemplates);
}
/**
* Gets the enabled ingest module templates for this ingest job.
*
* @return The list of enabled ingest module templates.
*/
@ -220,29 +235,12 @@ public class IngestJobSettings {
}
/**
* Sets the ingest module templates part of these ingest job settings.
*
* @param moduleTemplates The ingest module templates.
*/
void setIngestModuleTemplates(List<IngestModuleTemplate> moduleTemplates) {
this.moduleTemplates.clear();
this.moduleTemplates.addAll(moduleTemplates);
}
/**
* Gets the process unallocated space flag part of these ingest job
* settings.
* Gets the process unallocated space flag for this ingest job.
*
* @return True or false.
*
*/
boolean getProcessUnallocatedSpace() {
/*
* Used to be a simple flag but the processUnallocated checkbox was
* changed to a skip unallocated. This was due to the FileIngestFilters
* needing a default value which did not skip unallocated files. This
* method exists to maintain existing functionality.
*/
boolean processUnallocated = true;
if (!Objects.isNull(this.fileIngestFilter)) {
processUnallocated = (this.fileIngestFilter.ingoresUnallocatedSpace() == false);
@ -251,29 +249,7 @@ public class IngestJobSettings {
}
/**
* Returns the path to the ingest module settings folder.
*
* @return path to the module settings folder
*/
public Path getSavedModuleSettingsFolder() {
return Paths.get(IngestJobSettings.MODULE_SETTINGS_FOLDER_PATH, executionContext);
}
/**
* Returns the path to the ingest module settings folder from a static
* manner.
*
* @param context specify the context of the folder you wish to get
*
* @return path to the module settings folder
*/
static Path getSavedModuleSettingsFolder(String context) {
return Paths.get(IngestJobSettings.MODULE_SETTINGS_FOLDER_PATH, context);
}
/**
* Creates the folder for saving the individual ingest module settings part
* of these ingest job settings.
* Creates the module folder for these ingest job settings.
*/
private void createSavedModuleSettingsFolder() {
try {
@ -281,13 +257,14 @@ public class IngestJobSettings {
Files.createDirectories(folder);
this.moduleSettingsFolderPath = folder.toAbsolutePath().toString();
} catch (IOException | SecurityException ex) {
LOGGER.log(Level.SEVERE, "Failed to create ingest module settings directory " + this.moduleSettingsFolderPath, ex); //NON-NLS
logger.log(Level.SEVERE, "Failed to create ingest module settings directory " + this.moduleSettingsFolderPath, ex); //NON-NLS
this.warnings.add(NbBundle.getMessage(IngestJobSettings.class, "IngestJobSettings.createModuleSettingsFolder.warning")); //NON-NLS
}
}
/**
* Loads the saved or default ingest job settings context into memory.
* Loads the saved or default ingest job settings for the execution context
* into memory.
*/
private void load() {
/**
@ -317,8 +294,8 @@ public class IngestJobSettings {
* Get the enabled/disabled ingest modules settings for this context. By
* default, all loaded modules are enabled.
*/
HashSet<String> enabledModuleNames = getModulesNamesFromSetting(executionContext, IngestJobSettings.ENABLED_MODULES_KEY, makeCommaSeparatedValuesList(loadedModuleNames));
HashSet<String> disabledModuleNames = getModulesNamesFromSetting(executionContext, IngestJobSettings.DISABLED_MODULES_KEY, ""); //NON-NLS
HashSet<String> enabledModuleNames = getModulesNames(executionContext, IngestJobSettings.ENABLED_MODULES_PROPERTY, makeCsvList(loadedModuleNames));
HashSet<String> disabledModuleNames = getModulesNames(executionContext, IngestJobSettings.DISABLED_MODULES_PROPERTY, ""); //NON-NLS
/**
* Check for missing modules and create warnings if any are found.
@ -338,7 +315,7 @@ public class IngestJobSettings {
enabledModuleNames.remove(moduleName);
disabledModuleNames.remove(moduleName);
String warning = NbBundle.getMessage(IngestJobSettings.class, "IngestJobSettings.missingModule.warning", moduleName); //NON-NLS
LOGGER.log(Level.WARNING, warning);
logger.log(Level.WARNING, warning);
this.warnings.add(warning);
}
@ -367,14 +344,14 @@ public class IngestJobSettings {
* Update the enabled/disabled ingest module settings for this context
* to reflect any missing modules or newly discovered modules.
*/
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.ENABLED_MODULES_KEY, makeCommaSeparatedValuesList(enabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.DISABLED_MODULES_KEY, makeCommaSeparatedValuesList(disabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.ENABLED_MODULES_PROPERTY, makeCsvList(enabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.DISABLED_MODULES_PROPERTY, makeCsvList(disabledModuleNames));
/**
* Restore the last used File Ingest Filter
*/
if (ModuleSettings.settingExists(this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_KEY) == false) {
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_KEY, FilesSetsManager.getDefaultFilter().getName());
if (ModuleSettings.settingExists(this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_PROPERTY) == false) {
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_PROPERTY, FilesSetsManager.getDefaultFilter().getName());
}
try {
Map<String, FilesSet> fileIngestFilters = FilesSetsManager.getInstance()
@ -382,35 +359,37 @@ public class IngestJobSettings {
for (FilesSet fSet : FilesSetsManager.getStandardFileIngestFilters()) {
fileIngestFilters.put(fSet.getName(), fSet);
}
this.fileIngestFilter = fileIngestFilters.get(ModuleSettings.getConfigSetting(
this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_KEY));
this.fileIngestFilter = fileIngestFilters.get(ModuleSettings.getConfigSetting(this.executionContext, IngestJobSettings.LAST_FILE_INGEST_FILTER_PROPERTY));
} catch (FilesSetsManager.FilesSetsManagerException ex) {
this.fileIngestFilter = FilesSetsManager.getDefaultFilter();
LOGGER.log(Level.SEVERE, "Failed to get file ingest filter from .properties file, default filter being used", ex); //NON-NLS
logger.log(Level.SEVERE, "Failed to get file ingest filter from .properties file, default filter being used", ex); //NON-NLS
}
}
/**
* Gets the module names for a given key within these ingest job settings.
* Gets a list of enabled module names from the properties file for the
* execution context of these ingest job settings.
*
* @param context The identifier for the context for which to get the
* module names, e.g., the Add Data Source wizard or
* Run Ingest Modules context.
* @param key The key string.
* @param defaultSetting The default list of module names.
* @param executionContext The execution context identifier.
* @param propertyName The property name.
* @param defaultSetting The default list of module names to se and return
* if the property does not exist.
*
* @return The list of module names associated with the key.
* @return
*/
private static HashSet<String> getModulesNamesFromSetting(String context, String key, String defaultSetting) {
if (ModuleSettings.settingExists(context, key) == false) {
ModuleSettings.setConfigSetting(context, key, defaultSetting);
private static HashSet<String> getModulesNames(String executionContext, String propertyName, String defaultSetting) {
if (ModuleSettings.settingExists(executionContext, propertyName) == false) {
ModuleSettings.setConfigSetting(executionContext, propertyName, defaultSetting);
}
HashSet<String> moduleNames = new HashSet<>();
String modulesSetting = ModuleSettings.getConfigSetting(context, key);
String modulesSetting = ModuleSettings.getConfigSetting(executionContext, propertyName);
if (!modulesSetting.isEmpty()) {
String[] settingNames = modulesSetting.split(", ");
for (String name : settingNames) {
// Map some old core module names to the current core module names.
/*
* Map some obsolete core ingest module names to the current
* core ingest module names.
*/
switch (name) {
case "Thunderbird Parser": //NON-NLS
case "MBox Parser": //NON-NLS
@ -435,7 +414,7 @@ public class IngestJobSettings {
}
/**
* Get a set which contains all the names of enabled modules for the
* Gets a set which contains all the names of enabled modules for the
* specified context.
*
* @param context -the execution context (profile name) to check
@ -443,7 +422,7 @@ public class IngestJobSettings {
* @return the names of the enabled modules
*/
static List<String> getEnabledModules(String context) {
return new ArrayList<>(getModulesNamesFromSetting(context, ENABLED_MODULES_KEY, ""));
return new ArrayList<>(getModulesNames(context, ENABLED_MODULES_PROPERTY, ""));
}
/**
@ -457,14 +436,13 @@ public class IngestJobSettings {
* @return True or false
*/
private boolean isPythonModuleSettingsFile(String moduleSettingsFilePath) {
return moduleSettingsFilePath.contains(pythonModuleSettingsPrefixCS);
return moduleSettingsFilePath.contains(PYTHON_CLASS_PROXY_PREFIX);
}
/**
* Gets the saved or default ingest job settings for a given ingest module
* for these ingest job settings.
* Gets the saved or default ingest job settings for a given ingest module.
*
* @param factory The ingest module factory for an ingest module.
* @param factory The ingest module factory.
*
* @return The ingest module settings.
*/
@ -478,7 +456,7 @@ public class IngestJobSettings {
settings = (IngestModuleIngestJobSettings) in.readObject();
} catch (IOException | ClassNotFoundException ex) {
String warning = NbBundle.getMessage(IngestJobSettings.class, "IngestJobSettings.moduleSettingsLoad.warning", factory.getModuleDisplayName(), this.executionContext); //NON-NLS
LOGGER.log(Level.WARNING, warning, ex);
logger.log(Level.WARNING, warning, ex);
this.warnings.add(warning);
}
} else {
@ -486,7 +464,7 @@ public class IngestJobSettings {
settings = (IngestModuleIngestJobSettings) in.readObject();
} catch (IOException | ClassNotFoundException exception) {
String warning = NbBundle.getMessage(IngestJobSettings.class, "IngestJobSettings.moduleSettingsLoad.warning", factory.getModuleDisplayName(), this.executionContext); //NON-NLS
LOGGER.log(Level.WARNING, warning, exception);
logger.log(Level.WARNING, warning, exception);
this.warnings.add(warning);
}
}
@ -529,18 +507,17 @@ public class IngestJobSettings {
disabledModuleNames.add(moduleName);
}
}
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.ENABLED_MODULES_KEY, makeCommaSeparatedValuesList(enabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.DISABLED_MODULES_KEY, makeCommaSeparatedValuesList(disabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.ENABLED_MODULES_PROPERTY, makeCsvList(enabledModuleNames));
ModuleSettings.setConfigSetting(this.executionContext, IngestJobSettings.DISABLED_MODULES_PROPERTY, makeCsvList(disabledModuleNames));
/**
* Save the last used File Ingest Filter setting for this context.
*/
ModuleSettings.setConfigSetting(this.executionContext, LAST_FILE_INGEST_FILTER_KEY, fileIngestFilter.getName());
ModuleSettings.setConfigSetting(this.executionContext, LAST_FILE_INGEST_FILTER_PROPERTY, fileIngestFilter.getName());
}
/**
* Serializes the ingest job settings for this context for a given ingest
* module.
* Serializes the ingest job settings for a given ingest module.
*
* @param factory The ingest module factory for the module.
* @param settings The ingest job settings for the ingest module
@ -551,32 +528,40 @@ public class IngestJobSettings {
out.writeObject(settings);
} catch (IOException ex) {
String warning = NbBundle.getMessage(IngestJobSettings.class, "IngestJobSettings.moduleSettingsSave.warning", factory.getModuleDisplayName(), this.executionContext); //NON-NLS
LOGGER.log(Level.SEVERE, warning, ex);
logger.log(Level.SEVERE, warning, ex);
this.warnings.add(warning);
}
}
/**
* Makes a comma-separated values list from a hash set of strings.
* Makes a comma-separated values list from a collection of strings.
*
* @param input A hash set of strings.
* @param collection A collection of strings.
*
* @return The contents of the hash set as a single string of
* @return The contents of the collection as a single string of
* comma-separated values.
*/
private static String makeCommaSeparatedValuesList(HashSet<String> input) {
if (input == null || input.isEmpty()) {
return "";
}
private static String makeCsvList(Collection<String> collection) {
return collection.stream().collect(Collectors.joining(","));
}
ArrayList<String> list = new ArrayList<>();
list.addAll(input);
StringBuilder csvList = new StringBuilder();
for (int i = 0; i < list.size() - 1; ++i) {
csvList.append(list.get(i)).append(", ");
}
csvList.append(list.get(list.size() - 1));
return csvList.toString();
/**
* The type of ingest modules to run.
*/
public enum IngestType {
/**
* Run both data source level and file-level ingest modules.
*/
ALL_MODULES,
/**
* Run only data source level ingest modules.
*/
DATA_SOURCE_ONLY,
/**
* Run only file level ingest modules.
*/
FILES_ONLY
}
}