| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 17 | |
|
| 18 | |
|
| 19 | |
package org.owasp.dependencycheck.data.update; |
| 20 | |
|
| 21 | |
import java.io.File; |
| 22 | |
import java.io.FileInputStream; |
| 23 | |
import java.io.FileNotFoundException; |
| 24 | |
import java.io.FileOutputStream; |
| 25 | |
import java.io.IOException; |
| 26 | |
import java.io.InputStream; |
| 27 | |
import java.io.OutputStream; |
| 28 | |
import java.io.OutputStreamWriter; |
| 29 | |
import java.util.Properties; |
| 30 | |
import java.util.logging.Level; |
| 31 | |
import java.util.logging.Logger; |
| 32 | |
import org.owasp.dependencycheck.data.UpdateException; |
| 33 | |
import org.owasp.dependencycheck.data.nvdcve.CveDB; |
| 34 | |
import org.owasp.dependencycheck.utils.Settings; |
| 35 | |
|
| 36 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class DataStoreMetaInfo { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
public static final String BATCH = "batch"; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
public static final String MODIFIED = "modified"; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | |
private static final String UPDATE_PROPERTIES_FILE = "data.properties"; |
| 56 | |
|
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
public static final String LAST_UPDATED = "lastupdated.modified"; |
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
|
| 66 | |
public static final String LAST_UPDATED_BASE = "lastupdated."; |
| 67 | |
|
| 68 | |
|
| 69 | |
|
| 70 | 5 | private Properties properties = new Properties(); |
| 71 | |
|
| 72 | |
|
| 73 | |
|
| 74 | |
|
| 75 | |
private boolean batchUpdateMode; |
| 76 | |
|
| 77 | |
|
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
protected boolean isBatchUpdateMode() { |
| 83 | 1 | return batchUpdateMode; |
| 84 | |
} |
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
protected void setBatchUpdateMode(boolean batchUpdateMode) { |
| 92 | 1 | this.batchUpdateMode = batchUpdateMode; |
| 93 | 1 | } |
| 94 | |
|
| 95 | |
|
| 96 | |
|
| 97 | |
|
| 98 | 5 | public DataStoreMetaInfo() { |
| 99 | 5 | batchUpdateMode = !Settings.getString(Settings.KEYS.BATCH_UPDATE_URL, "").isEmpty(); |
| 100 | 5 | loadProperties(); |
| 101 | 5 | } |
| 102 | |
|
| 103 | |
|
| 104 | |
|
| 105 | |
|
| 106 | |
private void loadProperties() { |
| 107 | 5 | final File file = getPropertiesFile(); |
| 108 | 5 | if (file.exists()) { |
| 109 | 5 | InputStream is = null; |
| 110 | |
try { |
| 111 | 5 | is = new FileInputStream(file); |
| 112 | 0 | } catch (FileNotFoundException ignore) { |
| 113 | |
|
| 114 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINEST, null, ignore); |
| 115 | 5 | } |
| 116 | |
try { |
| 117 | 5 | properties.load(is); |
| 118 | 0 | } catch (IOException ex) { |
| 119 | 0 | final String msg = String.format("Unable to load properties file '%s'", file.getPath()); |
| 120 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.WARNING, msg); |
| 121 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINE, null, ex); |
| 122 | |
} finally { |
| 123 | 5 | if (is != null) { |
| 124 | |
try { |
| 125 | 5 | is.close(); |
| 126 | 0 | } catch (IOException ex) { |
| 127 | 0 | final String msg = String.format("Unable to close properties file '%s'", file.getPath()); |
| 128 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.WARNING, msg); |
| 129 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINE, null, ex); |
| 130 | 5 | } |
| 131 | |
} |
| 132 | |
} |
| 133 | |
} |
| 134 | 5 | } |
| 135 | |
|
| 136 | |
|
| 137 | |
|
| 138 | |
|
| 139 | |
|
| 140 | |
|
| 141 | |
public boolean isEmpty() { |
| 142 | 1 | return properties.isEmpty(); |
| 143 | |
} |
| 144 | |
|
| 145 | |
|
| 146 | |
|
| 147 | |
|
| 148 | |
|
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
public void save(NvdCveInfo updatedValue) throws UpdateException { |
| 153 | 1 | if (updatedValue == null) { |
| 154 | 0 | return; |
| 155 | |
} |
| 156 | 1 | final File cveProp = getPropertiesFile(); |
| 157 | 1 | final Properties prop = new Properties(); |
| 158 | 1 | if (cveProp.exists()) { |
| 159 | 1 | FileInputStream in = null; |
| 160 | |
try { |
| 161 | 1 | in = new FileInputStream(cveProp); |
| 162 | 1 | prop.load(in); |
| 163 | 0 | } catch (Exception ignoreMe) { |
| 164 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINEST, null, ignoreMe); |
| 165 | |
} finally { |
| 166 | 1 | if (in != null) { |
| 167 | |
try { |
| 168 | 1 | in.close(); |
| 169 | 0 | } catch (Exception ignoreMeToo) { |
| 170 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINEST, null, ignoreMeToo); |
| 171 | 1 | } |
| 172 | |
} |
| 173 | |
} |
| 174 | |
} |
| 175 | 1 | prop.put("version", CveDB.DB_SCHEMA_VERSION); |
| 176 | 1 | prop.put(LAST_UPDATED_BASE + updatedValue.getId(), String.valueOf(updatedValue.getTimestamp())); |
| 177 | |
|
| 178 | 1 | OutputStream os = null; |
| 179 | 1 | OutputStreamWriter out = null; |
| 180 | |
try { |
| 181 | 1 | os = new FileOutputStream(cveProp); |
| 182 | 1 | out = new OutputStreamWriter(os, "UTF-8"); |
| 183 | 1 | prop.store(out, "Meta data about data and data sources used by dependency-check"); |
| 184 | 0 | } catch (FileNotFoundException ex) { |
| 185 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINE, null, ex); |
| 186 | 0 | throw new UpdateException("Unable to find last updated properties file.", ex); |
| 187 | 0 | } catch (IOException ex) { |
| 188 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINE, null, ex); |
| 189 | 0 | throw new UpdateException("Unable to update last updated properties file.", ex); |
| 190 | |
} finally { |
| 191 | 1 | if (out != null) { |
| 192 | |
try { |
| 193 | 1 | out.close(); |
| 194 | 0 | } catch (IOException ex) { |
| 195 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINEST, null, ex); |
| 196 | 1 | } |
| 197 | |
} |
| 198 | 1 | if (os != null) { |
| 199 | |
try { |
| 200 | 1 | os.close(); |
| 201 | 0 | } catch (IOException ex) { |
| 202 | 0 | Logger.getLogger(DataStoreMetaInfo.class.getName()).log(Level.FINEST, null, ex); |
| 203 | 1 | } |
| 204 | |
} |
| 205 | |
} |
| 206 | 1 | } |
| 207 | |
|
| 208 | |
|
| 209 | |
|
| 210 | |
|
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
public String getProperty(String key) { |
| 216 | 1 | return properties.getProperty(key); |
| 217 | |
} |
| 218 | |
|
| 219 | |
|
| 220 | |
|
| 221 | |
|
| 222 | |
|
| 223 | |
|
| 224 | |
|
| 225 | |
|
| 226 | |
|
| 227 | |
public String getProperty(String key, String defaultValue) { |
| 228 | 1 | return properties.getProperty(key, defaultValue); |
| 229 | |
} |
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
public static File getPropertiesFile() { |
| 237 | 7 | final File dataDirectory = Settings.getDataFile(Settings.KEYS.DATA_DIRECTORY); |
| 238 | 7 | final File file = new File(dataDirectory, UPDATE_PROPERTIES_FILE); |
| 239 | 7 | return file; |
| 240 | |
} |
| 241 | |
} |