| 1 | |
|
| 2 | |
|
| 3 | |
|
| 4 | |
|
| 5 | |
|
| 6 | |
|
| 7 | |
|
| 8 | |
|
| 9 | |
|
| 10 | |
|
| 11 | |
|
| 12 | |
|
| 13 | |
|
| 14 | |
|
| 15 | |
|
| 16 | |
|
| 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 | |
|
| 37 | |
|
| 38 | |
|
| 39 | |
|
| 40 | |
public class NexusSearch { |
| 41 | |
|
| 42 | |
|
| 43 | |
|
| 44 | |
|
| 45 | |
private final URL rootURL; |
| 46 | |
|
| 47 | |
|
| 48 | |
|
| 49 | |
|
| 50 | |
private boolean useProxy; |
| 51 | |
|
| 52 | |
|
| 53 | |
|
| 54 | |
|
| 55 | 1 | private static final Logger LOGGER = Logger.getLogger(NexusSearch.class |
| 56 | |
.getName()); |
| 57 | |
|
| 58 | |
|
| 59 | |
|
| 60 | |
|
| 61 | |
|
| 62 | |
|
| 63 | |
|
| 64 | 4 | public NexusSearch(URL rootURL) { |
| 65 | 4 | this.rootURL = rootURL; |
| 66 | |
try { |
| 67 | 4 | if (null != Settings.getString(Settings.KEYS.PROXY_URL) |
| 68 | |
&& Settings.getBoolean(Settings.KEYS.ANALYZER_NEXUS_PROXY)) { |
| 69 | 0 | useProxy = true; |
| 70 | 0 | LOGGER.fine("Using proxy"); |
| 71 | |
} else { |
| 72 | 4 | useProxy = false; |
| 73 | 4 | LOGGER.fine("Not using proxy"); |
| 74 | |
} |
| 75 | 0 | } catch (InvalidSettingException ise) { |
| 76 | 0 | useProxy = false; |
| 77 | 4 | } |
| 78 | 4 | } |
| 79 | |
|
| 80 | |
|
| 81 | |
|
| 82 | |
|
| 83 | |
|
| 84 | |
|
| 85 | |
|
| 86 | |
|
| 87 | |
|
| 88 | |
|
| 89 | |
public MavenArtifact searchSha1(String sha1) throws IOException { |
| 90 | 4 | if (null == sha1 || !sha1.matches("^[0-9A-Fa-f]{40}$")) { |
| 91 | 2 | throw new IllegalArgumentException("Invalid SHA1 format"); |
| 92 | |
} |
| 93 | |
|
| 94 | 2 | final URL url = new URL(rootURL, String.format("identify/sha1/%s", |
| 95 | |
sha1.toLowerCase())); |
| 96 | |
|
| 97 | 2 | LOGGER.fine(String.format("Searching Nexus url %s", url.toString())); |
| 98 | |
|
| 99 | |
|
| 100 | |
|
| 101 | |
|
| 102 | |
|
| 103 | |
|
| 104 | 2 | final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(url, useProxy); |
| 105 | |
|
| 106 | 2 | conn.setDoOutput(true); |
| 107 | |
|
| 108 | |
|
| 109 | |
|
| 110 | 2 | conn.addRequestProperty("Accept", "application/xml"); |
| 111 | 2 | conn.connect(); |
| 112 | |
|
| 113 | 2 | if (conn.getResponseCode() == 200) { |
| 114 | |
try { |
| 115 | 1 | final DocumentBuilder builder = DocumentBuilderFactory |
| 116 | |
.newInstance().newDocumentBuilder(); |
| 117 | 1 | final Document doc = builder.parse(conn.getInputStream()); |
| 118 | 1 | final XPath xpath = XPathFactory.newInstance().newXPath(); |
| 119 | 1 | final String groupId = xpath |
| 120 | |
.evaluate( |
| 121 | |
"/org.sonatype.nexus.rest.model.NexusArtifact/groupId", |
| 122 | |
doc); |
| 123 | 1 | final String artifactId = xpath.evaluate( |
| 124 | |
"/org.sonatype.nexus.rest.model.NexusArtifact/artifactId", |
| 125 | |
doc); |
| 126 | 1 | final String version = xpath |
| 127 | |
.evaluate( |
| 128 | |
"/org.sonatype.nexus.rest.model.NexusArtifact/version", |
| 129 | |
doc); |
| 130 | 1 | final String link = xpath |
| 131 | |
.evaluate( |
| 132 | |
"/org.sonatype.nexus.rest.model.NexusArtifact/artifactLink", |
| 133 | |
doc); |
| 134 | 1 | return new MavenArtifact(groupId, artifactId, version, link); |
| 135 | 0 | } catch (Throwable e) { |
| 136 | |
|
| 137 | |
|
| 138 | 0 | throw new IOException(e.getMessage(), e); |
| 139 | |
} |
| 140 | 1 | } else if (conn.getResponseCode() == 404) { |
| 141 | 1 | throw new FileNotFoundException("Artifact not found in Nexus"); |
| 142 | |
} else { |
| 143 | 0 | final String msg = String.format("Could not connect to Nexus received response code: %d %s", |
| 144 | |
conn.getResponseCode(), conn.getResponseMessage()); |
| 145 | 0 | LOGGER.fine(msg); |
| 146 | 0 | throw new IOException(msg); |
| 147 | |
} |
| 148 | |
} |
| 149 | |
|
| 150 | |
|
| 151 | |
|
| 152 | |
|
| 153 | |
|
| 154 | |
|
| 155 | |
public boolean preflightRequest() { |
| 156 | |
try { |
| 157 | 4 | final HttpURLConnection conn = URLConnectionFactory.createHttpURLConnection(new URL(rootURL, "status"), useProxy); |
| 158 | 4 | conn.addRequestProperty("Accept", "application/xml"); |
| 159 | 4 | conn.connect(); |
| 160 | 4 | if (conn.getResponseCode() != 200) { |
| 161 | 0 | LOGGER.log(Level.WARNING, "Expected 200 result from Nexus, got {0}", conn.getResponseCode()); |
| 162 | 0 | return false; |
| 163 | |
} |
| 164 | 4 | final DocumentBuilder builder = DocumentBuilderFactory.newInstance().newDocumentBuilder(); |
| 165 | 4 | final Document doc = builder.parse(conn.getInputStream()); |
| 166 | 4 | if (!"status".equals(doc.getDocumentElement().getNodeName())) { |
| 167 | 0 | LOGGER.log(Level.WARNING, "Expected root node name of status, got {0}", doc.getDocumentElement().getNodeName()); |
| 168 | 0 | return false; |
| 169 | |
} |
| 170 | 0 | } catch (Throwable e) { |
| 171 | 0 | return false; |
| 172 | 4 | } |
| 173 | |
|
| 174 | 4 | return true; |
| 175 | |
} |
| 176 | |
} |
| 177 | |
|
| 178 | |
|