Merge pull request #6231 from kellykelly3/6787-commandline-failure-issue

6787 commandline failure issue
This commit is contained in:
Richard Cordovano 2020-09-04 10:16:51 -04:00 committed by GitHub
commit 9be752c45d
No known key found for this signature in database
GPG Key ID: 4AEE18F83AFDEB23
4 changed files with 101 additions and 232 deletions

View File

@ -1,60 +0,0 @@
/*
* Autopsy Forensic Browser
*
* Copyright 2014 Basis Technology Corp.
* Contact: carrier <at> sleuthkit <dot> org
*
* Licensed under the Apache License, Version 2.0 (the "License");
* you may not use this file except in compliance with the License.
* You may obtain a copy of the License at
*
* http://www.apache.org/licenses/LICENSE-2.0
*
* Unless required by applicable law or agreed to in writing, software
* distributed under the License is distributed on an "AS IS" BASIS,
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
* See the License for the specific language governing permissions and
* limitations under the License.
*/
package org.sleuthkit.autopsy.casemodule;
import java.util.HashSet;
import java.util.Map;
import java.util.Set;
import org.netbeans.api.sendopts.CommandException;
import org.netbeans.spi.sendopts.Env;
import org.netbeans.spi.sendopts.Option;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.util.lookup.ServiceProvider;
/**
* Allows Autopsy to get path to .aut file passed in via associating the file
* type in Windows.
*/
@ServiceProvider(service = OptionProcessor.class)
public class OpenFromArguments extends OptionProcessor {
/*
* Stores the .aut file if it was passed in as argument
*/
private String autPath = "";
private final Option option1 = Option.defaultArguments();
@Override
protected Set<Option> getOptions() {
final Set<Option> options = new HashSet<>();
options.add(option1);
return options;
}
@Override
protected void process(Env env, Map<Option, String[]> maps) throws CommandException {
if (maps.containsKey(option1)) {
autPath = maps.get(option1)[0];
}
}
public String getDefaultArg() {
return autPath;
}
}

View File

@ -59,20 +59,21 @@ public class StartupWindowProvider implements StartupWindowInterface {
private void init() {
if (startupWindowToUse == null) {
if (openCaseInUI()) {
new CommandLineOpenCaseManager().start();
return;
}
// first check whether we are running from command line
if (isRunningFromCommandLine()) {
// Autopsy is running from command line
logger.log(Level.INFO, "Running from command line"); //NON-NLS
startupWindowToUse = new CommandLineStartupWindow();
// kick off command line processing
new CommandLineIngestManager().start();
return;
String defaultArg = getDefaultArgument();
if(defaultArg != null) {
new CommandLineOpenCaseManager(defaultArg).start();
return;
} else {
// Autopsy is running from command line
logger.log(Level.INFO, "Running from command line"); //NON-NLS
startupWindowToUse = new CommandLineStartupWindow();
// kick off command line processing
new CommandLineIngestManager().start();
return;
}
}
//discover the registered windows
@ -123,41 +124,25 @@ public class StartupWindowProvider implements StartupWindowInterface {
* @return True if running from command line, false otherwise
*/
private boolean isRunningFromCommandLine() {
// first look up all OptionProcessors and see if running from command line option is set
Collection<? extends OptionProcessor> optionProcessors = Lookup.getDefault().lookupAll(OptionProcessor.class);
Iterator<? extends OptionProcessor> optionsIterator = optionProcessors.iterator();
while (optionsIterator.hasNext()) {
// find CommandLineOptionProcessor
OptionProcessor processor = optionsIterator.next();
if ((processor instanceof CommandLineOptionProcessor)) {
// check if we are running from command line
return ((CommandLineOptionProcessor) processor).isRunFromCommandLine();
}
CommandLineOptionProcessor processor = Lookup.getDefault().lookup(CommandLineOptionProcessor.class);
if(processor != null) {
return processor.isRunFromCommandLine();
}
return false;
}
/**
* Checks whether Autopsy was launched from the command line with the option
* to open an existing case.
*
* @return True if opening an existing case.
* Get the default argument from the CommandLineOptionProcessor.
*
* @return If set, the default argument otherwise null.
*/
private boolean openCaseInUI() {
// first look up all OptionProcessors and see if running from command line option is set
Collection<? extends OptionProcessor> optionProcessors = Lookup.getDefault().lookupAll(OptionProcessor.class);
Iterator<? extends OptionProcessor> optionsIterator = optionProcessors.iterator();
while (optionsIterator.hasNext()) {
// find CommandLineOptionProcessor
OptionProcessor processor = optionsIterator.next();
if ((processor instanceof OpenFromArguments)) {
// check if we are running from command line
String arg = ((OpenFromArguments) processor).getDefaultArg();
return arg != null && !arg.isEmpty();
}
private String getDefaultArgument() {
CommandLineOptionProcessor processor = Lookup.getDefault().lookup(CommandLineOptionProcessor.class);
if(processor != null) {
return processor.getDefaultArgument();
}
return false;
return null;
}
@Override

View File

@ -18,14 +18,8 @@
*/
package org.sleuthkit.autopsy.commandlineingest;
import java.nio.file.Paths;
import java.util.Collection;
import java.util.Iterator;
import java.util.logging.Level;
import org.netbeans.spi.sendopts.OptionProcessor;
import org.openide.util.Lookup;
import org.sleuthkit.autopsy.casemodule.CaseActionException;
import org.sleuthkit.autopsy.casemodule.OpenFromArguments;
import org.sleuthkit.autopsy.coreutils.Logger;
/**
@ -35,7 +29,12 @@ import org.sleuthkit.autopsy.coreutils.Logger;
public class CommandLineOpenCaseManager extends CommandLineManager {
private static final Logger LOGGER = Logger.getLogger(CommandLineOpenCaseManager.class.getName());
private final String casePath;
public CommandLineOpenCaseManager(String casePath) {
this.casePath = casePath;
}
/**
* Starts the thread to open the case.
*/
@ -51,21 +50,6 @@ public class CommandLineOpenCaseManager extends CommandLineManager {
@Override
public void run() {
String casePath = "";
// first look up all OptionProcessors and get input data from CommandLineOptionProcessor
Collection<? extends OptionProcessor> optionProcessors = Lookup.getDefault().lookupAll(OptionProcessor.class);
Iterator<? extends OptionProcessor> optionsIterator = optionProcessors.iterator();
while (optionsIterator.hasNext()) {
// find CommandLineOptionProcessor
OptionProcessor processor = optionsIterator.next();
if (processor instanceof OpenFromArguments) {
// check if we are running from command line
casePath = Paths.get(((OpenFromArguments) processor).getDefaultArg()).toAbsolutePath().toString();
break;
}
}
if (casePath == null || casePath.isEmpty()) {
LOGGER.log(Level.SEVERE, "No command line commands specified");
System.out.println("No command line commands specified");

View File

@ -20,6 +20,7 @@ package org.sleuthkit.autopsy.commandlineingest;
import java.io.File;
import java.util.ArrayList;
import java.util.Collections;
import java.util.HashSet;
import java.util.List;
import java.util.Map;
@ -52,6 +53,7 @@ public class CommandLineOptionProcessor extends OptionProcessor {
private final Option ingestProfileOption = Option.requiredArgument('p', "ingestProfile");
private final Option listAllDataSourcesCommandOption = Option.withoutArgument('l', "listAllDataSources");
private final Option generateReportsOption = Option.optionalArgument('g', "generateReports");
private final Option defaultArgument = Option.defaultArguments();
private boolean runFromCommandLine = false;
@ -60,6 +62,8 @@ public class CommandLineOptionProcessor extends OptionProcessor {
final static String CASETYPE_MULTI = "multi";
final static String CASETYPE_SINGLE = "single";
private String defaultArgumentValue = null;
@Override
protected Set<Option> getOptions() {
Set<Option> set = new HashSet<>();
@ -75,6 +79,7 @@ public class CommandLineOptionProcessor extends OptionProcessor {
set.add(ingestProfileOption);
set.add(listAllDataSourcesCommandOption);
set.add(generateReportsOption);
set.add(defaultArgument);
return set;
}
@ -82,16 +87,19 @@ public class CommandLineOptionProcessor extends OptionProcessor {
protected void process(Env env, Map<Option, String[]> values) throws CommandException {
logger.log(Level.INFO, "Processing Autopsy command line options"); //NON-NLS
System.out.println("Processing Autopsy command line options");
runFromCommandLine = false;
if (values.containsKey(defaultArgument)) {
defaultArgumentValue = values.get(defaultArgument)[0];
runFromCommandLine = true;
return;
}
// input arguments must contain at least one command
if (!(values.containsKey(createCaseCommandOption) || values.containsKey(addDataSourceCommandOption)
|| values.containsKey(runIngestCommandOption) || values.containsKey(listAllDataSourcesCommandOption)
|| values.containsKey(generateReportsOption))) {
// not running from command line
logger.log(Level.INFO, "No command line commands passed in as inputs. Not running from command line."); //NON-NLS
System.out.println("No command line commands passed in as inputs. Not running from command line.");
return;
handleError("Invalid command line, an input option must be supplied.");
}
// parse input parameters
@ -100,16 +108,12 @@ public class CommandLineOptionProcessor extends OptionProcessor {
if (values.containsKey(caseNameOption)) {
argDirs = values.get(caseNameOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Missing argument 'caseName'");
System.out.println("Missing argument 'caseName'");
return;
handleError("Missing argument 'caseName'");
}
inputCaseName = argDirs[0];
if (inputCaseName == null || inputCaseName.isEmpty()) {
logger.log(Level.SEVERE, "'caseName' argument is empty");
System.out.println("'caseName' argument is empty");
return;
handleError("'caseName' argument is empty");
}
}
@ -118,28 +122,16 @@ public class CommandLineOptionProcessor extends OptionProcessor {
argDirs = values.get(caseTypeOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Missing argument 'caseType'");
System.out.println("Missing argument 'caseType'");
return;
handleError("Missing argument 'caseType'");
}
caseType = argDirs[0];
if (caseType == null || caseType.isEmpty()) {
logger.log(Level.SEVERE, "'caseType' argument is empty");
System.out.println("'caseType' argument is empty");
return;
}
if (!caseType.equalsIgnoreCase(CASETYPE_MULTI) && !caseType.equalsIgnoreCase(CASETYPE_SINGLE)) {
logger.log(Level.SEVERE, "'caseType' argument is invalid");
System.out.println("'caseType' argument is invalid");
return;
}
if (caseType.equalsIgnoreCase(CASETYPE_MULTI) && !FeatureAccessUtils.canCreateMultiUserCases()) {
logger.log(Level.SEVERE, "Unable to create multi user case.");
System.out.println("Unable to create multi user case. Confirm that multi user settings are configured correctly.");
return;
handleError("'caseType' argument is empty");
} else if (!caseType.equalsIgnoreCase(CASETYPE_MULTI) && !caseType.equalsIgnoreCase(CASETYPE_SINGLE)) {
handleError("'caseType' argument is invalid");
} else if (caseType.equalsIgnoreCase(CASETYPE_MULTI) && !FeatureAccessUtils.canCreateMultiUserCases()) {
handleError("Unable to create multi user case. Confirm that multi user settings are configured correctly.");
}
}
@ -147,22 +139,16 @@ public class CommandLineOptionProcessor extends OptionProcessor {
if (values.containsKey(caseBaseDirOption)) {
argDirs = values.get(caseBaseDirOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Missing argument 'caseBaseDir'");
System.out.println("Missing argument 'caseBaseDir'");
return;
handleError("Missing argument 'caseBaseDir'");
}
caseBaseDir = argDirs[0];
if (caseBaseDir == null || caseBaseDir.isEmpty()) {
logger.log(Level.SEVERE, "Missing argument 'caseBaseDir' option");
System.out.println("Missing argument 'caseBaseDir' option");
return;
handleError("Missing argument 'caseBaseDir' option");
}
if (!(new File(caseBaseDir).exists()) || !(new File(caseBaseDir).isDirectory())) {
logger.log(Level.SEVERE, "'caseBaseDir' {0} directory doesn''t exist or is not a directory", caseBaseDir);
System.out.println("'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
return;
handleError("'caseBaseDir' directory doesn't exist or is not a directory: " + caseBaseDir);
}
}
@ -171,23 +157,17 @@ public class CommandLineOptionProcessor extends OptionProcessor {
argDirs = values.get(dataSourcePathOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
System.out.println("Missing argument 'dataSourcePath'");
return;
handleError("Missing argument 'dataSourcePath'");
}
dataSourcePath = argDirs[0];
// verify inputs
if (dataSourcePath == null || dataSourcePath.isEmpty()) {
logger.log(Level.SEVERE, "Missing argument 'dataSourcePath'");
System.out.println("Missing argument 'dataSourcePath'");
return;
handleError("Missing argument 'dataSourcePath'");
}
if (!(new File(dataSourcePath).exists())) {
logger.log(Level.SEVERE, "Input data source file {0} doesn''t exist", dataSourcePath);
System.out.println("Input data source file " + dataSourcePath + " doesn't exist");
return;
handleError("Input data source file " + dataSourcePath + " doesn't exist");
}
}
@ -196,17 +176,13 @@ public class CommandLineOptionProcessor extends OptionProcessor {
argDirs = values.get(dataSourceObjectIdOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Missing argument 'dataSourceObjectIdOption'");
System.out.println("Missing argument 'dataSourceObjectIdOption'");
return;
handleError("Missing argument 'dataSourceObjectIdOption'");
}
dataSourceId = argDirs[0];
// verify inputs
if (dataSourceId == null || dataSourceId.isEmpty()) {
logger.log(Level.SEVERE, "Input data source id is empty");
System.out.println("Input data source id is empty");
return;
handleError("Input data source id is empty");
}
}
@ -215,23 +191,17 @@ public class CommandLineOptionProcessor extends OptionProcessor {
argDirs = values.get(caseDirOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Argument missing from 'caseDir' option");
System.out.println("Argument missing from 'caseDir' option");
return;
handleError("Argument missing from 'caseDir' option");
}
caseDir = argDirs[0];
// verify inputs
if (caseDir == null || caseDir.isEmpty()) {
logger.log(Level.SEVERE, "Argument missing from 'caseDir'");
System.out.println("Argument missing from 'caseDir'");
return;
handleError("Argument missing from 'caseDir'");
}
if (!(new File(caseDir).exists()) || !(new File(caseDir).isDirectory())) {
logger.log(Level.SEVERE, "Case directory {0} does not exist or is not a directory", caseDir);
System.out.println("Case directory " + caseDir + " does not exist or is not a directory");
return;
handleError("Case directory " + caseDir + " does not exist or is not a directory");
}
}
@ -240,17 +210,13 @@ public class CommandLineOptionProcessor extends OptionProcessor {
argDirs = values.get(ingestProfileOption);
if (argDirs.length < 1) {
logger.log(Level.SEVERE, "Argument missing from 'ingestProfile' option");
System.out.println("Argument missing from 'ingestProfile' option");
return;
handleError("Argument missing from 'ingestProfile' option");
}
ingestProfile = argDirs[0];
// verify inputs
if (ingestProfile == null || ingestProfile.isEmpty()) {
logger.log(Level.SEVERE, "Missing argument 'ingestProfile'");
System.out.println("Missing argument 'ingestProfile'");
return;
handleError("Missing argument 'ingestProfile'");
}
}
@ -259,19 +225,13 @@ public class CommandLineOptionProcessor extends OptionProcessor {
if (values.containsKey(createCaseCommandOption)) {
// 'caseName' must always be specified for "CREATE_CASE" command
if (inputCaseName.isEmpty()) {
logger.log(Level.SEVERE, "'caseName' argument is empty");
System.out.println("'caseName' argument is empty");
runFromCommandLine = false;
return;
if (inputCaseName == null || inputCaseName.isEmpty()) {
handleError("'caseName' argument is empty");
}
// 'caseBaseDir' must always be specified for "CREATE_CASE" command
if (caseBaseDir.isEmpty()) {
logger.log(Level.SEVERE, "'caseBaseDir' argument is empty");
System.out.println("'caseBaseDir' argument is empty");
runFromCommandLine = false;
return;
if (caseBaseDir == null || caseBaseDir.isEmpty()) {
handleError("'caseBaseDir' argument is empty");
}
CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.CREATE_CASE);
@ -288,18 +248,12 @@ public class CommandLineOptionProcessor extends OptionProcessor {
// 'caseDir' must only be specified if the case is not being created during the current run
if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
// new case is not being created during this run, so 'caseDir' should have been specified
logger.log(Level.SEVERE, "'caseDir' argument is empty");
System.out.println("'caseDir' argument is empty");
runFromCommandLine = false;
return;
handleError("'caseDir' argument is empty");
}
// 'dataSourcePath' must always be specified for "ADD_DATA_SOURCE" command
if (dataSourcePath.isEmpty()) {
logger.log(Level.SEVERE, "'dataSourcePath' argument is empty");
System.out.println("'dataSourcePath' argument is empty");
runFromCommandLine = false;
return;
if (dataSourcePath == null || dataSourcePath.isEmpty()) {
handleError("'dataSourcePath' argument is empty");
}
CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.ADD_DATA_SOURCE);
@ -315,19 +269,13 @@ public class CommandLineOptionProcessor extends OptionProcessor {
// 'caseDir' must only be specified if the case is not being created during the current run
if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
// new case is not being created during this run, so 'caseDir' should have been specified
logger.log(Level.SEVERE, "'caseDir' argument is empty");
System.out.println("'caseDir' argument is empty");
runFromCommandLine = false;
return;
handleError("'caseDir' argument is empty");
}
// if new data source is being added during this run, then 'dataSourceId' is not specified
if (!values.containsKey(addDataSourceCommandOption) && dataSourceId.isEmpty()) {
// data source is not being added during this run, so 'dataSourceId' should have been specified
logger.log(Level.SEVERE, "'dataSourceId' argument is empty");
System.out.println("'dataSourceId' argument is empty");
runFromCommandLine = false;
return;
handleError("'dataSourceId' argument is empty");
}
CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.RUN_INGEST);
@ -342,12 +290,9 @@ public class CommandLineOptionProcessor extends OptionProcessor {
if (values.containsKey(listAllDataSourcesCommandOption)) {
// 'caseDir' must only be specified if the case is not being created during the current run
if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
if (!values.containsKey(createCaseCommandOption) && (caseDir == null || caseDir.isEmpty())) {
// new case is not being created during this run, so 'caseDir' should have been specified
logger.log(Level.SEVERE, "'caseDir' argument is empty");
System.out.println("'caseDir' argument is empty");
runFromCommandLine = false;
return;
handleError("'caseDir' argument is empty");
}
CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.LIST_ALL_DATA_SOURCES);
@ -363,25 +308,19 @@ public class CommandLineOptionProcessor extends OptionProcessor {
// 'caseDir' must only be specified if the case is not being created during the current run
if (!values.containsKey(createCaseCommandOption) && caseDir.isEmpty()) {
// new case is not being created during this run, so 'caseDir' should have been specified
logger.log(Level.SEVERE, "'caseDir' argument is empty");
System.out.println("'caseDir' argument is empty");
runFromCommandLine = false;
return;
handleError("'caseDir' argument is empty");
}
argDirs = values.get(generateReportsOption);
if (argDirs.length > 0) {
reportProfile = argDirs[0];
}
// If the user doesn't supply an options for generateReports the
// argsDirs length will be 0, so if reportProfile is empty
// something is not right.
if (reportProfile != null && reportProfile.isEmpty()) {
logger.log(Level.SEVERE, "'generateReports' argument is empty");
System.out.println("'generateReports' argument is empty");
runFromCommandLine = false;
return;
handleError("'generateReports' argument is empty");
}
CommandLineCommand newCommand = new CommandLineCommand(CommandLineCommand.CommandType.GENERATE_REPORTS);
@ -391,7 +330,7 @@ public class CommandLineOptionProcessor extends OptionProcessor {
}
commands.add(newCommand);
runFromCommandLine = true;
}
}
}
/**
@ -403,12 +342,33 @@ public class CommandLineOptionProcessor extends OptionProcessor {
return runFromCommandLine;
}
/**
* Return the value of the default argument.
*
* @return The default argument value or null if one was not set.
*/
public String getDefaultArgument() {
return defaultArgumentValue;
}
/**
* Returns list of all commands passed in via command line.
*
* @return list of input commands
*/
List<CommandLineCommand> getCommands() {
return commands;
return Collections.unmodifiableList(commands);
}
/**
* Send the error message to the log file and create the exception.
*
* @param errorMessage
*
* @throws CommandException
*/
private void handleError(String errorMessage) throws CommandException {
logger.log(Level.SEVERE, errorMessage);
throw new CommandException(1, errorMessage);
}
}