Repository navigation
Description
Found and investigated as part of working on #5052.
Description
When the registry serves a blob from the filesystem driver, it decides on the response status and the Content-Length before it opens the blob file. It takes the Content-Length from the size in the blob descriptor.
If the blob file is missing, cannot be opened, or is smaller than the size stored in the blob descriptor the client gets 200 OK and a truncated response body (where the body length does not match the Content-Length response header value).
No error is logged.
Examples
Two example scenarios:
-
Blob file is missing:
curlexits with code 18. Other headers omitted:$ curl -sS -o /dev/null -D - http://127.0.0.1:5055/v2/demo/blobs/sha256:e674ca9640a90b6db9754e2ebb05369b63773694772257bb9b0dbb8fe821765d HTTP/1.1 200 OK Content-Length: 1048576 curl: (18) transfer closed with 1048576 bytes remaining to read -
Blob file is truncated to half its size while the registry runs. The registry sends a 200 OK response and the original Content-Length, and curl reports
(18) transfer closed with 524288 bytes remaining to read.
In both cases, skopeo copy retries every second with a Range request. The registry responds to each request with 206 and no body. The range is valid for the size in the blob descriptor, but the blob file contains no bytes in that range. With skopeo at least this produces apparently an endless loop (I stopped it after a few minutes).
Blobs are immutable. A blob file smaller than the size in its blob descriptor should not exist. If this happens, the registry should treat the blob file as authoritative for now. A better solution can be considered later, if desired.
How to reproduce
Created with an agent. Run from the root of a checkout:
reproduce-both-cases.sh
#!/bin/sh
# Reproducer, filesystem storage driver: what a blob GET answers when the blob file is missing, and when it is
# shorter than the size the registry has recorded for the blob.
# Run from a checkout of distribution/distribution: sh reproduce-both-cases.sh
# or with an existing binary: sh reproduce-both-cases.sh /path/to/registry
# Needs curl, sha256sum and truncate. Uses port 5055 on 127.0.0.1 and a temporary directory.
set -eu
dir=$(mktemp -d)
reg=${1:-}
if [ -z "$reg" ]; then
go build -o "$dir/registry" ./cmd/registry
reg=$dir/registry
fi
cat > "$dir/config.yml" <<CONFIG
version: 0.1
storage:
cache:
blobdescriptor: inmemory
filesystem:
rootdirectory: $dir/storage
http:
addr: 127.0.0.1:5055
CONFIG
"$reg" serve "$dir/config.yml" > "$dir/registry.log" 2>&1 &
pid=$!
trap 'kill $pid' EXIT
url=http://127.0.0.1:5055/v2/demo/blobs
until curl -fsS -o /dev/null http://127.0.0.1:5055/v2/ 2>/dev/null; do sleep 0.2; done
push() { # pushes a new 1 MiB blob, prints its sha256
head -c 1048576 /dev/urandom > "$dir/blob"
hex=$(sha256sum "$dir/blob" | cut -d' ' -f1)
location=$(curl -fsS -X POST -D - -o /dev/null "$url/uploads/" | tr -d '\r' | sed -n 's/^[Ll]ocation: //p')
curl -fsS -X PUT -H 'Content-Type: application/octet-stream' --data-binary "@$dir/blob" -o /dev/null "$location&digest=sha256:$hex"
echo "$hex"
}
data() { echo "$dir/storage/docker/registry/v2/blobs/sha256/$(echo "$1" | cut -c1-2)/$1/data"; }
get() { # prints the command, the status line, Content-Length, curl's message and exit code
echo "\$ curl -sS -o /dev/null -D - $url/sha256:$1"
code=0
curl -sS -o /dev/null -D - -w '(%{size_download} body bytes received)\n' "$url/sha256:$1" > "$dir/response" 2>&1 || code=$?
grep -i '^HTTP\|^content-length\|^curl\|body bytes' "$dir/response"
echo "\$ echo \$?"
echo "$code"
}
# The running registry keeps both blobs' descriptors in its cache.
missing=$(push)
short=$(push)
rm "$(data "$missing")"
truncate -s 524288 "$(data "$short")"
echo "== blob file missing"
get "$missing"
echo "== blob file shorter than recorded (524288 of 1048576 bytes)"
get "$short"
echo "== registry log"
grep "GET /v2/demo/blobs/sha256:" "$dir/registry.log" | sed 's/sha256:\([0-9a-f]\{8\}\)[0-9a-f]*/sha256:\1.../; s/^[^"]*//'
grep -c 'level=error' "$dir/registry.log" | sed 's/^/error lines: /'Expected behavior
- Blob file missing or cannot be opened: an error response.
- Blob file smaller than the size in the blob descriptor: a response whose
Content-Lengthmatches its body. This is the proposed behavior for now. A better solution can be considered later.
registry version
f38cd9c (v3.1.2+unknown)
Additional Info
For a missing or smaller blob file, this defect occurs only when the registry uses a stale blob descriptor from the blob descriptor cache. The registry image's configuration uses inmemory for this cache. This was reproduced by running registry garbage-collect while the registry was running. If the blob file cannot be opened, the defect can be reproduced without a blob descriptor cache.
Related issues
Users have run into this before, after garbage collection or proxy cache expiry removed a blob's file. Most of the issues listed below focus on fixing what caused the blob file to go missing. This issue focuses on the registry's response when a blob file is missing. The registry should return an error regardless of why the file is missing.
| Issue | Relation |
|---|---|
| #2367 | same problem, after proxy expiry |
| #5010 | same problem, after proxy cache cleanup; asks for a refetch or an error; probably the same case as #2367 |
| #2966 | probably the same problem (reports a 0-byte layer), after garbage collection |
| #2778 | probably the same problem (shows only the client's "unexpected EOF"), after garbage collection |
| #5008 (PR) | would make the problem rarer (cache entries expire); does not change the response |
Activity
storage: serve filesystem blobs directly from the file
storage: serve filesystem blobs directly from the file
storage: serve filesystem blobs directly from the file
storage: serve filesystem blobs directly from the file
storage: serve filesystem blobs directly from the file