Skip to content
Merged
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
9 changes: 6 additions & 3 deletions CHANGELOG.md
Original file line number Diff line number Diff line change
Expand Up @@ -21,9 +21,12 @@ All notable changes to SOW are recorded here.
version is deleted and before completion. An in-place rewrite with restored
size and mtime stops further deletion. RPM v6 packages are
not supported and are reported as such.
- `create` and `prune` reject an existing `repomd.xml` that also lists metadata
SOW does not generate, such as comps groups, updateinfo, or modules, before
changing any file, instead of silently dropping it from the rebuilt index.
- `prune` rejects an existing `repomd.xml` that also lists metadata SOW does not
generate, such as comps groups, updateinfo, or modules, before changing any
file. `create` keeps replacing such an index, as createrepo_c does, so that
existing repositories, including EL8/9 repositories with legacy module metadata
from Pigsty v4.4 or earlier, still rebuild. It now reports the dropped data
types as a warning and in the JSON `dropped_metadata` result field.
- Accept ECDSA and EdDSA header signatures in `RPMSIGTAG_DSA`, where rpm 4.16 and
later store them. RPM packages signed with GnuPG's default Ed25519 keys now
verify, including after managed package signing.
Expand Down
3 changes: 2 additions & 1 deletion README.md
Original file line number Diff line number Diff line change
Expand Up @@ -132,7 +132,8 @@ successful pointer switch, or a rerun that completes interrupted cleanup, it rem
older SOW hash-named indexes. Other files, including an external `repomd.xml.asc`, stay
untouched and external signatures must be refreshed separately. An existing `repomd.xml`
that lists metadata SOW does not generate, such as comps groups, updateinfo, or modules,
is rejected instead of being replaced without it.
is replaced without it; `create` reports those data types as a warning and in the JSON
`dropped_metadata` field. `prune` rejects such an index instead.

The bounded `.sow-plain-rpm-history.json` file records at most three reference sets so a
rerun can identify the predecessor on either side of an interrupted pointer switch.
Expand Down
12 changes: 9 additions & 3 deletions internal/v2/plain/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -44,9 +44,6 @@ func Create(ctx context.Context, opts Options) (result Result, resultErr error)
result.Dir = root
result.Kept = []string{}
result.Removed = []string{}
if err := rejectUnsupportedRPMMetadata(root); err != nil {
return Result{}, err
}
if err := cleanupStalePlainState(root); err != nil {
return Result{}, err
}
Expand Down Expand Up @@ -95,10 +92,19 @@ func Create(ctx context.Context, opts Options) (result Result, resultErr error)
_ = os.RemoveAll(staged.dir)
return Result{}, err
}
// Create replaces a foreign repomd.xml, as createrepo_c does; for example,
// Pigsty relies on this to retire legacy synthetic module metadata. Report
// the data types that the rebuilt index no longer carries.
prior, err := inspectPriorRPMMetadata(root)
if err != nil {
_ = os.RemoveAll(staged.dir)
return Result{}, err
}
if err := publishStage(ctx, root, staged, scan, opts, &plan); err != nil {
return Result{}, err
}
populateResult(&result, scan, staged, opts, plan.changed)
result.DroppedMetadata = prior.unsupported
return result, nil
}

Expand Down
3 changes: 3 additions & 0 deletions internal/v2/plain/prune.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,6 +146,9 @@ func Prune(ctx context.Context, opts PruneOptions) (result PruneResult, resultEr
if err := validatePruneDirectory(root); err != nil {
return result, err
}
if err := rejectUnsupportedRPMMetadata(root); err != nil {
return result, err
}
plan, err := preparePublication(ctx, root, staged, scan, base)
if err != nil {
return result, err
Expand Down
97 changes: 66 additions & 31 deletions internal/v2/plain/prune_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -157,6 +157,7 @@ func TestPruneRejectsInvalidInputsBeforeChanges(t *testing.T) {
mustWritePruneFile(t, filepath.Join(dir, "repodata", "repomd.xml.asc"), []byte("signature"))
}},
{"group metadata", addRepomdGroupData},
{"module metadata", addRepomdModulesData},
{"managed state", func(t *testing.T, dir string) { mustMkdir(t, filepath.Join(dir, ".sow")) }},
{"marker symlink", func(t *testing.T, dir string) {
if err := os.Remove(filepath.Join(dir, "repo_complete")); err != nil {
Expand Down Expand Up @@ -413,49 +414,83 @@ func addRepomdGroupData(t *testing.T, dir string) {
mustWritePruneFile(t, filepath.Join(dir, "repodata", "comps.xml"), []byte("<comps/>\n"))
}

func TestCreateRejectsIndexWithUnsupportedMetadata(t *testing.T) {
dir := pruneFixture(t, true)
addRepomdGroupData(t, dir)
stage := mustMkdir(t, filepath.Join(dir, ".sow-plain-stage-preserved"))
mustWritePruneFile(t, filepath.Join(stage, "evidence"), []byte("preserve"))
before := pruneTreeSnapshot(t, dir)
_, err := Create(context.Background(), Options{Dir: dir, Pigsty: true})
if KindOf(err) != KindRejected || !strings.Contains(err.Error(), `"group"`) {
t.Fatalf("create over group metadata error=%v", err)
}
if after := pruneTreeSnapshot(t, dir); !reflect.DeepEqual(before, after) {
t.Fatal("rejected create changed directory")
}
if err := os.Remove(filepath.Join(dir, "repodata", "repomd.xml")); err != nil {
t.Fatal(err)
}
if _, err := Create(context.Background(), Options{Dir: dir, Pigsty: true}); err != nil {
t.Fatalf("create after removing foreign repomd.xml: %v", err)
// addRepomdModulesData models an EL8/9 repository built by Pigsty v4.4 or
// earlier with repo2module and modifyrepo_c --mdtype=modules.
func addRepomdModulesData(t *testing.T, dir string) {
t.Helper()
path := filepath.Join(dir, "repodata", "repomd.xml")
data := mustRead(t, path)
modules := []byte(`<data type="modules"><location href="repodata/legacy-modules.yaml.gz"/></data></repomd>`)
mustWritePruneFile(t, path, bytes.Replace(data, []byte("</repomd>"), modules, 1))
mustWritePruneFile(t, filepath.Join(dir, "repodata", "legacy-modules.yaml.gz"), []byte("modules"))
}

// Create keeps replacing a foreign repomd.xml, as createrepo_c does, so that
// existing Pigsty repositories with legacy module metadata still rebuild. It
// reports the data types the rebuilt index no longer carries.
func TestCreateReplacesForeignIndexAndReportsDroppedMetadata(t *testing.T) {
for name, test := range map[string]struct {
add func(*testing.T, string)
want []string
}{
"Pigsty legacy modules": {addRepomdModulesData, []string{"modules"}},
"groups and modules": {func(t *testing.T, dir string) {
addRepomdGroupData(t, dir)
addRepomdModulesData(t, dir)
}, []string{"group", "modules"}},
} {
t.Run(name, func(t *testing.T) {
dir := pruneFixture(t, true)
test.add(t, dir)
result, err := Create(context.Background(), Options{Dir: dir, Pigsty: true})
if err != nil || result.Noop || !reflect.DeepEqual(result.DroppedMetadata, test.want) {
t.Fatalf("create over foreign index=%+v err=%v", result, err)
}
repomd := mustRead(t, filepath.Join(dir, "repodata", "repomd.xml"))
if bytes.Contains(repomd, []byte(`type="modules"`)) || bytes.Contains(repomd, []byte(`type="group"`)) {
t.Fatalf("rebuilt index kept foreign data: %s", repomd)
}
again, err := Create(context.Background(), Options{Dir: dir, Pigsty: true})
if err != nil || !again.Noop || len(again.DroppedMetadata) != 0 {
t.Fatalf("repeat create=%+v err=%v", again, err)
}
})
}
}

func TestPlainRejectsOversizedPriorIndex(t *testing.T) {
for _, command := range []string{"create", "prune", "dry-run"} {
t.Run(command, func(t *testing.T) {
dir := pruneFixture(t, true)
path := filepath.Join(dir, "repodata", "repomd.xml")
body := `<repomd><!--` + strings.Repeat("x", maxPriorRepomdBytes) + `--><data type="group"/></repomd>`
mustWritePruneFile(t, path, []byte(body))
oversized := func(t *testing.T) string {
t.Helper()
dir := pruneFixture(t, true)
path := filepath.Join(dir, "repodata", "repomd.xml")
body := `<repomd><!--` + strings.Repeat("x", maxPriorRepomdBytes) + `--><data type="group"/></repomd>`
mustWritePruneFile(t, path, []byte(body))
return dir
}
for _, dry := range []bool{false, true} {
t.Run(fmt.Sprintf("prune dry=%t", dry), func(t *testing.T) {
dir := oversized(t)
before := pruneTreeSnapshot(t, dir)
var err error
if command == "create" {
_, err = Create(context.Background(), Options{Dir: dir, Pigsty: true})
} else {
_, err = Prune(context.Background(), PruneOptions{Dir: dir, DryRun: command == "dry-run"})
}
if KindOf(err) != KindRejected {
if _, err := Prune(context.Background(), PruneOptions{Dir: dir, DryRun: dry}); KindOf(err) != KindRejected {
t.Fatalf("oversized index accepted: %v", err)
}
if !reflect.DeepEqual(before, pruneTreeSnapshot(t, dir)) {
t.Fatal("rejected command changed repository")
}
})
}
// Create cannot list the types of an index it cannot inspect, but keeps
// its established behavior of replacing the old index.
t.Run("create", func(t *testing.T) {
dir := oversized(t)
result, err := Create(context.Background(), Options{Dir: dir, Pigsty: true})
if err != nil || len(result.DroppedMetadata) != 0 {
t.Fatalf("create over oversized index=%+v err=%v", result, err)
}
if info, err := os.Stat(filepath.Join(dir, "repodata", "repomd.xml")); err != nil || info.Size() > maxPriorRepomdBytes {
t.Fatalf("oversized index was not replaced: info=%v err=%v", info, err)
}
})
}

func pruneFixture(t *testing.T, indexed bool) string {
Expand Down
65 changes: 43 additions & 22 deletions internal/v2/plain/publish.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,9 +33,6 @@ type publicationPlan struct {
}

func preparePublication(ctx context.Context, root string, staged stagedBuild, scan scanResult, opts Options) (publicationPlan, error) {
if err := rejectUnsupportedRPMMetadata(root); err != nil {
return publicationPlan{}, err
}
plan := publicationPlan{}
if err := prepareRPMPublication(ctx, root, staged, &plan); err != nil {
return publicationPlan{}, err
Expand Down Expand Up @@ -166,55 +163,79 @@ var droppableRPMMetadataTypes = map[string]bool{
"filelists-ext": true, "filelists-ext_db": true, "filelists-ext_zck": true,
}

// rejectUnsupportedRPMMetadata refuses to replace a repomd.xml that also
// publishes metadata SOW cannot regenerate, such as comps groups, updateinfo
// or modules; replacing it would silently drop that metadata. A malformed old
// repomd.xml still does not block rebuilding.
func rejectUnsupportedRPMMetadata(root string) error {
// priorRPMMetadata describes data types in an existing repomd.xml that SOW
// does not regenerate, such as comps groups, updateinfo, or modules.
type priorRPMMetadata struct {
path string
unsupported []string // sorted and deduplicated
oversized bool // too large to inspect
}

// inspectPriorRPMMetadata reads only the data types of an existing repomd.xml.
// Missing, unsafe, or malformed indexes report nothing: publication planning
// handles unsafe paths, and a malformed old index never blocks rebuilding.
func inspectPriorRPMMetadata(root string) (priorRPMMetadata, error) {
repodata := filepath.Join(root, "repodata")
prior := priorRPMMetadata{path: filepath.Join(repodata, "repomd.xml")}
if info, err := os.Lstat(repodata); err != nil || !info.IsDir() || info.Mode()&os.ModeSymlink != 0 {
return nil // Missing indexes need no check; unsafe paths fail during publication planning.
return prior, nil
}
path := filepath.Join(repodata, "repomd.xml")
file, err := os.OpenFile(path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
file, err := os.OpenFile(prior.path, os.O_RDONLY|syscall.O_NOFOLLOW|syscall.O_NONBLOCK, 0)
if errors.Is(err, os.ErrNotExist) || errors.Is(err, syscall.ELOOP) {
return nil
return prior, nil
}
if err != nil {
return &Error{Kind: KindRuntime, Op: "inspect prior rpm index", Path: path, Err: err}
return prior, &Error{Kind: KindRuntime, Op: "inspect prior rpm index", Path: prior.path, Err: err}
}
defer file.Close()
if info, err := file.Stat(); err != nil || !info.Mode().IsRegular() {
return nil
return prior, nil
}
data, err := io.ReadAll(io.LimitReader(file, maxPriorRepomdBytes+1))
if err != nil {
return &Error{Kind: KindRuntime, Op: "inspect prior rpm index", Path: path, Err: err}
return prior, &Error{Kind: KindRuntime, Op: "inspect prior rpm index", Path: prior.path, Err: err}
}
if len(data) > maxPriorRepomdBytes {
return &Error{Kind: KindRejected, Op: "inspect prior rpm index", Path: path, Err: errors.New("repomd.xml exceeds the inspection limit; remove it explicitly to rebuild without its metadata")}
prior.oversized = true
return prior, nil
}
var repomd struct {
Data []struct {
Type string `xml:"type,attr"`
} `xml:"data"`
}
if err := xml.Unmarshal(data, &repomd); err != nil {
return nil
return prior, nil
}
seen := map[string]bool{}
var unsupported []string
for _, data := range repomd.Data {
if !droppableRPMMetadataTypes[data.Type] && !seen[data.Type] {
seen[data.Type] = true
unsupported = append(unsupported, strconv.Quote(data.Type))
prior.unsupported = append(prior.unsupported, data.Type)
}
}
if len(unsupported) == 0 {
sort.Strings(prior.unsupported)
return prior, nil
}

// rejectUnsupportedRPMMetadata is prune's guard. Create keeps its established
// replacement behavior and reports dropped types instead (see Result).
func rejectUnsupportedRPMMetadata(root string) error {
prior, err := inspectPriorRPMMetadata(root)
if err != nil {
return err
}
if prior.oversized {
return &Error{Kind: KindRejected, Op: "inspect prior rpm index", Path: prior.path, Err: errors.New("repomd.xml exceeds the inspection limit; remove it explicitly to rebuild without its metadata")}
}
if len(prior.unsupported) == 0 {
return nil
}
sort.Strings(unsupported)
return &Error{Kind: KindRejected, Op: "inspect prior rpm index", Path: path, Err: fmt.Errorf("repomd.xml also publishes %s metadata that SOW does not generate; remove repodata/repomd.xml to rebuild without it", strings.Join(unsupported, ", "))}
quoted := make([]string, len(prior.unsupported))
for i, name := range prior.unsupported {
quoted[i] = strconv.Quote(name)
}
return &Error{Kind: KindRejected, Op: "inspect prior rpm index", Path: prior.path, Err: fmt.Errorf("repomd.xml also publishes %s metadata that SOW does not generate; remove repodata/repomd.xml to rebuild without it", strings.Join(quoted, ", "))}
}

func prepareDEBPublication(ctx context.Context, root string, staged stagedBuild, plan *publicationPlan) error {
Expand Down
3 changes: 3 additions & 0 deletions internal/v2/plain/types.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,9 @@ type Result struct {
MarkerSHA256 string `json:"marker_sha256,omitempty"`
Noop bool `json:"noop"`
Recovered bool `json:"recovered"`
// DroppedMetadata lists repomd.xml data types, such as group or modules,
// that the replaced index carried but SOW does not generate.
DroppedMetadata []string `json:"dropped_metadata,omitempty"`
}

func normalizeOptions(ctx context.Context, opts Options) (Options, error) {
Expand Down
4 changes: 4 additions & 0 deletions internal/v2cli/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"fmt"
"io"
"strings"

"github.com/pgsty/sow/internal/v2/plain"
)
Expand Down Expand Up @@ -54,6 +55,9 @@ func ExecuteCreate(ctx context.Context, inv Invocation, stdout, stderr io.Writer
fmt.Fprintln(stderr, err)
return ExitRuntime
}
if len(result.DroppedMetadata) != 0 {
fmt.Fprintf(stderr, "warning: replaced repodata/repomd.xml without metadata that SOW does not generate: %s\n", strings.Join(result.DroppedMetadata, ", "))
}
return ExitOK
}

Expand Down
26 changes: 26 additions & 0 deletions internal/v2cli/create_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,32 @@ func TestExecuteCreateJSONAndOptionMapping(t *testing.T) {
}
}

func TestExecuteCreateReportsDroppedMetadata(t *testing.T) {
result := plain.Result{Dir: "/tmp/repo", RPM: 1, Kept: []string{"pkg.rpm"}, Removed: []string{}, DroppedMetadata: []string{"group", "modules"}}
create := func(context.Context, plain.Options) (plain.Result, error) { return result, nil }
for _, jsonOutput := range []bool{false, true} {
args := []string{"create", "repo"}
if jsonOutput {
args = append(args, "--json")
}
inv, err := Parse(args)
if err != nil {
t.Fatal(err)
}
var stdout, stderr bytes.Buffer
if code := ExecuteCreate(context.Background(), inv, &stdout, &stderr, create); code != ExitOK {
t.Fatalf("json=%t code=%d stderr=%q", jsonOutput, code, stderr.String())
}
if jsonOutput {
if !strings.Contains(stdout.String(), `"dropped_metadata":["group","modules"]`) || stderr.Len() != 0 {
t.Fatalf("json stdout=%q stderr=%q", stdout.String(), stderr.String())
}
} else if !strings.HasPrefix(stdout.String(), "created /tmp/repo:") || !strings.Contains(stderr.String(), "warning: replaced repodata/repomd.xml without metadata that SOW does not generate: group, modules") {
t.Fatalf("human stdout=%q stderr=%q", stdout.String(), stderr.String())
}
}
}

func TestExecuteCreateErrorMapping(t *testing.T) {
tests := []struct {
kind plain.ErrorKind
Expand Down
2 changes: 2 additions & 0 deletions internal/v2cli/help.go
Original file line number Diff line number Diff line change
Expand Up @@ -128,6 +128,8 @@ Options:
-h, --help Show help

--passfile requires --sign-with; SOW_GPG_PASSFILE alone does not enable signing.
An existing repomd.xml is replaced. Data types SOW does not generate, such as
group or modules, are reported as dropped.
`,
"prune": `Usage:
sow prune DIR [--dry-run] [--protect NAME]... [-j N] [--metadata-timestamp SECONDS] [-T DUR | -N] [--json]
Expand Down
Loading