Coverage Report - org.owasp.dependencycheck.data.update.EngineVersionCheck
 
Classes in this File Line Coverage Branch Coverage Complexity
EngineVersionCheck
44%
29/65
34%
13/38
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 java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 import org.apache.commons.io.IOUtils;
 28  
 import org.owasp.dependencycheck.data.nvdcve.CveDB;
 29  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseException;
 30  
 import org.owasp.dependencycheck.data.nvdcve.DatabaseProperties;
 31  
 import org.owasp.dependencycheck.data.update.exception.UpdateException;
 32  
 import org.owasp.dependencycheck.utils.DateUtil;
 33  
 import org.owasp.dependencycheck.utils.DependencyVersion;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 import org.owasp.dependencycheck.utils.URLConnectionFactory;
 36  
 import org.owasp.dependencycheck.utils.URLConnectionFailureException;
 37  
 
 38  
 /**
 39  
  *
 40  
  * @author Jeremy Long <jeremy.long@owasp.org>
 41  
  */
 42  
 public class EngineVersionCheck implements CachedWebDataSource {
 43  
 
 44  
     /**
 45  
      * Static logger.
 46  
      */
 47  1
     private static final Logger LOGGER = Logger.getLogger(EngineVersionCheck.class.getName());
 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  
     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  
         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  
         updateToVersion = version;
 82  
     }
 83  
 
 84  
     @Override
 85  
     public void update() throws UpdateException {
 86  
         try {
 87  0
             openDatabase();
 88  0
             final DatabaseProperties properties = cveDB.getDatabaseProperties();
 89  0
             final long lastChecked = Long.parseLong(properties.getProperty(ENGINE_VERSION_CHECKED_ON, "0"));
 90  0
             final long now = (new Date()).getTime();
 91  0
             updateToVersion = properties.getProperty(CURRENT_ENGINE_RELEASE, "");
 92  0
             final String currentVersion = Settings.getString(Settings.KEYS.APPLICATION_VERSION, "0.0.0");
 93  0
             final boolean updateNeeded = shouldUpdate(lastChecked, now, properties, currentVersion);
 94  0
             if (updateNeeded) {
 95  0
                 final String msg = String.format("A new version of dependency-check is available. Consider updating to version %s.",
 96  
                         updateToVersion);
 97  0
                 LOGGER.warning(msg);
 98  
             }
 99  0
         } catch (DatabaseException ex) {
 100  0
             LOGGER.log(Level.FINE, "Database Exception opening databases to retrieve properties", ex);
 101  0
             throw new UpdateException("Error occured updating database properties.");
 102  
         } finally {
 103  0
             closeDatabase();
 104  0
         }
 105  0
     }
 106  
 
 107  
     /**
 108  
      * Determines if a new version of the dependency-check engine has been released.
 109  
      *
 110  
      * @param lastChecked the epoch time of the last version check
 111  
      * @param now the current epoch time
 112  
      * @param properties the database properties object
 113  
      * @param currentVersion the current version of dependency-check
 114  
      * @return <code>true</code> if a newer version of the database has been released; otherwise <code>false</code>
 115  
      * @throws UpdateException thrown if there is an error connecting to the github documentation site or accessing the
 116  
      * local database.
 117  
      */
 118  
     protected boolean shouldUpdate(final long lastChecked, final long now, final DatabaseProperties properties,
 119  
             String currentVersion) throws UpdateException {
 120  
         //check every 30 days if we know there is an update, otherwise check every 7 days
 121  7
         int checkRange = 30;
 122  7
         if (updateToVersion.isEmpty()) {
 123  2
             checkRange = 7;
 124  
         }
 125  7
         if (!DateUtil.withinDateRange(lastChecked, now, checkRange)) {
 126  2
             final String currentRelease = getCurrentReleaseVersion();
 127  2
             if (currentRelease != null) {
 128  2
                 final DependencyVersion v = new DependencyVersion(currentRelease);
 129  2
                 if (v.getVersionParts() != null && v.getVersionParts().size() >= 3) {
 130  2
                     if (!currentRelease.equals(updateToVersion)) {
 131  2
                         properties.save(CURRENT_ENGINE_RELEASE, v.toString());
 132  
                     } else {
 133  0
                         properties.save(CURRENT_ENGINE_RELEASE, "");
 134  
                     }
 135  2
                     properties.save(ENGINE_VERSION_CHECKED_ON, Long.toString(now));
 136  2
                     updateToVersion = v.toString();
 137  
                 }
 138  
             }
 139  
         }
 140  7
         final DependencyVersion running = new DependencyVersion(currentVersion);
 141  7
         final DependencyVersion released = new DependencyVersion(updateToVersion);
 142  7
         if (running.compareTo(released) < 0) {
 143  3
             return true;
 144  
         }
 145  4
         return false;
 146  
     }
 147  
 
 148  
     /**
 149  
      * Opens the CVE and CPE data stores.
 150  
      *
 151  
      * @throws DatabaseException thrown if a data store cannot be opened
 152  
      */
 153  
     protected final void openDatabase() throws DatabaseException {
 154  0
         if (cveDB != null) {
 155  0
             return;
 156  
         }
 157  0
         cveDB = new CveDB();
 158  0
         cveDB.open();
 159  0
     }
 160  
 
 161  
     /**
 162  
      * Closes the CVE and CPE data stores.
 163  
      */
 164  
     protected void closeDatabase() {
 165  0
         if (cveDB != null) {
 166  
             try {
 167  0
                 cveDB.close();
 168  0
             } catch (Throwable ignore) {
 169  0
                 LOGGER.log(Level.FINEST, "Error closing the cveDB", ignore);
 170  0
             }
 171  
         }
 172  0
     }
 173  
 
 174  
     /**
 175  
      * Retrieves the current released version number from the github documentation site.
 176  
      *
 177  
      * @return the current released version number
 178  
      */
 179  
     protected String getCurrentReleaseVersion() {
 180  3
         HttpURLConnection conn = null;
 181  
         try {
 182  3
             final String str = Settings.getString(Settings.KEYS.ENGINE_VERSION_CHECK_URL, "http://jeremylong.github.io/DependencyCheck/current.txt");
 183  3
             final URL url = new URL(str);
 184  3
             conn = URLConnectionFactory.createHttpURLConnection(url);
 185  3
             conn.connect();
 186  3
             if (conn.getResponseCode() != 200) {
 187  0
                 return null;
 188  
             }
 189  3
             final String releaseVersion = IOUtils.toString(conn.getInputStream(), "UTF-8");
 190  3
             if (releaseVersion != null) {
 191  3
                 return releaseVersion.trim();
 192  
             }
 193  0
         } catch (MalformedURLException ex) {
 194  0
             LOGGER.log(Level.FINE, "unable to retrieve current release version of dependency-check", ex);
 195  0
         } catch (URLConnectionFailureException ex) {
 196  0
             LOGGER.log(Level.FINE, "unable to retrieve current release version of dependency-check", ex);
 197  0
         } catch (IOException ex) {
 198  0
             LOGGER.log(Level.FINE, "unable to retrieve current release version of dependency-check", ex);
 199  
         } finally {
 200  3
             if (conn != null) {
 201  3
                 conn.disconnect();
 202  
             }
 203  
         }
 204  0
         return null;
 205  
     }
 206  
 }