Skip to content

Commit 00b7709

Browse files
setup_builder: set http/insecure only on the Docker mirror entry so buildx imagetools create can push Docker Hub manifest lists from the fallback builder (#150)
* setup_builder: set http/insecure only on the Docker mirror's registry entry The mirror is the only plain-HTTP endpoint. With http = true on the docker.io entry, buildx read the fallback builder's config and sent imagetools create's manifest PUT to Docker Hub over plain HTTP; the 301 to HTTPS turned it into a GET that returned 404. Docker Hub gets BuildKit's defaults: HTTPS with certificate verification. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> * ci: test that Docker Hub is reached over verified HTTPS A stand-in for registry-1.docker.io runs on the runner: TLS on :443 and, like Docker Hub, a 301 to HTTPS for every plain-HTTP request on :80. - fallback builder: `buildx imagetools create` pushes a manifest list and no request reaches :80. - Blacksmith builder: `buildx build --push` succeeds with a trusted certificate and fails on x509 with an untrusted one. Co-Authored-By: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com> --------- Co-authored-by: Devin AI <158243242+devin-ai-integration[bot]@users.noreply.github.com>
1 parent 8fbda81 commit 00b7709

6 files changed

Lines changed: 211 additions & 4 deletions

File tree

Lines changed: 96 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,96 @@
1+
#!/usr/bin/env bash
2+
# Stands in for Docker Hub on this machine: registry-1.docker.io resolves to a
3+
# local registry that serves TLS on :443 and, like the real Hub, answers every
4+
# plain-HTTP request on :80 with a 301 to HTTPS.
5+
#
6+
# start serve a certificate from a CA added to the system trust store
7+
# untrusted re-serve with a certificate from a CA nothing trusts
8+
# http-log print the requests received on :80
9+
set -euo pipefail
10+
11+
DIR=/tmp/dockerhub-standin
12+
HOST=registry-1.docker.io
13+
HTTP_LOG="$DIR/http-requests.log"
14+
15+
make_cert() {
16+
local name=$1
17+
openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=$name CA" \
18+
-keyout "$DIR/$name-ca.key" -out "$DIR/$name-ca.crt" 2>/dev/null
19+
openssl req -newkey rsa:2048 -nodes -subj "/CN=$HOST" \
20+
-keyout "$DIR/$name.key" -out "$DIR/$name.csr" 2>/dev/null
21+
openssl x509 -req -in "$DIR/$name.csr" -days 1 \
22+
-CA "$DIR/$name-ca.crt" -CAkey "$DIR/$name-ca.key" -CAcreateserial \
23+
-extfile <(echo "subjectAltName=DNS:$HOST") -out "$DIR/$name.crt" 2>/dev/null
24+
}
25+
26+
serve() {
27+
local name=$1
28+
docker rm -f dockerhub-standin >/dev/null 2>&1 || true
29+
docker run -d --name dockerhub-standin -p 127.0.0.1:443:443 \
30+
-v "$DIR:/certs:ro" \
31+
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
32+
-e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/$name.crt" \
33+
-e "REGISTRY_HTTP_TLS_KEY=/certs/$name.key" \
34+
registry:2 >/dev/null
35+
for _ in $(seq 30); do
36+
if curl -fsS --cacert "$DIR/$name-ca.crt" "https://$HOST/v2/" >/dev/null 2>&1; then
37+
return 0
38+
fi
39+
sleep 1
40+
done
41+
echo "stand-in registry did not come up" >&2
42+
docker logs dockerhub-standin >&2
43+
return 1
44+
}
45+
46+
case "${1:-}" in
47+
start)
48+
mkdir -p "$DIR"
49+
# Pull while docker.io still resolves to the real Docker Hub.
50+
docker pull -q registry:2
51+
make_cert trusted
52+
make_cert untrusted
53+
chmod a+r "$DIR"/*.key
54+
sudo cp "$DIR/trusted-ca.crt" /usr/local/share/ca-certificates/dockerhub-standin.crt
55+
sudo update-ca-certificates >/dev/null
56+
57+
cat >"$DIR/redirect.py" <<'PY'
58+
import sys
59+
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer
60+
61+
LOG = sys.argv[1]
62+
63+
64+
class Redirect(BaseHTTPRequestHandler):
65+
def redirect(self):
66+
with open(LOG, "a") as log:
67+
log.write(f"{self.command} {self.path}\n")
68+
self.send_response(301)
69+
self.send_header("Location", f"https://{self.headers['Host']}{self.path}")
70+
self.send_header("Content-Length", "0")
71+
self.end_headers()
72+
73+
do_GET = do_HEAD = do_POST = do_PUT = do_PATCH = do_DELETE = redirect
74+
75+
76+
ThreadingHTTPServer(("127.0.0.1", 80), Redirect).serve_forever()
77+
PY
78+
: >"$HTTP_LOG"
79+
sudo nohup python3 "$DIR/redirect.py" "$HTTP_LOG" >"$DIR/redirect.out" 2>&1 &
80+
81+
echo "127.0.0.1 $HOST" | sudo tee -a /etc/hosts >/dev/null
82+
serve trusted
83+
curl -sS -o /dev/null -w '%{http_code}' "http://$HOST/v2/" | grep -qx 301
84+
: >"$HTTP_LOG"
85+
;;
86+
untrusted)
87+
serve untrusted
88+
;;
89+
http-log)
90+
cat "$HTTP_LOG"
91+
;;
92+
*)
93+
echo "usage: $0 start|untrusted|http-log" >&2
94+
exit 2
95+
;;
96+
esac
Lines changed: 99 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,99 @@
1+
name: Test Docker Hub HTTPS
2+
3+
# Checks that buildx and buildkitd reach Docker Hub over verified HTTPS while
4+
# the Blacksmith mirror stays on HTTP. .github/scripts/dockerhub-standin.sh
5+
# answers as registry-1.docker.io on the runner.
6+
7+
on:
8+
workflow_dispatch:
9+
pull_request:
10+
push:
11+
branches: [main, release/v2]
12+
13+
permissions:
14+
contents: read
15+
16+
env:
17+
IMAGE: docker.io/standin/setup-docker-builder-test
18+
19+
jobs:
20+
fallback-builder:
21+
name: Fallback builder pushes a manifest list over HTTPS
22+
runs-on: blacksmith
23+
steps:
24+
- name: Checkout
25+
uses: actions/checkout@v6
26+
27+
# Nothing listens on this port, so the sticky disk request fails and the
28+
# action warns and falls back to a local builder.
29+
- name: Setup Docker Builder with an unreachable sticky disk service
30+
uses: ./
31+
env:
32+
BLACKSMITH_STICKY_DISK_GRPC_PORT: "1"
33+
34+
- name: Assert the fallback builder is in use
35+
run: |
36+
docker buildx inspect --bootstrap | tee /tmp/builder.txt
37+
grep -Eq '^Driver:\s+docker-container$' /tmp/builder.txt
38+
39+
- name: Start the Docker Hub stand-in
40+
run: .github/scripts/dockerhub-standin.sh start
41+
42+
- name: Push a manifest list with imagetools create
43+
run: |
44+
docker buildx imagetools create -t "$IMAGE:manifest" registry.k8s.io/pause:3.10
45+
docker buildx imagetools inspect "$IMAGE:manifest"
46+
47+
- name: Assert nothing went to Docker Hub over plain HTTP
48+
run: |
49+
.github/scripts/dockerhub-standin.sh http-log
50+
test ! -s /tmp/dockerhub-standin/http-requests.log
51+
52+
blacksmith-builder:
53+
name: Blacksmith builder verifies Docker Hub TLS
54+
runs-on: blacksmith
55+
steps:
56+
- name: Checkout
57+
uses: actions/checkout@v6
58+
59+
# Before buildkitd starts, so it loads the stand-in's CA.
60+
- name: Start the Docker Hub stand-in
61+
run: .github/scripts/dockerhub-standin.sh start
62+
63+
- name: Setup Docker Builder
64+
uses: ./
65+
with:
66+
nofallback: "true"
67+
68+
- name: Assert the Blacksmith builder is in use
69+
run: |
70+
docker buildx inspect | tee /tmp/builder.txt
71+
grep -Eq '^Driver:\s+remote$' /tmp/builder.txt
72+
73+
- name: Write a Dockerfile that pulls nothing
74+
run: |
75+
mkdir -p /tmp/ctx
76+
date >/tmp/ctx/stamp
77+
printf 'FROM scratch\nCOPY stamp /stamp\n' >/tmp/ctx/Dockerfile
78+
79+
- name: Push over HTTPS with a trusted certificate
80+
run: docker buildx build --push -t "$IMAGE:trusted" /tmp/ctx
81+
82+
- name: Assert nothing went to Docker Hub over plain HTTP
83+
run: |
84+
.github/scripts/dockerhub-standin.sh http-log
85+
test ! -s /tmp/dockerhub-standin/http-requests.log
86+
87+
- name: Serve a certificate no CA on the runner trusts
88+
run: .github/scripts/dockerhub-standin.sh untrusted
89+
90+
- name: Assert the push fails on certificate verification
91+
run: |
92+
if docker buildx build --push -t "$IMAGE:untrusted" /tmp/ctx >/tmp/push.log 2>&1; then
93+
cat /tmp/push.log
94+
echo "::error::push succeeded against an untrusted certificate"
95+
exit 1
96+
fi
97+
cat /tmp/push.log
98+
grep -q 'x509' /tmp/push.log
99+
test ! -s /tmp/dockerhub-standin/http-requests.log

‎dist/index.js‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎dist/index.js.map‎

Lines changed: 1 addition & 1 deletion
Some generated files are not rendered by default. Learn more about customizing how changed files appear on GitHub.

‎src/setup-builder.test.ts‎

Lines changed: 11 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
import { describe, it, expect, vi, beforeEach } from "vitest";
22
import * as core from "@actions/core";
33
import * as fs from "fs";
4+
import * as TOML from "@iarna/toml";
45
import * as setupBuilder from "./setup_builder";
56
import { fromBinary } from "@bufbuild/protobuf";
67
import { GetStickyDiskResponseSchema } from "@buf/blacksmith_vm-agent.bufbuild_es/stickydisk/v1/stickydisk_pb.js";
@@ -141,6 +142,16 @@ describe("setup_builder", () => {
141142
expect(config).toContain('mirrors = [ "http://192.168.127.1:5000" ]');
142143
expect(config).toContain('[registry."docker.io"]');
143144
expect(config).toContain('[registry."192.168.127.1:5000"]');
145+
const parsed = TOML.parse(config) as {
146+
registry: Record<string, Record<string, unknown>>;
147+
};
148+
expect(parsed.registry["docker.io"]).toEqual({
149+
mirrors: ["http://192.168.127.1:5000"],
150+
});
151+
expect(parsed.registry["192.168.127.1:5000"]).toEqual({
152+
http: true,
153+
insecure: true,
154+
});
144155
expect(config).not.toContain("[grpc]");
145156
expect(core.info).toHaveBeenCalledWith(
146157
"Wrote Docker container BuildKit config to local-buildkitd.toml",

‎src/setup_builder.ts‎

Lines changed: 3 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -162,12 +162,13 @@ async function getRoutableHostDns(): Promise<string[]> {
162162
return publicDnsFallback;
163163
}
164164

165+
// The mirror is the only plain-HTTP endpoint, so only its own entry carries
166+
// `http`/`insecure`. Setting them on `docker.io` makes buildx send Docker Hub
167+
// requests over HTTP; the redirect to HTTPS turns a manifest PUT into a GET.
165168
function getDockerMirrorRegistryConfig(): TOML.JsonMap {
166169
return {
167170
"docker.io": {
168171
mirrors: ["http://192.168.127.1:5000"],
169-
http: true,
170-
insecure: true,
171172
},
172173
"192.168.127.1:5000": {
173174
http: true,

0 commit comments

Comments
 (0)