Coverage Report - org.owasp.dependencycheck.data.update.EngineVersionCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EngineVersionCheck
51%
40/78
53%
14/26
4.286
 
 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.update;
 19  
 
 20  
 import java.io.IOException;
 21  
 import java.net.HttpURLConnection;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.util.Date;
 25  
 import org.apache.commons.io.IOUtils;
 26  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 27  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 28  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 29  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 30  
 import org.owasp.dependencycheck.utils.DateUtil;
 31  
 import org.owasp.dependencycheck.utils.DependencyVersion;
 32  
 import org.owasp.dependencycheck.utils.Settings;
 33  
 import org.owasp.dependencycheck.utils.URLConnectionFactory;
 34  
 import org.owasp.dependencycheck.utils.URLConnectionFailureException;
 35  
 import org.slf4j.Logger;
 36  
 import org.slf4j.LoggerFactory;
 37  
 
 38  
 /**
 39  
  *
 40  
  * @author Jeremy Long
 41  
  */
 42  16
 public class EngineVersionCheck implements CachedWebDataSource {
 43  
 
 44  
     /**
 45  
      * Static logger.
 46  
      */
 47  8
     private static final Logger LOGGER = LoggerFactory.getLogger(EngineVersionCheck.class);
 48  
     /**
 49  
      * The property key indicating when the last version check occurred.
 50  
      */
 51  
     public static final String ENGINE_VERSION_CHECKED_ON = "VersionCheckOn";
 52  
     /**
 53  
      * The property key indicating when the last version check occurred.
 54  
      */
 55  
     public static final String CURRENT_ENGINE_RELEASE = "CurrentEngineRelease";
 56  
     /**
 57  
      * Reference to the Cve Database.
 58  
      */
 59  16
     private CveDB cveDB = null;
 60  
 
 61  
     /**
 62  
      * The version retrieved from the database properties or web to check against.
 63  
      */
 64  
     private String updateToVersion;
 65  
 
 66  
     /**
 67  
      * Getter for updateToVersion - only used for testing. Represents the version retrieved from the database.
 68  
      *
 69  
      * @return the version to test
 70  
      */
 71  
     protected String getUpdateToVersion() {
 72  0
         return updateToVersion;
 73  
     }
 74  
 
 75  
     /**
 76  
      * Setter for updateToVersion - only used for testing. Represents the version retrieved from the database.
 77  
      *
 78  
      * @param version the version to test
 79  
      */
 80  
     protected void setUpdateToVersion(String version) {
 81  31
         updateToVersion = version;
 82  31
     }
 83  
 
 84  
     @Override
 85  
     public void update() throws UpdateException {
 86  
         try {
 87  0
             openDatabase();
 88  0
             LOGGER.debug("Begin Engine Version Check");
 89  0
             final DatabaseProperties properties = cveDB.getDatabaseProperties();
 90  0
             final long lastChecked = Long.parseLong(properties.getProperty(ENGINE_VERSION_CHECKED_ON, "0"));
 91  0
             final long now = (new Date()).getTime();
 92  0
             updateToVersion = properties.getProperty(CURRENT_ENGINE_RELEASE, "");
 93  0
             final String currentVersion = Settings.getString(Settings.KEYS.APPLICATION_VERSION, "0.0.0");
 94  0
             LOGGER.debug("Last checked: {}", lastChecked);
 95  0
             LOGGER.debug("Now: {}", now);
 96  0
             LOGGER.debug("Current version: {}", currentVersion);
 97  0
             final boolean updateNeeded = shouldUpdate(lastChecked, now, properties, currentVersion);
 98  0
             if (updateNeeded) {
 99  0
                 LOGGER.warn("A new version of dependency-check is available. Consider updating to version {}.",
 100  
                         updateToVersion);
 101  
             }
 102  0
         } catch (DatabaseException ex) {
 103  0
             LOGGER.debug("Database Exception opening databases to retrieve properties", ex);
 104  0
             throw new UpdateException("Error occured updating database properties.");
 105  
         } finally {
 106  0
             closeDatabase();
 107  0
         }
 108  0
     }
 109  
 
 110  
     /**
 111  
      * Determines if a new version of the dependency-check engine has been released.
 112  
      *
 113  
      * @param lastChecked the epoch time of the last version check
 114  
      * @param now the current epoch time
 115  
      * @param properties the database properties object
 116  
      * @param currentVersion the current version of dependency-check
 117  
      * @return <code>true</code> if a newer version of the database has been released; otherwise <code>false</code>
 118  
      * @throws UpdateException thrown if there is an error connecting to the github documentation site or accessing the local
 119  
      * database.
 120  
      */
 121  
     protected boolean shouldUpdate(final long lastChecked, final long now, final DatabaseProperties properties,
 122  
             String currentVersion) throws UpdateException {
 123  
         //check every 30 days if we know there is an update, otherwise check every 7 days
 124  31
         int checkRange = 30;
 125  31
         if (updateToVersion.isEmpty()) {
 126  6
             checkRange = 7;
 127  
         }
 128  31
         if (!DateUtil.withinDateRange(lastChecked, now, checkRange)) {
 129  11
             LOGGER.debug("Checking web for new version.");
 130  11
             final String currentRelease = getCurrentReleaseVersion();
 131  11
             if (currentRelease != null) {
 132  6
                 final DependencyVersion v = new DependencyVersion(currentRelease);
 133  6
                 if (v.getVersionParts() != null && v.getVersionParts().size() >= 3) {
 134  6
                     updateToVersion = v.toString();
 135  6
                     if (!currentRelease.equals(updateToVersion)) {
 136  0
                         properties.save(CURRENT_ENGINE_RELEASE, updateToVersion);
 137  
                     } else {
 138  6
                         properties.save(CURRENT_ENGINE_RELEASE, "");
 139  
                     }
 140  6
                     properties.save(ENGINE_VERSION_CHECKED_ON, Long.toString(now));
 141  
                 }
 142  
             }
 143  11
             LOGGER.debug("Current Release: {}", updateToVersion);
 144  
         }
 145  31
         final DependencyVersion running = new DependencyVersion(currentVersion);
 146  31
         final DependencyVersion released = new DependencyVersion(updateToVersion);
 147  31
         if (running.compareTo(released) < 0) {
 148  9
             LOGGER.debug("Upgrade recommended");
 149  9
             return true;
 150  
         }
 151  22
         LOGGER.debug("Upgrade not needed");
 152  22
         return false;
 153  
     }
 154  
 
 155  
     /**
 156  
      * Opens the CVE and CPE data stores.
 157  
      *
 158  
      * @throws DatabaseException thrown if a data store cannot be opened
 159  
      */
 160  
     protected final void openDatabase() throws DatabaseException {
 161  0
         if (cveDB != null) {
 162  0
             return;
 163  
         }
 164  0
         cveDB = new CveDB();
 165  0
         cveDB.open();
 166  0
     }
 167  
 
 168  
     /**
 169  
      * Closes the CVE and CPE data stores.
 170  
      */
 171  
     protected void closeDatabase() {
 172  0
         if (cveDB != null) {
 173  
             try {
 174  0
                 cveDB.close();
 175  0
                 cveDB = null;
 176  0
             } catch (Throwable ignore) {
 177  0
                 LOGGER.trace("Error closing the cveDB", ignore);
 178  0
             }
 179  
         }
 180  0
     }
 181  
 
 182  
     /**
 183  
      * Retrieves the current released version number from the github documentation site.
 184  
      *
 185  
      * @return the current released version number
 186  
      */
 187  
     protected String getCurrentReleaseVersion() {
 188  19
         HttpURLConnection conn = null;
 189  
         try {
 190  19
             final String str = Settings.getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL, "http://jeremylong.github.io/DependencyCheck/current.txt");
 191  19
             final URL url = new URL(str);
 192  19
             conn = URLConnectionFactory.createHttpURLConnection(url);
 193  19
             conn.connect();
 194  9
             if (conn.getResponseCode() != 200) {
 195  0
                 return null;
 196  
             }
 197  9
             final String releaseVersion = IOUtils.toString(conn.getInputStream(), "UTF-8");
 198  9
             if (releaseVersion != null) {
 199  9
                 return releaseVersion.trim();
 200  
             }
 201  0
         } catch (MalformedURLException ex) {
 202  0
             LOGGER.debug("unable to retrieve current release version of dependency-check", ex);
 203  0
         } catch (URLConnectionFailureException ex) {
 204  0
             LOGGER.debug("unable to retrieve current release version of dependency-check", ex);
 205  10
         } catch (IOException ex) {
 206  10
             LOGGER.debug("unable to retrieve current release version of dependency-check", ex);
 207  
         } finally {
 208  19
             if (conn != null) {
 209  19
                 conn.disconnect();
 210  
             }
 211  
         }
 212  10
         return null;
 213  
     }
 214  
 }