DatabaseManager.java

  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. import com.google.common.io.Resources;
  20. import java.io.File;
  21. import java.io.IOException;
  22. import java.io.InputStream;
  23. import java.net.URL;
  24. import java.nio.charset.StandardCharsets;
  25. import java.sql.PreparedStatement;
  26. import java.sql.Connection;
  27. import java.sql.Driver;
  28. import java.sql.DriverManager;
  29. import java.sql.ResultSet;
  30. import java.sql.SQLException;
  31. import java.sql.Statement;
  32. import java.util.Locale;
  33. import java.util.ResourceBundle;
  34. import javax.annotation.concurrent.ThreadSafe;
  35. import org.anarres.jdiagnostics.DefaultQuery;
  36. import org.apache.commons.dbcp2.BasicDataSource;
  37. import org.apache.commons.io.IOUtils;
  38. import org.owasp.dependencycheck.utils.DBUtils;
  39. import org.owasp.dependencycheck.utils.DependencyVersion;
  40. import org.owasp.dependencycheck.utils.DependencyVersionUtil;
  41. import org.owasp.dependencycheck.utils.FileUtils;
  42. import org.owasp.dependencycheck.utils.Settings;
  43. import org.slf4j.Logger;
  44. import org.slf4j.LoggerFactory;

  45. /**
  46.  * Loads the configured database driver and returns the database connection. If
  47.  * the embedded H2 database is used obtaining a connection will ensure the
  48.  * database file exists and that the appropriate table structure has been
  49.  * created.
  50.  *
  51.  * @author Jeremy Long
  52.  */
  53. @ThreadSafe
  54. public final class DatabaseManager {

  55.     /**
  56.      * The Logger.
  57.      */
  58.     private static final Logger LOGGER = LoggerFactory.getLogger(DatabaseManager.class);
  59.     /**
  60.      * Resource location for SQL file used to create the database schema.
  61.      */
  62.     public static final String DB_STRUCTURE_RESOURCE = "data/initialize.sql";
  63.     /**
  64.      * Resource location for SQL file used to create the database schema.
  65.      */
  66.     public static final String DB_STRUCTURE_UPDATE_RESOURCE = "data/upgrade_%s.sql";
  67.     /**
  68.      * The URL that discusses upgrading non-H2 databases.
  69.      */
  70.     public static final String UPGRADE_HELP_URL = "https://jeremylong.github.io/DependencyCheck/data/upgrade.html";
  71.     /**
  72.      * The database driver used to connect to the database.
  73.      */
  74.     private Driver driver = null;
  75.     /**
  76.      * The database connection string.
  77.      */
  78.     private String connectionString = null;
  79.     /**
  80.      * The username to connect to the database.
  81.      */
  82.     private String userName = null;
  83.     /**
  84.      * The password for the database.
  85.      */
  86.     private String password = null;
  87.     /**
  88.      * Counter to ensure that calls to ensureSchemaVersion does not end up in an
  89.      * endless loop.
  90.      */
  91.     private int callDepth = 0;
  92.     /**
  93.      * The configured settings.
  94.      */
  95.     private final Settings settings;
  96.     /**
  97.      * Flag indicating if the database connection is for an H2 database.
  98.      */
  99.     private boolean isH2;
  100.     /**
  101.      * Flag indicating if the database connection is for an Oracle database.
  102.      */
  103.     private boolean isOracle;
  104.     /**
  105.      * The database product name.
  106.      */
  107.     private String databaseProductName;
  108.     /**
  109.      * The database connection pool.
  110.      */
  111.     private BasicDataSource connectionPool;

  112.     /**
  113.      * Private constructor for this factory class; no instance is ever needed.
  114.      *
  115.      * @param settings the configured settings
  116.      * @throws DatabaseException thrown if we are unable to connect to the
  117.      * database
  118.      */
  119.     public DatabaseManager(Settings settings) throws DatabaseException {
  120.         this.settings = settings;
  121.         initialize();
  122.     }

  123.     /**
  124.      * Initializes the connection factory. Ensuring that the appropriate drivers
  125.      * are loaded and that a connection can be made successfully.
  126.      *
  127.      * @throws DatabaseException thrown if we are unable to connect to the
  128.      * database
  129.      */
  130.     private void initialize() throws DatabaseException {
  131.         final boolean autoUpdate = settings.getBoolean(Settings.KEYS.AUTO_UPDATE, true);
  132.         Connection conn = null;
  133.         try {
  134.             //load the driver if necessary
  135.             final String driverName = settings.getString(Settings.KEYS.DB_DRIVER_NAME, "");
  136.             if (!driverName.isEmpty()) {
  137.                 final String driverPath = settings.getString(Settings.KEYS.DB_DRIVER_PATH, "");
  138.                 LOGGER.debug("Loading driver '{}'", driverName);
  139.                 try {
  140.                     if (!driverPath.isEmpty()) {
  141.                         LOGGER.debug("Loading driver from: {}", driverPath);
  142.                         driver = DriverLoader.load(driverName, driverPath);
  143.                     } else {
  144.                         driver = DriverLoader.load(driverName);
  145.                     }
  146.                 } catch (DriverLoadException ex) {
  147.                     LOGGER.debug("Unable to load database driver", ex);
  148.                     throw new DatabaseException("Unable to load database driver", ex);
  149.                 }
  150.             }
  151.             userName = settings.getString(Settings.KEYS.DB_USER, "dcuser");
  152.             //yes, yes - hard-coded password - only if there isn't one in the properties file.
  153.             password = settings.getString(Settings.KEYS.DB_PASSWORD, "DC-Pass1337!");
  154.             try {
  155.                 connectionString = settings.getConnectionString(
  156.                         Settings.KEYS.DB_CONNECTION_STRING,
  157.                         Settings.KEYS.DB_FILE_NAME);
  158.             } catch (IOException ex) {
  159.                 LOGGER.debug("Unable to retrieve the database connection string", ex);
  160.                 throw new DatabaseException("Unable to retrieve the database connection string", ex);
  161.             }
  162.             isH2 = isH2Connection(connectionString);
  163.             boolean shouldCreateSchema = false;
  164.             try {
  165.                 if (autoUpdate && isH2) {
  166.                     shouldCreateSchema = !h2DataFileExists();
  167.                     LOGGER.debug("Need to create DB Structure: {}", shouldCreateSchema);
  168.                 }
  169.             } catch (IOException ioex) {
  170.                 LOGGER.debug("Unable to verify database exists", ioex);
  171.                 throw new DatabaseException("Unable to verify database exists", ioex);
  172.             }
  173.             LOGGER.debug("Loading database connection");
  174.             LOGGER.debug("Connection String: {}", connectionString);
  175.             LOGGER.debug("Database User: {}", userName);

  176.             try {
  177.                 if (connectionString.toLowerCase().contains("integrated security=true")
  178.                         || connectionString.toLowerCase().contains("trusted_connection=true")) {
  179.                     conn = DriverManager.getConnection(connectionString);
  180.                 } else {
  181.                     conn = DriverManager.getConnection(connectionString, userName, password);
  182.                 }
  183.             } catch (SQLException ex) {
  184.                 if (ex.getMessage().contains("java.net.UnknownHostException") && connectionString.contains("AUTO_SERVER=TRUE;")) {
  185.                     connectionString = connectionString.replace("AUTO_SERVER=TRUE;", "");
  186.                     try {
  187.                         conn = DriverManager.getConnection(connectionString, userName, password);
  188.                         settings.setString(Settings.KEYS.DB_CONNECTION_STRING, connectionString);
  189.                         LOGGER.debug("Unable to start the database in server mode; reverting to single user mode");
  190.                     } catch (SQLException sqlex) {
  191.                         LOGGER.debug("Unable to connect to the database", ex);
  192.                         throw new DatabaseException("Unable to connect to the database", ex);
  193.                     }
  194.                 } else if (isH2 && ex.getMessage().contains("file version or invalid file header")) {
  195.                     LOGGER.error("Incompatible or corrupt database found. To resolve this issue please remove the existing "
  196.                             + "database by running purge");
  197.                     throw new DatabaseException("Incompatible or corrupt database found; run the purge command to resolve the issue");
  198.                 } else {
  199.                     LOGGER.debug("Unable to connect to the database", ex);
  200.                     throw new DatabaseException("Unable to connect to the database", ex);
  201.                 }
  202.             }
  203.             databaseProductName = determineDatabaseProductName(conn);
  204.             isOracle = "oracle".equals(databaseProductName);
  205.             if (shouldCreateSchema) {
  206.                 try {
  207.                     createTables(conn);
  208.                 } catch (DatabaseException dex) {
  209.                     LOGGER.debug("", dex);
  210.                     throw new DatabaseException("Unable to create the database structure", dex);
  211.                 }
  212.             }
  213.             try {
  214.                 ensureSchemaVersion(conn);
  215.             } catch (DatabaseException dex) {
  216.                 LOGGER.debug("", dex);
  217.                 throw new DatabaseException("Database schema does not match this version of dependency-check", dex);
  218.             }
  219.         } finally {
  220.             if (conn != null) {
  221.                 try {
  222.                     conn.close();
  223.                 } catch (SQLException ex) {
  224.                     LOGGER.debug("An error occurred closing the connection", ex);
  225.                 }
  226.             }
  227.         }
  228.     }

  229.     /**
  230.      * Tries to determine the product name of the database.
  231.      *
  232.      * @param conn the database connection
  233.      * @return the product name of the database if successful, {@code null} else
  234.      */
  235.     private String determineDatabaseProductName(Connection conn) {
  236.         try {
  237.             final String databaseProductName = conn.getMetaData().getDatabaseProductName().toLowerCase();
  238.             LOGGER.debug("Database product: {}", databaseProductName);
  239.             return databaseProductName;
  240.         } catch (SQLException se) {
  241.             LOGGER.warn("Problem determining database product!", se);
  242.             return null;
  243.         }
  244.     }

  245.     /**
  246.      * Cleans up resources and unloads any registered database drivers. This
  247.      * needs to be called to ensure the driver is unregistered prior to the
  248.      * finalize method being called as during shutdown the class loader used to
  249.      * load the driver may be unloaded prior to the driver being de-registered.
  250.      */
  251.     public void cleanup() {
  252.         if (driver != null) {
  253.             DriverLoader.cleanup(driver);
  254.             driver = null;
  255.         }
  256.         connectionString = null;
  257.         userName = null;
  258.         password = null;
  259.     }

  260.     /**
  261.      * Determines if the H2 database file exists. If it does not exist then the
  262.      * data structure will need to be created.
  263.      *
  264.      * @return true if the H2 database file does not exist; otherwise false
  265.      * @throws IOException thrown if the data directory does not exist and
  266.      * cannot be created
  267.      */
  268.     public boolean h2DataFileExists() throws IOException {
  269.         return h2DataFileExists(settings);
  270.     }

  271.     /**
  272.      * Determines if the H2 database file exists. If it does not exist then the
  273.      * data structure will need to be created.
  274.      *
  275.      * @param configuration the configured settings
  276.      * @return true if the H2 database file does not exist; otherwise false
  277.      * @throws IOException thrown if the data directory does not exist and
  278.      * cannot be created
  279.      */
  280.     public static boolean h2DataFileExists(Settings configuration) throws IOException {
  281.         final File file = getH2DataFile(configuration);
  282.         return file.exists();
  283.     }

  284.     /**
  285.      * Returns a reference to the H2 database file.
  286.      *
  287.      * @param configuration the configured settings
  288.      * @return the path to the H2 database file
  289.      * @throws IOException thrown if there is an error
  290.      */
  291.     public static File getH2DataFile(Settings configuration) throws IOException {
  292.         final File dir = configuration.getH2DataDirectory();
  293.         final String fileName = configuration.getString(Settings.KEYS.DB_FILE_NAME);
  294.         return new File(dir, fileName);
  295.     }

  296.     /**
  297.      * Returns the database product name.
  298.      *
  299.      * @return the database product name
  300.      */
  301.     public String getDatabaseProductName() {
  302.         return databaseProductName;
  303.     }

  304.     /**
  305.      * Determines if the connection string is for an H2 database.
  306.      *
  307.      * @return true if the connection string is for an H2 database
  308.      */
  309.     public boolean isH2Connection() {
  310.         return isH2;
  311.     }

  312.     /**
  313.      * Determines if the connection string is for an Oracle database.
  314.      *
  315.      * @return true if the connection string is for an Oracle database
  316.      */
  317.     public boolean isOracle() {
  318.         return isOracle;
  319.     }

  320.     /**
  321.      * Determines if the connection string is for an H2 database.
  322.      *
  323.      * @param configuration the configured settings
  324.      * @return true if the connection string is for an H2 database
  325.      */
  326.     public static boolean isH2Connection(Settings configuration) {
  327.         final String connStr;
  328.         try {
  329.             connStr = configuration.getConnectionString(
  330.                     Settings.KEYS.DB_CONNECTION_STRING,
  331.                     Settings.KEYS.DB_FILE_NAME);
  332.         } catch (IOException ex) {
  333.             LOGGER.debug("Unable to get connectionn string", ex);
  334.             return false;
  335.         }
  336.         return isH2Connection(connStr);
  337.     }

  338.     /**
  339.      * Determines if the connection string is for an H2 database.
  340.      *
  341.      * @param connectionString the connection string
  342.      * @return true if the connection string is for an H2 database
  343.      */
  344.     public static boolean isH2Connection(String connectionString) {
  345.         return connectionString.startsWith("jdbc:h2:file:");
  346.     }

  347.     /**
  348.      * Creates the database structure (tables and indexes) to store the CVE
  349.      * data.
  350.      *
  351.      * @param conn the database connection
  352.      * @throws DatabaseException thrown if there is a Database Exception
  353.      */
  354.     private void createTables(Connection conn) throws DatabaseException {
  355.         LOGGER.debug("Creating database structure");
  356.         final String dbStructure;
  357.         try {
  358.             dbStructure = getResource(DB_STRUCTURE_RESOURCE);

  359.             Statement statement = null;
  360.             try {
  361.                 statement = conn.createStatement();
  362.                 statement.execute(dbStructure);
  363.             } catch (SQLException ex) {
  364.                 LOGGER.debug("", ex);
  365.                 throw new DatabaseException("Unable to create database statement", ex);
  366.             } finally {
  367.                 DBUtils.closeStatement(statement);
  368.             }
  369.         } catch (IOException ex) {
  370.             throw new DatabaseException("Unable to create database schema", ex);
  371.         } catch (LinkageError ex) {
  372.             LOGGER.debug(new DefaultQuery(ex).call().toString());
  373.         }
  374.     }

  375.     private String getResource(String resource) throws IOException {
  376.         String dbStructure;
  377.         try {
  378.             final URL url = Resources.getResource(resource);
  379.             dbStructure = Resources.toString(url, StandardCharsets.UTF_8);
  380.         } catch (IllegalArgumentException ex) {
  381.             LOGGER.debug("Resources.getResource(String) failed to find the DB Structure Resource", ex);
  382.             try (InputStream is = FileUtils.getResourceAsStream(resource)) {
  383.                 dbStructure = IOUtils.toString(is, StandardCharsets.UTF_8);
  384.             }
  385.         }
  386.         return dbStructure;
  387.     }

  388.     /**
  389.      * Updates the database schema by loading the upgrade script for the version
  390.      * specified. The intended use is that if the current schema version is 2.9
  391.      * then we would call updateSchema(conn, "2.9"). This would load the
  392.      * upgrade_2.9.sql file and execute it against the database. The upgrade
  393.      * script must update the 'version' in the properties table.
  394.      *
  395.      * @param conn the database connection object
  396.      * @param appExpectedVersion the schema version that the application expects
  397.      * @param currentDbVersion the current schema version of the database
  398.      * @throws DatabaseException thrown if there is an exception upgrading the
  399.      * database schema
  400.      */
  401.     private void updateSchema(Connection conn, DependencyVersion appExpectedVersion, DependencyVersion currentDbVersion)
  402.             throws DatabaseException {

  403.         if (connectionString.startsWith("jdbc:h2:file:")) {
  404.             LOGGER.debug("Updating database structure");
  405.             final String updateFile = String.format(DB_STRUCTURE_UPDATE_RESOURCE, currentDbVersion.toString());
  406.             if ("data/upgrade_4.2.sql".equals(updateFile) && !FileUtils.getResourceAsFile(updateFile).exists()) {
  407.                 throw new DatabaseException("unable to upgrade the database schema - please run the dependency-check "
  408.                         + "purge command to remove the existing database");
  409.             }
  410.             try {
  411.                 final String dbStructureUpdate = getResource(updateFile);
  412.                 Statement statement = null;
  413.                 try {
  414.                     statement = conn.createStatement();
  415.                     statement.execute(dbStructureUpdate);
  416.                 } catch (SQLException ex) {
  417.                     throw new DatabaseException(String.format("Unable to upgrade the database schema from %s to %s",
  418.                             currentDbVersion, appExpectedVersion.toString()), ex);
  419.                 } finally {
  420.                     DBUtils.closeStatement(statement);
  421.                 }
  422.             } catch (IllegalArgumentException | IOException ex) {
  423.                 final String msg = String.format("Upgrade SQL file does not exist: %s", updateFile);
  424.                 throw new DatabaseException(msg, ex);
  425.             }
  426.         } else {
  427.             final int e0 = Integer.parseInt(appExpectedVersion.getVersionParts().get(0));
  428.             final int c0 = Integer.parseInt(currentDbVersion.getVersionParts().get(0));
  429.             final int e1 = Integer.parseInt(appExpectedVersion.getVersionParts().get(1));
  430.             final int c1 = Integer.parseInt(currentDbVersion.getVersionParts().get(1));
  431.             //CSOFF: EmptyBlock
  432.             if (e0 == c0 && e1 < c1) {
  433.                 LOGGER.warn("A new version of dependency-check is available; consider upgrading");
  434.                 settings.setBoolean(Settings.KEYS.AUTO_UPDATE, false);
  435.             } else if (e0 == c0 && e1 == c1) {
  436.                 //do nothing - not sure how we got here, but just in case...
  437.             } else {
  438.                 LOGGER.error("The database schema must be upgraded to use this version of dependency-check. Please see {} for more information.",
  439.                         UPGRADE_HELP_URL);
  440.                 throw new DatabaseException("Database schema is out of date");
  441.             }
  442.             //CSON: EmptyBlock
  443.         }
  444.     }

  445.     /**
  446.      * Returns a resource bundle containing the SQL Statements needed for the
  447.      * database engine being used.
  448.      *
  449.      * @return a resource bundle containing the SQL Statements
  450.      */
  451.     public ResourceBundle getSqlStatements() {
  452.         final ResourceBundle statementBundle = getDatabaseProductName() != null
  453.                 ? ResourceBundle.getBundle("data/dbStatements", new Locale(getDatabaseProductName()))
  454.                 : ResourceBundle.getBundle("data/dbStatements");
  455.         return statementBundle;
  456.     }

  457.     /**
  458.      * Uses the provided connection to check the specified schema version within
  459.      * the database.
  460.      *
  461.      * @param conn the database connection object
  462.      * @throws DatabaseException thrown if the schema version is not compatible
  463.      * with this version of dependency-check
  464.      */
  465.     private void ensureSchemaVersion(Connection conn) throws DatabaseException {
  466.         ResultSet rs = null;
  467.         PreparedStatement ps = null;
  468.         final ResourceBundle statementBundle = getSqlStatements();
  469.         final String sql = statementBundle.getString("SELECT_SCHEMA_VERSION");
  470.         try {
  471.             ps = conn.prepareStatement(sql);
  472.             rs = ps.executeQuery();
  473.             if (rs.next()) {
  474.                 final String dbSchemaVersion = settings.getString(Settings.KEYS.DB_VERSION);
  475.                 final DependencyVersion appDbVersion = DependencyVersionUtil.parseVersion(dbSchemaVersion);
  476.                 if (appDbVersion == null) {
  477.                     throw new DatabaseException("Invalid application database schema");
  478.                 }
  479.                 final DependencyVersion db = DependencyVersionUtil.parseVersion(rs.getString(1));
  480.                 if (db == null) {
  481.                     throw new DatabaseException("Invalid database schema");
  482.                 }
  483.                 LOGGER.debug("DC Schema: {}", appDbVersion);
  484.                 LOGGER.debug("DB Schema: {}", db);
  485.                 if (appDbVersion.compareTo(db) > 0) {
  486.                     final boolean autoUpdate = settings.getBoolean(Settings.KEYS.AUTO_UPDATE, true);
  487.                     if (autoUpdate) {
  488.                         updateSchema(conn, appDbVersion, db);
  489.                         if (++callDepth < 10) {
  490.                             ensureSchemaVersion(conn);
  491.                         }
  492.                     } else {
  493.                         throw new DatabaseException("Old database schema identified - please execute "
  494.                                 + "dependency-check without the no-update configuration to continue");
  495.                     }
  496.                 }
  497.             } else {
  498.                 throw new DatabaseException("Database schema is missing");
  499.             }
  500.         } catch (SQLException ex) {
  501.             LOGGER.debug("", ex);
  502.             throw new DatabaseException("Unable to check the database schema version", ex);
  503.         } finally {
  504.             DBUtils.closeResultSet(rs);
  505.             DBUtils.closeStatement(ps);
  506.         }
  507.     }

  508.     /**
  509.      * Opens the database connection pool.
  510.      */
  511.     public void open() {
  512.         connectionPool = new BasicDataSource();
  513.         if (driver != null) {
  514.             connectionPool.setDriver(driver);
  515.         }
  516.         connectionPool.setUrl(connectionString);
  517.         connectionPool.setUsername(userName);
  518.         connectionPool.setPassword(password);
  519.     }

  520.     /**
  521.      * Closes the database connection pool.
  522.      */
  523.     public void close() {
  524.         try {
  525.             connectionPool.close();
  526.         } catch (SQLException ex) {
  527.             LOGGER.debug("Error closing the connection pool", ex);
  528.         }
  529.         connectionPool = null;
  530.     }

  531.     /**
  532.      * Returns if the connection pool is open.
  533.      *
  534.      * @return if the connection pool is open
  535.      */
  536.     public boolean isOpen() {
  537.         return connectionPool != null;
  538.     }

  539.     /**
  540.      * Constructs a new database connection object per the database
  541.      * configuration.
  542.      *
  543.      * @return a database connection object
  544.      * @throws DatabaseException thrown if there is an exception obtaining the
  545.      * database connection
  546.      */
  547.     public Connection getConnection() throws DatabaseException {
  548.         try {
  549.             return connectionPool.getConnection();
  550.         } catch (SQLException ex) {
  551.             throw new DatabaseException("Error connecting to the database", ex);
  552.         }
  553.     }
  554. }