Coverage Report - org.owasp.dependencycheck.data.nvdcve.ConnectionFactory
 
Classes in this File Line Coverage Branch Coverage Complexity
ConnectionFactory
40%
68/168
32%
11/34
7.5
 
 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) 2014 Jeremy Long. All Rights Reserved.
 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  
  * Loads the configured database driver and returns the database connection. If the embedded H2 database is used obtaining a
 40  
  * connection will ensure the database file exists and that the appropriate table structure has been created.
 41  
  *
 42  
  * @author Jeremy Long
 43  
  */
 44  
 public final class ConnectionFactory {
 45  
 
 46  
     /**
 47  
      * The Logger.
 48  
      */
 49  1
     private static final Logger LOGGER = LoggerFactory.getLogger(ConnectionFactory.class);
 50  
     /**
 51  
      * The version of the current DB Schema.
 52  
      */
 53  1
     public static final String DB_SCHEMA_VERSION = Settings.getString(Settings.KEYS.DB_VERSION);
 54  
     /**
 55  
      * Resource location for SQL file used to create the database schema.
 56  
      */
 57  
     public static final String DB_STRUCTURE_RESOURCE = "data/initialize.sql";
 58  
     /**
 59  
      * Resource location for SQL file used to create the database schema.
 60  
      */
 61  
     public static final String DB_STRUCTURE_UPDATE_RESOURCE = "data/upgrade_%s.sql";
 62  
     /**
 63  
      * The URL that discusses upgrading non-H2 databases.
 64  
      */
 65  
     public static final String UPGRADE_HELP_URL = "http://jeremylong.github.io/DependencyCheck/data/upgrade.html";
 66  
     /**
 67  
      * The database driver used to connect to the database.
 68  
      */
 69  1
     private static Driver driver = null;
 70  
     /**
 71  
      * The database connection string.
 72  
      */
 73  1
     private static String connectionString = null;
 74  
     /**
 75  
      * The username to connect to the database.
 76  
      */
 77  1
     private static String userName = null;
 78  
     /**
 79  
      * The password for the database.
 80  
      */
 81  1
     private static String password = null;
 82  
 
 83  
     /**
 84  
      * Private constructor for this factory class; no instance is ever needed.
 85  
      */
 86  0
     private ConnectionFactory() {
 87  0
     }
 88  
 
 89  
     /**
 90  
      * Initializes the connection factory. Ensuring that the appropriate drivers are loaded and that a connection can be made
 91  
      * successfully.
 92  
      *
 93  
      * @throws DatabaseException thrown if we are unable to connect to the database
 94  
      */
 95  
     public static synchronized void initialize() throws DatabaseException {
 96  
         //this only needs to be called once.
 97  11
         if (connectionString != null) {
 98  9
             return;
 99  
         }
 100  2
         Connection conn = null;
 101  
         try {
 102  
             //load the driver if necessary
 103  2
             final String driverName = Settings.getString(Settings.KEYS.DB_DRIVER_NAME, "");
 104  2
             if (!driverName.isEmpty()) { //likely need to load the correct driver
 105  2
                 LOGGER.debug("Loading driver: {}", driverName);
 106  2
                 final String driverPath = Settings.getString(Settings.KEYS.DB_DRIVER_PATH, "");
 107  
                 try {
 108  2
                     if (!driverPath.isEmpty()) {
 109  0
                         LOGGER.debug("Loading driver from: {}", driverPath);
 110  0
                         driver = DriverLoader.load(driverName, driverPath);
 111  
                     } else {
 112  2
                         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  2
                 }
 118  
             }
 119  2
             userName = Settings.getString(Settings.KEYS.DB_USER, "dcuser");
 120  
             //yes, yes - hard-coded password - only if there isn't one in the properties file.
 121  2
             password = Settings.getString(Settings.KEYS.DB_PASSWORD, "DC-Pass1337!");
 122  
             try {
 123  2
                 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  2
             }
 131  2
             boolean shouldCreateSchema = false;
 132  
             try {
 133  2
                 if (connectionString.startsWith("jdbc:h2:file:")) { //H2
 134  2
                     shouldCreateSchema = !h2DataFileExists();
 135  2
                     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  2
             }
 141  2
             LOGGER.debug("Loading database connection");
 142  2
             LOGGER.debug("Connection String: {}", connectionString);
 143  2
             LOGGER.debug("Database User: {}", userName);
 144  
 
 145  
             try {
 146  2
                 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  2
             }
 164  
 
 165  2
             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  2
                 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  2
             }
 179  
         } finally {
 180  2
             if (conn != null) {
 181  
                 try {
 182  2
                     conn.close();
 183  0
                 } catch (SQLException ex) {
 184  0
                     LOGGER.debug("An error occurred closing the connection", ex);
 185  2
                 }
 186  
             }
 187  
         }
 188  2
     }
 189  
 
 190  
     /**
 191  
      * Cleans up resources and unloads any registered database drivers. This needs to be called to ensure the driver is
 192  
      * unregistered prior to the finalize method being called as during shutdown the class loader used to load the driver may be
 193  
      * unloaded prior to the driver being de-registered.
 194  
      */
 195  
     public static synchronized void cleanup() {
 196  1
         if (driver != null) {
 197  
             try {
 198  1
                 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  1
             }
 205  1
             driver = null;
 206  
         }
 207  1
         connectionString = null;
 208  1
         userName = null;
 209  1
         password = null;
 210  1
     }
 211  
 
 212  
     /**
 213  
      * Constructs a new database connection object per the database configuration.
 214  
      *
 215  
      * @return a database connection object
 216  
      * @throws DatabaseException thrown if there is an exception loading the database connection
 217  
      */
 218  
     public static Connection getConnection() throws DatabaseException {
 219  7
         initialize();
 220  7
         Connection conn = null;
 221  
         try {
 222  7
             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  7
         }
 227  7
         return conn;
 228  
     }
 229  
 
 230  
     /**
 231  
      * Determines if the H2 database file exists. If it does not exist then the data structure will need to be created.
 232  
      *
 233  
      * @return true if the H2 database file does not exist; otherwise false
 234  
      * @throws IOException thrown if the data directory does not exist and cannot be created
 235  
      */
 236  
     private static boolean h2DataFileExists() throws IOException {
 237  2
         final File dir = Settings.getDataDirectory();
 238  2
         final String fileName = Settings.getString(Settings.KEYS.DB_FILE_NAME);
 239  2
         final File file = new File(dir, fileName);
 240  2
         return file.exists();
 241  
     }
 242  
 
 243  
     /**
 244  
      * Creates the database structure (tables and indexes) to store the CVE data.
 245  
      *
 246  
      * @param conn the database connection
 247  
      * @throws DatabaseException thrown if there is a Database Exception
 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  
      * Updates the database schema by loading the upgrade script for the version specified. The intended use is that if the
 275  
      * current schema version is 2.9 then we would call updateSchema(conn, "2.9"). This would load the upgrade_2.9.sql file and
 276  
      * execute it against the database. The upgrade script must update the 'version' in the properties table.
 277  
      *
 278  
      * @param conn the database connection object
 279  
      * @param schema the current schema version that is being upgraded
 280  
      * @throws DatabaseException thrown if there is an exception upgrading the database schema
 281  
      */
 282  
     private static void updateSchema(Connection conn, String schema) throws DatabaseException {
 283  
         final String databaseProductName;
 284  
         try {
 285  0
             databaseProductName = conn.getMetaData().getDatabaseProductName();
 286  0
         } catch (SQLException ex) {
 287  0
             throw new DatabaseException("Unable to get the database product name");
 288  0
         }
 289  0
         if ("h2".equalsIgnoreCase(databaseProductName)) {
 290  0
             LOGGER.debug("Updating database structure");
 291  0
             InputStream is = null;
 292  0
             String updateFile = null;
 293  
             try {
 294  0
                 updateFile = String.format(DB_STRUCTURE_UPDATE_RESOURCE, schema);
 295  0
                 is = ConnectionFactory.class.getClassLoader().getResourceAsStream(updateFile);
 296  0
                 if (is == null) {
 297  0
                     throw new DatabaseException(String.format("Unable to load update file '%s'", updateFile));
 298  
                 }
 299  0
                 final String dbStructureUpdate = IOUtils.toString(is, "UTF-8");
 300  
 
 301  0
                 Statement statement = null;
 302  
                 try {
 303  0
                     statement = conn.createStatement();
 304  0
                     final boolean success = statement.execute(dbStructureUpdate);
 305  0
                     if (!success && statement.getUpdateCount() <= 0) {
 306  0
                         throw new DatabaseException(String.format("Unable to upgrade the database schema to %s", schema));
 307  
                     }
 308  0
                 } catch (SQLException ex) {
 309  0
                     LOGGER.debug("", ex);
 310  0
                     throw new DatabaseException("Unable to update database schema", ex);
 311  
                 } finally {
 312  0
                     DBUtils.closeStatement(statement);
 313  0
                 }
 314  0
             } catch (IOException ex) {
 315  0
                 final String msg = String.format("Upgrade SQL file does not exist: %s", updateFile);
 316  0
                 throw new DatabaseException(msg, ex);
 317  
             } finally {
 318  0
                 IOUtils.closeQuietly(is);
 319  0
             }
 320  0
         } else {
 321  0
             LOGGER.error("The database schema must be upgraded to use this version of dependency-check. Please see {} for more information.", UPGRADE_HELP_URL);
 322  0
             throw new DatabaseException("Database schema is out of date");
 323  
         }
 324  0
     }
 325  
 
 326  
     /**
 327  
      * Counter to ensure that calls to ensureSchemaVersion does not end up in an endless loop.
 328  
      */
 329  1
     private static int callDepth = 0;
 330  
 
 331  
     /**
 332  
      * Uses the provided connection to check the specified schema version within the database.
 333  
      *
 334  
      * @param conn the database connection object
 335  
      * @throws DatabaseException thrown if the schema version is not compatible with this version of dependency-check
 336  
      */
 337  
     private static void ensureSchemaVersion(Connection conn) throws DatabaseException {
 338  2
         ResultSet rs = null;
 339  2
         CallableStatement cs = null;
 340  
         try {
 341  
             //TODO convert this to use DatabaseProperties
 342  2
             cs = conn.prepareCall("SELECT value FROM properties WHERE id = 'version'");
 343  2
             rs = cs.executeQuery();
 344  2
             if (rs.next()) {
 345  2
                 final DependencyVersion current = DependencyVersionUtil.parseVersion(DB_SCHEMA_VERSION);
 346  2
                 final DependencyVersion db = DependencyVersionUtil.parseVersion(rs.getString(1));
 347  2
                 if (current.compareTo(db) > 0) {
 348  0
                     LOGGER.debug("Current Schema: " + DB_SCHEMA_VERSION);
 349  0
                     LOGGER.debug("DB Schema: " + rs.getString(1));
 350  0
                     updateSchema(conn, rs.getString(1));
 351  0
                     if (++callDepth < 10) {
 352  0
                         ensureSchemaVersion(conn);
 353  
                     }
 354  
                 }
 355  2
             } else {
 356  0
                 throw new DatabaseException("Database schema is missing");
 357  
             }
 358  0
         } catch (SQLException ex) {
 359  0
             LOGGER.debug("", ex);
 360  0
             throw new DatabaseException("Unable to check the database schema version");
 361  
         } finally {
 362  2
             DBUtils.closeResultSet(rs);
 363  2
             DBUtils.closeStatement(cs);
 364  2
         }
 365  2
     }
 366  
 }