fix: retry truncated seed downloads in cloud-init
Task 1 R2 P2-1 (Astra). A seed stream cut by the console's stream deadline is a truncated 200 (curl exit 18), which curl --retry does not retry, so set -e ended the boot. The script now fetches in a bounded loop: each attempt starts from an empty seed.part with --max-time of at most 300 s; a transport error, 429, 5xx or truncated 200 is retried after 5 s, any other refusal (403 included) is final. All attempts end by the earlier of 600 s from the first and the token's expiry, inside the 900 s script bound. The real-curl script-run witness adds a truncated first response then a complete one (two requests, one sleep, seed verified) and a 403 (one request, bootstrap_failed). Golden regenerated; the harness shims sleep.
Co-Authored-By: Claude Opus 5.5 <noreply@anthropic.com>
5 files changed,
+106
−13
internal/console/managedseed_linux_test.go+60 −9internal/managedboot/boottest/boottest.go+1 −0internal/managedboot/cloudinit_v1.sh.tmpl+21 −1internal/managedboot/testdata/userdata-v1.golden+21 −1internal/managedboot/userdata.go+3 −2
CI failed — run details
diff --git a/internal/console/managedseed_linux_test.go b/internal/console/managedseed_linux_test.go| index 2acfae94d..e8755cb7f 100644 |
| --- a/internal/console/managedseed_linux_test.go |
| +++ b/internal/console/managedseed_linux_test.go |
| @@ -24,8 +24,9 @@ import ( |
| // with real curl and python3 and logging shims for everything that changes |
| // the machine. It needs no privileges and never skips. |
| func TestManagedCloudInitScriptRun(t *testing.T) { |
| - for _, good := range []bool{true, false} { |
| - t.Run(map[bool]string{true: "right bytes", false: "wrong bytes"}[good], func(t *testing.T) { |
| + for _, kind := range []string{"right bytes", "wrong bytes", "truncated then complete", "refused"} { |
| + t.Run(kind, func(t *testing.T) { |
| + good := kind == "right bytes" || kind == "truncated then complete" |
| s, _, _, _ := managedWorkerFixture(t) |
| now := time.Now().UTC() |
| s.cli = newCLILimits() |
| @@ -39,8 +40,13 @@ func TestManagedCloudInitScriptRun(t *testing.T) { |
| b, _ := io.ReadAll(r.Body) |
| mu.Lock() |
| bodies = append(bodies, string(b)) |
| + first := len(bodies) == 1 |
| mu.Unlock() |
| r.Body = io.NopCloser(bytes.NewReader(b)) |
| + if kind == "truncated then complete" && first { |
| + // A 200 whose body is cut short, as a stream deadline does. |
| + w = &truncatingWriter{ResponseWriter: w, keep: 10} |
| + } |
| s.handleManagedSeed(w, r) |
| })) |
| defer srv.Close() |
| @@ -55,7 +61,10 @@ func TestManagedCloudInitScriptRun(t *testing.T) { |
| } |
| key, _, _ := openManagedEnrolKey(s.cfg.AlertSealKeyPath) |
| token, _ := deriveManagedEnrolToken(key, b.ClientToken) |
| - if !good { |
| + if kind == "refused" { |
| + workerAdvance(t, s, b, func(b *managedBox) { b.EnrolUsedAt = now }) |
| + } |
| + if kind == "wrong bytes" { |
| _, etag, _ := s.cfg.Releases.Get(context.Background(), b.Boot.SeedKey) |
| if _, err = s.cfg.Releases.Put(context.Background(), b.Boot.SeedKey, append([]byte("x"), managedTestSeed...), etag); err != nil { |
| t.Fatal(err) |
| @@ -86,20 +95,43 @@ func TestManagedCloudInitScriptRun(t *testing.T) { |
| if bounded != 1 { |
| t.Fatalf("re-exec %d times", bounded) |
| } |
| + // A cut-short body is retried once from an empty file; a 403 is |
| + // final after one request. |
| + attempts, sleeps := 1, 0 |
| + if kind == "truncated then complete" { |
| + attempts, sleeps = 2, 1 |
| + } |
| + mu.Lock() |
| + if len(bodies) != attempts { |
| + t.Fatalf("%d seed requests, want %d", len(bodies), attempts) |
| + } |
| + for _, body := range bodies { |
| + if body != `{"token":"`+token+`"}`+"\n" { |
| + t.Fatalf("seed request body %q", body) |
| + } |
| + } |
| + mu.Unlock() |
| + var slept, curls int |
| + for _, c := range calls { |
| + if strings.HasPrefix(c, "sleep ") { |
| + slept++ |
| + } |
| + if strings.HasPrefix(c, "curl ") { |
| + curls++ |
| + } |
| + } |
| + if slept != sleeps || curls != attempts { |
| + t.Fatalf("sleeps %d curls %d", slept, curls) |
| + } |
| var curl string |
| for _, c := range calls { |
| if strings.HasPrefix(c, "curl ") { |
| curl = c |
| } |
| } |
| - if want := "curl --proto =https --tlsv1.2 -fsS --retry 20 --retry-max-time 600 --max-filesize 100663296 -H Content-Type: application/json --data-binary @" + bootDir + "/seed-request.json -o " + bootDir + "/seed.part " + srv.URL + "/api/fleet/enrol/seed"; curl != want { |
| + if want := "curl --proto =https --tlsv1.2 -fsS --connect-timeout 10 --max-time 300 --max-filesize 100663296 -H Content-Type: application/json --data-binary @" + bootDir + "/seed-request.json -o " + bootDir + "/seed.part -w %{http_code} " + srv.URL + "/api/fleet/enrol/seed"; curl != want { |
| t.Fatalf("curl call\n got %q\nwant %q", curl, want) |
| } |
| - mu.Lock() |
| - if len(bodies) != 1 || bodies[0] != `{"token":"`+token+`"}`+"\n" { |
| - t.Fatalf("seed request bodies %q", bodies) |
| - } |
| - mu.Unlock() |
| if _, err = os.Stat(filepath.Join(bootDir, "seed.part")); good && !os.IsNotExist(err) { |
| t.Fatal("seed.part left behind", err) |
| } |
| @@ -213,3 +245,22 @@ func TestManagedCloudInitScriptRun(t *testing.T) { |
| }) |
| } |
| } |
| + |
| +// truncatingWriter sends the headers and the first keep body bytes, then |
| +// aborts the connection. |
| +type truncatingWriter struct { |
| + http.ResponseWriter |
| + keep int |
| +} |
| + |
| +func (w *truncatingWriter) Write(p []byte) (int, error) { |
| + if len(p) > w.keep { |
| + _, _ = w.ResponseWriter.Write(p[:w.keep]) |
| + if f, ok := w.ResponseWriter.(http.Flusher); ok { |
| + f.Flush() |
| + } |
| + panic(http.ErrAbortHandler) |
| + } |
| + w.keep -= len(p) |
| + return w.ResponseWriter.Write(p) |
| +} |
diff --git a/internal/managedboot/boottest/boottest.go b/internal/managedboot/boottest/boottest.go| index 35ed39047..26f0bbf94 100644 |
| --- a/internal/managedboot/boottest/boottest.go |
| +++ b/internal/managedboot/boottest/boottest.go |
| @@ -51,6 +51,7 @@ func New(t testing.TB, script, owner, caBundle string) *Harness { |
| "systemctl": "exit 0\n", |
| "useradd": "exit 0\n", |
| "chown": "exit 0\n", |
| + "sleep": "exit 0\n", |
| "id": "[ \"$1\" = -u ] && echo " + ServiceUID + "\nexit 0\n", |
| "timeout": "shift\nexec \"$@\"\n", |
| } |
diff --git a/internal/managedboot/cloudinit_v1.sh.tmpl b/internal/managedboot/cloudinit_v1.sh.tmpl| index d40e94a0a..041332a3c 100644 |
| --- a/internal/managedboot/cloudinit_v1.sh.tmpl |
| +++ b/internal/managedboot/cloudinit_v1.sh.tmpl |
| @@ -141,7 +141,27 @@ put /var/lib/carlos-managed-boot/seed-request.json 600 <<'SEEDREQUEST' |
| SEEDREQUEST |
| printf '%s' {{sq .SeedSHA256}} | put /var/lib/carlos-managed-boot/seed.sha256 600 |
| -curl --proto =https --tlsv1.2 -fsS --retry 20 --retry-max-time 600 --max-filesize 100663296 -H 'Content-Type: application/json' --data-binary @"$B/seed-request.json" -o "$B/seed.part" "$ORIGIN/api/fleet/enrol/seed" |
| +# Fetch the seed. A transport error, a 429 or 5xx, or a body cut short is |
| +# retried from an empty file; any other refusal (403 included) is final. |
| +# All attempts end by the earlier of 600 s from now and the token's expiry. |
| +D=$(( $(date +%s) + 600 )) |
| +[ "$D" -lt {{sq .Expires}} ] || D={{sq .Expires}} |
| +fetch() { |
| + left=$(( D - $(date +%s) )) |
| + [ "$left" -gt 0 ] || return 2 |
| + [ "$left" -lt 300 ] || left=300 |
| + rm -f "$B/seed.part" |
| + code=$(curl --proto =https --tlsv1.2 -fsS --connect-timeout 10 --max-time "$left" --max-filesize 100663296 -H 'Content-Type: application/json' --data-binary @"$B/seed-request.json" -o "$B/seed.part" -w '%{http_code}' "$ORIGIN/api/fleet/enrol/seed") && return 0 |
| + case $code in 000|200|429|5??) return 1 ;; esac |
| + return 2 |
| +} |
| +while :; do |
| + r=0 |
| + fetch || r=$? |
| + [ "$r" != 0 ] || break |
| + [ "$r" = 1 ] || exit 1 |
| + sleep 5 |
| +done |
| chmod 0700 "$B/seed.part" |
| python3 -I "$B/verify.py" seed.part |
| # The verified seed is durable before any unit can depend on it. |
diff --git a/internal/managedboot/testdata/userdata-v1.golden b/internal/managedboot/testdata/userdata-v1.golden| index 673345546..ae581ea92 100644 |
| --- a/internal/managedboot/testdata/userdata-v1.golden |
| +++ b/internal/managedboot/testdata/userdata-v1.golden |
| @@ -141,7 +141,27 @@ put /var/lib/carlos-managed-boot/seed-request.json 600 <<'SEEDREQUEST' |
| SEEDREQUEST |
| printf '%s' 'abababababababababababababababababababababababababababababababab' | put /var/lib/carlos-managed-boot/seed.sha256 600 |
| -curl --proto =https --tlsv1.2 -fsS --retry 20 --retry-max-time 600 --max-filesize 100663296 -H 'Content-Type: application/json' --data-binary @"$B/seed-request.json" -o "$B/seed.part" "$ORIGIN/api/fleet/enrol/seed" |
| +# Fetch the seed. A transport error, a 429 or 5xx, or a body cut short is |
| +# retried from an empty file; any other refusal (403 included) is final. |
| +# All attempts end by the earlier of 600 s from now and the token's expiry. |
| +D=$(( $(date +%s) + 600 )) |
| +[ "$D" -lt '1794227400' ] || D='1794227400' |
| +fetch() { |
| + left=$(( D - $(date +%s) )) |
| + [ "$left" -gt 0 ] || return 2 |
| + [ "$left" -lt 300 ] || left=300 |
| + rm -f "$B/seed.part" |
| + code=$(curl --proto =https --tlsv1.2 -fsS --connect-timeout 10 --max-time "$left" --max-filesize 100663296 -H 'Content-Type: application/json' --data-binary @"$B/seed-request.json" -o "$B/seed.part" -w '%{http_code}' "$ORIGIN/api/fleet/enrol/seed") && return 0 |
| + case $code in 000|200|429|5??) return 1 ;; esac |
| + return 2 |
| +} |
| +while :; do |
| + r=0 |
| + fetch || r=$? |
| + [ "$r" != 0 ] || break |
| + [ "$r" = 1 ] || exit 1 |
| + sleep 5 |
| +done |
| chmod 0700 "$B/seed.part" |
| python3 -I "$B/verify.py" seed.part |
| # The verified seed is durable before any unit can depend on it. |
diff --git a/internal/managedboot/userdata.go b/internal/managedboot/userdata.go| index 54c912c91..711fb3af1 100644 |
| --- a/internal/managedboot/userdata.go |
| +++ b/internal/managedboot/userdata.go |
| @@ -4,6 +4,7 @@ import ( |
| _ "embed" |
| "encoding/json" |
| "errors" |
| + "strconv" |
| "strings" |
| "text/template" |
| ) |
| @@ -27,7 +28,7 @@ func shellQuote(v string) (string, error) { |
| } |
| type renderData struct { |
| - Origin, SeedSHA256, BootInput, SeedRequest string |
| + Origin, SeedSHA256, BootInput, SeedRequest, Expires string |
| } |
| // RenderUserData renders in's cloud-init script. It is pure: the same |
| @@ -53,7 +54,7 @@ func RenderUserData(in LaunchInput) (string, error) { |
| return "", errInvalid |
| } |
| var out strings.Builder |
| - if err = t.Execute(&out, renderData{Origin: in.Frozen.ConsoleOrigin, SeedSHA256: in.Frozen.SeedSHA256, BootInput: string(input), SeedRequest: string(request)}); err != nil { |
| + if err = t.Execute(&out, renderData{Origin: in.Frozen.ConsoleOrigin, SeedSHA256: in.Frozen.SeedSHA256, BootInput: string(input), SeedRequest: string(request), Expires: strconv.FormatInt(in.Expires.Unix(), 10)}); err != nil { |
| return "", errInvalid |
| } |
| if out.Len() > MaxUserDataBytes { |