Skip to content
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
96 changes: 96 additions & 0 deletions .github/scripts/dockerhub-standin.sh
Original file line number Diff line number Diff line change
@@ -0,0 +1,96 @@
#!/usr/bin/env bash
# Stands in for Docker Hub on this machine: registry-1.docker.io resolves to a
# local registry that serves TLS on :443 and, like the real Hub, answers every
# plain-HTTP request on :80 with a 301 to HTTPS.
#
# start serve a certificate from a CA added to the system trust store
# untrusted re-serve with a certificate from a CA nothing trusts
# http-log print the requests received on :80
set -euo pipefail

DIR=/tmp/dockerhub-standin
HOST=registry-1.docker.io
HTTP_LOG="$DIR/http-requests.log"

make_cert() {
local name=$1
openssl req -x509 -newkey rsa:2048 -nodes -days 1 -subj "/CN=$name CA" \
-keyout "$DIR/$name-ca.key" -out "$DIR/$name-ca.crt" 2>/dev/null
openssl req -newkey rsa:2048 -nodes -subj "/CN=$HOST" \
-keyout "$DIR/$name.key" -out "$DIR/$name.csr" 2>/dev/null
openssl x509 -req -in "$DIR/$name.csr" -days 1 \
-CA "$DIR/$name-ca.crt" -CAkey "$DIR/$name-ca.key" -CAcreateserial \
-extfile <(echo "subjectAltName=DNS:$HOST") -out "$DIR/$name.crt" 2>/dev/null
}

serve() {
local name=$1
docker rm -f dockerhub-standin >/dev/null 2>&1 || true
docker run -d --name dockerhub-standin -p 127.0.0.1:443:443 \
-v "$DIR:/certs:ro" \
-e REGISTRY_HTTP_ADDR=0.0.0.0:443 \
-e "REGISTRY_HTTP_TLS_CERTIFICATE=/certs/$name.crt" \
-e "REGISTRY_HTTP_TLS_KEY=/certs/$name.key" \
registry:2 >/dev/null
for _ in $(seq 30); do
if curl -fsS --cacert "$DIR/$name-ca.crt" "https://$HOST/v2/" >/dev/null 2>&1; then
return 0
fi
sleep 1
done
echo "stand-in registry did not come up" >&2
docker logs dockerhub-standin >&2
return 1
}

case "${1:-}" in
start)
mkdir -p "$DIR"
# Pull while docker.io still resolves to the real Docker Hub.
docker pull -q registry:2
make_cert trusted
make_cert untrusted
chmod a+r "$DIR"/*.key
sudo cp "$DIR/trusted-ca.crt" /usr/local/share/ca-certificates/dockerhub-standin.crt
sudo update-ca-certificates >/dev/null

cat >"$DIR/redirect.py" <<'PY'
import sys
from http.server import BaseHTTPRequestHandler, ThreadingHTTPServer

LOG = sys.argv[1]


class Redirect(BaseHTTPRequestHandler):
def redirect(self):
with open(LOG, "a") as log:
log.write(f"{self.command} {self.path}\n")
self.send_response(301)
self.send_header("Location", f"https://{self.headers['Host']}{self.path}")
self.send_header("Content-Length", "0")
self.end_headers()

do_GET = do_HEAD = do_POST = do_PUT = do_PATCH = do_DELETE = redirect


ThreadingHTTPServer(("127.0.0.1", 80), Redirect).serve_forever()
PY
: >"$HTTP_LOG"
sudo nohup python3 "$DIR/redirect.py" "$HTTP_LOG" >"$DIR/redirect.out" 2>&1 &

echo "127.0.0.1 $HOST" | sudo tee -a /etc/hosts >/dev/null
serve trusted
curl -sS -o /dev/null -w '%{http_code}' "http://$HOST/v2/" | grep -qx 301
: >"$HTTP_LOG"
;;
untrusted)
serve untrusted
;;
http-log)
cat "$HTTP_LOG"
;;
*)
echo "usage: $0 start|untrusted|http-log" >&2
exit 2
;;
esac
103 changes: 103 additions & 0 deletions .github/workflows/test-dockerhub-https.yml
Original file line number Diff line number Diff line change
@@ -0,0 +1,103 @@
name: Test Docker Hub HTTPS

# Checks that buildx and buildkitd reach Docker Hub over verified HTTPS while
# the Blacksmith mirror stays on HTTP. .github/scripts/dockerhub-standin.sh
# answers as registry-1.docker.io on the runner.

on:
workflow_dispatch:
pull_request:
push:
branches: [main, release/v2]

permissions:
contents: read

env:
IMAGE: docker.io/standin/setup-docker-builder-test

jobs:
fallback-builder:
name: Fallback builder pushes a manifest list over HTTPS
runs-on: blacksmith
steps:
- name: Checkout
uses: actions/checkout@v6

- name: Assert BLACKSMITH_AGENT_ADDR is advertised
run: test -n "$BLACKSMITH_AGENT_ADDR"

# No cache-key: the action warns and falls back to a local builder.
- name: Setup Docker Builder without cache-key
uses: ./

- name: Assert the fallback builder is in use
run: |
docker buildx inspect --bootstrap | tee /tmp/builder.txt
grep -Eq '^Driver:\s+docker-container$' /tmp/builder.txt

- name: Start the Docker Hub stand-in
run: .github/scripts/dockerhub-standin.sh start

- name: Push a manifest list with imagetools create
run: |
docker buildx imagetools create -t "$IMAGE:manifest" registry.k8s.io/pause:3.10
docker buildx imagetools inspect "$IMAGE:manifest"

- name: Assert nothing went to Docker Hub over plain HTTP
run: |
.github/scripts/dockerhub-standin.sh http-log
test ! -s /tmp/dockerhub-standin/http-requests.log

blacksmith-builder:
Comment thread
devin-ai-integration[bot] marked this conversation as resolved.
Fixed
name: Blacksmith builder verifies Docker Hub TLS
runs-on: blacksmith
steps:
- name: Checkout
uses: actions/checkout@v6

- name: Assert BLACKSMITH_AGENT_ADDR is advertised
run: test -n "$BLACKSMITH_AGENT_ADDR"

# Before buildkitd starts, so it loads the stand-in's CA.
- name: Start the Docker Hub stand-in
run: .github/scripts/dockerhub-standin.sh start

- name: Setup Docker Builder
uses: ./
with:
cache-key: test/setup-docker-builder-dockerhub-https
nofallback: "true"

- name: Assert the Blacksmith builder is in use
run: |
docker buildx inspect | tee /tmp/builder.txt
grep -Eq '^Driver:\s+remote$' /tmp/builder.txt

- name: Write a Dockerfile that pulls nothing
run: |
mkdir -p /tmp/ctx
date >/tmp/ctx/stamp
printf 'FROM scratch\nCOPY stamp /stamp\n' >/tmp/ctx/Dockerfile

- name: Push over HTTPS with a trusted certificate
run: docker buildx build --push -t "$IMAGE:trusted" /tmp/ctx

- name: Assert nothing went to Docker Hub over plain HTTP
run: |
.github/scripts/dockerhub-standin.sh http-log
test ! -s /tmp/dockerhub-standin/http-requests.log

- name: Serve a certificate no CA on the runner trusts
run: .github/scripts/dockerhub-standin.sh untrusted

- name: Assert the push fails on certificate verification
run: |
if docker buildx build --push -t "$IMAGE:untrusted" /tmp/ctx >/tmp/push.log 2>&1; then
cat /tmp/push.log
echo "::error::push succeeded against an untrusted certificate"
exit 1
fi
cat /tmp/push.log
grep -q 'x509' /tmp/push.log
test ! -s /tmp/dockerhub-standin/http-requests.log
10 changes: 5 additions & 5 deletions dist/index.js

Large diffs are not rendered by default.

2 changes: 1 addition & 1 deletion dist/index.js.map

Large diffs are not rendered by default.

30 changes: 30 additions & 0 deletions src/setup-builder.test.ts
Original file line number Diff line number Diff line change
@@ -1,6 +1,7 @@
import { describe, it, expect, vi, beforeEach } from "vitest";
import * as core from "@actions/core";
import * as fs from "fs";
import * as TOML from "@iarna/toml";
import * as setupBuilder from "./setup_builder";
import { UserInputError } from "./user-input-error";
import { fromBinary } from "@bufbuild/protobuf";
Expand Down Expand Up @@ -199,6 +200,16 @@ describe("setup_builder", () => {
expect(config).toContain('mirrors = [ "http://192.168.127.1:5000" ]');
expect(config).toContain('[registry."docker.io"]');
expect(config).toContain('[registry."192.168.127.1:5000"]');
const parsed = TOML.parse(config) as {
registry: Record<string, Record<string, unknown>>;
};
expect(parsed.registry["docker.io"]).toEqual({
mirrors: ["http://192.168.127.1:5000"],
});
expect(parsed.registry["192.168.127.1:5000"]).toEqual({
http: true,
insecure: true,
});
expect(config).not.toContain("[grpc]");
expect(core.info).toHaveBeenCalledWith(
"Wrote Docker container BuildKit config to local-buildkitd.toml",
Expand Down Expand Up @@ -235,6 +246,25 @@ describe("setup_builder", () => {
expect(config).toContain('keepDuration = "192h"');
expect(config).toContain("max-parallelism = 4");
});

it("sets http and insecure only on the Docker mirror entry", async () => {
const writeFile = vi.mocked(fs.promises.writeFile);

await setupBuilder.writeBuildkitdTomlFile(4, "tcp://127.0.0.1:1234", [
"10.0.0.1",
]);

const parsed = TOML.parse(writeFile.mock.calls[0][1] as string) as {
registry: Record<string, Record<string, unknown>>;
};
expect(parsed.registry["docker.io"]).toEqual({
mirrors: ["http://192.168.127.1:5000"],
});
expect(parsed.registry["192.168.127.1:5000"]).toEqual({
http: true,
insecure: true,
});
});
});

describe("logBuildCacheContents", () => {
Expand Down
5 changes: 3 additions & 2 deletions src/setup_builder.ts
Original file line number Diff line number Diff line change
Expand Up @@ -168,6 +168,9 @@ async function getRoutableHostDns(): Promise<string[]> {
return publicDnsFallback;
}

// The mirror is the only plain-HTTP endpoint, so only its own entry carries
// `http`/`insecure`. Setting them on `docker.io` makes buildx send Docker Hub
// requests over HTTP; the redirect to HTTPS turns a manifest PUT into a GET.
function getDockerMirrorRegistryConfig(): TOML.JsonMap {
const agentAddr = reporter.getAgentAddr();
if (!agentAddr) {
Expand All @@ -177,8 +180,6 @@ function getDockerMirrorRegistryConfig(): TOML.JsonMap {
return {
"docker.io": {
mirrors: [`http://${mirror}`],
http: true,
insecure: true,
},
[mirror]: {
http: true,
Expand Down
Loading