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