Coverage Report - org.owasp.dependencycheck.data.nvdcve.DatabaseProperties
 
Classes in this File Line Coverage Branch Coverage Complexity
DatabaseProperties
20%
7/35
0%
0/12
1.778
 
 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.nvdcve;
 19  
 
 20  
 import java.text.DateFormat;
 21  
 import java.text.SimpleDateFormat;
 22  
 import java.util.Date;
 23  
 import java.util.Map;
 24  
 import java.util.Map.Entry;
 25  
 import java.util.Properties;
 26  
 import java.util.TreeMap;
 27  
 import java.util.logging.Level;
 28  
 import java.util.logging.Logger;
 29  
 import org.owasp.dependencycheck.data.update.NvdCveInfo;
 30  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 31  
 
 32  
 /**
 33  
  * This is a wrapper around a set of properties that are stored in the database.
 34  
  *
 35  
  * @author Jeremy Long <jeremy.long@owasp.org>
 36  
  */
 37  
 public class DatabaseProperties {
 38  
 
 39  
     /**
 40  
      * The Logger.
 41  
      */
 42  1
     private static final Logger LOGGER = Logger.getLogger(DatabaseProperties.class.getName());
 43  
     /**
 44  
      * Modified key word, used as a key to store information about the modified file (i.e. the containing the last 8
 45  
      * days of updates)..
 46  
      */
 47  
     public static final String MODIFIED = "Modified";
 48  
     /**
 49  
      * The properties file key for the last updated field - used to store the last updated time of the Modified NVD CVE
 50  
      * xml file.
 51  
      */
 52  
     public static final String LAST_UPDATED = "NVD CVE Modified";
 53  
     /**
 54  
      * Stores the last updated time for each of the NVD CVE files. These timestamps should be updated if we process the
 55  
      * modified file within 7 days of the last update.
 56  
      */
 57  
     public static final String LAST_UPDATED_BASE = "NVD CVE ";
 58  
     /**
 59  
      * A collection of properties about the data.
 60  
      */
 61  
     private Properties properties;
 62  
     /**
 63  
      * A reference to the database.
 64  
      */
 65  
     private CveDB cveDB;
 66  
 
 67  
     /**
 68  
      * Constructs a new data properties object.
 69  
      *
 70  
      * @param cveDB the database object holding the properties
 71  
      */
 72  3
     DatabaseProperties(CveDB cveDB) {
 73  3
         this.cveDB = cveDB;
 74  3
         loadProperties();
 75  3
     }
 76  
 
 77  
     /**
 78  
      * Loads the properties from the database.
 79  
      */
 80  
     private void loadProperties() {
 81  3
         this.properties = cveDB.getProperties();
 82  3
     }
 83  
 
 84  
     /**
 85  
      * Returns whether or not any properties are set.
 86  
      *
 87  
      * @return whether or not any properties are set
 88  
      */
 89  
     public boolean isEmpty() {
 90  0
         return properties == null || properties.isEmpty();
 91  
     }
 92  
 
 93  
     /**
 94  
      * Saves the last updated information to the properties file.
 95  
      *
 96  
      * @param updatedValue the updated NVD CVE entry
 97  
      * @throws UpdateException is thrown if there is an update exception
 98  
      */
 99  
     public void save(NvdCveInfo updatedValue) throws UpdateException {
 100  0
         if (updatedValue == null) {
 101  0
             return;
 102  
         }
 103  0
         save(LAST_UPDATED_BASE + updatedValue.getId(), String.valueOf(updatedValue.getTimestamp()));
 104  0
     }
 105  
 
 106  
     /**
 107  
      * Saves the key value pair to the properties store.
 108  
      *
 109  
      * @param key the property key
 110  
      * @param value the property value
 111  
      * @throws UpdateException is thrown if there is an update exception
 112  
      */
 113  
     public void save(String key, String value) throws UpdateException {
 114  0
         properties.put(key, value);
 115  0
         cveDB.saveProperty(key, value);
 116  0
     }
 117  
 
 118  
     /**
 119  
      * Returns the property value for the given key. If the key is not contained in the underlying properties null is
 120  
      * returned.
 121  
      *
 122  
      * @param key the property key
 123  
      * @return the value of the property
 124  
      */
 125  
     public String getProperty(String key) {
 126  0
         return properties.getProperty(key);
 127  
     }
 128  
 
 129  
     /**
 130  
      * Returns the property value for the given key. If the key is not contained in the underlying properties the
 131  
      * default value is returned.
 132  
      *
 133  
      * @param key the property key
 134  
      * @param defaultValue the default value
 135  
      * @return the value of the property
 136  
      */
 137  
     public String getProperty(String key, String defaultValue) {
 138  0
         return properties.getProperty(key, defaultValue);
 139  
     }
 140  
 
 141  
     /**
 142  
      * Returns the collection of Database Properties as a properties collection.
 143  
      *
 144  
      * @return the collection of Database Properties
 145  
      */
 146  
     public Properties getProperties() {
 147  0
         return properties;
 148  
     }
 149  
 
 150  
     /**
 151  
      * Returns a map of the meta data from the database properties. This primarily contains timestamps of when the NVD
 152  
      * CVE information was last updated.
 153  
      *
 154  
      * @return a map of the database meta data
 155  
      */
 156  
     public Map<String, String> getMetaData() {
 157  0
         final Map<String, String> map = new TreeMap<String, String>();
 158  0
         for (Entry<Object, Object> entry : properties.entrySet()) {
 159  0
             final String key = (String) entry.getKey();
 160  0
             if (!"version".equals(key)) {
 161  0
                 if (key.startsWith("NVD CVE ")) {
 162  
                     try {
 163  0
                         final long epoch = Long.parseLong((String) entry.getValue());
 164  0
                         final Date date = new Date(epoch);
 165  0
                         final DateFormat format = new SimpleDateFormat("dd/MM/yyyy HH:mm:ss");
 166  0
                         final String formatted = format.format(date);
 167  0
                         map.put(key, formatted);
 168  0
                     } catch (Throwable ex) { //deliberately being broad in this catch clause
 169  0
                         LOGGER.log(Level.FINE, "Unable to parse timestamp from DB", ex);
 170  0
                         map.put(key, (String) entry.getValue());
 171  0
                     }
 172  
                 } else {
 173  0
                     map.put(key, (String) entry.getValue());
 174  
                 }
 175  
             }
 176  0
         }
 177  0
         return map;
 178  
     }
 179  
 }