Files
mesh-host/internal/apply/contents_owner_test.go
T
jschoubben 684931e099 Give a directory's contents before the directory, matched by user
Review round 1: the contents are now given before the directory's own owner
changes, so a walk that fails leaves the directory as it was and the next apply
walks again; an entry gone meanwhile is nothing to give. An entry is matched by
its user alone: one of another user is left with everything below it, and one
given the new user gets the new group only where its group was the previous
group. A test now applies a directory whose owner changed, with stand-ins for
the chowns, and shows the contents given first and a placed child left.

Builds: design 47 H2 H-A-550, H-A-554 (2)
2026-11-23 12:25:59 +01:00

156 lines
5.9 KiB
Go

package apply
import (
"fmt"
"os"
"path/filepath"
"sort"
"strings"
"syscall"
"testing"
"git.novox.be/novox/mesh-sdk/go/declaration"
"github.com/novox/mesh-host/internal/store"
)
// A directory whose owner changes gives its contents the new owner (H-A-545 (2), H-A-550): what its previous owner
// held, never a path the declaration places (a declared child with the same previous owner keeps its owner, and what
// lies below it), never what another account owns, never through a link, never on another file system.
func TestADirectoryGivesItsContentsTheNewOwner(t *testing.T) {
me, myGroup := os.Getuid(), os.Getgid()
to := [2]int{me + 4242, myGroup + 4242}
cases := []struct {
name string
from [2]int
placed func(root string) []string
other string // a directory the stand-in deviceOf says is on another file system
group int // the group an entry keeps, when not to's
want []string
}{
{name: "what the previous owner wrote is given", from: [2]int{me, myGroup},
want: []string{"data", "data/repo", "kept", "kept/webhook.secret", "link", "token"}},
{name: "a declared child with the same previous owner keeps its owner, and what lies below it",
from: [2]int{me, myGroup}, placed: func(root string) []string { return []string{filepath.Join(root, "data")} },
want: []string{"kept", "kept/webhook.secret", "link", "token"}},
{name: "what another user owns is left", from: [2]int{me + 1, myGroup}},
{name: "the new user is given, the group only where it was the previous group", from: [2]int{me, myGroup + 1},
group: myGroup, want: []string{"data", "data/repo", "kept", "kept/webhook.secret", "link", "token"}},
{name: "another file system is not crossed", from: [2]int{me, myGroup}, other: "kept",
want: []string{"data", "data/repo", "link", "token"}},
}
for _, c := range cases {
t.Run(c.name, func(t *testing.T) {
outside := t.TempDir()
if err := os.WriteFile(filepath.Join(outside, "elsewhere"), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
root := t.TempDir()
for _, d := range []string{"data", "kept"} {
if err := os.Mkdir(filepath.Join(root, d), 0o700); err != nil {
t.Fatal(err)
}
}
for _, f := range []string{"data/repo", "kept/webhook.secret", "token"} {
if err := os.WriteFile(filepath.Join(root, f), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
// A link to a directory outside: owned as a link, and never followed.
if err := os.Symlink(outside, filepath.Join(root, "link")); err != nil {
t.Fatal(err)
}
var given []string
wasChown, wasDevice := lchownContent, deviceOf
t.Cleanup(func() { lchownContent, deviceOf = wasChown, wasDevice })
wantGroup := to[1]
if c.group != 0 {
wantGroup = c.group
}
lchownContent = func(path string, uid, gid int) error {
if uid != to[0] || gid != wantGroup {
t.Errorf("%s given to %d:%d, want %d:%d", path, uid, gid, to[0], wantGroup)
}
rel, _ := filepath.Rel(root, path)
given = append(given, rel)
return nil
}
deviceOf = func(path string) (uint64, error) {
if c.other != "" && filepath.Clean(path) == filepath.Join(root, c.other) {
return 2, nil
}
return 1, nil
}
placed := map[string]bool{}
if c.placed != nil {
for _, p := range c.placed(root) {
placed[filepath.Clean(p)] = true
}
}
n, err := giveContents(root, c.from, to, placed)
if err != nil {
t.Fatal(err)
}
sort.Strings(given)
if n != len(c.want) || strings.Join(given, ",") != strings.Join(c.want, ",") {
t.Fatalf("gave %d: %v, want %v", n, given, c.want)
}
})
}
// The placed paths are every resource's target, cleaned.
got := placedPaths([]declaration.Resource{&declaration.Directory{Path: "/var/lib/gitea/data/"}},
[]declaration.Resource{&declaration.File{Path: "/var/lib/gitea/server.env"}})
if !got["/var/lib/gitea/data"] || !got["/var/lib/gitea/server.env"] || len(got) != 2 {
t.Fatalf("placed %v", got)
}
}
// Through the apply (review r1 of fix/gitea-runs-as): a directory found with another owner than declared gives its
// contents the new owner first, then itself; a placed child with the same previous owner keeps its owner; and a walk that
// fails leaves the directory's owner as it was.
func TestADirectoryWhoseOwnerChangedGivesItsContentsFirst(t *testing.T) {
me, myGroup := os.Getuid(), os.Getgid()
root := t.TempDir()
dir := filepath.Join(root, "state")
for _, d := range []string{dir, filepath.Join(dir, "data")} {
if err := os.Mkdir(d, 0o700); err != nil {
t.Fatal(err)
}
}
for _, f := range []string{"webhook.secret", "data/repo"} {
if err := os.WriteFile(filepath.Join(dir, f), []byte("x"), 0o600); err != nil {
t.Fatal(err)
}
}
owner := fmt.Sprintf("%d:%d", me+4242, myGroup+4242)
r := &declaration.Directory{ID: "gitea.state", Path: dir, Mode: "0700", Owner: owner}
placed := map[string]bool{filepath.Join(dir, "data"): true}
for _, fails := range []bool{true, false} {
var order []string
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
lchownContent = func(path string, uid, gid int) error {
if fails {
return syscall.EPERM
}
rel, _ := filepath.Rel(dir, path)
order = append(order, rel)
return nil
}
ownDirectory = func(path, o string) error { order = append(order, "."); return nil }
fchownDirectory = func(fd, uid, gid int) error { order = append(order, "."); return nil }
_, err := applyDirectory(r, store.Applied{Target: dir}, true, placed)
lchownContent, ownDirectory, fchownDirectory = wasContent, wasOwn, wasF
if fails {
if err == nil || len(order) != 0 {
t.Fatalf("a failing walk answered %v and changed %v; want an error and the directory's owner left", err, order)
}
continue
}
if err != nil {
t.Fatal(err)
}
if strings.Join(order, ",") != "webhook.secret,." {
t.Fatalf("changed %v, want the contents but the placed data, then the directory", order)
}
}
}