Autopsy  4.4.1
Graphical digital forensics platform for The Sleuth Kit and other tools.
Ingester.java
Go to the documentation of this file.
1 /*
2  * Autopsy Forensic Browser
3  *
4  * Copyright 2011-2017 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 java.io.BufferedReader;
22 import java.util.HashMap;
23 import java.util.Map;
24 import java.util.logging.Level;
25 import org.apache.solr.client.solrj.SolrServerException;
26 import org.apache.solr.common.SolrInputDocument;
27 import org.openide.util.NbBundle;
32 import org.sleuthkit.datamodel.AbstractFile;
33 import org.sleuthkit.datamodel.BlackboardArtifact;
34 import org.sleuthkit.datamodel.DerivedFile;
35 import org.sleuthkit.datamodel.Directory;
36 import org.sleuthkit.datamodel.File;
37 import org.sleuthkit.datamodel.LayoutFile;
38 import org.sleuthkit.datamodel.LocalDirectory;
39 import org.sleuthkit.datamodel.LocalFile;
40 import org.sleuthkit.datamodel.SlackFile;
41 import org.sleuthkit.datamodel.SleuthkitItemVisitor;
42 import org.sleuthkit.datamodel.SleuthkitVisitableItem;
43 import org.sleuthkit.datamodel.TskCoreException;
44 
48 //JMTODO: Should this class really be a singleton?
49 class Ingester {
50 
51  private static final Logger logger = Logger.getLogger(Ingester.class.getName());
52  private volatile boolean uncommitedIngests = false;
53  private final Server solrServer = KeywordSearch.getServer();
54  private static final SolrFieldsVisitor SOLR_FIELDS_VISITOR = new SolrFieldsVisitor();
55  private static Ingester instance;
56  private static final int SINGLE_READ_CHARS = 512;
57 
58  private Ingester() {
59  }
60 
61  public static synchronized Ingester getDefault() {
62  if (instance == null) {
63  instance = new Ingester();
64  }
65  return instance;
66  }
67 
68  //JMTODO: this is probably useless
69  @Override
70  @SuppressWarnings("FinalizeDeclaration")
71  protected void finalize() throws Throwable {
72  super.finalize();
73 
74  // Warn if files might have been left uncommited.
75  if (uncommitedIngests) {
76  logger.warning("Ingester was used to add files that it never committed."); //NON-NLS
77  }
78  }
79 
90  void indexMetaDataOnly(AbstractFile file) throws IngesterException {
91  indexChunk("", file.getName(), getContentFields(file));
92  }
93 
104  void indexMetaDataOnly(BlackboardArtifact artifact) throws IngesterException {
105  indexChunk("", new ArtifactTextExtractor().getName(artifact), getContentFields(artifact));
106  }
107 
116  private Map<String, String> getContentFields(SleuthkitVisitableItem item) {
117  return item.accept(SOLR_FIELDS_VISITOR);
118  }
119 
140  < T extends SleuthkitVisitableItem> boolean indexText(TextExtractor< T> extractor, T source, IngestJobContext context) throws Ingester.IngesterException {
141  final long sourceID = extractor.getID(source);
142  final String sourceName = extractor.getName(source);
143 
144  int numChunks = 0; //unknown until chunking is done
145 
146  if (extractor.isDisabled()) {
147  /* some Extractors, notable the strings extractor, have options
148  * which can be configured such that no extraction should be done */
149  return true;
150  }
151 
152  Map<String, String> fields = getContentFields(source);
153  //Get a reader for the content of the given source
154  try (BufferedReader reader = new BufferedReader(extractor.getReader(source));) {
155  Chunker chunker = new Chunker(reader);
156  for (Chunk chunk : chunker) {
157  String chunkId = Server.getChunkIdString(sourceID, numChunks + 1);
158  fields.put(Server.Schema.ID.toString(), chunkId);
159  fields.put(Server.Schema.CHUNK_SIZE.toString(), String.valueOf(chunk.getBaseChunkLength()));
160  try {
161  //add the chunk text to Solr index
162  indexChunk(chunk.toString(), sourceName, fields);
163  numChunks++;
164  } catch (Ingester.IngesterException ingEx) {
165  extractor.logWarning("Ingester had a problem with extracted string from file '" //NON-NLS
166  + sourceName + "' (id: " + sourceID + ").", ingEx);//NON-NLS
167 
168  throw ingEx; //need to rethrow to signal error and move on
169  }
170  }
171  if (chunker.hasException()) {
172  extractor.logWarning("Error chunking content from " + sourceID + ": " + sourceName, chunker.getException());
173  return false;
174  }
175  } catch (Exception ex) {
176  extractor.logWarning("Unexpected error, can't read content stream from " + sourceID + ": " + sourceName, ex);//NON-NLS
177  return false;
178  } finally {
179  //after all chunks, index just the meta data, including the numChunks, of the parent file
180  fields.put(Server.Schema.NUM_CHUNKS.toString(), Integer.toString(numChunks));
181  //reset id field to base document id
182  fields.put(Server.Schema.ID.toString(), Long.toString(sourceID));
183  //"parent" docs don't have chunk_size
184  fields.remove(Server.Schema.CHUNK_SIZE.toString());
185  indexChunk(null, sourceName, fields);
186  }
187 
188  return true;
189  }
190 
204  private void indexChunk(String chunk, String sourceName, Map<String, String> fields) throws IngesterException {
205  if (fields.get(Server.Schema.IMAGE_ID.toString()) == null) {
206  //JMTODO: actually if the we couldn't get the image id it is set to -1,
207  // but does this really mean we don't want to index it?
208 
209  //skip the file, image id unknown
210  String msg = NbBundle.getMessage(Ingester.class,
211  "Ingester.ingest.exception.unknownImgId.msg", sourceName); //JMTODO: does this need to ne internationalized?
212  logger.log(Level.SEVERE, msg);
213  throw new IngesterException(msg);
214  }
215 
216  //Make a SolrInputDocument out of the field map
217  SolrInputDocument updateDoc = new SolrInputDocument();
218  for (String key : fields.keySet()) {
219  updateDoc.addField(key, fields.get(key));
220  }
221  //add the content to the SolrInputDocument
222  //JMTODO: can we just add it to the field map before passing that in?
223  updateDoc.addField(Server.Schema.CONTENT.toString(), chunk);
224 
225  try {
226  //TODO: consider timeout thread, or vary socket timeout based on size of indexed content
227  solrServer.addDocument(updateDoc);
228  uncommitedIngests = true;
229 
230  } catch (KeywordSearchModuleException | NoOpenCoreException ex) {
231  //JMTODO: does this need to be internationalized?
232  throw new IngesterException(
233  NbBundle.getMessage(Ingester.class, "Ingester.ingest.exception.err.msg", sourceName), ex);
234  }
235  }
236 
241  void commit() {
242  try {
243  solrServer.commit();
244  uncommitedIngests = false;
245  } catch (NoOpenCoreException | SolrServerException ex) {
246  logger.log(Level.WARNING, "Error commiting index", ex); //NON-NLS
247 
248  }
249  }
250 
254  static private class SolrFieldsVisitor extends SleuthkitItemVisitor.Default<Map<String, String>> {
255 
256  @Override
257  protected Map<String, String> defaultVisit(SleuthkitVisitableItem svi) {
258  return new HashMap<>();
259  }
260 
261  @Override
262  public Map<String, String> visit(File f) {
263  return getCommonAndMACTimeFields(f);
264  }
265 
266  @Override
267  public Map<String, String> visit(DerivedFile df) {
268  return getCommonAndMACTimeFields(df);
269  }
270 
271  @Override
272  public Map<String, String> visit(Directory d) {
273  return getCommonAndMACTimeFields(d);
274  }
275 
276  @Override
277  public Map<String, String> visit(LocalDirectory ld){
278  return getCommonAndMACTimeFields(ld);
279  }
280 
281  @Override
282  public Map<String, String> visit(LayoutFile lf) {
283  // layout files do not have times
284  return getCommonFields(lf);
285  }
286 
287  @Override
288  public Map<String, String> visit(LocalFile lf) {
289  return getCommonAndMACTimeFields(lf);
290  }
291 
292  @Override
293  public Map<String, String> visit(SlackFile f) {
294  return getCommonAndMACTimeFields(f);
295  }
296 
306  private Map<String, String> getCommonAndMACTimeFields(AbstractFile file) {
307  Map<String, String> params = getCommonFields(file);
308  params.put(Server.Schema.CTIME.toString(), ContentUtils.getStringTimeISO8601(file.getCtime(), file));
309  params.put(Server.Schema.ATIME.toString(), ContentUtils.getStringTimeISO8601(file.getAtime(), file));
310  params.put(Server.Schema.MTIME.toString(), ContentUtils.getStringTimeISO8601(file.getMtime(), file));
311  params.put(Server.Schema.CRTIME.toString(), ContentUtils.getStringTimeISO8601(file.getCrtime(), file));
312  return params;
313  }
314 
323  private Map<String, String> getCommonFields(AbstractFile file) {
324  Map<String, String> params = new HashMap<>();
325  params.put(Server.Schema.ID.toString(), Long.toString(file.getId()));
326  try {
327  params.put(Server.Schema.IMAGE_ID.toString(), Long.toString(file.getDataSource().getId()));
328  } catch (TskCoreException ex) {
329  logger.log(Level.SEVERE, "Could not get data source id to properly index the file " + file.getId(), ex); //NON-NLS
330  params.put(Server.Schema.IMAGE_ID.toString(), Long.toString(-1));
331  }
332  params.put(Server.Schema.FILE_NAME.toString(), file.getName());
333  return params;
334  }
335 
343  @Override
344  public Map<String, String> visit(BlackboardArtifact artifact) {
345  Map<String, String> params = new HashMap<>();
346  params.put(Server.Schema.ID.toString(), Long.toString(artifact.getArtifactID()));
347  try {
348  params.put(Server.Schema.IMAGE_ID.toString(), Long.toString(ArtifactTextExtractor.getDataSource(artifact).getId()));
349  } catch (TskCoreException ex) {
350  logger.log(Level.SEVERE, "Could not get data source id to properly index the artifact " + artifact.getArtifactID(), ex); //NON-NLS
351  params.put(Server.Schema.IMAGE_ID.toString(), Long.toString(-1));
352  }
353  return params;
354  }
355  }
356 
361  static class IngesterException extends Exception {
362 
363  private static final long serialVersionUID = 1L;
364 
365  IngesterException(String message, Throwable ex) {
366  super(message, ex);
367  }
368 
369  IngesterException(String message) {
370  super(message);
371  }
372  }
373 }
Map< String, String > visit(LocalDirectory ld)
Definition: Ingester.java:277
Map< String, String > getCommonAndMACTimeFields(AbstractFile file)
Definition: Ingester.java:306
static String getStringTimeISO8601(long epochSeconds, TimeZone tzone)
Map< String, String > getCommonFields(AbstractFile file)
Definition: Ingester.java:323
Map< String, String > visit(BlackboardArtifact artifact)
Definition: Ingester.java:344
Map< String, String > defaultVisit(SleuthkitVisitableItem svi)
Definition: Ingester.java:257

Copyright © 2012-2016 Basis Technology. Generated on: Fri Sep 29 2017
This work is licensed under a Creative Commons Attribution-Share Alike 3.0 United States License.