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