updates for TSK api

This commit is contained in:
Greg DiCristofaro 2021-02-12 14:43:13 -05:00
parent fd9265ed7f
commit 9d917af21e
2 changed files with 101 additions and 46 deletions

View File

@ -41,6 +41,11 @@ class AddEditHostDialog extends javax.swing.JDialog {
private final Set<String> hostNamesUpper; private final Set<String> hostNamesUpper;
private final Host initialHost; private final Host initialHost;
/**
* Main constructor.
* @param parent The parent frame for this dialog.
* @param currentHosts The current set of hosts in the case.
*/
AddEditHostDialog(java.awt.Frame parent, Collection<Host> currentHosts) { AddEditHostDialog(java.awt.Frame parent, Collection<Host> currentHosts) {
this(parent, currentHosts, null); this(parent, currentHosts, null);
} }

View File

@ -18,19 +18,25 @@
*/ */
package org.sleuthkit.autopsy.datamodel.hosts; package org.sleuthkit.autopsy.datamodel.hosts;
import java.util.ArrayList;
import java.util.Collections; import java.util.Collections;
import java.util.HashMap;
import java.util.List; import java.util.List;
import java.util.Map;
import java.util.Objects; import java.util.Objects;
import java.util.Vector; import java.util.Vector;
import java.util.logging.Level; import java.util.logging.Level;
import java.util.stream.Collectors; import java.util.stream.Collectors;
import javax.swing.JFrame; import javax.swing.JFrame;
import javax.swing.ListModel; import javax.swing.ListModel;
import org.apache.commons.collections4.CollectionUtils;
import org.openide.util.NbBundle.Messages; import org.openide.util.NbBundle.Messages;
import org.sleuthkit.autopsy.casemodule.Case; import org.sleuthkit.autopsy.casemodule.Case;
import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException; import org.sleuthkit.autopsy.casemodule.NoCurrentCaseException;
import org.sleuthkit.autopsy.coreutils.Logger; import org.sleuthkit.autopsy.coreutils.Logger;
import org.sleuthkit.datamodel.DataSource;
import org.sleuthkit.datamodel.Host; import org.sleuthkit.datamodel.Host;
import org.sleuthkit.datamodel.SleuthkitCase;
import org.sleuthkit.datamodel.TskCoreException; import org.sleuthkit.datamodel.TskCoreException;
/** /**
@ -40,17 +46,39 @@ import org.sleuthkit.datamodel.TskCoreException;
"ManageHostsDialog_title_text=Manage Hosts" "ManageHostsDialog_title_text=Manage Hosts"
}) })
public class ManageHostsDialog extends javax.swing.JDialog { public class ManageHostsDialog extends javax.swing.JDialog {
private static class HostListItem {
private final Host host;
HostListItem(Host host) { /**
* List item to be used with jlist.
*/
private static class HostListItem {
private final Host host;
private final List<DataSource> dataSources;
/**
* Main constructor.
* @param host The host.
* @param dataSources The data sources that are children of this host.
*/
HostListItem(Host host, List<DataSource> dataSources) {
this.host = host; this.host = host;
this.dataSources = dataSources;
} }
/**
* @return The host.
*/
Host getHost() { Host getHost() {
return host; return host;
} }
/**
* @return The data sources associated with this host.
*/
List<DataSource> getDataSources() {
return dataSources;
}
@Override @Override
public String toString() { public String toString() {
return host == null ? "" : host.getName(); return host == null ? "" : host.getName();
@ -82,16 +110,16 @@ public class ManageHostsDialog extends javax.swing.JDialog {
return this.host.getId() == other.getHost().getId(); return this.host.getId() == other.getHost().getId();
} }
} }
private static final Logger logger = Logger.getLogger(ManageHostsDialog.class.getName()); private static final Logger logger = Logger.getLogger(ManageHostsDialog.class.getName());
private static final long serialVersionUID = 1L; private static final long serialVersionUID = 1L;
private List<Host> hostListData = Collections.emptyList(); private Map<Host, List<DataSource>> hostChildrenMap = Collections.emptyMap();
/** /**
* Main constructor. * Main constructor.
*
* @param parent The parent frame. * @param parent The parent frame.
*/ */
public ManageHostsDialog(java.awt.Frame parent) { public ManageHostsDialog(java.awt.Frame parent) {
@ -132,8 +160,12 @@ public class ManageHostsDialog extends javax.swing.JDialog {
* @param selectedHost * @param selectedHost
*/ */
private void deleteHost(Host selectedHost) { private void deleteHost(Host selectedHost) {
if (selectedHost != null) { if (selectedHost != null && selectedHost.getName() != null) {
System.out.println("Deleting: " + selectedHost); try {
Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().deleteHost(selectedHost.getName());
} catch (NoCurrentCaseException | TskCoreException e) {
logger.log(Level.WARNING, String.format("Unable to delete host '%s' at this time.", selectedHost.getName()), e);
}
refresh(); refresh();
} }
} }
@ -147,9 +179,12 @@ public class ManageHostsDialog extends javax.swing.JDialog {
if (selectedHost != null) { if (selectedHost != null) {
String newHostName = getAddEditDialogName(selectedHost); String newHostName = getAddEditDialogName(selectedHost);
if (newHostName != null) { if (newHostName != null) {
//TODO selectedHost.setName(newHostName);
logger.log(Level.SEVERE, String.format("This needs to edit host %d to change to %s.", selectedHost.getId(), newHostName)); try {
//Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().updateHostName(selectedHost.getId(), newHostName); Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().updateHost(selectedHost);
} catch (NoCurrentCaseException | TskCoreException e) {
logger.log(Level.WARNING, String.format("Unable to update host '%s' with id: %d at this time.", selectedHost.getName(), selectedHost.getId()), e);
}
refresh(); refresh();
} }
} }
@ -167,7 +202,7 @@ public class ManageHostsDialog extends javax.swing.JDialog {
? (JFrame) this.getRootPane().getParent() ? (JFrame) this.getRootPane().getParent()
: null; : null;
AddEditHostDialog addEditDialog = new AddEditHostDialog(parent, hostListData, origValue); AddEditHostDialog addEditDialog = new AddEditHostDialog(parent, hostChildrenMap.keySet(), origValue);
addEditDialog.setResizable(false); addEditDialog.setResizable(false);
addEditDialog.setLocationRelativeTo(parent); addEditDialog.setLocationRelativeTo(parent);
addEditDialog.setVisible(true); addEditDialog.setVisible(true);
@ -196,10 +231,11 @@ public class ManageHostsDialog extends javax.swing.JDialog {
private void refreshData() { private void refreshData() {
HostListItem selectedItem = hostList.getSelectedValue(); HostListItem selectedItem = hostList.getSelectedValue();
Long selectedId = selectedItem == null || selectedItem.getHost() == null ? null : selectedItem.getHost().getId(); Long selectedId = selectedItem == null || selectedItem.getHost() == null ? null : selectedItem.getHost().getId();
hostListData = getHostListData(); hostChildrenMap = getHostListData();
Vector<HostListItem> jlistData = hostListData.stream() Vector<HostListItem> jlistData = hostChildrenMap.entrySet().stream()
.map(HostListItem::new) .sorted((a, b) -> getNameOrEmpty(a.getKey()).compareTo(getNameOrEmpty(b.getKey())))
.map(entry -> new HostListItem(entry.getKey(), entry.getValue()))
.collect(Collectors.toCollection(Vector::new)); .collect(Collectors.toCollection(Vector::new));
hostList.setListData(jlistData); hostList.setListData(jlistData);
@ -217,27 +253,6 @@ public class ManageHostsDialog extends javax.swing.JDialog {
} }
} }
/**
* Retrieves the current list of hosts for the case.
*
* @return The list of hosts to be displayed in the list (sorted
* alphabetically).
*/
private List<Host> getHostListData() {
List<Host> toRet = null;
try {
toRet = Case.getCurrentCaseThrows().getSleuthkitCase().getHostManager().getHosts();
} catch (TskCoreException | NoCurrentCaseException ex) {
logger.log(Level.WARNING, "There was an error while fetching hosts for current case.", ex);
}
return (toRet == null)
? Collections.emptyList()
: toRet.stream()
.filter(h -> h != null)
.sorted((a, b) -> getNameOrEmpty(a).compareToIgnoreCase(getNameOrEmpty(b)))
.collect(Collectors.toList());
}
/** /**
* Returns the name of the host or an empty string if the host or name of * Returns the name of the host or an empty string if the host or name of
* host is null. * host is null.
@ -249,14 +264,49 @@ public class ManageHostsDialog extends javax.swing.JDialog {
return (h == null || h.getName() == null) ? "" : h.getName(); return (h == null || h.getName() == null) ? "" : h.getName();
} }
/**
* Retrieves the current list of hosts for the case.
*
* @return The list of hosts to be displayed in the list (sorted
* alphabetically).
*/
private Map<Host, List<DataSource>> getHostListData() {
Map<Host, List<DataSource>> hostMapping = new HashMap<>();
try {
SleuthkitCase curCase = Case.getCurrentCaseThrows().getSleuthkitCase();
List<Host> hosts = curCase.getHostManager().getHosts();
List<DataSource> dataSources = curCase.getDataSources();
if (dataSources != null) {
for (DataSource ds : dataSources) {
List<DataSource> hostDataSources = hostMapping.computeIfAbsent(ds.getHost(), (d) -> new ArrayList<>());
hostDataSources.add(ds);
}
}
if (hosts != null) {
for (Host host : hosts) {
hostMapping.putIfAbsent(host, Collections.emptyList());
}
}
} catch (TskCoreException | NoCurrentCaseException ex) {
logger.log(Level.WARNING, "There was an error while fetching hosts for current case.", ex);
}
return hostMapping;
}
/** /**
* Refreshes component's enabled state and displayed host data. * Refreshes component's enabled state and displayed host data.
*/ */
private void refreshComponents() { private void refreshComponents() {
Host selectedHost = getSelectedHost(); HostListItem selectedItem = hostList.getSelectedValue();
boolean itemSelected = selectedHost != null; Host selectedHost = selectedItem == null ? null : selectedItem.getHost();
this.editButton.setEnabled(itemSelected); List<DataSource> dataSources = selectedItem == null ? null : selectedItem.getDataSources();
this.deleteButton.setEnabled(itemSelected); this.editButton.setEnabled(selectedHost != null);
this.deleteButton.setEnabled(selectedHost != null && CollectionUtils.isEmpty(dataSources));
String nameTextFieldStr = selectedHost != null && selectedHost.getName() != null ? selectedHost.getName() : ""; String nameTextFieldStr = selectedHost != null && selectedHost.getName() != null ? selectedHost.getName() : "";
this.hostNameTextField.setText(nameTextFieldStr); this.hostNameTextField.setText(nameTextFieldStr);