Skip to content

Commit 284cb43

Browse files
committed
fix(registry): sort layer entries by archive name, not platform path ordering
1 parent e4088aa commit 284cb43

2 files changed

Lines changed: 50 additions & 10 deletions

File tree

‎src/main/java/io/github/intisy/docker/registry/LayerBuilder.java‎

Lines changed: 26 additions & 9 deletions
Original file line numberDiff line numberDiff line change
@@ -14,6 +14,7 @@
1414
import java.nio.file.attribute.BasicFileAttributes;
1515
import java.util.ArrayList;
1616
import java.util.Collections;
17+
import java.util.Comparator;
1718
import java.util.List;
1819
import java.util.zip.GZIPOutputStream;
1920

@@ -32,7 +33,7 @@ public final class LayerBuilder {
3233
private static final int DIRECTORY_MODE = 0755;
3334
private static final int BUFFER_BYTES = 64 * 1024;
3435

35-
public static Layer fromDirectory(Path source, String pathInImage, Path outputFile) throws IOException {
36+
public static Layer fromDirectory(final Path source, String pathInImage, Path outputFile) throws IOException {
3637
final List<Path> files = new ArrayList<Path>();
3738
Files.walkFileTree(source, new SimpleFileVisitor<Path>() {
3839
@Override
@@ -41,7 +42,12 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
4142
return FileVisitResult.CONTINUE;
4243
}
4344
});
44-
Collections.sort(files);
45+
Collections.sort(files, new Comparator<Path>() {
46+
@Override
47+
public int compare(Path a, Path b) {
48+
return archiveName(source, a).compareTo(archiveName(source, b));
49+
}
50+
});
4551

4652
String prefix = pathInImage.startsWith("/") ? pathInImage.substring(1) : pathInImage;
4753
if (!prefix.endsWith("/")) {
@@ -60,7 +66,7 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
6066
tar.closeArchiveEntry();
6167
}
6268
for (Path file : files) {
63-
String name = prefix + source.relativize(file).toString().replace('\\', '/');
69+
String name = prefix + archiveName(source, file);
6470
long size = Files.size(file);
6571
TarArchiveEntry entry = new TarArchiveEntry(name);
6672
pin(entry, isUnderBin(name) ? EXECUTABLE_MODE : REGULAR_MODE, size);
@@ -83,14 +89,14 @@ public FileVisitResult visitFile(Path file, BasicFileAttributes attributes) {
8389
* would be non-executable exactly on the machine this project is developed on.
8490
*/
8591
static boolean isUnderBin(String nameInArchive) {
86-
return nameInArchive.contains("/bin/");
92+
return nameInArchive.contains("/bin/") || nameInArchive.startsWith("bin/");
8793
}
8894

8995
static List<String> directoriesFor(String prefix, Path source, List<Path> files) {
9096
List<String> directories = new ArrayList<String>();
9197
java.util.Set<String> seen = new java.util.TreeSet<String>();
9298
for (Path file : files) {
93-
String relative = source.relativize(file).toString().replace('\\', '/');
99+
String relative = archiveName(source, file);
94100
int slash = relative.indexOf('/');
95101
while (slash >= 0) {
96102
seen.add(prefix + relative.substring(0, slash) + "/");
@@ -107,12 +113,23 @@ static List<String> directoriesFor(String prefix, Path source, List<Path> files)
107113
return directories;
108114
}
109115

116+
/**
117+
* @implNote {@code Path} natural ordering is case-insensitive on Windows and case-sensitive on
118+
* Linux, so sorting by {@code Path} directly would order two names differing only by case
119+
* differently depending on the building platform, changing the digest with it. This is the
120+
* string that is actually written to the archive, so sorting by it is platform-independent.
121+
*/
122+
static String archiveName(Path source, Path file) {
123+
return source.relativize(file).toString().replace('\\', '/');
124+
}
125+
110126
/**
111127
* @implNote the uid, gid and owner name calls are redundant with commons-compress 1.24.0's own
112-
* defaults for the {@code TarArchiveEntry(String)} constructor used above, and are kept
113-
* deliberately: the library sources {@code userName} from {@code System.getProperty("user.name")}
114-
* on another of its constructor paths, so a future constructor change here would otherwise leak
115-
* the building machine's identity into the layer and silently break cross-machine determinism.
128+
* defaults for the {@code TarArchiveEntry(String)} constructor used above: it reads
129+
* {@code userName} from {@code System.getProperty("user.name")} during construction and resets
130+
* it to {@code ""} immediately afterward, in that same overload's own body. The calls stay to
131+
* guard against a future change to which constructor is used, which would otherwise leak the
132+
* building machine's identity into the layer and silently break cross-machine determinism.
116133
*/
117134
private static void pin(TarArchiveEntry entry, int mode, long size) {
118135
entry.setMode((entry.isDirectory() ? TarArchiveEntry.DEFAULT_DIR_MODE : 0) | mode);

‎src/test/java/io/github/intisy/docker/unit/LayerBuilderTest.java‎

Lines changed: 24 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -99,7 +99,7 @@ public void diffIdIsTheUncompressedDigestAndDigestIsTheCompressedOne(@TempDir Pa
9999
* is pinned at all.
100100
*/
101101
@Test
102-
void everyEntryHasItsVaryingMetadataPinned(@TempDir Path tmp) throws IOException {
102+
public void everyEntryHasItsVaryingMetadataPinned(@TempDir Path tmp) throws IOException {
103103
Layer layer = LayerBuilder.fromDirectory(
104104
launcherLikeTree(tmp), "/opt/spisor/launcher", tmp.resolve("layer.tar.gz"));
105105

@@ -121,6 +121,29 @@ void everyEntryHasItsVaryingMetadataPinned(@TempDir Path tmp) throws IOException
121121
}
122122
}
123123

124+
/**
125+
* {@code java.nio.file.Path} natural ordering is case-insensitive on Windows and
126+
* case-sensitive on Linux, so sorting {@code Path} objects directly would order these two
127+
* files differently depending on which platform built the layer, changing the digest with it.
128+
* The archive must order entries by the normalized name string instead, which is what actually
129+
* gets written and is platform-independent.
130+
*/
131+
@Test
132+
public void entriesAreOrderedByTheArchiveNameNotThePlatformPathOrdering(@TempDir Path tmp) throws IOException {
133+
Path dist = tmp.resolve("dist");
134+
Files.createDirectories(dist);
135+
Files.write(dist.resolve("Zeta.txt"), new byte[] {1});
136+
Files.write(dist.resolve("apple.txt"), new byte[] {1});
137+
138+
Layer layer = LayerBuilder.fromDirectory(dist, "/opt/spisor/launcher", tmp.resolve("layer.tar.gz"));
139+
140+
List<String> names = entryNames(layer.file());
141+
int zeta = names.indexOf("opt/spisor/launcher/Zeta.txt");
142+
int apple = names.indexOf("opt/spisor/launcher/apple.txt");
143+
assertTrue(zeta >= 0 && apple >= 0, names.toString());
144+
assertEquals("Zeta.txt".compareTo("apple.txt") < 0, zeta < apple, names.toString());
145+
}
146+
124147
/**
125148
* @implNote {@link Digests#sha256(InputStream)} does not close its argument; on Windows a
126149
* lingering open handle on the layer file blocks {@code @TempDir} cleanup after the test.

0 commit comments

Comments
 (0)