Skip to content

Commit 746cbfc

Browse files
committed
feat(registry): assemble a new image config and manifest from a base plus one layer
1 parent 284cb43 commit 746cbfc

5 files changed

Lines changed: 396 additions & 0 deletions

File tree

Lines changed: 141 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,141 @@
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.JsonObject;
6+
7+
import java.nio.charset.StandardCharsets;
8+
import java.util.List;
9+
10+
/**
11+
* Rewrites a base image's config and manifest so that they carry one additional layer.
12+
*
13+
* @author Finn Birich
14+
*/
15+
public final class ImageAssembler {
16+
private static final Gson GSON = new Gson();
17+
18+
public static final class Assembled {
19+
private final String configJson;
20+
private final String configDigest;
21+
private final long configSize;
22+
private final String manifestJson;
23+
private final String manifestMediaType;
24+
25+
Assembled(String configJson, String configDigest, long configSize,
26+
String manifestJson, String manifestMediaType) {
27+
this.configJson = configJson;
28+
this.configDigest = configDigest;
29+
this.configSize = configSize;
30+
this.manifestJson = manifestJson;
31+
this.manifestMediaType = manifestMediaType;
32+
}
33+
34+
public String configJson() {
35+
return configJson;
36+
}
37+
38+
public String configDigest() {
39+
return configDigest;
40+
}
41+
42+
public long configSize() {
43+
return configSize;
44+
}
45+
46+
public String manifestJson() {
47+
return manifestJson;
48+
}
49+
50+
public String manifestMediaType() {
51+
return manifestMediaType;
52+
}
53+
}
54+
55+
public static Assembled append(String baseConfigJson, String baseManifestJson,
56+
Layer layer, ImageConfigOverrides overrides) {
57+
JsonObject config = GSON.fromJson(baseConfigJson, JsonObject.class);
58+
config.getAsJsonObject("rootfs").getAsJsonArray("diff_ids").add(layer.diffId());
59+
addHistory(config);
60+
applyOverrides(config, overrides);
61+
62+
String configJson = GSON.toJson(config);
63+
byte[] configBytes = configJson.getBytes(StandardCharsets.UTF_8);
64+
String configDigest = Digests.sha256(configBytes);
65+
66+
JsonObject manifest = GSON.fromJson(baseManifestJson, JsonObject.class);
67+
JsonObject configDescriptor = manifest.getAsJsonObject("config");
68+
configDescriptor.addProperty("digest", configDigest);
69+
configDescriptor.addProperty("size", configBytes.length);
70+
71+
JsonObject layerDescriptor = new JsonObject();
72+
layerDescriptor.addProperty("mediaType", layerMediaTypeMatching(manifest));
73+
layerDescriptor.addProperty("digest", layer.digest());
74+
layerDescriptor.addProperty("size", layer.size());
75+
manifest.getAsJsonArray("layers").add(layerDescriptor);
76+
77+
String mediaType = manifest.has("mediaType")
78+
? manifest.get("mediaType").getAsString()
79+
: RegistryClient.OCI_MANIFEST;
80+
return new Assembled(configJson, configDigest, configBytes.length, GSON.toJson(manifest), mediaType);
81+
}
82+
83+
/**
84+
* @implNote a docker-schema2 manifest and an OCI manifest use different layer media types, and
85+
* mixing them in one manifest is rejected. Taking the type from the layers already there keeps
86+
* the assembled manifest internally consistent whichever shape the base image used.
87+
*/
88+
private static String layerMediaTypeMatching(JsonObject manifest) {
89+
JsonArray layers = manifest.getAsJsonArray("layers");
90+
if (layers != null && layers.size() > 0) {
91+
JsonObject first = layers.get(0).getAsJsonObject();
92+
if (first.has("mediaType")) {
93+
return first.get("mediaType").getAsString();
94+
}
95+
}
96+
return RegistryClient.OCI_LAYER_GZIP;
97+
}
98+
99+
private static void addHistory(JsonObject config) {
100+
JsonArray history = config.getAsJsonArray("history");
101+
if (history == null) {
102+
history = new JsonArray();
103+
config.add("history", history);
104+
}
105+
JsonObject entry = new JsonObject();
106+
entry.addProperty("created_by", "docker-java registry assembler");
107+
history.add(entry);
108+
}
109+
110+
private static void applyOverrides(JsonObject config, ImageConfigOverrides overrides) {
111+
JsonObject inner = config.getAsJsonObject("config");
112+
if (inner == null) {
113+
inner = new JsonObject();
114+
config.add("config", inner);
115+
}
116+
if (overrides.entrypoint() != null) {
117+
inner.add("Entrypoint", toArray(overrides.entrypoint()));
118+
}
119+
if (overrides.clearCmd()) {
120+
inner.remove("Cmd");
121+
} else if (overrides.cmd() != null) {
122+
inner.add("Cmd", toArray(overrides.cmd()));
123+
}
124+
if (overrides.env() != null) {
125+
inner.add("Env", toArray(overrides.env()));
126+
}
127+
if (overrides.workingDir() != null) {
128+
inner.addProperty("WorkingDir", overrides.workingDir());
129+
}
130+
}
131+
132+
private static JsonArray toArray(List<String> values) {
133+
JsonArray array = new JsonArray();
134+
for (String value : values) {
135+
array.add(value);
136+
}
137+
return array;
138+
}
139+
140+
private ImageAssembler() {}
141+
}
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 java.util.List;
4+
5+
/**
6+
* What to stamp into the assembled image's config.
7+
*
8+
* @implNote a null entrypoint or cmd means "leave the base value alone"; an empty list means
9+
* "clear it". The difference matters: a base JRE image ships a Cmd that must be cleared or the
10+
* container runs that instead of ours.
11+
*
12+
* @author Finn Birich
13+
*/
14+
public final class ImageConfigOverrides {
15+
private List<String> entrypoint;
16+
private List<String> cmd;
17+
private List<String> env;
18+
private String workingDir;
19+
private boolean clearCmd;
20+
21+
public ImageConfigOverrides withEntrypoint(List<String> value) {
22+
this.entrypoint = value;
23+
return this;
24+
}
25+
26+
public ImageConfigOverrides withCmd(List<String> value) {
27+
this.cmd = value;
28+
this.clearCmd = value == null;
29+
return this;
30+
}
31+
32+
public ImageConfigOverrides withEnv(List<String> value) {
33+
this.env = value;
34+
return this;
35+
}
36+
37+
public ImageConfigOverrides withWorkingDir(String value) {
38+
this.workingDir = value;
39+
return this;
40+
}
41+
42+
List<String> entrypoint() {
43+
return entrypoint;
44+
}
45+
46+
List<String> cmd() {
47+
return cmd;
48+
}
49+
50+
boolean clearCmd() {
51+
return clearCmd;
52+
}
53+
54+
List<String> env() {
55+
return env;
56+
}
57+
58+
String workingDir() {
59+
return workingDir;
60+
}
61+
}
Lines changed: 155 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,155 @@
1+
package io.github.intisy.docker.unit;
2+
3+
import com.google.gson.Gson;
4+
import com.google.gson.JsonObject;
5+
import io.github.intisy.docker.registry.Digests;
6+
import io.github.intisy.docker.registry.ImageAssembler;
7+
import io.github.intisy.docker.registry.ImageConfigOverrides;
8+
import io.github.intisy.docker.registry.Layer;
9+
import org.junit.jupiter.api.Tag;
10+
import org.junit.jupiter.api.Test;
11+
12+
import java.io.ByteArrayOutputStream;
13+
import java.io.IOException;
14+
import java.io.InputStream;
15+
import java.nio.charset.StandardCharsets;
16+
import java.nio.file.Paths;
17+
import java.util.Arrays;
18+
19+
import static org.junit.jupiter.api.Assertions.assertEquals;
20+
import static org.junit.jupiter.api.Assertions.assertTrue;
21+
22+
@Tag("unit")
23+
public class ImageAssemblerTest {
24+
private static final Gson GSON = new Gson();
25+
private static final String NEW_DIFF_ID =
26+
"sha256:1234123412341234123412341234123412341234123412341234123412341234";
27+
private static final String NEW_DIGEST =
28+
"sha256:5678567856785678567856785678567856785678567856785678567856785678";
29+
30+
private static Layer layer() {
31+
return new Layer(Paths.get("layer.tar.gz"), NEW_DIFF_ID, NEW_DIGEST, 4096L);
32+
}
33+
34+
private static ImageConfigOverrides overrides() {
35+
return new ImageConfigOverrides()
36+
.withEntrypoint(Arrays.asList("/opt/spisor/launcher/bin/launcher",
37+
"--registry=/var/lib/spisor/registry"))
38+
.withCmd(null)
39+
.withWorkingDir("/var/lib/spisor");
40+
}
41+
42+
private static ImageAssembler.Assembled assemble() throws IOException {
43+
return ImageAssembler.append(fixture("base-config.json"), fixture("base-manifest.json"),
44+
layer(), overrides());
45+
}
46+
47+
@Test
48+
public void theNewDiffIdIsAppendedLast() throws IOException {
49+
JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class);
50+
com.google.gson.JsonArray diffIds = config.getAsJsonObject("rootfs").getAsJsonArray("diff_ids");
51+
52+
assertEquals(3, diffIds.size());
53+
assertEquals(NEW_DIFF_ID, diffIds.get(2).getAsString());
54+
}
55+
56+
@Test
57+
public void theNewLayerDescriptorIsAppendedLastWithItsCompressedDigestAndSize() throws IOException {
58+
JsonObject manifest = GSON.fromJson(assemble().manifestJson(), JsonObject.class);
59+
com.google.gson.JsonArray layers = manifest.getAsJsonArray("layers");
60+
61+
assertEquals(3, layers.size());
62+
JsonObject added = layers.get(2).getAsJsonObject();
63+
assertEquals(NEW_DIGEST, added.get("digest").getAsString());
64+
assertEquals(4096L, added.get("size").getAsLong());
65+
}
66+
67+
@Test
68+
public void entrypointAndWorkingDirAreStampedAndCmdIsCleared() throws IOException {
69+
JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class)
70+
.getAsJsonObject("config");
71+
72+
assertEquals("/opt/spisor/launcher/bin/launcher",
73+
config.getAsJsonArray("Entrypoint").get(0).getAsString());
74+
assertEquals("--registry=/var/lib/spisor/registry",
75+
config.getAsJsonArray("Entrypoint").get(1).getAsString());
76+
assertEquals("/var/lib/spisor", config.get("WorkingDir").getAsString());
77+
assertTrue(config.get("Cmd") == null || config.get("Cmd").isJsonNull(),
78+
"a base Cmd left in place would run jshell instead of the launcher");
79+
}
80+
81+
@Test
82+
public void theBaseEnvironmentSurvives() throws IOException {
83+
JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class)
84+
.getAsJsonObject("config");
85+
86+
assertTrue(config.getAsJsonArray("Env").toString().contains("JAVA_HOME=/opt/java/openjdk"),
87+
"dropping the base env would remove JAVA_HOME and PATH and the launcher would not start");
88+
}
89+
90+
/**
91+
* The manifest's config descriptor must describe the config document we actually produced.
92+
* If the digest or the size disagrees by one byte the registry rejects the manifest, and the
93+
* message it gives is not obviously about this.
94+
*/
95+
@Test
96+
public void theManifestConfigDescriptorMatchesTheProducedConfigExactly() throws IOException {
97+
ImageAssembler.Assembled assembled = assemble();
98+
JsonObject manifest = GSON.fromJson(assembled.manifestJson(), JsonObject.class);
99+
JsonObject descriptor = manifest.getAsJsonObject("config");
100+
101+
byte[] configBytes = assembled.configJson().getBytes(StandardCharsets.UTF_8);
102+
assertEquals(Digests.sha256(configBytes), descriptor.get("digest").getAsString());
103+
assertEquals(configBytes.length, descriptor.get("size").getAsLong());
104+
assertEquals(assembled.configDigest(), descriptor.get("digest").getAsString());
105+
assertEquals(configBytes.length, assembled.configSize());
106+
}
107+
108+
@Test
109+
public void aHistoryEntryIsAdded() throws IOException {
110+
JsonObject config = GSON.fromJson(assemble().configJson(), JsonObject.class);
111+
assertEquals(2, config.getAsJsonArray("history").size());
112+
}
113+
114+
@Test
115+
public void anOciBaseProducesAnOciManifest() throws IOException {
116+
assertEquals("application/vnd.oci.image.manifest.v1+json", assemble().manifestMediaType());
117+
}
118+
119+
/**
120+
* A docker-schema2 manifest and an OCI manifest use different layer media types, and a
121+
* manifest that mixes them is rejected. Taking the added layer's type from the layers already
122+
* present is what keeps an assembled manifest internally consistent whichever shape the base
123+
* image happened to use, and base images in the wild are both.
124+
*/
125+
@Test
126+
public void aDockerSchema2BaseKeepsDockerMediaTypesThroughout() throws IOException {
127+
String dockerManifest = fixture("base-manifest.json")
128+
.replace("application/vnd.oci.image.manifest.v1+json",
129+
"application/vnd.docker.distribution.manifest.v2+json")
130+
.replace("application/vnd.oci.image.config.v1+json",
131+
"application/vnd.docker.container.image.v1+json")
132+
.replace("application/vnd.oci.image.layer.v1.tar+gzip",
133+
"application/vnd.docker.image.rootfs.diff.tar.gzip");
134+
135+
ImageAssembler.Assembled assembled =
136+
ImageAssembler.append(fixture("base-config.json"), dockerManifest, layer(), overrides());
137+
138+
assertEquals("application/vnd.docker.distribution.manifest.v2+json", assembled.manifestMediaType());
139+
JsonObject manifest = GSON.fromJson(assembled.manifestJson(), JsonObject.class);
140+
assertEquals("application/vnd.docker.image.rootfs.diff.tar.gzip",
141+
manifest.getAsJsonArray("layers").get(2).getAsJsonObject().get("mediaType").getAsString());
142+
}
143+
144+
private static String fixture(String name) throws IOException {
145+
InputStream in = ImageAssemblerTest.class.getResourceAsStream("/registry/" + name);
146+
ByteArrayOutputStream out = new ByteArrayOutputStream();
147+
byte[] buffer = new byte[8192];
148+
int read;
149+
while ((read = in.read(buffer)) != -1) {
150+
out.write(buffer, 0, read);
151+
}
152+
in.close();
153+
return new String(out.toByteArray(), StandardCharsets.UTF_8);
154+
}
155+
}
Lines changed: 18 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,18 @@
1+
{
2+
"architecture": "amd64",
3+
"os": "linux",
4+
"config": {
5+
"Env": ["PATH=/usr/local/bin:/usr/bin:/bin", "JAVA_HOME=/opt/java/openjdk"],
6+
"Cmd": ["jshell"]
7+
},
8+
"rootfs": {
9+
"type": "layers",
10+
"diff_ids": [
11+
"sha256:aaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaaa",
12+
"sha256:bbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbbb"
13+
]
14+
},
15+
"history": [
16+
{ "created": "2026-01-01T00:00:00Z", "created_by": "base" }
17+
]
18+
}

0 commit comments

Comments
 (0)