Coverage Report - org.owasp.dependencycheck.data.nexus.NexusSearch
 
Classes in this File Line Coverage Branch Coverage Complexity
NexusSearch
0%
0/59
0%
0/24
8.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.data.nexus;
 19  
 
 20  
 import java.io.FileNotFoundException;
 21  
 import java.io.IOException;
 22  
 import java.net.HttpURLConnection;
 23  
 import java.net.URL;
 24  
 import java.util.logging.Level;
 25  
 import java.util.logging.Logger;
 26  
 import javax.xml.parsers.DocumentBuilder;
 27  
 import javax.xml.parsers.DocumentBuilderFactory;
 28  
 import javax.xml.xpath.XPath;
 29  
 import javax.xml.xpath.XPathFactory;
 30  
 import org.owasp.dependencycheck.utils.InvalidSettingException;
 31  
 import org.owasp.dependencycheck.utils.Settings;
 32  
 import org.owasp.dependencycheck.utils.URLConnectionFactory;
 33  
 import org.w3c.dom.Document;
 34  
 
 35  
 /**
 36  
  * Class of methods to search Nexus repositories.
 37  
  *
 38  
  * @author colezlaw
 39  
  */
 40  
 public class NexusSearch {
 41  
 
 42  
     /**
 43  
      * The root URL for the Nexus repository service.
 44  
      */
 45  
     private final URL rootURL;
 46  
 
 47  
     /**
 48  
      * Whether to use the Proxy when making requests.
 49  
      */
 50  
     private boolean useProxy;
 51  
     /**
 52  
      * The username to use if the Nexus requires authentication.
 53  
      */
 54  0
     private String userName = null;
 55  
     /**
 56  
      * The password to use if the Nexus requires authentication.
 57  
      */
 58  
     private char[] password;
 59  
     /**
 60  
      * Used for logging.
 61  
      */
 62  0
     private static final Logger LOGGER = Logger.getLogger(NexusSearch.class.getName());
 63  
 
 64  
     /**
 65  
      * Creates a NexusSearch for the given repository URL.
 66  
      *
 67  
      * @param rootURL the root URL of the repository on which searches should execute. full URL's are calculated relative to this
 68  
      * URL, so it should end with a /
 69  
      */
 70  0
     public NexusSearch(URL rootURL) {
 71  0
         this.rootURL = rootURL;
 72  
         try {
 73  0
             if (null != Settings.getString(Settings.KEYS.PROXY_SERVER)
 74  
                     && Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY)) {
 75  0
                 useProxy = true;
 76  0
                 LOGGER.fine("Using proxy");
 77  
             } else {
 78  0
                 useProxy = false;
 79  0
                 LOGGER.fine("Not using proxy");
 80  
             }
 81  0
         } catch (InvalidSettingException ise) {
 82  0
             useProxy = false;
 83  0
         }
 84  0
     }
 85  
 
 86  
     /**
 87  
      * Searches the configured Nexus repository for the given sha1 hash. If the artifact is found, a <code>MavenArtifact</code> is
 88  
      * populated with the coordinate information.
 89  
      *
 90  
      * @param sha1 The SHA-1 hash string for which to search
 91  
      * @return the populated Maven coordinates
 92  
      * @throws IOException if it's unable to connect to the specified repository or if the specified artifact is not found.
 93  
      */
 94  
     public MavenArtifact searchSha1(String sha1) throws IOException {
 95  0
         if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) {
 96  0
             throw new IllegalArgumentException("Invalid SHA1 format");
 97  
         }
 98  
 
 99  0
         final URL url = new URL(rootURL, String.format("identify/sha1/%s",
 100  
                 sha1.toLowerCase()));
 101  
 
 102  0
         LOGGER.fine(String.format("Searching Nexus url %s", url.toString()));
 103  
 
 104  
         // Determine if we need to use a proxy. The rules:
 105  
         // 1) If the proxy is set, AND the setting is set to true, use the proxy
 106  
         // 2) Otherwise, don't use the proxy (either the proxy isn't configured,
 107  
         // or proxy is specifically set to false
 108  
         HttpURLConnection conn;
 109  0
         conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
 110  0
         conn.setDoOutput(true);
 111  
 
 112  
         // JSON would be more elegant, but there's not currently a dependency
 113  
         // on JSON, so don't want to add one just for this
 114  0
         conn.addRequestProperty("Accept", "application/xml");
 115  0
         conn.connect();
 116  
 
 117  0
         if (conn.getResponseCode() == 200) {
 118  
             try {
 119  0
                 final DocumentBuilder builder = DocumentBuilderFactory
 120  
                         .newInstance().newDocumentBuilder();
 121  0
                 final Document doc = builder.parse(conn.getInputStream());
 122  0
                 final XPath xpath = XPathFactory.newInstance().newXPath();
 123  0
                 final String groupId = xpath
 124  
                         .evaluate(
 125  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/groupId",
 126  
                                 doc);
 127  0
                 final String artifactId = xpath.evaluate(
 128  
                         "/org.sonatype.nexus.rest.model.NexusArtifact/artifactId",
 129  
                         doc);
 130  0
                 final String version = xpath
 131  
                         .evaluate(
 132  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/version",
 133  
                                 doc);
 134  0
                 final String link = xpath
 135  
                         .evaluate(
 136  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink",
 137  
                                 doc);
 138  0
                 final String pomLink = xpath
 139  
                         .evaluate(
 140  
                                 "/org.sonatype.nexus.rest.model.NexusArtifact/pomLink",
 141  
                                 doc);
 142  0
                 final MavenArtifact ma = new MavenArtifact(groupId, artifactId, version);
 143  0
                 if (link != null && !"".equals(link)) {
 144  0
                     ma.setArtifactUrl(link);
 145  
                 }
 146  0
                 if (pomLink != null && !"".equals(pomLink)) {
 147  0
                     ma.setPomUrl(pomLink);
 148  
                 }
 149  0
                 return ma;
 150  0
             } catch (Throwable e) {
 151  
                 // Anything else is jacked-up XML stuff that we really can't recover
 152  
                 // from well
 153  0
                 throw new IOException(e.getMessage(), e);
 154  
             }
 155  0
         } else if (conn.getResponseCode() == 404) {
 156  0
             throw new FileNotFoundException("Artifact not found in Nexus");
 157  
         } else {
 158  0
             final String msg = String.format("Could not connect to Nexus received response code: %d %s",
 159  
                     conn.getResponseCode(), conn.getResponseMessage());
 160  0
             LOGGER.fine(msg);
 161  0
             throw new IOException(msg);
 162  
         }
 163  
     }
 164  
 
 165  
     /**
 166  
      * Do a preflight request to see if the repository is actually working.
 167  
      *
 168  
      * @return whether the repository is listening and returns the /status URL correctly
 169  
      */
 170  
     public boolean preflightRequest() {
 171  
         HttpURLConnection conn;
 172  
         try {
 173  0
             final URL url = new URL(rootURL, "status");
 174  0
             conn = URLConnectionFactory.createHttpURLConnection(url, useProxy);
 175  0
             conn.addRequestProperty("Accept", "application/xml");
 176  0
             conn.connect();
 177  0
             if (conn.getResponseCode() != 200) {
 178  0
                 LOGGER.log(Level.WARNING, "Expected 200 result from Nexus, got {0}", conn.getResponseCode());
 179  0
                 return false;
 180  
             }
 181  0
             final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder();
 182  0
             final Document doc = builder.parse(conn.getInputStream());
 183  0
             if (!"status".equals(doc.getDocumentElement().getNodeName())) {
 184  0
                 LOGGER.log(Level.WARNING, "Expected root node name of status, got {0}", doc.getDocumentElement().getNodeName());
 185  0
                 return false;
 186  
             }
 187  0
         } catch (Throwable e) {
 188  0
             return false;
 189  0
         }
 190  
 
 191  0
         return true;
 192  
     }
 193  
 }
 194  
 
 195  
 // vim: cc=120:sw=4:ts=4:sts=4