1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18 package org.owasp.dependencycheck.data.nvdcve;
19
20 import java.time.Instant;
21 import java.time.ZoneId;
22 import java.time.ZonedDateTime;
23 import java.time.format.DateTimeFormatter;
24 import java.util.Map;
25 import java.util.Map.Entry;
26 import java.util.Properties;
27 import java.util.TreeMap;
28 import javax.annotation.concurrent.ThreadSafe;
29
30 import org.owasp.dependencycheck.data.update.exception.UpdateException;
31 import org.owasp.dependencycheck.utils.DateUtil;
32 import org.slf4j.Logger;
33 import org.slf4j.LoggerFactory;
34
35
36
37
38
39
40
41 @ThreadSafe
42 public class DatabaseProperties {
43
44
45
46
47 private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseProperties.class);
48
49
50
51 public static final String NVD_API_LAST_MODIFIED = "nvd.api.last.modified";
52
53
54
55 public static final String NVD_API_LAST_CHECKED = "nvd.api.last.checked";
56
57
58
59 public static final String NVD_CACHE_LAST_CHECKED = "nvd.cache.last.checked";
60
61
62
63 public static final String NVD_CACHE_LAST_MODIFIED = "nvd.cache.last.modified";
64
65
66
67 public static final String LAST_CPE_UPDATE = "LAST_CPE_UPDATE";
68
69
70
71 public static final String VERSION = "version";
72
73
74
75 public static final String KEV_LAST_CHECKED = "kev.checked";
76
77
78
79 public static final String RETIRE_LAST_CHECKED = "retirejs.checked";
80
81
82
83 public static final String HOSTED_SUPPRESSION_LAST_CHECKED = "hosted.suppression.checked";
84
85
86
87 public static final String KEV_VERSION = "kev.version";
88
89
90
91 private final Properties properties;
92
93
94
95 private final CveDB cveDB;
96
97
98
99
100
101
102 DatabaseProperties(CveDB cveDB) {
103 this.cveDB = cveDB;
104 this.properties = cveDB.getProperties();
105 }
106
107
108
109
110
111
112 public synchronized boolean isEmpty() {
113 return properties == null || properties.isEmpty();
114 }
115
116
117
118
119
120
121
122
123 public synchronized void save(String key, String value) throws UpdateException {
124 properties.put(key, value);
125 cveDB.saveProperty(key, value);
126 }
127
128
129
130
131
132
133
134
135 public synchronized String getProperty(String key) {
136 return properties.getProperty(key);
137 }
138
139
140
141
142
143
144
145
146
147 public synchronized String getProperty(String key, String defaultValue) {
148 return properties.getProperty(key, defaultValue);
149 }
150
151
152
153
154
155
156 public synchronized Properties getProperties() {
157 return properties;
158 }
159
160
161
162
163
164
165
166
167 public synchronized Map<String, String> getMetaData() {
168 final Map<String, String> map = new TreeMap<>();
169 for (Entry<Object, Object> entry : properties.entrySet()) {
170 final String key = (String) entry.getKey();
171 if (!"version".equals(key)) {
172 if (DatabaseProperties.NVD_API_LAST_CHECKED.equals(key)) {
173 map.put("NVD API Last Checked", entry.getValue().toString());
174
175 } else if (DatabaseProperties.NVD_API_LAST_MODIFIED.equals(key)) {
176 map.put("NVD API Last Modified", entry.getValue().toString());
177
178 } else if (DatabaseProperties.NVD_CACHE_LAST_CHECKED.equals(key)) {
179 map.put("NVD Cache Last Checked", entry.getValue().toString());
180
181 } else if (DatabaseProperties.NVD_CACHE_LAST_MODIFIED.equals(key)) {
182 map.put("NVD Cache Last Modified", entry.getValue().toString());
183 }
184 }
185 }
186 return map;
187 }
188
189
190
191
192
193
194
195 public ZonedDateTime getTimestamp(String key) {
196 return DatabaseProperties.getTimestamp(properties, key);
197 }
198
199
200
201
202
203
204
205 public void save(String key, ZonedDateTime timestamp) throws UpdateException {
206 final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
207 save(key, dtf.format(timestamp));
208 }
209
210
211
212
213
214
215
216
217 public static void setTimestamp(Properties properties, String key, ZonedDateTime timestamp) throws UpdateException {
218 final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
219 properties.put(key, dtf.format(timestamp));
220 }
221
222
223
224
225
226
227
228
229 public static ZonedDateTime getTimestamp(Properties properties, String key) {
230 final DateTimeFormatter dtf = DateTimeFormatter.ofPattern("uuuu-MM-dd'T'HH:mm:ssX");
231 final String val = properties.getProperty(key);
232 if (val != null) {
233 final String value = properties.getProperty(key);
234 return ZonedDateTime.parse(value, dtf);
235 }
236 return null;
237 }
238
239
240
241
242
243
244
245
246 public static ZonedDateTime getIsoTimestamp(Properties properties, String key) {
247
248 final DateTimeFormatter dtf = DateTimeFormatter.ISO_DATE_TIME;
249 final String val = properties.getProperty(key);
250 if (val != null) {
251 final String value = properties.getProperty(key);
252 return ZonedDateTime.parse(value, dtf);
253 }
254 return null;
255 }
256
257
258
259
260
261
262
263 public long getPropertyInSeconds(String key) {
264 final String value = getProperty(key, "0");
265 return DateUtil.getEpochValueInSeconds(value);
266 }
267
268 }