Coverage Report - org.owasp.dependencycheck.analyzer.NexusAnalyzer
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusAnalyzer
12%
6/48
0%
0/28
4
 
 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.dependency.Identifier;
 34  
 import org.owasp.dependencycheck.utils.Settings;
 35  
 
 36  
 /**
 37  
  * Analyzer which will attempt to locate a dependency on a Nexus service by SHA-1 digest of the dependency.
 38  
  *
 39  
  * There are two settings which govern this behavior:
 40  
  *
 41  
  * <ul>
 42  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_ENABLED} determines whether this analyzer is
 43  
  * even enabled. This can be overridden by setting the system property.</li>
 44  
  * <li>{@link org.owasp.dependencycheck.utils.Settings.KEYS#ANALYZER_NEXUS_URL} the URL to a Nexus service to search by
 45  
  * SHA-1. There is an expected <code>%s</code> in this where the SHA-1 will get entered.</li>
 46  
  * </ul>
 47  
  *
 48  
  * @author colezlaw
 49  
  */
 50  
 public class NexusAnalyzer extends AbstractFileTypeAnalyzer {
 51  
 
 52  
     /**
 53  
      * The logger.
 54  
      */
 55  1
     private static final Logger LOGGER = Logger.getLogger(NexusAnalyzer.class.getName());
 56  
 
 57  
     /**
 58  
      * The name of the analyzer.
 59  
      */
 60  
     private static final String ANALYZER_NAME = "Nexus Analyzer";
 61  
 
 62  
     /**
 63  
      * The phase in which the analyzer runs.
 64  
      */
 65  1
     private static final AnalysisPhase ANALYSIS_PHASE = AnalysisPhase.INFORMATION_COLLECTION;
 66  
 
 67  
     /**
 68  
      * The types of files on which this will work.
 69  
      */
 70  1
     private static final Set<String> SUPPORTED_EXTENSIONS = newHashSet("jar");
 71  
 
 72  
     /**
 73  
      * The Nexus Search to be set up for this analyzer.
 74  
      */
 75  
     private NexusSearch searcher;
 76  
 
 77  
     /**
 78  
      * Initializes the analyzer once before any analysis is performed.
 79  
      *
 80  
      * @throws Exception if there's an error during initialization
 81  
      */
 82  
     @Override
 83  
     public void initializeFileTypeAnalyzer() throws Exception {
 84  0
         LOGGER.fine("Initializing Nexus Analyzer");
 85  0
         LOGGER.fine(String.format("Nexus Analyzer enabled: %s", isEnabled()));
 86  0
         if (isEnabled()) {
 87  0
             final String searchUrl = Settings.getString(Settings.KEYS.ANALYZER_NEXUS_URL);
 88  0
             LOGGER.fine(String.format("Nexus Analyzer URL: %s", searchUrl));
 89  
             try {
 90  0
                 searcher = new NexusSearch(new URL(searchUrl));
 91  0
                 if (!searcher.preflightRequest()) {
 92  0
                     LOGGER.warning("There was an issue getting Nexus status. Disabling analyzer.");
 93  0
                     setEnabled(false);
 94  
                 }
 95  0
             } catch (MalformedURLException mue) {
 96  
                 // I know that initialize can throw an exception, but we'll
 97  
                 // just disable the analyzer if the URL isn't valid
 98  0
                 LOGGER.warning(String.format("Property %s not a valid URL. Nexus Analyzer disabled", searchUrl));
 99  0
                 setEnabled(false);
 100  0
             }
 101  
         }
 102  0
     }
 103  
 
 104  
     /**
 105  
      * Returns the analyzer's name.
 106  
      *
 107  
      * @return the name of the analyzer
 108  
      */
 109  
     @Override
 110  
     public String getName() {
 111  4
         return ANALYZER_NAME;
 112  
     }
 113  
 
 114  
     /**
 115  
      * Returns the key used in the properties file to reference the analyzer's enabled property.
 116  
      *
 117  
      * @return the analyzer's enabled property setting key
 118  
      */
 119  
     @Override
 120  
     protected String getAnalyzerEnabledSettingKey() {
 121  2
         return Settings.KEYS.ANALYZER_NEXUS_ENABLED;
 122  
     }
 123  
 
 124  
     /**
 125  
      * Returns the analysis phase under which the analyzer runs.
 126  
      *
 127  
      * @return the phase under which this analyzer runs
 128  
      */
 129  
     @Override
 130  
     public AnalysisPhase getAnalysisPhase() {
 131  1
         return ANALYSIS_PHASE;
 132  
     }
 133  
 
 134  
     /**
 135  
      * Returns the extensions for which this Analyzer runs.
 136  
      *
 137  
      * @return the extensions for which this Analyzer runs
 138  
      */
 139  
     @Override
 140  
     public Set<String> getSupportedExtensions() {
 141  0
         return SUPPORTED_EXTENSIONS;
 142  
     }
 143  
 
 144  
     /**
 145  
      * Performs the analysis.
 146  
      *
 147  
      * @param dependency the dependency to analyze
 148  
      * @param engine the engine
 149  
      * @throws AnalysisException when there's an exception during analysis
 150  
      */
 151  
     @Override
 152  
     public void analyzeFileType(Dependency dependency, Engine engine) throws AnalysisException {
 153  
         try {
 154  0
             final MavenArtifact ma = searcher.searchSha1(dependency.getSha1sum());
 155  0
             if (ma.getGroupId() != null && !"".equals(ma.getGroupId())) {
 156  0
                 dependency.getVendorEvidence().addEvidence("nexus", "groupid", ma.getGroupId(), Confidence.HIGH);
 157  
             }
 158  0
             if (ma.getArtifactId() != null && !"".equals(ma.getArtifactId())) {
 159  0
                 dependency.getProductEvidence().addEvidence("nexus", "artifactid", ma.getArtifactId(), Confidence.HIGH);
 160  
             }
 161  0
             if (ma.getVersion() != null && !"".equals(ma.getVersion())) {
 162  0
                 dependency.getVersionEvidence().addEvidence("nexus", "version", ma.getVersion(), Confidence.HIGH);
 163  
             }
 164  0
             if (ma.getArtifactUrl() != null && !"".equals(ma.getArtifactUrl())) {
 165  0
                 boolean found = false;
 166  0
                 for (Identifier i : dependency.getIdentifiers()) {
 167  0
                     if ("maven".equals(i.getType()) && i.getValue().equals(ma.toString())) {
 168  0
                         found = true;
 169  0
                         i.setConfidence(Confidence.HIGHEST);
 170  0
                         i.setUrl(ma.getArtifactUrl());
 171  0
                         break;
 172  
                     }
 173  0
                 }
 174  0
                 if (!found) {
 175  0
                     dependency.addIdentifier("maven", ma.toString(), ma.getArtifactUrl(), Confidence.HIGHEST);
 176  
                 }
 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("Artifact 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  
 }