Coverage Report - org.owasp.dependencycheck.analyzer.NexusAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusAnalyzer
18%
13/69
4%
1/24
3.375
 
 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.analyzer;
 19  
 
 20  
 import java.io.File;
 21  
 import java.io.FileNotFoundException;
 22  
 import java.io.IOException;
 23  
 import java.net.MalformedURLException;
 24  
 import java.net.URL;
 25  
 import java.util.Set;
 26  
 import java.util.logging.Level;
 27  
 import java.util.logging.Logger;
 28  
 import org.apache.commons.io.FileUtils;
 29  
 import org.owasp.dependencycheck.Engine;
 30  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 31  
 import org.owasp.dependencycheck.data.nexus.MavenArtifact;
 32  
 import org.owasp.dependencycheck.data.nexus.NexusSearch;
 33  
 import org.owasp.dependencycheck.dependency.Confidence;
 34  
 import org.owasp.dependencycheck.dependency.Dependency;
 35  
 import org.owasp.dependencycheck.dependency.Evidence;
 36  
 import org.owasp.dependencycheck.xml.pom.PomUtils;
 37  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 38  
 import org.owasp.dependencycheck.utils.DownloadFailedException;
 39  
 import org.owasp.dependencycheck.utils.Downloader;
 40  
 import org.owasp.dependencycheck.utils.Settings;
 41  
 
 42  
 /**
 43  
  * Analyzer which will attempt to locate a dependency on a Nexus service by SHA-1 digest of the dependency.
 44  
  *
 45  
  * There are two settings which govern this behavior:
 46  
  *
 47  
  * <ul>
 48  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_ENABLED} determines whether this analyzer is even
 49  
  * enabled. This can be overridden by setting the system property.</li>
 50  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_URL} the URL to a Nexus service to search by SHA-1.
 51  
  * There is an expected <code>%s</code> in this where the SHA-1 will get entered.</li>
 52  
  * </ul>
 53  
  *
 54  
  * @author colezlaw
 55  
  */
 56  2
 public class NexusAnalyzer extends AbstractFileTypeAnalyzer {
 57  
 
 58  
     /**
 59  
      * The default URL - this will be used by the CentralAnalyzer to determine whether to enable this.
 60  
      */
 61  
     public static final String DEFAULT_URL = "https://repository.sonatype.org/service/local/";
 62  
 
 63  
     /**
 64  
      * The logger.
 65  
      */
 66  1
     private static final Logger LOGGER = Logger.getLogger(NexusAnalyzer.class.getName());
 67  
 
 68  
     /**
 69  
      * The name of the analyzer.
 70  
      */
 71  
     private static final String ANALYZER_NAME = "Nexus Analyzer";
 72  
 
 73  
     /**
 74  
      * The phase in which the analyzer runs.
 75  
      */
 76  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 77  
 
 78  
     /**
 79  
      * The types of files on which this will work.
 80  
      */
 81  1
     private static final Set<String> SUPPORTED_EXTENSIONS = newHashSet("jar");
 82  
 
 83  
     /**
 84  
      * The Nexus Search to be set up for this analyzer.
 85  
      */
 86  
     private NexusSearch searcher;
 87  
 
 88  
     /**
 89  
      * Field indicating if the analyzer is enabled.
 90  
      */
 91  2
     private final boolean enabled = checkEnabled();
 92  
 
 93  
     /**
 94  
      * Determines if this analyzer is enabled
 95  
      *
 96  
      * @return <code>true</code> if the analyzer is enabled; otherwise <code>false</code>
 97  
      */
 98  
     private boolean checkEnabled() {
 99  
         /* Enable this analyzer ONLY if the Nexus URL has been set to something
 100  
          other than the default one (if it's the default one, we'll use the
 101  
          central one) and it's enabled by the user.
 102  
          */
 103  2
         boolean retval = false;
 104  
         try {
 105  2
             if ((!DEFAULT_URL.equals(Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL)))
 106  
                     && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED)) {
 107  0
                 LOGGER.info("Enabling Nexus analyzer");
 108  0
                 retval = true;
 109  
             } else {
 110  2
                 LOGGER.fine("Nexus analyzer disabled, using Central instead");
 111  
             }
 112  0
         } catch (InvalidSettingException ise) {
 113  0
             LOGGER.warning("Invalid setting. Disabling Nexus analyzer");
 114  2
         }
 115  
 
 116  2
         return retval;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Determine whether to enable this analyzer or not.
 121  
      *
 122  
      * @return whether the analyzer should be enabled
 123  
      */
 124  
     @Override
 125  
     public boolean isEnabled() {
 126  0
         return enabled;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Initializes the analyzer once before any analysis is performed.
 131  
      *
 132  
      * @throws Exception if there's an error during initialization
 133  
      */
 134  
     @Override
 135  
     public void initializeFileTypeAnalyzer() throws Exception {
 136  0
         LOGGER.fine("Initializing Nexus Analyzer");
 137  0
         LOGGER.fine(String.format("Nexus Analyzer enabled: %s", isEnabled()));
 138  0
         if (isEnabled()) {
 139  0
             final String searchUrl = Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL);
 140  0
             LOGGER.fine(String.format("Nexus Analyzer URL: %s", searchUrl));
 141  
             try {
 142  0
                 searcher = new NexusSearch(new URL(searchUrl));
 143  0
                 if (!searcher.preflightRequest()) {
 144  0
                     LOGGER.warning("There was an issue getting Nexus status. Disabling analyzer.");
 145  0
                     setEnabled(false);
 146  
                 }
 147  0
             } catch (MalformedURLException mue) {
 148  
                 // I know that initialize can throw an exception, but we'll
 149  
                 // just disable the analyzer if the URL isn't valid
 150  0
                 LOGGER.warning(String.format("Property %s not a valid URL. Nexus Analyzer disabled", searchUrl));
 151  0
                 setEnabled(false);
 152  0
             }
 153  
         }
 154  0
     }
 155  
 
 156  
     /**
 157  
      * Returns the analyzer's name.
 158  
      *
 159  
      * @return the name of the analyzer
 160  
      */
 161  
     @Override
 162  
     public String getName() {
 163  4
         return ANALYZER_NAME;
 164  
     }
 165  
 
 166  
     /**
 167  
      * Returns the key used in the properties file to reference the analyzer's enabled property.
 168  
      *
 169  
      * @return the analyzer's enabled property setting key
 170  
      */
 171  
     @Override
 172  
     protected String getAnalyzerEnabledSettingKey() {
 173  2
         return Settings.KEYS.ANALYZER_NEXUS_ENABLED;
 174  
     }
 175  
 
 176  
     /**
 177  
      * Returns the analysis phase under which the analyzer runs.
 178  
      *
 179  
      * @return the phase under which this analyzer runs
 180  
      */
 181  
     @Override
 182  
     public AnalysisPhase getAnalysisPhase() {
 183  1
         return ANALYSIS_PHASE;
 184  
     }
 185  
 
 186  
     /**
 187  
      * Returns the extensions for which this Analyzer runs.
 188  
      *
 189  
      * @return the extensions for which this Analyzer runs
 190  
      */
 191  
     @Override
 192  
     public Set<String> getSupportedExtensions() {
 193  0
         return SUPPORTED_EXTENSIONS;
 194  
     }
 195  
 
 196  
     /**
 197  
      * Performs the analysis.
 198  
      *
 199  
      * @param dependency the dependency to analyze
 200  
      * @param engine the engine
 201  
      * @throws AnalysisException when there's an exception during analysis
 202  
      */
 203  
     @Override
 204  
     public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException {
 205  0
         if (!isEnabled()) {
 206  0
             return;
 207  
         }
 208  
         try {
 209  0
             final MavenArtifact ma = searcher.searchSha1(dependency.getSha1sum());
 210  0
             dependency.addAsEvidence("nexus", ma, Confidence.HIGH);
 211  0
             boolean pomAnalyzed = false;
 212  0
             LOGGER.fine("POM URL " + ma.getPomUrl());
 213  0
             for (Evidence e : dependency.getVendorEvidence()) {
 214  0
                 if ("pom".equals(e.getSource())) {
 215  0
                     pomAnalyzed = true;
 216  0
                     break;
 217  
                 }
 218  0
             }
 219  0
             if (!pomAnalyzed && ma.getPomUrl() != null) {
 220  0
                 File pomFile = null;
 221  
                 try {
 222  0
                     final File baseDir = Settings.getTempDirectory();
 223  0
                     pomFile = File.createTempFile("pom", ".xml", baseDir);
 224  0
                     if (!pomFile.delete()) {
 225  0
                         final String msg = String.format("Unable to fetch pom.xml for %s from Nexus repository; "
 226  
                                 + "this could result in undetected CPE/CVEs.", dependency.getFileName());
 227  0
                         LOGGER.warning(msg);
 228  0
                         LOGGER.fine("Unable to delete temp file");
 229  
                     }
 230  0
                     LOGGER.fine(String.format("Downloading %s", ma.getPomUrl()));
 231  0
                     Downloader.fetchFile(new URL(ma.getPomUrl()), pomFile);
 232  0
                     PomUtils.analyzePOM(dependency, pomFile);
 233  0
                 } catch (DownloadFailedException ex) {
 234  0
                     final String msg = String.format("Unable to download pom.xml for %s from Nexus repository; "
 235  
                             + "this could result in undetected CPE/CVEs.", dependency.getFileName());
 236  0
                     LOGGER.warning(msg);
 237  
                 } finally {
 238  0
                     if (pomFile != null && !FileUtils.deleteQuietly(pomFile)) {
 239  0
                         pomFile.deleteOnExit();
 240  
                     }
 241  
                 }
 242  
             }
 243  0
         } catch (IllegalArgumentException iae) {
 244  
             //dependency.addAnalysisException(new AnalysisException("Invalid SHA-1"));
 245  0
             LOGGER.info(String.format("invalid sha-1 hash on %s", dependency.getFileName()));
 246  0
         } catch (FileNotFoundException fnfe) {
 247  
             //dependency.addAnalysisException(new AnalysisException("Artifact not found on repository"));
 248  0
             LOGGER.fine(String.format("Artifact not found in repository '%s'", dependency.getFileName()));
 249  0
             LOGGER.log(Level.FINE, fnfe.getMessage(), fnfe);
 250  0
         } catch (IOException ioe) {
 251  
             //dependency.addAnalysisException(new AnalysisException("Could not connect to repository", ioe));
 252  0
             LOGGER.log(Level.FINE, "Could not connect to nexus repository", ioe);
 253  0
         }
 254  0
     }
 255  
 }