Skip to content

Commit ecef669

Browse files
committed
feat(registry): select the linux/amd64 manifest out of an image index
1 parent 6eb6230 commit ecef669

3 files changed

Lines changed: 156 additions & 0 deletions

File tree

Lines changed: 61 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,61 @@
1+
package io.github.intisy.docker.registry;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonArray;
5+
import com.google.gson.JsonElement;
6+
import com.google.gson.JsonObject;
7+
8+
import java.io.IOException;
9+
import java.util.ArrayList;
10+
import java.util.List;
11+
12+
/**
13+
* Picks one platform's manifest out of a multi-architecture index.
14+
*
15+
* @author Finn Birich
16+
*/
17+
public final class PlatformSelector {
18+
private static final Gson GSON = new Gson();
19+
20+
public static boolean isIndex(String mediaType) {
21+
if (mediaType == null) {
22+
return false;
23+
}
24+
String bare = mediaType.split(";")[0].trim();
25+
return RegistryClient.OCI_INDEX.equals(bare) || RegistryClient.DOCKER_MANIFEST_LIST.equals(bare);
26+
}
27+
28+
/**
29+
* @throws IOException when no entry matches, naming every platform the index does carry, so
30+
* the operator sees why rather than only that.
31+
*/
32+
public static String digestFor(String indexJson, String os, String architecture) throws IOException {
33+
JsonObject index = GSON.fromJson(indexJson, JsonObject.class);
34+
JsonArray manifests = index.getAsJsonArray("manifests");
35+
List<String> available = new ArrayList<String>();
36+
if (manifests != null) {
37+
for (JsonElement element : manifests) {
38+
JsonObject entry = element.getAsJsonObject();
39+
JsonObject platform = entry.getAsJsonObject("platform");
40+
if (platform == null) {
41+
continue;
42+
}
43+
String entryOs = asString(platform, "os");
44+
String entryArchitecture = asString(platform, "architecture");
45+
available.add(entryOs + "/" + entryArchitecture);
46+
if (os.equals(entryOs) && architecture.equals(entryArchitecture)) {
47+
return asString(entry, "digest");
48+
}
49+
}
50+
}
51+
throw new IOException("index carries no " + os + "/" + architecture
52+
+ " manifest; it offers " + available);
53+
}
54+
55+
private static String asString(JsonObject object, String field) {
56+
JsonElement value = object.get(field);
57+
return value == null || value.isJsonNull() ? null : value.getAsString();
58+
}
59+
60+
private PlatformSelector() {}
61+
}
Lines changed: 70 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,70 @@
1+
package io.github.intisy.docker.unit;
2+
3+
import io.github.intisy.docker.registry.PlatformSelector;
4+
import io.github.intisy.docker.registry.RegistryClient;
5+
import org.junit.jupiter.api.Tag;
6+
import org.junit.jupiter.api.Test;
7+
8+
import java.io.IOException;
9+
import java.io.InputStream;
10+
import java.io.ByteArrayOutputStream;
11+
import java.nio.charset.StandardCharsets;
12+
13+
import static org.junit.jupiter.api.Assertions.assertEquals;
14+
import static org.junit.jupiter.api.Assertions.assertFalse;
15+
import static org.junit.jupiter.api.Assertions.assertTrue;
16+
import static org.junit.jupiter.api.Assertions.fail;
17+
18+
@Tag("unit")
19+
public class PlatformSelectorTest {
20+
21+
/**
22+
* The arm64 entry is deliberately first in the fixture. Taking the first entry is the bug this
23+
* test exists to prevent: it yields an image that pulls perfectly and then fails to exec.
24+
*/
25+
@Test
26+
public void picksAmd64EvenWhenArm64ComesFirst() throws IOException {
27+
assertEquals("sha256:2222222222222222222222222222222222222222222222222222222222222222",
28+
PlatformSelector.digestFor(fixture(), "linux", "amd64"));
29+
}
30+
31+
@Test
32+
public void missingPlatformFailsAndNamesWhatWasAvailable() throws IOException {
33+
try {
34+
PlatformSelector.digestFor(fixture(), "linux", "s390x");
35+
fail("expected an IOException");
36+
} catch (IOException expected) {
37+
assertTrue(expected.getMessage().contains("linux/s390x"), expected.getMessage());
38+
assertTrue(expected.getMessage().contains("linux/amd64"), expected.getMessage());
39+
}
40+
}
41+
42+
@Test
43+
public void indexMediaTypesAreRecognisedAndManifestsAreNot() {
44+
assertTrue(PlatformSelector.isIndex(RegistryClient.OCI_INDEX));
45+
assertTrue(PlatformSelector.isIndex(RegistryClient.DOCKER_MANIFEST_LIST));
46+
assertFalse(PlatformSelector.isIndex(RegistryClient.OCI_MANIFEST));
47+
assertFalse(PlatformSelector.isIndex(null));
48+
}
49+
50+
/**
51+
* Registries append parameters to Content-Type. Matching the header exactly would classify a
52+
* real index as a plain manifest and then try to read layers out of it.
53+
*/
54+
@Test
55+
public void indexMediaTypeWithParametersStillCounts() {
56+
assertTrue(PlatformSelector.isIndex(RegistryClient.OCI_INDEX + "; charset=utf-8"));
57+
}
58+
59+
private static String fixture() throws IOException {
60+
InputStream in = PlatformSelectorTest.class.getResourceAsStream("/registry/temurin-index.json");
61+
ByteArrayOutputStream out = new ByteArrayOutputStream();
62+
byte[] buffer = new byte[8192];
63+
int read;
64+
while ((read = in.read(buffer)) != -1) {
65+
out.write(buffer, 0, read);
66+
}
67+
in.close();
68+
return new String(out.toByteArray(), StandardCharsets.UTF_8);
69+
}
70+
}
Lines changed: 25 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,25 @@
1+
{
2+
"schemaVersion": 2,
3+
"mediaType": "application/vnd.oci.image.index.v1+json",
4+
"manifests": [
5+
{
6+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
7+
"digest": "sha256:1111111111111111111111111111111111111111111111111111111111111111",
8+
"size": 1234,
9+
"platform": { "architecture": "arm64", "os": "linux" }
10+
},
11+
{
12+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
13+
"digest": "sha256:2222222222222222222222222222222222222222222222222222222222222222",
14+
"size": 1234,
15+
"platform": { "architecture": "amd64", "os": "linux" }
16+
},
17+
{
18+
"mediaType": "application/vnd.oci.image.manifest.v1+json",
19+
"digest": "sha256:3333333333333333333333333333333333333333333333333333333333333333",
20+
"size": 566,
21+
"platform": { "architecture": "unknown", "os": "unknown" },
22+
"annotations": { "vnd.docker.reference.type": "attestation-manifest" }
23+
}
24+
]
25+
}

0 commit comments

Comments
 (0)