Coverage Report - org.owasp.dependencycheck.data.cpe.CpeMemoryIndex
 
Classes in this File Line Coverage Branch Coverage Complexity
CpeMemoryIndex
79%
78/98
53%
15/28
3.417
 
 1  
 /*
 2  
  * This file is part of dependency-check-core.
 3  
  *
 4  
  * Licensed under the Apache License, Version 2.0 (the "License");
 5  
  * you may not use this file except in compliance with the License.
 6  
  * You may obtain a copy of the License at
 7  
  *
 8  
  *     http://www.apache.org/licenses/LICENSE-2.0
 9  
  *
 10  
  * Unless required by applicable law or agreed to in writing, software
 11  
  * distributed under the License is distributed on an "AS IS" BASIS,
 12  
  * WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
 13  
  * See the License for the specific language governing permissions and
 14  
  * limitations under the License.
 15  
  *
 16  
  * Copyright (c) 2013 Jeremy Long. All Rights Reserved.
 17  
  */
 18  
 package org.owasp.dependencycheck.data.cpe;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.util.HashMap;
 22  
 import java.util.Map;
 23  
 import java.util.Set;
 24  
 import org.apache.lucene.analysis.Analyzer;
 25  
 import org.apache.lucene.analysis.core.KeywordAnalyzer;
 26  
 import org.apache.lucene.analysis.miscellaneous.PerFieldAnalyzerWrapper;
 27  
 import org.apache.lucene.document.Document;
 28  
 import org.apache.lucene.document.Field;
 29  
 import org.apache.lucene.document.TextField;
 30  
 import org.apache.lucene.index.CorruptIndexException;
 31  
 import org.apache.lucene.index.DirectoryReader;
 32  
 import org.apache.lucene.index.IndexReader;
 33  
 import org.apache.lucene.index.IndexWriter;
 34  
 import org.apache.lucene.index.IndexWriterConfig;
 35  
 import org.apache.lucene.queryparser.classic.ParseException;
 36  
 import org.apache.lucene.queryparser.classic.QueryParser;
 37  
 import org.apache.lucene.search.IndexSearcher;
 38  
 import org.apache.lucene.search.Query;
 39  
 import org.apache.lucene.search.TopDocs;
 40  
 import org.apache.lucene.store.RAMDirectory;
 41  
 import org.owasp.dependencycheck.data.lucene.LuceneUtils;
 42  
 import org.owasp.dependencycheck.data.lucene.SearchFieldAnalyzer;
 43  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 44  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 45  
 import org.owasp.dependencycheck.utils.Pair;
 46  
 import org.slf4j.Logger;
 47  
 import org.slf4j.LoggerFactory;
 48  
 
 49  
 /**
 50  
  * An in memory lucene index that contains the vendor/product combinations from
 51  
  * the CPE (application) identifiers within the NVD CVE data.
 52  
  *
 53  
  * @author Jeremy Long
 54  
  */
 55  
 public final class CpeMemoryIndex {
 56  
 
 57  
     /**
 58  
      * The logger.
 59  
      */
 60  1
     private static final Logger LOGGER = LoggerFactory.getLogger(CpeMemoryIndex.class);
 61  
     /**
 62  
      * singleton instance.
 63  
      */
 64  1
     private static final CpeMemoryIndex INSTANCE = new CpeMemoryIndex();
 65  
 
 66  
     /**
 67  
      * private constructor for singleton.
 68  
      */
 69  1
     private CpeMemoryIndex() {
 70  1
     }
 71  
 
 72  
     /**
 73  
      * Gets the singleton instance of the CpeMemoryIndex.
 74  
      *
 75  
      * @return the instance of the CpeMemoryIndex
 76  
      */
 77  
     public static CpeMemoryIndex getInstance() {
 78  2
         return INSTANCE;
 79  
     }
 80  
     /**
 81  
      * The in memory Lucene index.
 82  
      */
 83  
     private RAMDirectory index;
 84  
     /**
 85  
      * The Lucene IndexReader.
 86  
      */
 87  
     private IndexReader indexReader;
 88  
     /**
 89  
      * The Lucene IndexSearcher.
 90  
      */
 91  
     private IndexSearcher indexSearcher;
 92  
     /**
 93  
      * The Lucene Analyzer used for Searching.
 94  
      */
 95  
     private Analyzer searchingAnalyzer;
 96  
     /**
 97  
      * The Lucene QueryParser used for Searching.
 98  
      */
 99  
     private QueryParser queryParser;
 100  
     /**
 101  
      * The search field analyzer for the product field.
 102  
      */
 103  
     private SearchFieldAnalyzer productFieldAnalyzer;
 104  
     /**
 105  
      * The search field analyzer for the vendor field.
 106  
      */
 107  
     private SearchFieldAnalyzer vendorFieldAnalyzer;
 108  
 
 109  
     /**
 110  
      * Creates and loads data into an in memory index.
 111  
      *
 112  
      * @param cve the data source to retrieve the cpe data
 113  
      * @throws IndexException thrown if there is an error creating the index
 114  
      */
 115  
     public void open(CveDB cve) throws IndexException {
 116  2
         synchronized (INSTANCE) {
 117  2
             if (!openState) {
 118  2
                 index = new RAMDirectory();
 119  2
                 buildIndex(cve);
 120  
                 try {
 121  2
                     indexReader = DirectoryReader.open(index);
 122  0
                 } catch (IOException ex) {
 123  0
                     throw new IndexException(ex);
 124  2
                 }
 125  2
                 indexSearcher = new IndexSearcher(indexReader);
 126  2
                 searchingAnalyzer = createSearchingAnalyzer();
 127  2
                 queryParser = new QueryParser(LuceneUtils.CURRENT_VERSION, Fields.DOCUMENT_KEY, searchingAnalyzer);
 128  2
                 openState = true;
 129  
             }
 130  2
         }
 131  2
     }
 132  
     /**
 133  
      * A flag indicating whether or not the index is open.
 134  
      */
 135  1
     private boolean openState = false;
 136  
 
 137  
     /**
 138  
      * returns whether or not the index is open.
 139  
      *
 140  
      * @return whether or not the index is open
 141  
      */
 142  
     public boolean isOpen() {
 143  0
         return openState;
 144  
     }
 145  
 
 146  
     /**
 147  
      * Creates an Analyzer for searching the CPE Index.
 148  
      *
 149  
      * @return the CPE Analyzer.
 150  
      */
 151  
     private Analyzer createSearchingAnalyzer() {
 152  4
         final Map<String, Analyzer> fieldAnalyzers = new HashMap<String, Analyzer>();
 153  4
         fieldAnalyzers.put(Fields.DOCUMENT_KEY, new KeywordAnalyzer());
 154  4
         productFieldAnalyzer = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
 155  4
         vendorFieldAnalyzer = new SearchFieldAnalyzer(LuceneUtils.CURRENT_VERSION);
 156  4
         fieldAnalyzers.put(Fields.PRODUCT, productFieldAnalyzer);
 157  4
         fieldAnalyzers.put(Fields.VENDOR, vendorFieldAnalyzer);
 158  
 
 159  4
         return new PerFieldAnalyzerWrapper(new KeywordAnalyzer(), fieldAnalyzers);
 160  
     }
 161  
 
 162  
     /**
 163  
      * Closes the CPE Index.
 164  
      */
 165  
     public void close() {
 166  2
         if (searchingAnalyzer != null) {
 167  2
             searchingAnalyzer.close();
 168  2
             searchingAnalyzer = null;
 169  
         }
 170  2
         if (indexReader != null) {
 171  
             try {
 172  2
                 indexReader.close();
 173  0
             } catch (IOException ex) {
 174  0
                 LOGGER.trace("", ex);
 175  2
             }
 176  2
             indexReader = null;
 177  
         }
 178  2
         queryParser = null;
 179  2
         indexSearcher = null;
 180  2
         if (index != null) {
 181  2
             index.close();
 182  2
             index = null;
 183  
         }
 184  2
         openState = false;
 185  2
     }
 186  
 
 187  
     /**
 188  
      * Builds the CPE Lucene Index based off of the data within the CveDB.
 189  
      *
 190  
      * @param cve the data base containing the CPE data
 191  
      * @throws IndexException thrown if there is an issue creating the index
 192  
      */
 193  
     private void buildIndex(CveDB cve) throws IndexException {
 194  2
         Analyzer analyzer = null;
 195  2
         IndexWriter indexWriter = null;
 196  
         try {
 197  2
             analyzer = createSearchingAnalyzer();
 198  2
             final IndexWriterConfig conf = new IndexWriterConfig(LuceneUtils.CURRENT_VERSION, analyzer);
 199  2
             indexWriter = new IndexWriter(index, conf);
 200  
             try {
 201  
                 // Tip: reuse the Document and Fields for performance...
 202  
                 // See "Re-use Document and Field instances" from
 203  
                 // http://wiki.apache.org/lucene-java/ImproveIndexingSpeed
 204  2
                 final Document doc = new Document();
 205  2
                 final Field v = new TextField(Fields.VENDOR, Fields.VENDOR, Field.Store.YES);
 206  2
                 final Field p = new TextField(Fields.PRODUCT, Fields.PRODUCT, Field.Store.YES);
 207  2
                 doc.add(v);
 208  2
                 doc.add(p);
 209  
 
 210  2
                 final Set<Pair<String, String>> data = cve.getVendorProductList();
 211  2
                 for (Pair<String, String> pair : data) {
 212  
                     //todo figure out why there are null products
 213  53114
                     if (pair.getLeft() != null && pair.getRight() != null) {
 214  53112
                         v.setStringValue(pair.getLeft());
 215  53112
                         p.setStringValue(pair.getRight());
 216  53112
                         indexWriter.addDocument(doc);
 217  53112
                         resetFieldAnalyzer();
 218  
                     }
 219  53114
                 }
 220  0
             } catch (DatabaseException ex) {
 221  0
                 LOGGER.debug("", ex);
 222  0
                 throw new IndexException("Error reading CPE data", ex);
 223  2
             }
 224  0
         } catch (CorruptIndexException ex) {
 225  0
             throw new IndexException("Unable to close an in-memory index", ex);
 226  0
         } catch (IOException ex) {
 227  0
             throw new IndexException("Unable to close an in-memory index", ex);
 228  
         } finally {
 229  2
             if (indexWriter != null) {
 230  
                 try {
 231  
                     try {
 232  2
                         indexWriter.commit();
 233  
                     } finally {
 234  2
                         indexWriter.close(true);
 235  2
                     }
 236  0
                 } catch (CorruptIndexException ex) {
 237  0
                     throw new IndexException("Unable to close an in-memory index", ex);
 238  0
                 } catch (IOException ex) {
 239  0
                     throw new IndexException("Unable to close an in-memory index", ex);
 240  2
                 }
 241  2
                 if (analyzer != null) {
 242  2
                     analyzer.close();
 243  
                 }
 244  
             }
 245  
         }
 246  2
     }
 247  
 
 248  
     /**
 249  
      * Resets the product and vendor field analyzers.
 250  
      */
 251  
     private void resetFieldAnalyzer() {
 252  53123
         if (productFieldAnalyzer != null) {
 253  53123
             productFieldAnalyzer.clear();
 254  
         }
 255  53123
         if (vendorFieldAnalyzer != null) {
 256  53123
             vendorFieldAnalyzer.clear();
 257  
         }
 258  53123
     }
 259  
 
 260  
     /**
 261  
      * Searches the index using the given search string.
 262  
      *
 263  
      * @param searchString the query text
 264  
      * @param maxQueryResults the maximum number of documents to return
 265  
      * @return the TopDocs found by the search
 266  
      * @throws ParseException thrown when the searchString is invalid
 267  
      * @throws IOException is thrown if there is an issue with the underlying
 268  
      * Index
 269  
      */
 270  
     public TopDocs search(String searchString, int maxQueryResults) throws ParseException, IOException {
 271  11
         if (searchString == null || searchString.trim().isEmpty()) {
 272  0
             throw new ParseException("Query is null or empty");
 273  
         }
 274  11
         LOGGER.debug(searchString);
 275  11
         final Query query = queryParser.parse(searchString);
 276  11
         return search(query, maxQueryResults);
 277  
     }
 278  
 
 279  
     /**
 280  
      * Searches the index using the given query.
 281  
      *
 282  
      * @param query the query used to search the index
 283  
      * @param maxQueryResults the max number of results to return
 284  
      * @return the TopDocs found be the query
 285  
      * @throws CorruptIndexException thrown if the Index is corrupt
 286  
      * @throws IOException thrown if there is an IOException
 287  
      */
 288  
     public TopDocs search(Query query, int maxQueryResults) throws CorruptIndexException, IOException {
 289  11
         resetFieldAnalyzer();
 290  11
         return indexSearcher.search(query, maxQueryResults);
 291  
     }
 292  
 
 293  
     /**
 294  
      * Retrieves a document from the Index.
 295  
      *
 296  
      * @param documentId the id of the document to retrieve
 297  
      * @return the Document
 298  
      * @throws IOException thrown if there is an IOException
 299  
      */
 300  
     public Document getDocument(int documentId) throws IOException {
 301  21
         return indexSearcher.doc(documentId);
 302  
     }
 303  
 
 304  
     /**
 305  
      * Returns the number of CPE entries stored in the index.
 306  
      *
 307  
      * @return the number of CPE entries stored in the index
 308  
      */
 309  
     public int numDocs() {
 310  0
         if (indexReader == null) {
 311  0
             return -1;
 312  
         }
 313  0
         return indexReader.numDocs();
 314  
     }
 315  
 }