GoogleContainerTools / jib

🏗 Build container images for your Java applications.
Apache License 2.0
13.67k stars 1.44k forks source link

NPE when adding LayerEntry relative to the root '/' #2195

Open briandealwis opened 4 years ago

briandealwis commented 4 years ago

Environment:

Description of the issue:

Seeing NPEs when using jib-core to create a layer with content placed at "/":

Caused by: java.lang.NullPointerException
    at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder$UniqueTarArchiveEntries.add(ReproducibleLayerBuilder.java:75)
    at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder$UniqueTarArchiveEntries.access$100(ReproducibleLayerBuilder.java:47)
    at com.google.cloud.tools.jib.image.ReproducibleLayerBuilder.build(ReproducibleLayerBuilder.java:118)
    at com.google.cloud.tools.jib.builder.steps.BuildAndCacheApplicationLayerStep.call(BuildAndCacheApplicationLayerStep.java:107)
    at com.google.cloud.tools.jib.builder.steps.BuildAndCacheApplicationLayerStep.call(BuildAndCacheApplicationLayerStep.java:37)
    ... 6 more

The code in question: https://github.com/GoogleContainerTools/jib/blob/a692b8b0cde1f982f6e28309dd08ca80460950e2/jib-core/src/main/java/com/google/cloud/tools/jib/image/ReproducibleLayerBuilder.java#L73-L76

The layer configurations had been built with LayerConfiguration.Builder.addEntryRecursive()

In this case, tarEntryPath.getName() is / (the root directory). namePath is a sun.nio.fs.UnixPath on "/" with filesystem of type sun.nio.fs.MacOSXFileSystem. But namePath.getParent() returns null, which adheres to the contract of Path#getParent(): Returns the parent path, or null if this path does not have a parent.

So an obvious fix is to change this guard to

 if (namePath.getParent() == null || namePath.getParent() != namePath.getRoot()) {

But should we be adding a tar entry for the root directory?

briandealwis commented 4 years ago

Reproducible with java -jar cram-xxx.jar --docker gcr.io/distroless/static image target where target is a directory. This causes target to be placed in /. Specifying target:/path works as /path's parent is not null.

TadCordle commented 4 years ago

I don't think there's any harm in having an explicit entry for /, is there? Maybe we can just go with the quick fix you suggested.

loosebazooka commented 4 years ago

Lets not generate this entry: / for now.

wilx commented 4 years ago

Adding a directory into "/" was the first thing I tried. It would be nice if this worked.