|
| 1 | +package io.github.intisy.docker.registry; |
| 2 | + |
| 3 | +import java.io.IOException; |
| 4 | +import java.io.UnsupportedEncodingException; |
| 5 | +import java.net.URLEncoder; |
| 6 | +import java.util.HashMap; |
| 7 | +import java.util.Map; |
| 8 | + |
| 9 | +/** |
| 10 | + * A client for one registry, speaking the OCI distribution v2 API and nothing else. It never |
| 11 | + * contacts a Docker daemon. |
| 12 | + * |
| 13 | + * @author Finn Birich |
| 14 | + */ |
| 15 | +public final class RegistryClient { |
| 16 | + public static final String OCI_MANIFEST = "application/vnd.oci.image.manifest.v1+json"; |
| 17 | + public static final String OCI_INDEX = "application/vnd.oci.image.index.v1+json"; |
| 18 | + public static final String DOCKER_MANIFEST = "application/vnd.docker.distribution.manifest.v2+json"; |
| 19 | + public static final String DOCKER_MANIFEST_LIST = "application/vnd.docker.distribution.manifest.list.v2+json"; |
| 20 | + public static final String OCI_LAYER_GZIP = "application/vnd.oci.image.layer.v1.tar+gzip"; |
| 21 | + public static final String OCI_CONFIG = "application/vnd.oci.image.config.v1+json"; |
| 22 | + |
| 23 | + private static final String ACCEPT_ALL_MANIFESTS = |
| 24 | + OCI_INDEX + "," + OCI_MANIFEST + "," + DOCKER_MANIFEST_LIST + "," + DOCKER_MANIFEST; |
| 25 | + |
| 26 | + private final RegistryHttp http; |
| 27 | + private final String host; |
| 28 | + private final String scheme; |
| 29 | + |
| 30 | + public RegistryClient(RegistryHttp http, String host, boolean plainHttp) { |
| 31 | + this.http = http; |
| 32 | + this.host = host; |
| 33 | + this.scheme = plainHttp ? "http://" : "https://"; |
| 34 | + } |
| 35 | + |
| 36 | + public static final class Manifest { |
| 37 | + private final String mediaType; |
| 38 | + private final String digest; |
| 39 | + private final byte[] body; |
| 40 | + |
| 41 | + Manifest(String mediaType, String digest, byte[] body) { |
| 42 | + this.mediaType = mediaType; |
| 43 | + this.digest = digest; |
| 44 | + this.body = body; |
| 45 | + } |
| 46 | + |
| 47 | + public String mediaType() { |
| 48 | + return mediaType; |
| 49 | + } |
| 50 | + |
| 51 | + public String digest() { |
| 52 | + return digest; |
| 53 | + } |
| 54 | + |
| 55 | + public byte[] bytes() { |
| 56 | + return body; |
| 57 | + } |
| 58 | + |
| 59 | + public String text() { |
| 60 | + return new String(body, java.nio.charset.StandardCharsets.UTF_8); |
| 61 | + } |
| 62 | + } |
| 63 | + |
| 64 | + public Manifest getManifest(ImageReference reference) throws IOException { |
| 65 | + Map<String, String> headers = new HashMap<String, String>(); |
| 66 | + headers.put("Accept", ACCEPT_ALL_MANIFESTS); |
| 67 | + String url = base(reference.repository()) + "/manifests/" + reference.reference(); |
| 68 | + RegistryResponse response = http.send("GET", url, headers, RegistryBody.none()); |
| 69 | + require(response, "GET manifest " + reference); |
| 70 | + String digest = response.header("Docker-Content-Digest"); |
| 71 | + if (digest == null) { |
| 72 | + digest = Digests.sha256(response.bytes()); |
| 73 | + } |
| 74 | + return new Manifest(response.header("Content-Type"), digest, response.bytes()); |
| 75 | + } |
| 76 | + |
| 77 | + public boolean blobExists(String repository, String digest) throws IOException { |
| 78 | + RegistryResponse response = http.send("HEAD", base(repository) + "/blobs/" + digest, |
| 79 | + new HashMap<String, String>(), RegistryBody.none()); |
| 80 | + if (response.status() == 404) { |
| 81 | + return false; |
| 82 | + } |
| 83 | + require(response, "HEAD blob " + digest); |
| 84 | + return true; |
| 85 | + } |
| 86 | + |
| 87 | + public RegistryResponse getBlob(String repository, String digest) throws IOException { |
| 88 | + RegistryResponse response = http.send("GET", base(repository) + "/blobs/" + digest, |
| 89 | + new HashMap<String, String>(), RegistryBody.none()); |
| 90 | + require(response, "GET blob " + digest); |
| 91 | + return response; |
| 92 | + } |
| 93 | + |
| 94 | + /** |
| 95 | + * @implNote the two-step upload (POST for a session, PUT with the digest) rather than the |
| 96 | + * single-POST form, because the monolithic POST is optional in the spec and registries differ |
| 97 | + * about it, while every registry implements the two-step. |
| 98 | + */ |
| 99 | + public void putBlob(String repository, String digest, RegistryBody body) throws IOException { |
| 100 | + RegistryResponse session = http.send("POST", base(repository) + "/blobs/uploads/", |
| 101 | + new HashMap<String, String>(), RegistryBody.none()); |
| 102 | + require(session, "POST blob upload session for " + digest); |
| 103 | + String location = session.header("Location"); |
| 104 | + if (location == null) { |
| 105 | + throw new IOException("registry accepted the upload session for " + digest |
| 106 | + + " but sent no Location header"); |
| 107 | + } |
| 108 | + String uploadUrl = absolute(location) + (location.indexOf('?') >= 0 ? "&" : "?") |
| 109 | + + "digest=" + encode(digest); |
| 110 | + Map<String, String> headers = new HashMap<String, String>(); |
| 111 | + headers.put("Content-Type", "application/octet-stream"); |
| 112 | + RegistryResponse upload = http.send("PUT", uploadUrl, headers, body); |
| 113 | + require(upload, "PUT blob " + digest); |
| 114 | + } |
| 115 | + |
| 116 | + public void putManifest(ImageReference target, String mediaType, byte[] manifestJson) throws IOException { |
| 117 | + Map<String, String> headers = new HashMap<String, String>(); |
| 118 | + headers.put("Content-Type", mediaType); |
| 119 | + RegistryResponse response = http.send("PUT", |
| 120 | + base(target.repository()) + "/manifests/" + target.reference(), |
| 121 | + headers, RegistryBody.ofBytes(manifestJson)); |
| 122 | + require(response, "PUT manifest " + target); |
| 123 | + } |
| 124 | + |
| 125 | + private String base(String repository) { |
| 126 | + return scheme + host + "/v2/" + repository; |
| 127 | + } |
| 128 | + |
| 129 | + private String absolute(String location) { |
| 130 | + return location.startsWith("http://") || location.startsWith("https://") |
| 131 | + ? location |
| 132 | + : scheme + host + location; |
| 133 | + } |
| 134 | + |
| 135 | + private static void require(RegistryResponse response, String what) throws IOException { |
| 136 | + if (!response.isSuccessful()) { |
| 137 | + throw new IOException(what + " failed: HTTP " + response.status() + ": " + response.text()); |
| 138 | + } |
| 139 | + } |
| 140 | + |
| 141 | + private static String encode(String value) { |
| 142 | + try { |
| 143 | + return URLEncoder.encode(value, "UTF-8"); |
| 144 | + } catch (UnsupportedEncodingException impossible) { |
| 145 | + throw new IllegalStateException("UTF-8 is required by every JRE", impossible); |
| 146 | + } |
| 147 | + } |
| 148 | +} |
0 commit comments