| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
public final class CpeMemoryIndex { |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | 1 | private static final Logger LOGGER = LoggerFactory.getLogger(CpeMemoryIndex.class); |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 1 | private static final CpeMemoryIndex INSTANCE = new CpeMemoryIndex(); |
| 65 | |
|
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 1 | private CpeMemoryIndex() { |
| 70 | 1 | } |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | |
public static CpeMemoryIndex getInstance() { |
| 78 | 2 | return INSTANCE; |
| 79 | |
} |
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
private RAMDirectory index; |
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
private IndexReader indexReader; |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
private IndexSearcher indexSearcher; |
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
private Analyzer searchingAnalyzer; |
| 96 | |
|
| 97 | |
|
| 98 | |
|
| 99 | |
private QueryParser queryParser; |
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
private SearchFieldAnalyzer productFieldAnalyzer; |
| 104 | |
|
| 105 | |
|
| 106 | |
|
| 107 | |
private SearchFieldAnalyzer vendorFieldAnalyzer; |
| 108 | |
|
| 109 | |
|
| 110 | |
|
| 111 | |
|
| 112 | |
|
| 113 | |
|
| 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 | |
|
| 134 | |
|
| 135 | 1 | private boolean openState = false; |
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
|
| 142 | |
public boolean isOpen() { |
| 143 | 0 | return openState; |
| 144 | |
} |
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 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 | |
|
| 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 | |
|
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 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 | |
|
| 202 | |
|
| 203 | |
|
| 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 | |
|
| 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 | |
|
| 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 | |
|
| 262 | |
|
| 263 | |
|
| 264 | |
|
| 265 | |
|
| 266 | |
|
| 267 | |
|
| 268 | |
|
| 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 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
|
| 284 | |
|
| 285 | |
|
| 286 | |
|
| 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 | |
|
| 295 | |
|
| 296 | |
|
| 297 | |
|
| 298 | |
|
| 299 | |
|
| 300 | |
public Document getDocument(int documentId) throws IOException { |
| 301 | 21 | return indexSearcher.doc(documentId); |
| 302 | |
} |
| 303 | |
|
| 304 | |
|
| 305 | |
|
| 306 | |
|
| 307 | |
|
| 308 | |
|
| 309 | |
public int numDocs() { |
| 310 | 0 | if (indexReader == null) { |
| 311 | 0 | return -1; |
| 312 | |
} |
| 313 | 0 | return indexReader.numDocs(); |
| 314 | |
} |
| 315 | |
} |