Autopsy  4.12.0
Graphical digital forensics platform for The Sleuth Kit and other tools.
SolrSearchService.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2015-2019 Basis Technology Corp.
5  * Contact: carrier <at> sleuthkit <dot> org
6  *
7  * Licensed under the Apache License, Version 2.0 (the "License");
8  * you may not use this file except in compliance with the License.
9  * You may obtain a copy of the License at
10  *
11  * http://www.apache.org/licenses/LICENSE-2.0
12  *
13  * Unless required by applicable law or agreed to in writing, software
14  * distributed under the License is distributed on an "AS IS" BASIS,
15  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
16  * See the License for the specific language governing permissions and
17  * limitations under the License.
18  */
19 package org.sleuthkit.autopsy.keywordsearch;
20 
21 import com.google.common.eventbus.Subscribe;
22 import java.io.File;
23 import java.io.IOException;
24 import java.io.Reader;
25 import java.lang.reflect.InvocationTargetException;
26 import java.net.InetAddress;
27 import java.util.ArrayList;
28 import java.util.List;
29 import java.util.MissingResourceException;
30 import java.util.logging.Level;
31 import javax.swing.JDialog;
32 import javax.swing.JOptionPane;
33 import javax.swing.SwingUtilities;
34 import org.apache.commons.io.FileUtils;
35 import org.apache.commons.lang.math.NumberUtils;
36 import org.apache.solr.client.solrj.SolrServerException;
37 import org.apache.solr.client.solrj.impl.HttpSolrServer;
38 import org.openide.util.NbBundle;
39 import org.openide.util.lookup.ServiceProvider;
40 import org.openide.util.lookup.ServiceProviders;
53 import org.sleuthkit.datamodel.Blackboard;
54 import org.sleuthkit.datamodel.BlackboardArtifact;
55 import org.sleuthkit.datamodel.Content;
56 import org.sleuthkit.datamodel.TskCoreException;
57 
62 @ServiceProviders(value = {
63  @ServiceProvider(service = KeywordSearchService.class)
64  ,
65  @ServiceProvider(service = AutopsyService.class)
66 })
68 
69  private static final String BAD_IP_ADDRESS_FORMAT = "ioexception occurred when talking to server"; //NON-NLS
70  private static final String SERVER_REFUSED_CONNECTION = "server refused connection"; //NON-NLS
71  private static final int IS_REACHABLE_TIMEOUT_MS = 1000;
72  private static final int LARGE_INDEX_SIZE_GB = 50;
73  private static final int GIANT_INDEX_SIZE_GB = 500;
74  private static final Logger logger = Logger.getLogger(SolrSearchService.class.getName());
75 
91  @Override
92  public void index(Content content) throws TskCoreException {
93  /*
94  * TODO (JIRA-1099): The following code has some issues that need to be
95  * resolved. For artifacts, it is assumed that the posting of artifacts
96  * is only occuring during an ingest job with an enabled keyword search
97  * ingest module handling index commits; it also assumes that the
98  * artifacts are only posted by modules in the either the file level
99  * ingest pipeline or the first stage data source level ingest pipeline,
100  * so that the artifacts will be searched during a periodic or final
101  * keyword search. It also assumes that the only other type of Content
102  * for which this API will be called are Reports generated at a time
103  * when doing a commit is required and desirable, i.e., in a context
104  * other than an ingest job.
105  */
106  if (content == null) {
107  return;
108  }
109  final Ingester ingester = Ingester.getDefault();
110  if (content instanceof BlackboardArtifact) {
111  BlackboardArtifact artifact = (BlackboardArtifact) content;
112  if (artifact.getArtifactID() > 0) {
113  /*
114  * Artifact indexing is only supported for artifacts that use
115  * negative artifact ids to avoid overlapping with the object
116  * ids of other types of Content.
117  */
118  return;
119  }
120  try {
121  TextExtractor blackboardExtractor = TextExtractorFactory.getExtractor(content, null);
122  Reader blackboardExtractedTextReader = blackboardExtractor.getReader();
123  String sourceName = artifact.getDisplayName() + "_" + artifact.getArtifactID();
124  ingester.indexMetaDataOnly(artifact, sourceName);
125  ingester.indexText(blackboardExtractedTextReader, artifact.getArtifactID(), sourceName, content, null);
126  } catch (Ingester.IngesterException | TextExtractorFactory.NoTextExtractorFound | TextExtractor.InitReaderException ex) {
127  throw new TskCoreException("Error indexing artifact", ex);
128  }
129  } else {
130  try {
131  TextExtractor contentExtractor = TextExtractorFactory.getExtractor(content, null);
132  Reader contentExtractedTextReader = contentExtractor.getReader();
133  ingester.indexText(contentExtractedTextReader, content.getId(), content.getName(), content, null);
134  } catch (TextExtractorFactory.NoTextExtractorFound | Ingester.IngesterException | TextExtractor.InitReaderException ex) {
135  try {
136  // Try the StringsTextExtractor if Tika extractions fails.
137  TextExtractor stringsExtractor = TextExtractorFactory.getStringsExtractor(content, null);
138  Reader stringsExtractedTextReader = stringsExtractor.getReader();
139  ingester.indexText(stringsExtractedTextReader, content.getId(), content.getName(), content, null);
140  } catch (Ingester.IngesterException | TextExtractor.InitReaderException ex1) {
141  throw new TskCoreException("Error indexing content", ex1);
142  }
143  }
144  ingester.commit();
145  }
146  }
147 
156  @Override
157  public void tryConnect(String host, int port) throws KeywordSearchServiceException {
158  HttpSolrServer solrServer = null;
159  if (host == null || host.isEmpty()) {
160  throw new KeywordSearchServiceException(NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.MissingHostname")); //NON-NLS
161  }
162  try {
163  solrServer = new HttpSolrServer("http://" + host + ":" + Integer.toString(port) + "/solr"); //NON-NLS
164  KeywordSearch.getServer().connectToSolrServer(solrServer);
165  } catch (SolrServerException ex) {
166  throw new KeywordSearchServiceException(NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.HostnameOrPort")); //NON-NLS
167  } catch (IOException ex) {
168  String result = NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.HostnameOrPort"); //NON-NLS
169  String message = ex.getCause().getMessage().toLowerCase();
170  if (message.startsWith(SERVER_REFUSED_CONNECTION)) {
171  try {
172  if (InetAddress.getByName(host).isReachable(IS_REACHABLE_TIMEOUT_MS)) {
173  // if we can reach the host, then it's probably port problem
174  result = Bundle.SolrConnectionCheck_Port();
175  } else {
176  result = NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.HostnameOrPort"); //NON-NLS
177  }
178  } catch (IOException | MissingResourceException any) {
179  // it may be anything
180  result = NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.HostnameOrPort"); //NON-NLS
181  }
182  } else if (message.startsWith(BAD_IP_ADDRESS_FORMAT)) {
183  result = NbBundle.getMessage(SolrSearchService.class, "SolrConnectionCheck.Hostname"); //NON-NLS
184  }
185  throw new KeywordSearchServiceException(result);
186  } catch (NumberFormatException ex) {
187  throw new KeywordSearchServiceException(Bundle.SolrConnectionCheck_Port());
188  } catch (IllegalArgumentException ex) {
189  throw new KeywordSearchServiceException(ex.getMessage());
190  } finally {
191  if (null != solrServer) {
192  solrServer.shutdown();
193  }
194  }
195  }
196 
202  @NbBundle.Messages({
203  "# {0} - case directory", "SolrSearchService.exceptionMessage.noIndexMetadata=Unable to create IndexMetaData from case directory: {0}",
204  "SolrSearchService.exceptionMessage.noCurrentSolrCore=IndexMetadata did not contain a current Solr core so could not delete the case",
205  "# {0} - index folder path", "SolrSearchService.exceptionMessage.failedToDeleteIndexFiles=Failed to delete text index files at {0}"
206  })
207  @Override
209  String caseDirectory = metadata.getCaseDirectory();
210  IndexMetadata indexMetadata;
211  try {
212  indexMetadata = new IndexMetadata(caseDirectory);
213  } catch (IndexMetadata.TextIndexMetadataException ex) {
214  logger.log(Level.WARNING, NbBundle.getMessage(SolrSearchService.class, "SolrSearchService.exceptionMessage.noIndexMetadata", caseDirectory), ex);
215  throw new KeywordSearchServiceException(NbBundle.getMessage(SolrSearchService.class, "SolrSearchService.exceptionMessage.noIndexMetadata", caseDirectory), ex);
216  }
217  //find the index for the current version of solr (the one we are connected to) and delete its core using the index name
218  String currentSchema = IndexFinder.getCurrentSchemaVersion();
219  String currentSolr = IndexFinder.getCurrentSolrVersion();
220  for (Index index : indexMetadata.getIndexes()) {
221  if (index.getSolrVersion().equals(currentSolr) && index.getSchemaVersion().equals(currentSchema)) {
222  /*
223  * Unload/delete the core on the server and then delete the text
224  * index files.
225  */
226  KeywordSearch.getServer().deleteCore(index.getIndexName(), metadata);
227  if (!FileUtil.deleteDir(new File(index.getIndexPath()).getParentFile())) {
228  throw new KeywordSearchServiceException(Bundle.SolrSearchService_exceptionMessage_failedToDeleteIndexFiles(index.getIndexPath()));
229  }
230  }
231  return; //only one core exists for each combination of solr and schema version
232  }
233 
234  //this code this code will only execute if an index for the current core was not found
235  logger.log(Level.WARNING, NbBundle.getMessage(SolrSearchService.class,
236  "SolrSearchService.exceptionMessage.noCurrentSolrCore"));
237  throw new KeywordSearchServiceException(NbBundle.getMessage(SolrSearchService.class,
238  "SolrSearchService.exceptionMessage.noCurrentSolrCore"));
239  }
240 
241  @Override
242  public String getServiceName() {
243  return NbBundle.getMessage(this.getClass(), "SolrSearchService.ServiceName");
244  }
245 
254  @Override
255  @NbBundle.Messages({
256  "SolrSearch.lookingForMetadata.msg=Looking for text index metadata file",
257  "SolrSearch.readingIndexes.msg=Reading text index metadata file",
258  "SolrSearch.findingIndexes.msg=Looking for existing text index directories",
259  "SolrSearch.creatingNewIndex.msg=Creating new text index",
260  "SolrSearch.checkingForLatestIndex.msg=Looking for text index with latest Solr and schema version",
261  "SolrSearch.indentifyingIndex.msg=Identifying text index to use",
262  "SolrSearch.openCore.msg=Opening text index",
263  "SolrSearch.openLargeCore.msg=Opening text index. This may take several minutes.",
264  "SolrSearch.openGiantCore.msg=Opening text index. Text index for this case is very large and may take long time to load.",
265  "SolrSearch.complete.msg=Text index successfully opened"})
267  if (context.cancelRequested()) {
268  return;
269  }
270 
271  ProgressIndicator progress = context.getProgressIndicator();
272  int totalNumProgressUnits = 7;
273  int progressUnitsCompleted = 0;
274 
275  String caseDirPath = context.getCase().getCaseDirectory();
276  Case theCase = context.getCase();
277  List<Index> indexes = new ArrayList<>();
278  progress.start(Bundle.SolrSearch_lookingForMetadata_msg(), totalNumProgressUnits);
279  if (IndexMetadata.isMetadataFilePresent(caseDirPath)) {
280  try {
281  // metadata file exists, get list of existing Solr cores for this case
282  progressUnitsCompleted++;
283  progress.progress(Bundle.SolrSearch_findingIndexes_msg(), progressUnitsCompleted);
284  IndexMetadata indexMetadata = new IndexMetadata(caseDirPath);
285  indexes = indexMetadata.getIndexes();
286  } catch (IndexMetadata.TextIndexMetadataException ex) {
287  logger.log(Level.SEVERE, String.format("Unable to read text index metadata file"), ex);
288  throw new AutopsyServiceException("Unable to read text index metadata file", ex);
289  }
290  } else {
291  // metadata file doesn't exist.
292  // do case subdirectory search to look for Solr 4 Schema 1.8 indexes
293  progressUnitsCompleted++;
294  progress.progress(Bundle.SolrSearch_findingIndexes_msg(), progressUnitsCompleted);
295  Index oldIndex = IndexFinder.findOldIndexDir(theCase);
296  if (oldIndex != null) {
297  // add index to the list of indexes that exist for this case
298  indexes.add(oldIndex);
299  }
300  }
301 
302  if (context.cancelRequested()) {
303  return;
304  }
305 
306  // check if we found any existing indexes
307  Index currentVersionIndex = null;
308  if (indexes.isEmpty()) {
309  // new case that doesn't have an existing index. create new index folder
310  progressUnitsCompleted++;
311  progress.progress(Bundle.SolrSearch_creatingNewIndex_msg(), progressUnitsCompleted);
312  currentVersionIndex = IndexFinder.createLatestVersionIndexDir(theCase);
313  // add current index to the list of indexes that exist for this case
314  indexes.add(currentVersionIndex);
315  } else {
316  // check if one of the existing indexes is for latest Solr version and schema
317  progressUnitsCompleted++;
318  progress.progress(Bundle.SolrSearch_checkingForLatestIndex_msg(), progressUnitsCompleted);
319  currentVersionIndex = IndexFinder.findLatestVersionIndexDir(indexes);
320  if (currentVersionIndex == null) {
321  // found existing index(es) but none were for latest Solr version and schema version
322  progressUnitsCompleted++;
323  progress.progress(Bundle.SolrSearch_indentifyingIndex_msg(), progressUnitsCompleted);
324  Index indexToUse = IndexFinder.identifyIndexToUse(indexes);
325  if (indexToUse == null) {
326  // unable to find index that can be used
327  throw new AutopsyServiceException("Unable to find index that can be used for this case");
328  }
329 
330  if (context.cancelRequested()) {
331  return;
332  }
333 
334  double currentSolrVersion = NumberUtils.toDouble(IndexFinder.getCurrentSolrVersion());
335  double indexSolrVersion = NumberUtils.toDouble(indexToUse.getSolrVersion());
336  if (indexSolrVersion == currentSolrVersion) {
337  // latest Solr version but schema not compatible. index should be used in read-only mode
338  if (!indexToUse.isCompatible(IndexFinder.getCurrentSchemaVersion()) && RuntimeProperties.runningWithGUI()) {
339  // pop up a message box to indicate the read-only restrictions.
340  JOptionPane optionPane = new JOptionPane(
341  NbBundle.getMessage(this.getClass(), "SolrSearchService.IndexReadOnlyDialog.msg"),
342  JOptionPane.WARNING_MESSAGE,
343  JOptionPane.DEFAULT_OPTION);
344  try {
345  SwingUtilities.invokeAndWait(() -> {
346  JDialog dialog = optionPane.createDialog(NbBundle.getMessage(this.getClass(), "SolrSearchService.IndexReadOnlyDialog.title"));
347  dialog.setVisible(true);
348  });
349  } catch (InterruptedException ex) {
350  // Cancelled
351  return;
352  } catch (InvocationTargetException ex) {
353  throw new AutopsyServiceException("Error displaying limited search features warning dialog", ex);
354  }
355  }
356  // proceed with case open
357  currentVersionIndex = indexToUse;
358  } else {
359  // index needs to be upgraded to latest supported version of Solr
360  throw new AutopsyServiceException("Unable to find index to use for Case open");
361  }
362  }
363  }
364 
365  try {
366  // update text index metadata file
367  if (!indexes.isEmpty()) {
368  IndexMetadata indexMetadata = new IndexMetadata(caseDirPath, indexes);
369  }
370  } catch (IndexMetadata.TextIndexMetadataException ex) {
371  throw new AutopsyServiceException("Failed to save Solr core info in text index metadata file", ex);
372  }
373 
374  // open core
375  try {
376  // check text index size to gauge estimated time to open/load the index
377  long indexSizeInBytes = FileUtils.sizeOfDirectory(new File(currentVersionIndex.getIndexPath()));
378  long sizeInGb = indexSizeInBytes / 1000000000;
379  if (sizeInGb < LARGE_INDEX_SIZE_GB) {
380  progress.progress(Bundle.SolrSearch_openCore_msg(), totalNumProgressUnits - 1);
381  } else if (sizeInGb >= LARGE_INDEX_SIZE_GB && sizeInGb < GIANT_INDEX_SIZE_GB) {
382  progress.switchToIndeterminate(Bundle.SolrSearch_openLargeCore_msg());
383  } else {
384  progress.switchToIndeterminate(Bundle.SolrSearch_openGiantCore_msg());
385  }
386 
387  KeywordSearch.getServer().openCoreForCase(theCase, currentVersionIndex);
388  } catch (KeywordSearchModuleException ex) {
389  throw new AutopsyServiceException(String.format("Failed to open or create core for %s", caseDirPath), ex);
390  }
391  if (context.cancelRequested()) {
392  return;
393  }
394 
395  theCase.getSleuthkitCase().registerForEvents(this);
396 
397  progress.progress(Bundle.SolrSearch_complete_msg(), totalNumProgressUnits);
398  }
399 
408  @Override
410  /*
411  * TODO (JIRA 2525): The following code KeywordSearch.CaseChangeListener
412  * gambles that any BlackboardResultWriters (SwingWorkers) will complete
413  * in less than roughly two seconds. This stuff should be reworked using
414  * an ExecutorService and tasks with Futures.
415  */
416  AdHocSearchChildFactory.BlackboardResultWriter.stopAllWriters();
417  try {
418  Thread.sleep(2000);
419  } catch (InterruptedException ex) {
420  logger.log(Level.SEVERE, "Unexpected interrupt while waiting for BlackboardResultWriters to terminate", ex);
421  }
422 
423  try {
424  KeywordSearch.getServer().closeCore();
425  } catch (KeywordSearchModuleException ex) {
426  throw new AutopsyServiceException(String.format("Failed to close core for %s", context.getCase().getCaseDirectory()), ex);
427  }
428 
429  context.getCase().getSleuthkitCase().unregisterForEvents(this);
430  }
431 
437  @NbBundle.Messages("SolrSearchService.indexingError=Unable to index blackboard artifact.")
438  @Subscribe
439  void handleNewArtifacts(Blackboard.ArtifactsPostedEvent event) {
440  for (BlackboardArtifact artifact : event.getArtifacts()) {
441  if (artifact.getArtifactTypeID() != BlackboardArtifact.ARTIFACT_TYPE.TSK_KEYWORD_HIT.getTypeID()) { //don't index KWH artifacts.
442  try {
443  index(artifact);
444  } catch (TskCoreException ex) {
445  //TODO: is this the right error handling?
446  logger.log(Level.SEVERE, "Unable to index blackboard artifact " + artifact.getArtifactID(), ex); //NON-NLS
447  MessageNotifyUtil.Notify.error(Bundle.SolrSearchService_indexingError(), artifact.getDisplayName());
448  }
449  }
450  }
451  }
452 
462  @Deprecated
463  @Override
464  public void indexArtifact(BlackboardArtifact artifact) throws TskCoreException {
465  if (artifact == null) {
466  return;
467  }
468 
469  // We only support artifact indexing for Autopsy versions that use
470  // the negative range for artifact ids.
471  if (artifact.getArtifactID() > 0) {
472  return;
473  }
474  final Ingester ingester = Ingester.getDefault();
475 
476  try {
477  String sourceName = artifact.getDisplayName() + "_" + artifact.getArtifactID();
478  TextExtractor blackboardExtractor = TextExtractorFactory.getExtractor((Content) artifact, null);
479  Reader blackboardExtractedTextReader = blackboardExtractor.getReader();
480  ingester.indexMetaDataOnly(artifact, sourceName);
481  ingester.indexText(blackboardExtractedTextReader, artifact.getId(), sourceName, artifact, null);
482  } catch (Ingester.IngesterException | TextExtractorFactory.NoTextExtractorFound | TextExtractor.InitReaderException ex) {
483  throw new TskCoreException(ex.getCause().getMessage(), ex);
484  }
485  }
486 
487 }
void start(String message, int totalWorkUnits)
static TextExtractor getStringsExtractor(Content content, Lookup context)
static TextExtractor getExtractor(Content content, Lookup context)
synchronized static Logger getLogger(String name)
Definition: Logger.java:124
static boolean deleteDir(File dirPath)
Definition: FileUtil.java:49

Copyright © 2012-2018 Basis Technology. Generated on: Wed Sep 18 2019
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.