Coverage Report - org.owasp.dependencycheck.analyzer.NexusAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusAnalyzer
33%
14/42
9%
2/22
3.667
 
 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.FileNotFoundException;
 21  
 import java.io.IOException;
 22  
 import java.net.MalformedURLException;
 23  
 import java.net.URL;
 24  
 import java.util.Set;
 25  
 import java.util.logging.Level;
 26  
 import java.util.logging.Logger;
 27  
 import org.owasp.dependencycheck.Engine;
 28  
 import org.owasp.dependencycheck.analyzer.exception.AnalysisException;
 29  
 import org.owasp.dependencycheck.data.nexus.MavenArtifact;
 30  
 import org.owasp.dependencycheck.data.nexus.NexusSearch;
 31  
 import org.owasp.dependencycheck.dependency.Confidence;
 32  
 import org.owasp.dependencycheck.dependency.Dependency;
 33  
 import org.owasp.dependencycheck.utils.Settings;
 34  
 
 35  
 /**
 36  
  * Analyzer which will attempt to locate a dependency on a Nexus service by SHA-1 digest of the dependency.
 37  
  *
 38  
  * There are two settings which govern this behavior:
 39  
  *
 40  
  * <ul>
 41  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_ENABLED} determines whether this analyzer is
 42  
  * even enabled. This can be overridden by setting the system property.</li>
 43  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_URL} the URL to a Nexus service to search by
 44  
  * SHA-1. There is an expected <code>%s</code> in this where the SHA-1 will get entered.</li>
 45  
  * </ul>
 46  
  *
 47  
  * @author colezlaw
 48  
  */
 49  
 public class NexusAnalyzer extends AbstractAnalyzer {
 50  
 
 51  
     /**
 52  
      * The logger
 53  
      */
 54  1
     private static final Logger LOGGER = Logger.getLogger(NexusAnalyzer.class.getName());
 55  
 
 56  
     /**
 57  
      * The name of the analyzer
 58  
      */
 59  
     private static final String ANALYZER_NAME = "Nexus Analyzer";
 60  
 
 61  
     /**
 62  
      * The phase in which the analyzer runs
 63  
      */
 64  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 65  
 
 66  
     /**
 67  
      * The types of files on which this will work.
 68  
      */
 69  1
     private static final Set<String> SUPPORTED_EXTENSIONS = newHashSet("jar");
 70  
 
 71  
     /**
 72  
      * Whether this is actually enabled. Will get set during initialization.
 73  
      */
 74  
     private boolean enabled = false;
 75  
 
 76  
     /**
 77  
      * The Nexus Search to be set up for this analyzer.
 78  
      */
 79  
     private NexusSearch searcher;
 80  
 
 81  
     /**
 82  
      * Initializes the analyzer once before any analysis is performed.
 83  
      *
 84  
      * @throws Exception if there's an error during initialization
 85  
      */
 86  
     @Override
 87  
     public void initialize() throws Exception {
 88  3
         enabled = Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_ENABLED);
 89  3
         LOGGER.fine("Initializing Nexus Analyzer");
 90  3
         LOGGER.fine(String.format("Nexus Analyzer enabled: %s", enabled));
 91  3
         if (enabled) {
 92  0
             final String searchUrl = Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL);
 93  0
             LOGGER.fine(String.format("Nexus Analyzer URL: %s", searchUrl));
 94  
             try {
 95  0
                 searcher = new NexusSearch(new URL(searchUrl));
 96  0
                 if (!searcher.preflightRequest()) {
 97  0
                     LOGGER.warning("There was an issue getting Nexus status. Disabling analyzer.");
 98  0
                     enabled = false;
 99  
                 }
 100  0
             } catch (MalformedURLException mue) {
 101  
                 // I know that initialize can throw an exception, but we'll
 102  
                 // just disable the analyzer if the URL isn't valid
 103  0
                 LOGGER.warning(String.format("Property %s not a valid URL. Nexus Analyzer disabled", searchUrl));
 104  0
                 enabled = false;
 105  0
             }
 106  
         }
 107  3
     }
 108  
 
 109  
     /**
 110  
      * Returns the analyzer's name.
 111  
      *
 112  
      * @return the name of the analyzer
 113  
      */
 114  
     @Override
 115  
     public String getName() {
 116  9
         return ANALYZER_NAME;
 117  
     }
 118  
 
 119  
     /**
 120  
      * Returns the analysis phase under which the analyzer runs.
 121  
      *
 122  
      * @return the phase under which this analyzer runs
 123  
      */
 124  
     @Override
 125  
     public AnalysisPhase getAnalysisPhase() {
 126  6
         return ANALYSIS_PHASE;
 127  
     }
 128  
 
 129  
     /**
 130  
      * Returns the extensions for which this Analyzer runs.
 131  
      *
 132  
      * @return the extensions for which this Analyzer runs
 133  
      */
 134  
     @Override
 135  
     public Set<String> getSupportedExtensions() {
 136  138
         return SUPPORTED_EXTENSIONS;
 137  
     }
 138  
 
 139  
     /**
 140  
      * Determines whether the incoming extension is supported.
 141  
      *
 142  
      * @param extension the extension to check for support
 143  
      * @return whether the extension is supported
 144  
      */
 145  
     @Override
 146  
     public boolean supportsExtension(String extension) {
 147  134
         return SUPPORTED_EXTENSIONS.contains(extension);
 148  
     }
 149  
 
 150  
     /**
 151  
      * Performs the analysis.
 152  
      *
 153  
      * @param dependency the dependency to analyze
 154  
      * @param engine the engine
 155  
      * @throws AnalysisException when there's an exception during analysis
 156  
      */
 157  
     @Override
 158  
     public void analyze(Dependency dependency, Engine engine) throws AnalysisException {
 159  
         // Make a quick exit if this analyzer is disabled
 160  5
         if (!enabled) {
 161  5
             return;
 162  
         }
 163  
 
 164  
         try {
 165  0
             final MavenArtifact ma = searcher.searchSha1(dependency.getSha1sum());
 166  0
             if (ma.getGroupId() != null && !"".equals(ma.getGroupId())) {
 167  0
                 dependency.getVendorEvidence().addEvidence("nexus", "groupid", ma.getGroupId(), Confidence.HIGH);
 168  
             }
 169  0
             if (ma.getArtifactId() != null && !"".equals(ma.getArtifactId())) {
 170  0
                 dependency.getProductEvidence().addEvidence("nexus", "artifactid", ma.getArtifactId(), Confidence.HIGH);
 171  
             }
 172  0
             if (ma.getVersion() != null && !"".equals(ma.getVersion())) {
 173  0
                 dependency.getVersionEvidence().addEvidence("nexus", "version", ma.getVersion(), Confidence.HIGH);
 174  
             }
 175  0
             if (ma.getArtifactUrl() != null && !"".equals(ma.getArtifactUrl())) {
 176  0
                 dependency.addIdentifier("maven", ma.toString(), ma.getArtifactUrl(), Confidence.HIGHEST);
 177  
             }
 178  0
         } catch (IllegalArgumentException iae) {
 179  
             //dependency.addAnalysisException(new AnalysisException("Invalid SHA-1"));
 180  0
             LOGGER.info(String.format("invalid sha-1 hash on %s", dependency.getFileName()));
 181  0
         } catch (FileNotFoundException fnfe) {
 182  
             //dependency.addAnalysisException(new AnalysisException("Artifact not found on repository"));
 183  0
             LOGGER.fine(String.format("Artificat not found in repository '%s'", dependency.getFileName()));
 184  0
             LOGGER.log(Level.FINE, fnfe.getMessage(), fnfe);
 185  0
         } catch (IOException ioe) {
 186  
             //dependency.addAnalysisException(new AnalysisException("Could not connect to repository", ioe));
 187  0
             LOGGER.log(Level.FINE, "Could not connect to nexus repository", ioe);
 188  0
         }
 189  0
     }
 190  
 }
 191  
 
 192  
 // vim: cc=120:sw=4:ts=4:sts=4