diff --git a/internal/deploy/deploy.go b/internal/deploy/deploy.go index 0a76cae..5c9f718 100644 --- a/internal/deploy/deploy.go +++ b/internal/deploy/deploy.go @@ -266,12 +266,13 @@ func (d *Deployer) Deploy(ctx context.Context, only []string, modules bool) erro return errors.Join(errs...) } -// srcPlan is a source's resolved envs, the branches that got none and why, and the envs to build (nil = all). +// srcPlan is a source's resolved envs, the branches that got none and why, names lost to collisions, and the envs to build (nil = all). type srcPlan struct { - s *Source - envs map[string]envRef - skipped map[string]error - want map[string]bool + s *Source + envs map[string]envRef + skipped map[string]error + collided map[string]bool + want map[string]bool } // selectEnvs resolves args to envs as a full deploy would: env names in any source first, else raw branch names. @@ -354,17 +355,6 @@ func (d *Deployer) aliasesOf(env string) []string { // Aliases maps alias env name -> target env name. func (d *Deployer) Aliases() map[string]string { return d.cfg.W10k.Aliases } -// collided returns the env names a branch collision kept from being resolved. -func (p *srcPlan) collided() map[string]bool { - out := map[string]bool{} - for b := range p.skipped { - if name, _, err := p.s.EnvName(b); err == nil { - out[name] = true - } - } - return out -} - // skipErrs returns the distinct reasons, in branch order, for skipped branches match accepts. func (p *srcPlan) skipErrs(match func(string) bool) []error { var out []error @@ -386,11 +376,11 @@ func (d *Deployer) count(outcome string) { type envRef struct{ branch, sha string } -// Environments maps env name -> branch and sha, and each branch that gets no env to why. -func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envRef, map[string]error, error) { +// Environments maps env name -> branch and sha, each branch that gets no env to why, and the names no branch won because only corrected branches claimed them. +func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envRef, map[string]error, map[string]bool, error) { all, err := d.branches(ctx, d.srcMirror(s)) if err != nil { - return nil, nil, err + return nil, nil, nil, err } names := make([]string, 0, len(all)) for b := range all { @@ -399,7 +389,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR sort.Strings(names) claims := map[string][]string{} exact := map[string]string{} - skipped := map[string]error{} + skipped, collided := map[string]error{}, map[string]bool{} for _, b := range names { if s.Ignored(b) { continue @@ -426,6 +416,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR for _, b := range bs { skipped[b] = err } + collided[name] = true continue } for _, b := range bs { @@ -435,7 +426,7 @@ func (d *Deployer) Environments(ctx context.Context, s *Source) (map[string]envR } envs[name] = envRef{win, all[win]} } - return envs, skipped, nil + return envs, skipped, collided, nil } func (d *Deployer) planSource(ctx context.Context, s *Source) (*srcPlan, error) { @@ -446,9 +437,9 @@ func (d *Deployer) planSource(ctx context.Context, s *Source) (*srcPlan, error) return nil, err } bctx, end := d.phase(ctx, "source.branches", "source", s.Name) - envs, skipped, err := d.Environments(bctx, s) + envs, skipped, collided, err := d.Environments(bctx, s) end("envs", len(envs)) - return &srcPlan{s: s, envs: envs, skipped: skipped}, err + return &srcPlan{s: s, envs: envs, skipped: skipped, collided: collided}, err } func (d *Deployer) deploySource(ctx context.Context, p *srcPlan, modules bool) error { @@ -486,7 +477,7 @@ func (d *Deployer) deploySource(ctx context.Context, p *srcPlan, modules bool) e } wg.Wait() if full { - errs = append(errs, d.purge(s, p.envs, p.collided())) + errs = append(errs, d.purge(s, p.envs, p.collided)) } return errors.Join(errs...) } diff --git a/internal/deploy/deploy_test.go b/internal/deploy/deploy_test.go index d2923db..3824aa6 100644 --- a/internal/deploy/deploy_test.go +++ b/internal/deploy/deploy_test.go @@ -1774,3 +1774,30 @@ func TestAliasUntouchedWhenTargetHasNoLink(t *testing.T) { t.Fatalf("production -> %s, want %s untouched", readlinkT(t, prod), old) } } + +func TestCorrectedBranchCollisionKeepsLiveEnv(t *testing.T) { + f := newFixture(t) + commit(t, f.control, map[string]string{"manifests/site.pp": "node default {}\n"}) + gitT(t, f.control, "checkout", "-q", "-b", "feature-x") + sha := commit(t, f.control, map[string]string{"manifests/site.pp": "node fx {}\n"}) + gitT(t, f.control, "checkout", "-q", "main") + f.mustDeploy(false) + live := f.env("feature_x") + if !strings.HasPrefix(filepath.Base(live), sha+"-") { + t.Fatalf("setup: feature_x -> %q, want sha %s", live, sha) + } + gitT(t, f.control, "branch", "feature.x") + for range 3 { + err := f.deploy(false) + if err == nil || !strings.Contains(err.Error(), "feature-x") || !strings.Contains(err.Error(), "feature.x") { + t.Fatalf("want collision error naming both branches, got %v", err) + } + if got := f.env("feature_x"); got != live { + t.Fatalf("feature_x -> %q after colliding push, want live %q", got, live) + } + f.clock = f.clock.Add(11 * time.Minute) + } + if !exists(live) || f.env("main") == "" { + t.Fatal("live feature_x worktree must survive GC past grace") + } +} diff --git a/internal/deploy/forge.go b/internal/deploy/forge.go index 9f97539..12e042e 100644 --- a/internal/deploy/forge.go +++ b/internal/deploy/forge.go @@ -5,6 +5,7 @@ import ( "compress/gzip" "context" "encoding/json" + "errors" "fmt" "io" "net/http" @@ -18,6 +19,8 @@ const ( defaultForge = "https://forgeapi.puppet.com" maxForgeDownload = 100 << 20 maxForgeExtracted = 512 << 20 + maxForgeMeta = 1 << 20 + maxVersion = 64 ) var httpClient = &http.Client{Timeout: 5 * time.Minute} @@ -61,10 +64,10 @@ func forgeLatest(ctx context.Context, base, slug string) (string, error) { Version string `json:"version"` } `json:"current_release"` } - if err := json.NewDecoder(body).Decode(&r); err != nil { + if err := json.NewDecoder(&capReader{r: body, n: maxForgeMeta}).Decode(&r); err != nil { return "", fmt.Errorf("%s: %w", slug, err) } - if !versionStr.MatchString(r.CurrentRelease.Version) { + if v := r.CurrentRelease.Version; len(v) > maxVersion || !versionStr.MatchString(v) { return "", fmt.Errorf("%s: forge returned no usable current_release", slug) } return r.CurrentRelease.Version, nil @@ -105,7 +108,7 @@ func (c *capReader) Read(p []byte) (int, error) { } n, err := c.r.Read(p) if c.n -= int64(n); c.n < 0 { - return 0, fmt.Errorf("archive exceeds size cap") + return 0, errors.New("size cap exceeded") } return n, err } diff --git a/internal/deploy/forge_test.go b/internal/deploy/forge_test.go index 6598222..ba554e9 100644 --- a/internal/deploy/forge_test.go +++ b/internal/deploy/forge_test.go @@ -4,7 +4,11 @@ import ( "archive/tar" "bytes" "compress/gzip" + "context" + "fmt" "io" + "net/http" + "net/http/httptest" "os" "path/filepath" "strings" @@ -116,3 +120,33 @@ func TestCapReaderErrorsAtCap(t *testing.T) { t.Fatalf("under cap: %q %v", b, err) } } + +func forgeServer(t *testing.T, version string, pad int) string { + srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { + _, _ = fmt.Fprintf(w, `{"current_release":{"version":"%s%s"}}`, version, strings.Repeat("a", pad)) + })) + t.Cleanup(srv.Close) + return srv.URL +} + +func TestForgeLatestCapped(t *testing.T) { + v, err := forgeLatest(context.Background(), forgeServer(t, "", maxForgeMeta+1), "acme-x") + if err == nil || !strings.Contains(err.Error(), "size cap") { + t.Fatalf("want size cap error, got version len %d, err %v", len(v), err) + } +} + +func TestForgeLatestBoundsVersion(t *testing.T) { + for version, ok := range map[string]bool{ + "1.2.3": true, + strings.Repeat("1", 64): true, + strings.Repeat("1", 65): false, + "": false, + "1.0 ; rm": false, + } { + v, err := forgeLatest(context.Background(), forgeServer(t, version, 0), "acme-x") + if ok != (err == nil) || (ok && v != version) { + t.Errorf("version %q: got %q, %v", version, v, err) + } + } +}