| 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.io.File; |
| 21 | |
import java.io.IOException; |
| 22 | |
import java.io.InputStream; |
| 23 | |
import java.sql.CallableStatement; |
| 24 | |
import java.sql.Connection; |
| 25 | |
import java.sql.Driver; |
| 26 | |
import java.sql.DriverManager; |
| 27 | |
import java.sql.ResultSet; |
| 28 | |
import java.sql.SQLException; |
| 29 | |
import java.sql.Statement; |
| 30 | |
import org.apache.commons.io.IOUtils; |
| 31 | |
import org.owasp.dependencycheck.utils.DBUtils; |
| 32 | |
import org.owasp.dependencycheck.utils.DependencyVersion; |
| 33 | |
import org.owasp.dependencycheck.utils.DependencyVersionUtil; |
| 34 | |
import org.owasp.dependencycheck.utils.Settings; |
| 35 | |
import org.slf4j.Logger; |
| 36 | |
import org.slf4j.LoggerFactory; |
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
|
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
public final class ConnectionFactory { |
| 45 | |
|
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | 2 | private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionFactory.class); |
| 50 | |
|
| 51 | |
|
| 52 | |
|
| 53 | 2 | public static final String DB_SCHEMA_VERSION = Settings.getString(Settings.KEYS.DB_VERSION); |
| 54 | |
|
| 55 | |
|
| 56 | |
|
| 57 | |
public static final String DB_STRUCTURE_RESOURCE = "data/initialize.sql"; |
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
public static final String DB_STRUCTURE_UPDATE_RESOURCE = "data/upgrade_%s.sql"; |
| 62 | |
|
| 63 | |
|
| 64 | |
|
| 65 | |
public static final String UPGRADE_HELP_URL = "http://jeremylong.github.io/DependencyCheck/data/upgrade.html"; |
| 66 | |
|
| 67 | |
|
| 68 | |
|
| 69 | 2 | private static Driver driver = null; |
| 70 | |
|
| 71 | |
|
| 72 | |
|
| 73 | 2 | private static String connectionString = null; |
| 74 | |
|
| 75 | |
|
| 76 | |
|
| 77 | 2 | private static String userName = null; |
| 78 | |
|
| 79 | |
|
| 80 | |
|
| 81 | 2 | private static String password = null; |
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | 0 | private ConnectionFactory() { |
| 87 | 0 | } |
| 88 | |
|
| 89 | |
|
| 90 | |
|
| 91 | |
|
| 92 | |
|
| 93 | |
|
| 94 | |
|
| 95 | |
public static synchronized void initialize() throws DatabaseException { |
| 96 | |
|
| 97 | 38 | if (connectionString != null) { |
| 98 | 34 | return; |
| 99 | |
} |
| 100 | 4 | Connection conn = null; |
| 101 | |
try { |
| 102 | |
|
| 103 | 4 | final String driverName = Settings.getString(Settings.KEYS.DB_DRIVER_NAME, ""); |
| 104 | 4 | if (!driverName.isEmpty()) { |
| 105 | 4 | LOGGER.debug("Loading driver: {}", driverName); |
| 106 | 4 | final String driverPath = Settings.getString(Settings.KEYS.DB_DRIVER_PATH, ""); |
| 107 | |
try { |
| 108 | 4 | if (!driverPath.isEmpty()) { |
| 109 | 0 | LOGGER.debug("Loading driver from: {}", driverPath); |
| 110 | 0 | driver = DriverLoader.load(driverName, driverPath); |
| 111 | |
} else { |
| 112 | 4 | driver = DriverLoader.load(driverName); |
| 113 | |
} |
| 114 | 0 | } catch (DriverLoadException ex) { |
| 115 | 0 | LOGGER.debug("Unable to load database driver", ex); |
| 116 | 0 | throw new DatabaseException("Unable to load database driver"); |
| 117 | 4 | } |
| 118 | |
} |
| 119 | 4 | userName = Settings.getString(Settings.KEYS.DB_USER, "dcuser"); |
| 120 | |
|
| 121 | 4 | password = Settings.getString(Settings.KEYS.DB_PASSWORD, "DC-Pass1337!"); |
| 122 | |
try { |
| 123 | 4 | connectionString = Settings.getConnectionString( |
| 124 | |
Settings.KEYS.DB_CONNECTION_STRING, |
| 125 | |
Settings.KEYS.DB_FILE_NAME); |
| 126 | 0 | } catch (IOException ex) { |
| 127 | 0 | LOGGER.debug( |
| 128 | |
"Unable to retrieve the database connection string", ex); |
| 129 | 0 | throw new DatabaseException("Unable to retrieve the database connection string"); |
| 130 | 4 | } |
| 131 | 4 | boolean shouldCreateSchema = false; |
| 132 | |
try { |
| 133 | 4 | if (connectionString.startsWith("jdbc:h2:file:")) { |
| 134 | 4 | shouldCreateSchema = !h2DataFileExists(); |
| 135 | 4 | LOGGER.debug("Need to create DB Structure: {}", shouldCreateSchema); |
| 136 | |
} |
| 137 | 0 | } catch (IOException ioex) { |
| 138 | 0 | LOGGER.debug("Unable to verify database exists", ioex); |
| 139 | 0 | throw new DatabaseException("Unable to verify database exists"); |
| 140 | 4 | } |
| 141 | 4 | LOGGER.debug("Loading database connection"); |
| 142 | 4 | LOGGER.debug("Connection String: {}", connectionString); |
| 143 | 4 | LOGGER.debug("Database User: {}", userName); |
| 144 | |
|
| 145 | |
try { |
| 146 | 4 | conn = DriverManager.getConnection(connectionString, userName, password); |
| 147 | 0 | } catch (SQLException ex) { |
| 148 | 0 | if (ex.getMessage().contains("java.net.UnknownHostException") && connectionString.contains("AUTO_SERVER=TRUE;")) { |
| 149 | 0 | connectionString = connectionString.replace("AUTO_SERVER=TRUE;", ""); |
| 150 | |
try { |
| 151 | 0 | conn = DriverManager.getConnection(connectionString, userName, password); |
| 152 | 0 | Settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString); |
| 153 | 0 | LOGGER.debug( |
| 154 | |
"Unable to start the database in server mode; reverting to single user mode"); |
| 155 | 0 | } catch (SQLException sqlex) { |
| 156 | 0 | LOGGER.debug("Unable to connect to the database", ex); |
| 157 | 0 | throw new DatabaseException("Unable to connect to the database"); |
| 158 | 0 | } |
| 159 | |
} else { |
| 160 | 0 | LOGGER.debug("Unable to connect to the database", ex); |
| 161 | 0 | throw new DatabaseException("Unable to connect to the database"); |
| 162 | |
} |
| 163 | 4 | } |
| 164 | |
|
| 165 | 4 | if (shouldCreateSchema) { |
| 166 | |
try { |
| 167 | 0 | createTables(conn); |
| 168 | 0 | } catch (DatabaseException dex) { |
| 169 | 0 | LOGGER.debug("", dex); |
| 170 | 0 | throw new DatabaseException("Unable to create the database structure"); |
| 171 | 0 | } |
| 172 | |
} |
| 173 | |
try { |
| 174 | 4 | ensureSchemaVersion(conn); |
| 175 | 0 | } catch (DatabaseException dex) { |
| 176 | 0 | LOGGER.debug("", dex); |
| 177 | 0 | throw new DatabaseException("Database schema does not match this version of dependency-check", dex); |
| 178 | 4 | } |
| 179 | |
} finally { |
| 180 | 4 | if (conn != null) { |
| 181 | |
try { |
| 182 | 4 | conn.close(); |
| 183 | 0 | } catch (SQLException ex) { |
| 184 | 0 | LOGGER.debug("An error occurred closing the connection", ex); |
| 185 | 4 | } |
| 186 | |
} |
| 187 | |
} |
| 188 | 4 | } |
| 189 | |
|
| 190 | |
|
| 191 | |
|
| 192 | |
|
| 193 | |
|
| 194 | |
|
| 195 | |
public static synchronized void cleanup() { |
| 196 | 2 | if (driver != null) { |
| 197 | |
try { |
| 198 | 2 | DriverManager.deregisterDriver(driver); |
| 199 | 0 | } catch (SQLException ex) { |
| 200 | 0 | LOGGER.debug("An error occurred unloading the database driver", ex); |
| 201 | 0 | } catch (Throwable unexpected) { |
| 202 | 0 | LOGGER.debug( |
| 203 | |
"An unexpected throwable occurred unloading the database driver", unexpected); |
| 204 | 2 | } |
| 205 | 2 | driver = null; |
| 206 | |
} |
| 207 | 2 | connectionString = null; |
| 208 | 2 | userName = null; |
| 209 | 2 | password = null; |
| 210 | 2 | } |
| 211 | |
|
| 212 | |
|
| 213 | |
|
| 214 | |
|
| 215 | |
|
| 216 | |
|
| 217 | |
|
| 218 | |
public static Connection getConnection() throws DatabaseException { |
| 219 | 28 | initialize(); |
| 220 | 28 | Connection conn = null; |
| 221 | |
try { |
| 222 | 28 | conn = DriverManager.getConnection(connectionString, userName, password); |
| 223 | 0 | } catch (SQLException ex) { |
| 224 | 0 | LOGGER.debug("", ex); |
| 225 | 0 | throw new DatabaseException("Unable to connect to the database"); |
| 226 | 28 | } |
| 227 | 28 | return conn; |
| 228 | |
} |
| 229 | |
|
| 230 | |
|
| 231 | |
|
| 232 | |
|
| 233 | |
|
| 234 | |
|
| 235 | |
|
| 236 | |
private static boolean h2DataFileExists() throws IOException { |
| 237 | 4 | final File dir = Settings.getDataDirectory(); |
| 238 | 4 | final String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME); |
| 239 | 4 | final File file = new File(dir, fileName); |
| 240 | 4 | return file.exists(); |
| 241 | |
} |
| 242 | |
|
| 243 | |
|
| 244 | |
|
| 245 | |
|
| 246 | |
|
| 247 | |
|
| 248 | |
|
| 249 | |
private static void createTables(Connection conn) throws DatabaseException { |
| 250 | 0 | LOGGER.debug("Creating database structure"); |
| 251 | 0 | InputStream is = null; |
| 252 | |
try { |
| 253 | 0 | is = ConnectionFactory.class.getClassLoader().getResourceAsStream(DB_STRUCTURE_RESOURCE); |
| 254 | 0 | final String dbStructure = IOUtils.toString(is, "UTF-8"); |
| 255 | |
|
| 256 | 0 | Statement statement = null; |
| 257 | |
try { |
| 258 | 0 | statement = conn.createStatement(); |
| 259 | 0 | statement.execute(dbStructure); |
| 260 | 0 | } catch (SQLException ex) { |
| 261 | 0 | LOGGER.debug("", ex); |
| 262 | 0 | throw new DatabaseException("Unable to create database statement", ex); |
| 263 | |
} finally { |
| 264 | 0 | DBUtils.closeStatement(statement); |
| 265 | 0 | } |
| 266 | 0 | } catch (IOException ex) { |
| 267 | 0 | throw new DatabaseException("Unable to create database schema", ex); |
| 268 | |
} finally { |
| 269 | 0 | IOUtils.closeQuietly(is); |
| 270 | 0 | } |
| 271 | 0 | } |
| 272 | |
|
| 273 | |
|
| 274 | |
|
| 275 | |
|
| 276 | |
|
| 277 | |
|
| 278 | |
|
| 279 | |
|
| 280 | |
|
| 281 | |
|
| 282 | |
|
| 283 | |
private static void updateSchema(Connection conn, DependencyVersion appExpectedVersion, DependencyVersion currentDbVersion) |
| 284 | |
throws DatabaseException { |
| 285 | |
|
| 286 | |
final String databaseProductName; |
| 287 | |
try { |
| 288 | 0 | databaseProductName = conn.getMetaData().getDatabaseProductName(); |
| 289 | 0 | } catch (SQLException ex) { |
| 290 | 0 | throw new DatabaseException("Unable to get the database product name"); |
| 291 | 0 | } |
| 292 | 0 | if ("h2".equalsIgnoreCase(databaseProductName)) { |
| 293 | 0 | LOGGER.debug("Updating database structure"); |
| 294 | 0 | InputStream is = null; |
| 295 | 0 | String updateFile = null; |
| 296 | |
try { |
| 297 | 0 | updateFile = String.format(DB_STRUCTURE_UPDATE_RESOURCE, currentDbVersion.toString()); |
| 298 | 0 | is = ConnectionFactory.class.getClassLoader().getResourceAsStream(updateFile); |
| 299 | 0 | if (is == null) { |
| 300 | 0 | throw new DatabaseException(String.format("Unable to load update file '%s'", updateFile)); |
| 301 | |
} |
| 302 | 0 | final String dbStructureUpdate = IOUtils.toString(is, "UTF-8"); |
| 303 | |
|
| 304 | 0 | Statement statement = null; |
| 305 | |
try { |
| 306 | 0 | statement = conn.createStatement(); |
| 307 | 0 | final boolean success = statement.execute(dbStructureUpdate); |
| 308 | 0 | if (!success && statement.getUpdateCount() <= 0) { |
| 309 | 0 | throw new DatabaseException(String.format("Unable to upgrade the database schema to %s", |
| 310 | 0 | currentDbVersion.toString())); |
| 311 | |
} |
| 312 | 0 | } catch (SQLException ex) { |
| 313 | 0 | LOGGER.debug("", ex); |
| 314 | 0 | throw new DatabaseException("Unable to update database schema", ex); |
| 315 | |
} finally { |
| 316 | 0 | DBUtils.closeStatement(statement); |
| 317 | 0 | } |
| 318 | 0 | } catch (IOException ex) { |
| 319 | 0 | final String msg = String.format("Upgrade SQL file does not exist: %s", updateFile); |
| 320 | 0 | throw new DatabaseException(msg, ex); |
| 321 | |
} finally { |
| 322 | 0 | IOUtils.closeQuietly(is); |
| 323 | 0 | } |
| 324 | 0 | } else { |
| 325 | 0 | final int e0 = Integer.parseInt(appExpectedVersion.getVersionParts().get(0)); |
| 326 | 0 | final int c0 = Integer.parseInt(currentDbVersion.getVersionParts().get(0)); |
| 327 | 0 | final int e1 = Integer.parseInt(appExpectedVersion.getVersionParts().get(1)); |
| 328 | 0 | final int c1 = Integer.parseInt(currentDbVersion.getVersionParts().get(1)); |
| 329 | 0 | if (e0 == c0 && e1 < c1) { |
| 330 | 0 | LOGGER.warn("A new version of dependency-check is available; consider upgrading"); |
| 331 | 0 | Settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false); |
| 332 | 0 | } else if (e0 == c0 && e1 == c1) { |
| 333 | |
|
| 334 | |
} else { |
| 335 | 0 | LOGGER.error("The database schema must be upgraded to use this version of dependency-check. Please see {} for more information.", |
| 336 | |
UPGRADE_HELP_URL); |
| 337 | 0 | throw new DatabaseException("Database schema is out of date"); |
| 338 | |
} |
| 339 | |
} |
| 340 | 0 | } |
| 341 | |
|
| 342 | |
|
| 343 | |
|
| 344 | |
|
| 345 | 2 | private static int callDepth = 0; |
| 346 | |
|
| 347 | |
|
| 348 | |
|
| 349 | |
|
| 350 | |
|
| 351 | |
|
| 352 | |
|
| 353 | |
private static void ensureSchemaVersion(Connection conn) throws DatabaseException { |
| 354 | 4 | ResultSet rs = null; |
| 355 | 4 | CallableStatement cs = null; |
| 356 | |
try { |
| 357 | |
|
| 358 | 4 | cs = conn.prepareCall("SELECT value FROM properties WHERE id = 'version'"); |
| 359 | 4 | rs = cs.executeQuery(); |
| 360 | 4 | if (rs.next()) { |
| 361 | 4 | final DependencyVersion appDbVersion = DependencyVersionUtil.parseVersion(DB_SCHEMA_VERSION); |
| 362 | 4 | final DependencyVersion db = DependencyVersionUtil.parseVersion(rs.getString(1)); |
| 363 | 4 | if (appDbVersion.compareTo(db) > 0) { |
| 364 | 0 | LOGGER.debug("Current Schema: {}", DB_SCHEMA_VERSION); |
| 365 | 0 | LOGGER.debug("DB Schema: {}", rs.getString(1)); |
| 366 | 0 | updateSchema(conn, appDbVersion, db); |
| 367 | 0 | if (++callDepth < 10) { |
| 368 | 0 | ensureSchemaVersion(conn); |
| 369 | |
} |
| 370 | |
} |
| 371 | 4 | } else { |
| 372 | 0 | throw new DatabaseException("Database schema is missing"); |
| 373 | |
} |
| 374 | 0 | } catch (SQLException ex) { |
| 375 | 0 | LOGGER.debug("", ex); |
| 376 | 0 | throw new DatabaseException("Unable to check the database schema version"); |
| 377 | |
} finally { |
| 378 | 4 | DBUtils.closeResultSet(rs); |
| 379 | 4 | DBUtils.closeStatement(cs); |
| 380 | 4 | } |
| 381 | 4 | } |
| 382 | |
} |