Skip to content

Commit 9e83930

Browse files
committed
feat(registry): add an oci distribution v2 client with no daemon dependency
1 parent d169a12 commit 9e83930

8 files changed

Lines changed: 693 additions & 0 deletions

File tree

Lines changed: 52 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package io.github.intisy.docker.registry;
2+
3+
import java.io.ByteArrayInputStream;
4+
import java.io.InputStream;
5+
6+
/**
7+
* A request body, either fully in memory or streamed with a known length. A blob can be a hundred
8+
* megabytes, so the streaming form exists to keep it off the heap.
9+
*
10+
* @author Finn Birich
11+
*/
12+
public final class RegistryBody {
13+
private static final RegistryBody NONE = new RegistryBody(null, null, -1);
14+
15+
private final byte[] bytes;
16+
private final InputStream stream;
17+
private final long length;
18+
19+
private RegistryBody(byte[] bytes, InputStream stream, long length) {
20+
this.bytes = bytes;
21+
this.stream = stream;
22+
this.length = length;
23+
}
24+
25+
public static RegistryBody none() {
26+
return NONE;
27+
}
28+
29+
public static RegistryBody ofBytes(byte[] content) {
30+
return new RegistryBody(content, null, content.length);
31+
}
32+
33+
public static RegistryBody ofStream(InputStream content, long length) {
34+
return new RegistryBody(null, content, length);
35+
}
36+
37+
public boolean isPresent() {
38+
return bytes != null || stream != null;
39+
}
40+
41+
public byte[] bytes() {
42+
return bytes;
43+
}
44+
45+
public long length() {
46+
return length;
47+
}
48+
49+
public InputStream openStream() {
50+
return stream != null ? stream : new ByteArrayInputStream(bytes == null ? new byte[0] : bytes);
51+
}
52+
}
Lines changed: 148 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,148 @@
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+
}
Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,15 @@
1+
package io.github.intisy.docker.registry;
2+
3+
import java.io.IOException;
4+
import java.util.Map;
5+
6+
/**
7+
* The one seam between the registry client and the network. Implemented for real by
8+
* {@link UrlRegistryHttp} and by a recording fake in the tests.
9+
*
10+
* @author Finn Birich
11+
*/
12+
public interface RegistryHttp {
13+
RegistryResponse send(String method, String url, Map<String, String> headers, RegistryBody body)
14+
throws IOException;
15+
}
Lines changed: 47 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,47 @@
1+
package io.github.intisy.docker.registry;
2+
3+
import java.nio.charset.StandardCharsets;
4+
import java.util.Collections;
5+
import java.util.HashMap;
6+
import java.util.Map;
7+
import java.util.TreeMap;
8+
9+
/**
10+
* @implNote headers are held case-insensitively because {@code HttpURLConnection} preserves
11+
* whatever casing the server sent, and registries disagree about {@code Docker-Content-Digest}.
12+
*
13+
* @author Finn Birich
14+
*/
15+
public final class RegistryResponse {
16+
private final int status;
17+
private final Map<String, String> headers;
18+
private final byte[] body;
19+
20+
public RegistryResponse(int status, Map<String, String> headers, byte[] body) {
21+
this.status = status;
22+
Map<String, String> insensitive = new TreeMap<String, String>(String.CASE_INSENSITIVE_ORDER);
23+
insensitive.putAll(headers == null ? new HashMap<String, String>() : headers);
24+
this.headers = Collections.unmodifiableMap(insensitive);
25+
this.body = body == null ? new byte[0] : body;
26+
}
27+
28+
public int status() {
29+
return status;
30+
}
31+
32+
public boolean isSuccessful() {
33+
return status >= 200 && status < 300;
34+
}
35+
36+
public String header(String name) {
37+
return headers.get(name);
38+
}
39+
40+
public byte[] bytes() {
41+
return body;
42+
}
43+
44+
public String text() {
45+
return new String(body, StandardCharsets.UTF_8);
46+
}
47+
}

0 commit comments

Comments
 (0)