Skip to content

Commit f255e86

Browse files
committed
Fix logged exception for dependency URIs representing directories
1 parent e9c489f commit f255e86

File tree

3 files changed

+91
-32
lines changed

3 files changed

+91
-32
lines changed

telemetry/src/main/java/datadog/telemetry/dependency/DependencyResolver.java

Lines changed: 37 additions & 29 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@
22

33
import java.io.File;
44
import java.io.FileInputStream;
5+
import java.io.IOException;
56
import java.io.InputStream;
67
import java.net.URI;
78
import java.util.Collections;
@@ -12,41 +13,48 @@
1213
public class DependencyResolver {
1314

1415
private static final Logger log = LoggerFactory.getLogger(DependencyResolver.class);
15-
private static final String JAR_SUFFIX = ".jar";
1616

1717
public static List<Dependency> resolve(URI uri) {
18-
final String scheme = uri.getScheme();
1918
try {
20-
JarReader.Extracted metadata = null;
21-
String path = null;
22-
if ("file".equals(scheme)) {
23-
File f;
24-
if (uri.isOpaque()) {
25-
f = new File(uri.getSchemeSpecificPart());
26-
} else {
27-
f = new File(uri);
28-
}
29-
path = f.getAbsolutePath();
30-
metadata = JarReader.readJarFile(path);
31-
} else if ("jar".equals(scheme) && uri.getSchemeSpecificPart().startsWith("file:")) {
32-
path = uri.getSchemeSpecificPart().substring("file:".length());
33-
metadata = JarReader.readNestedJarFile(path);
34-
} else {
35-
log.debug("unsupported dependency type: {}", uri);
36-
return Collections.emptyList();
37-
}
38-
final List<Dependency> dependencies =
39-
Dependency.fromMavenPom(metadata.jarName, metadata.pomProperties);
40-
if (!dependencies.isEmpty()) {
41-
return dependencies;
42-
}
43-
try (final InputStream is = new FileInputStream(path)) {
44-
return Collections.singletonList(
45-
Dependency.guessFallbackNoPom(metadata.manifest, metadata.jarName, is));
46-
}
19+
return internalResolve(uri);
4720
} catch (Throwable t) {
4821
log.debug("Failed to determine dependency for uri {}", uri, t);
4922
}
5023
return Collections.emptyList();
5124
}
25+
26+
static List<Dependency> internalResolve(final URI uri) throws IOException {
27+
final String scheme = uri.getScheme();
28+
JarReader.Extracted metadata;
29+
String path;
30+
if ("file".equals(scheme)) {
31+
File f;
32+
if (uri.isOpaque()) {
33+
f = new File(uri.getSchemeSpecificPart());
34+
} else {
35+
f = new File(uri);
36+
}
37+
path = f.getAbsolutePath();
38+
metadata = JarReader.readJarFile(path);
39+
} else if ("jar".equals(scheme) && uri.getSchemeSpecificPart().startsWith("file:")) {
40+
path = uri.getSchemeSpecificPart().substring("file:".length());
41+
metadata = JarReader.readNestedJarFile(path);
42+
} else {
43+
log.debug("unsupported dependency type: {}", uri);
44+
return Collections.emptyList();
45+
}
46+
if (metadata.isDirectory) {
47+
log.debug("Extracting dependencies from directories is not supported: {}", uri);
48+
return Collections.emptyList();
49+
}
50+
final List<Dependency> dependencies =
51+
Dependency.fromMavenPom(metadata.jarName, metadata.pomProperties);
52+
if (!dependencies.isEmpty()) {
53+
return dependencies;
54+
}
55+
try (final InputStream is = new FileInputStream(path)) {
56+
return Collections.singletonList(
57+
Dependency.guessFallbackNoPom(metadata.manifest, metadata.jarName, is));
58+
}
59+
}
5260
}

telemetry/src/main/java/datadog/telemetry/dependency/JarReader.java

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -19,18 +19,25 @@ static class Extracted {
1919
final String jarName;
2020
final Map<String, Properties> pomProperties;
2121
final Attributes manifest;
22+
final boolean isDirectory;
2223

2324
public Extracted(
2425
final String jarName,
2526
final Map<String, Properties> pomProperties,
26-
final Attributes manifest) {
27+
final Attributes manifest,
28+
final boolean isDirectory) {
2729
this.jarName = jarName;
2830
this.pomProperties = pomProperties;
2931
this.manifest = manifest;
32+
this.isDirectory = isDirectory;
3033
}
3134
}
3235

3336
public static Extracted readJarFile(String jarPath) throws IOException {
37+
final File jarFile = new File(jarPath);
38+
if (jarFile.isDirectory()) {
39+
return new Extracted(jarFile.getName(), new HashMap<>(), new Attributes(), true);
40+
}
3441
try (final JarFile jar = new JarFile(jarPath, false /* no verify */)) {
3542
final Map<String, Properties> pomProperties = new HashMap<>();
3643
final Enumeration<? extends ZipEntry> entries = jar.entries();
@@ -47,7 +54,7 @@ public static Extracted readJarFile(String jarPath) throws IOException {
4754
final Manifest manifest = jar.getManifest();
4855
final Attributes attributes =
4956
(manifest == null) ? new Attributes() : manifest.getMainAttributes();
50-
return new Extracted(new File(jar.getName()).getName(), pomProperties, attributes);
57+
return new Extracted(new File(jar.getName()).getName(), pomProperties, attributes, false);
5158
}
5259
}
5360

@@ -66,6 +73,10 @@ public static Extracted readNestedJarFile(final String jarPath) throws IOExcepti
6673
if (entry == null) {
6774
throw new NoSuchFileException("Nested jar not found: " + jarPath);
6875
}
76+
if (entry.isDirectory()) {
77+
return new Extracted(
78+
new File(innerJarPath).getName(), new HashMap<>(), new Attributes(), true);
79+
}
6980
try (final InputStream is = outerJar.getInputStream(entry);
7081
final JarInputStream innerJar = new JarInputStream(is, false /* no verify */)) {
7182
final Map<String, Properties> pomProperties = new HashMap<>();
@@ -80,7 +91,7 @@ public static Extracted readNestedJarFile(final String jarPath) throws IOExcepti
8091
final Manifest manifest = innerJar.getManifest();
8192
final Attributes attributes =
8293
(manifest == null) ? new Attributes() : manifest.getMainAttributes();
83-
return new Extracted(new File(innerJarPath).getName(), pomProperties, attributes);
94+
return new Extracted(new File(innerJarPath).getName(), pomProperties, attributes, false);
8495
}
8596
}
8697
}

telemetry/src/test/groovy/datadog/telemetry/dependency/DependencyResolverSpecification.groovy

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -192,6 +192,46 @@ class DependencyResolverSpecification extends DepSpecification {
192192
hash: '6438819DAB9C9AC18D8A6922C8A923C2ADAEA85D')
193193
}
194194

195+
void 'attempt to extract dependencies from directory'() {
196+
given:
197+
File dir = new File(testDir, 'dir')
198+
dir.mkdirs()
199+
200+
when:
201+
def deps = DependencyResolver.resolve(dir.toURI())
202+
203+
then:
204+
deps.isEmpty()
205+
206+
when: 'resolve without catching exceptions'
207+
deps = DependencyResolver.internalResolve(dir.toURI())
208+
209+
then: 'it does not throw'
210+
deps.isEmpty()
211+
}
212+
213+
void 'attempt to extract dependencies from directory within jar'() {
214+
given:
215+
File file = new File(testDir, "app.jar")
216+
ZipOutputStream out = new ZipOutputStream(new FileOutputStream(file))
217+
ZipEntry e = new ZipEntry("classes/")
218+
out.putNextEntry(e)
219+
out.closeEntry()
220+
out.close()
221+
222+
when:
223+
def deps = DependencyResolver.resolve(new URI('jar:file:' + file.getAbsolutePath() + "!/classes!/"))
224+
225+
then:
226+
deps.isEmpty()
227+
228+
when: 'resolve without catching exceptions'
229+
deps = DependencyResolver.internalResolve(new URI('jar:file:' + file.getAbsolutePath() + "!/classes!/"))
230+
231+
then: 'it does not throw'
232+
deps.isEmpty()
233+
}
234+
195235
private static void knownJarCheck(Map opts) {
196236
File jarFile = getJar(opts['jarName'])
197237
List<Dependency> deps = DependencyResolver.resolve(jarFile.toURI())

0 commit comments

Comments
 (0)