carlos / platform Public

mediaimage: accept ECR's 201 on a blob PATCH so push reaches ECR

The media spike's first push failed with "upload: HTTP 201": ECR answers
the blob PATCH with 201 Created (with a Location and a Range) where the
OCI distribution spec v1.1.0 says 202. The PATCH now accepts 202 or 201.
Every other step keeps exactly the spec's status (end-2 to end-7), and
each check now cites its row of the spec's endpoint table.

The fake registry gains an ECR mode and per-stage status overrides.
TestPushToECR fails on the old code with the spike's error;
TestPushRefusesAnswersTheSpecDoesNotGive pins that no step got wider.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
claude agent, operated by paul@keymail.dev pushed by paul@keymail.dev 5df47753da8bd06d4c81b5c722f90602111fafb1 parent e468866
2 files changed, +132 −14
  • cmd/mediaimage/mediaimage_test.go +89 −5
  • cmd/mediaimage/push.go +43 −9
diff --git a/cmd/mediaimage/mediaimage_test.go b/cmd/mediaimage/mediaimage_test.go
index 7bed360d2..ea7178168 100644
--- a/cmd/mediaimage/mediaimage_test.go
+++ b/cmd/mediaimage/mediaimage_test.go
@@ -677,7 +677,15 @@ type fakeRegistry struct {
failDigest bool // a manifest read by digest answers 500
redirect string // every POST, PATCH and PUT is sent here instead
location func(stage string) string // replaces an upload's Location
- srv *httptest.Server
+ // ecr answers a blob PATCH the way Amazon ECR does: 201 Created with a
+ // Location and a Range, where the OCI spec says 202 (media spike,
+ // 2026-11-20).
+ ecr bool
+ // status replaces the success code of a stage: "start" (POST), "data"
+ // (PATCH), "finish" (blob PUT) or "manifest" (manifest PUT). The stage
+ // still does its work, so only the client's reading of it is tested.
+ status map[string]int
+ srv *httptest.Server
}
func newFakeRegistry(t *testing.T) *fakeRegistry {
@@ -711,19 +719,30 @@ func (r *fakeRegistry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
w.Header().Set("Location", r.location(stage))
}
}
+ answer := func(stage string, normal int) {
+ if code, ok := r.status[stage]; ok {
+ normal = code
+ }
+ w.WriteHeader(normal)
+ }
path := strings.TrimPrefix(req.URL.Path, "/v2/engine/")
switch {
case strings.HasPrefix(path, "blobs/uploads/") && req.Method == http.MethodPost:
id := strconv.Itoa(len(r.uploads) + 1)
r.uploads[id] = nil
locate("start", "/v2/engine/blobs/uploads/"+id+"?state=s")
- w.WriteHeader(http.StatusAccepted)
+ answer("start", http.StatusAccepted)
case strings.HasPrefix(path, "blobs/uploads/") && req.Method == http.MethodPatch:
id := strings.TrimPrefix(path, "blobs/uploads/")
body, _ := io.ReadAll(req.Body)
r.uploads[id] = append(r.uploads[id], body...)
locate("data", "/v2/engine/blobs/uploads/"+id+"?state=t")
- w.WriteHeader(http.StatusAccepted)
+ if r.ecr {
+ w.Header().Set("Range", fmt.Sprintf("0-%d", len(r.uploads[id])-1))
+ answer("data", http.StatusCreated)
+ return
+ }
+ answer("data", http.StatusAccepted)
case strings.HasPrefix(path, "blobs/uploads/") && req.Method == http.MethodPut:
id := strings.TrimPrefix(path, "blobs/uploads/")
digest := req.URL.Query().Get("digest")
@@ -732,7 +751,7 @@ func (r *fakeRegistry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
return
}
r.blobs[digest] = r.uploads[id]
- w.WriteHeader(http.StatusCreated)
+ answer("finish", http.StatusCreated)
case strings.HasPrefix(path, "blobs/"):
data, ok := r.blobs[strings.TrimPrefix(path, "blobs/")]
if !ok {
@@ -750,7 +769,7 @@ func (r *fakeRegistry) ServeHTTP(w http.ResponseWriter, req *http.Request) {
r.manifests[ref] = body
r.manifests["sha256:"+digestOf(body)] = body
w.Header().Set("Docker-Content-Digest", "sha256:"+digestOf(body))
- w.WriteHeader(http.StatusCreated)
+ answer("manifest", http.StatusCreated)
case strings.HasPrefix(path, "manifests/sha256:") && r.failDigest:
w.WriteHeader(http.StatusInternalServerError)
case strings.HasPrefix(path, "manifests/"):
@@ -1927,6 +1946,71 @@ func TestPushUnderTheReleaseTag(t *testing.T) {
}
}
+// TestPushToECR: Amazon ECR answers a blob PATCH with 201 Created, a
+// Location and a Range, where the OCI spec says 202. The media spike's
+// first push failed on it ("upload: HTTP 201").
+func TestPushToECR(t *testing.T) {
+ f := newFixture(t)
+ info := f.build(t)
+ reg := newFakeRegistry(t)
+ reg.password = "s3cret"
+ reg.ecr = true
+
+ digest, err := pushImage(t.Context(), pushOptionsFor(reg, f.out, "v0.40.0"))
+ if err != nil {
+ t.Fatalf("push to an ECR-shaped registry: %v", err)
+ }
+ if digest != info.Digest {
+ t.Fatalf("push printed %s, the build made %s", digest, info.Digest)
+ }
+ m, _ := readLayout(t, f.out)
+ for _, d := range append([]ociDescriptor{m.Config}, m.Layers...) {
+ want, err := os.ReadFile(blobPath(f.out, d.Digest))
+ if err != nil {
+ t.Fatal(err)
+ }
+ if !bytes.Equal(reg.blobs[d.Digest], want) {
+ t.Fatalf("blob %s did not arrive intact", d.Digest)
+ }
+ }
+ if !slices.Equal(reg.puts, []string{"v0.40.0"}) {
+ t.Fatalf("manifest pushed as %v, want only the release tag", reg.puts)
+ }
+}
+
+// TestPushRefusesAnswersTheSpecDoesNotGive: each step accepts the OCI
+// distribution spec's status for it (and ECR's 201 on PATCH) and nothing
+// else. A success code from the wrong step is still a failure.
+func TestPushRefusesAnswersTheSpecDoesNotGive(t *testing.T) {
+ f := newFixture(t)
+ f.build(t)
+ for _, c := range []struct {
+ stage string
+ code int
+ }{
+ {"start", http.StatusOK},
+ {"start", http.StatusCreated},
+ {"start", http.StatusNoContent},
+ {"data", http.StatusOK},
+ {"data", http.StatusNoContent},
+ {"finish", http.StatusOK},
+ {"finish", http.StatusAccepted},
+ {"finish", http.StatusNoContent},
+ {"manifest", http.StatusOK},
+ {"manifest", http.StatusAccepted},
+ } {
+ t.Run(fmt.Sprintf("%s %d", c.stage, c.code), func(t *testing.T) {
+ reg := newFakeRegistry(t)
+ reg.status = map[string]int{c.stage: c.code}
+ _, err := pushImage(t.Context(), pushOptionsFor(reg, f.out, "v0.40.0"))
+ var status *statusError
+ if !errors.As(err, &status) || status.Code != c.code {
+ t.Fatalf("push = %v, want HTTP %d refused", err, c.code)
+ }
+ })
+ }
+}
+
func TestPushRefusesAReusedTag(t *testing.T) {
f := newFixture(t)
f.build(t)
diff --git a/cmd/mediaimage/push.go b/cmd/mediaimage/push.go
index bfce0cc49..876d16229 100644
--- a/cmd/mediaimage/push.go
+++ b/cmd/mediaimage/push.go
@@ -15,6 +15,7 @@ import (
"os"
"path/filepath"
"regexp"
+ "slices"
"strings"
)
@@ -174,7 +175,26 @@ func send(ctx context.Context, reg *registry, method, target, contentType string
return read(resp)
}
+// Every status this file accepts is the one the OCI Distribution
+// Specification v1.1.0 (spec.md, "Endpoints" table, end-2 to end-7) gives
+// for that step, and nothing wider:
+//
+// - end-3, GET a manifest ("Pulling manifests"): 200, or 404 when absent;
+// - end-2, HEAD a blob ("Checking if content exists in the registry"):
+// 200, or 404 when absent;
+// - end-4a, POST to open an upload ("Pushing a blob monolithically", "POST
+// then PUT"): 202 with a Location;
+// - end-5, PATCH the bytes ("Pushing a blob in chunks"): 202 with a
+// Location, and 201 as well, because Amazon ECR answers 201 there (see
+// uploadBlob);
+// - end-6, PUT to close the upload with the digest: 201;
+// - end-7, PUT a manifest ("Pushing Manifests"): 201.
+//
+// Any other answer is an error, so a registry that did something else is
+// never taken for one that stored the image.
+
// fetchManifest returns the manifest a reference names, nil when absent.
+// OCI distribution spec v1.1.0, end-3 ("Pulling manifests"): 200 or 404.
func fetchManifest(ctx context.Context, reg *registry, ref string) ([]byte, error) {
var data []byte
err := send(ctx, reg, http.MethodGet, reg.url("manifests/"+ref), "", nil, 0, func(resp *http.Response) error {
@@ -191,10 +211,10 @@ func fetchManifest(ctx context.Context, reg *registry, ref string) ([]byte, erro
return data, err
}
-// expect turns any other status into an error.
-func expect(op string, want int) func(*http.Response) error {
+// expect turns any status but the ones wanted into an error.
+func expect(op string, want ...int) func(*http.Response) error {
return func(resp *http.Response) error {
- if resp.StatusCode != want {
+ if !slices.Contains(want, resp.StatusCode) {
return &statusError{Op: op, Code: resp.StatusCode}
}
return nil
@@ -202,9 +222,11 @@ func expect(op string, want int) func(*http.Response) error {
}
// uploadBlob sends one blob the registry does not hold yet: open a session,
-// send the bytes in one PATCH, close it with the digest.
+// send the bytes in one PATCH, close it with the digest. The statuses are
+// the OCI distribution spec v1.1.0's for each step (see above fetchManifest).
func uploadBlob(ctx context.Context, reg *registry, layout string, blob ociDescriptor) error {
present := false
+ // end-2, "Checking if content exists in the registry": 200 or 404.
err := send(ctx, reg, http.MethodHead, reg.url("blobs/"+blob.Digest), "", nil, 0, func(resp *http.Response) error {
switch resp.StatusCode {
case http.StatusOK:
@@ -234,11 +256,14 @@ func uploadBlob(ctx context.Context, reg *registry, layout string, blob ociDescr
}
return loc.String(), nil
}
+ // end-4a, "POST then PUT": opening a session MUST answer 202 Accepted
+ // with a Location. 201 is only for a POST that carries the digest
+ // (end-4b) or a mount (end-11), and this POST is neither.
start := reg.url("blobs/uploads/")
location := ""
err = send(ctx, reg, http.MethodPost, start, "", nil, 0, func(resp *http.Response) error {
- if resp.StatusCode != http.StatusAccepted {
- return &statusError{Op: "start upload", Code: resp.StatusCode}
+ if err := expect("start upload", http.StatusAccepted)(resp); err != nil {
+ return err
}
var err error
location, err = next(start, resp)
@@ -253,10 +278,16 @@ func uploadBlob(ctx context.Context, reg *registry, layout string, blob ociDescr
return err
}
defer f.Close()
+ // end-5, "Pushing a blob in chunks": the spec says a PATCH MUST answer
+ // 202 Accepted with a Location. Amazon ECR answers 201 Created, with a
+ // Location and a Range (the media spike reproduced it with curl on
+ // 2026-11-20), and a push that refused it could never reach ECR. Other
+ // registry clients accept 201 here for the same reason. Either answer
+ // leaves the session open, so both go on to the closing PUT.
current := location
err = send(ctx, reg, http.MethodPatch, current, "application/octet-stream", f, blob.Size, func(resp *http.Response) error {
- if resp.StatusCode != http.StatusAccepted {
- return &statusError{Op: "upload", Code: resp.StatusCode}
+ if err := expect("upload", http.StatusAccepted, http.StatusCreated)(resp); err != nil {
+ return err
}
var err error
location, err = next(current, resp)
@@ -273,10 +304,13 @@ func uploadBlob(ctx context.Context, reg *registry, layout string, blob ociDescr
q := final.Query()
q.Set("digest", blob.Digest)
final.RawQuery = q.Encode()
+ // end-6, "POST then PUT": closing the session with the digest MUST
+ // answer 201 Created.
return send(ctx, reg, http.MethodPut, final.String(), "application/octet-stream", nil, 0, expect("finish upload", http.StatusCreated))
}
-// putManifest writes the manifest under the tag.
+// putManifest writes the manifest under the tag. OCI distribution spec
+// v1.1.0, end-7 ("Pushing Manifests"): 201 Created.
func putManifest(ctx context.Context, reg *registry, tag string, manifest []byte) error {
return send(ctx, reg, http.MethodPut, reg.url("manifests/"+tag), mediaTypeOCIManifest, bytes.NewReader(manifest), int64(len(manifest)), expect("write manifest", http.StatusCreated))
}