|
| 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. The |
| 87 | + * {@code OCI_LAYER_GZIP} fallback only applies when the base manifest has no layers at all; a |
| 88 | + * real base image always has at least one, so this never mixes a Docker manifest with an OCI |
| 89 | + * layer type in practice. |
| 90 | + */ |
| 91 | + private static String layerMediaTypeMatching(JsonObject manifest) { |
| 92 | + JsonArray layers = manifest.getAsJsonArray("layers"); |
| 93 | + if (layers != null && layers.size() > 0) { |
| 94 | + JsonObject first = layers.get(0).getAsJsonObject(); |
| 95 | + if (first.has("mediaType")) { |
| 96 | + return first.get("mediaType").getAsString(); |
| 97 | + } |
| 98 | + } |
| 99 | + return RegistryClient.OCI_LAYER_GZIP; |
| 100 | + } |
| 101 | + |
| 102 | + /** |
| 103 | + * @implNote no {@code created} timestamp is written deliberately: a wall-clock value would |
| 104 | + * change the config bytes, and therefore the config digest and manifest, on every build, so |
| 105 | + * two builds of identical inputs would push different images. Same determinism property as |
| 106 | + * why {@link LayerBuilder} pins mtime to zero. |
| 107 | + */ |
| 108 | + private static void addHistory(JsonObject config) { |
| 109 | + JsonArray history = config.getAsJsonArray("history"); |
| 110 | + if (history == null) { |
| 111 | + history = new JsonArray(); |
| 112 | + config.add("history", history); |
| 113 | + } |
| 114 | + JsonObject entry = new JsonObject(); |
| 115 | + entry.addProperty("created_by", "docker-java registry assembler"); |
| 116 | + history.add(entry); |
| 117 | + } |
| 118 | + |
| 119 | + private static void applyOverrides(JsonObject config, ImageConfigOverrides overrides) { |
| 120 | + JsonObject inner = config.getAsJsonObject("config"); |
| 121 | + if (inner == null) { |
| 122 | + inner = new JsonObject(); |
| 123 | + config.add("config", inner); |
| 124 | + } |
| 125 | + if (overrides.entrypoint() != null) { |
| 126 | + inner.add("Entrypoint", toArray(overrides.entrypoint())); |
| 127 | + } |
| 128 | + if (overrides.clearCmd()) { |
| 129 | + inner.remove("Cmd"); |
| 130 | + } else if (overrides.cmd() != null) { |
| 131 | + inner.add("Cmd", toArray(overrides.cmd())); |
| 132 | + } |
| 133 | + if (overrides.env() != null) { |
| 134 | + inner.add("Env", toArray(overrides.env())); |
| 135 | + } |
| 136 | + if (overrides.workingDir() != null) { |
| 137 | + inner.addProperty("WorkingDir", overrides.workingDir()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static JsonArray toArray(List<String> values) { |
| 142 | + JsonArray array = new JsonArray(); |
| 143 | + for (String value : values) { |
| 144 | + array.add(value); |
| 145 | + } |
| 146 | + return array; |
| 147 | + } |
| 148 | + |
| 149 | + private ImageAssembler() {} |
| 150 | +} |
0 commit comments