344 lines
15 KiB
Go
344 lines
15 KiB
Go
package apply
|
|
|
|
import (
|
|
"context"
|
|
"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
|
|
back bool // the pass back (H-A-602): the directory goes from to's account back to from's, the owner read stubbed
|
|
linked bool // token has a second name, token.again (H-A-579 (2))
|
|
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"}},
|
|
{name: "an entry with two hard links is left with its owner, and said", from: [2]int{me, myGroup}, linked: true,
|
|
want: []string{"data", "data/repo", "kept", "kept/webhook.secret", "link"}},
|
|
{name: "given back to the earlier account it keeps the same three: a placed child, an entry of another user, a file with two links",
|
|
from: [2]int{me, myGroup}, back: true, linked: true,
|
|
placed: func(root string) []string { return []string{filepath.Join(root, "data")} }, want: []string{"link"}},
|
|
}
|
|
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)
|
|
}
|
|
if c.linked {
|
|
if err := os.Link(filepath.Join(root, "token"), filepath.Join(root, "token.again")); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
}
|
|
var given, said []string
|
|
wasChown, wasDevice, wasJournal := lchownContent, deviceOf, journal
|
|
t.Cleanup(func() { lchownContent, deviceOf, journal = wasChown, wasDevice, wasJournal })
|
|
journal = func(line string) { said = append(said, line) }
|
|
wantGroup := to[1]
|
|
from, target := c.from, to
|
|
if c.back {
|
|
// What the pass gave is now owned by the new account: every entry but kept reads as to's, and goes back to c.from.
|
|
from, target, wantGroup = to, c.from, c.from[1]
|
|
ownerRead = func(info os.FileInfo) (int, int, bool) {
|
|
if info.Name() == "kept" { // another user's entry, and what lies below it
|
|
return to[0] + 1, to[1], true
|
|
}
|
|
return to[0], to[1], true
|
|
}
|
|
defer func() { ownerRead = statOwner }()
|
|
}
|
|
if c.group != 0 {
|
|
wantGroup = c.group
|
|
}
|
|
lchownContent = func(path string, uid, gid int) error {
|
|
if uid != target[0] || gid != wantGroup {
|
|
t.Errorf("%s given to %d:%d, want %d:%d", path, uid, gid, target[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, from, target, 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)
|
|
}
|
|
if c.linked {
|
|
if len(said) != 2 || !strings.Contains(said[0]+said[1], filepath.Join(root, "token")+" has 2 hard links") {
|
|
t.Errorf("said %q, want one line for each name of token naming its 2 hard links", said)
|
|
}
|
|
} else if len(said) != 0 {
|
|
t.Errorf("said %q, want nothing", said)
|
|
}
|
|
})
|
|
}
|
|
// 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)
|
|
}
|
|
}
|
|
}
|
|
|
|
// Design 47 H2 H-A-579 (1), H-A-641, H-A-701: the owner pass runs with the module's units stopped, and the apply starts
|
|
// them once it completes; a pass that fails leaves them stopped and fails the apply naming the entry, and the next apply
|
|
// runs the pass again and starts them. The units of another module are left as they are.
|
|
func TestAnOwnerPassRunsWithTheModulesUnitsStopped(t *testing.T) {
|
|
me, myGroup := os.Getuid(), os.Getgid()
|
|
for name, fails := range map[string]bool{"the pass completes": false, "the pass fails on an entry": true} {
|
|
t.Run(name, func(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "state")
|
|
if err := os.Mkdir(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "webhook.secret"), []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
raw := fmt.Sprintf(`{"declaration":1,"resources":[
|
|
{"id":"app.state","type":"directory","path":%q,"mode":"0700","owner":"%d:%d"},
|
|
{"id":"app.run","type":"service","unit":"app.service","state":"running"},
|
|
{"id":"other.run","type":"service","unit":"other.service","state":"running"}]}`, dir, me+4242, myGroup+4242)
|
|
m := unitsMachine(map[string]*fakeUnit{
|
|
"app.service": {active: "active", enabled: "enabled"},
|
|
"other.service": {active: "active", enabled: "enabled"},
|
|
})
|
|
var events []string
|
|
run := func(ctx context.Context, name string, args ...string) (string, error) {
|
|
out, err := m.run(ctx, name, args...)
|
|
if name == "systemctl" {
|
|
args = oldShape(name, args)
|
|
if args[0] == "stop" || args[0] == "start" {
|
|
events = append(events, args[0]+" "+args[len(args)-1])
|
|
}
|
|
}
|
|
return out, err
|
|
}
|
|
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
|
|
t.Cleanup(func() { lchownContent, ownDirectory, fchownDirectory = wasContent, wasOwn, wasF })
|
|
lchownContent = func(path string, uid, gid int) error {
|
|
if fails {
|
|
return syscall.EPERM
|
|
}
|
|
events = append(events, "give "+filepath.Base(path))
|
|
return nil
|
|
}
|
|
ownDirectory = func(path, o string) error { events = append(events, "own directory"); return nil }
|
|
fchownDirectory = func(fd, uid, gid int) error { events = append(events, "own directory"); return nil }
|
|
known := store.State{}
|
|
known.Record(store.Applied{ID: "app.run", Type: "service", Target: "app.service"})
|
|
known.Record(store.Applied{ID: "app.state", Type: "directory", Target: dir})
|
|
_, after, err := Apply(context.Background(), archHost(t), parse(t, raw), known, store.OriginDeclared, run, nil, nil)
|
|
if fails {
|
|
if err == nil || !strings.Contains(err.Error(), "webhook.secret") {
|
|
t.Fatalf("the apply answered %v, want a failure naming the entry", err)
|
|
}
|
|
if got := strings.Join(events, ","); got != "stop app.service" || m.units["app.service"].active != "inactive" {
|
|
t.Fatalf("events %q, app.service %s: want it stopped and left stopped", got, m.units["app.service"].active)
|
|
}
|
|
// The next apply runs the pass again from the start, and starts the unit once it completes.
|
|
events = nil
|
|
lchownContent = func(path string, uid, gid int) error {
|
|
events = append(events, "give "+filepath.Base(path))
|
|
return nil
|
|
}
|
|
if _, _, err = Apply(context.Background(), archHost(t), parse(t, raw), after, store.OriginDeclared, run, nil, nil); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
} else if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
want := "stop app.service,give webhook.secret,own directory,start app.service"
|
|
if fails {
|
|
want = "give webhook.secret,own directory,start app.service" // already stopped by the apply that failed
|
|
}
|
|
if strings.Join(events, ",") != want {
|
|
t.Fatalf("events %q, want %q", strings.Join(events, ","), want)
|
|
}
|
|
if m.units["app.service"].active != "active" || m.units["other.service"].active != "active" || m.did("systemctl stop other.service") {
|
|
t.Fatalf("units are %+v, asked %v", m.units, m.asked)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// Design 47 H2 H-A-641 (answered by H-A-735): the pass at the engine's start, where no apply follows, stops the module's
|
|
// units before it and starts them after it; one whose pass fails leaves them stopped.
|
|
func TestTheOwnerPassAtTheEnginesStartStopsAndStartsTheUnits(t *testing.T) {
|
|
me, myGroup := os.Getuid(), os.Getgid()
|
|
for name, fails := range map[string]bool{"the pass completes": false, "the pass fails": true} {
|
|
t.Run(name, func(t *testing.T) {
|
|
dir := filepath.Join(t.TempDir(), "state")
|
|
if err := os.Mkdir(dir, 0o700); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if err := os.WriteFile(filepath.Join(dir, "webhook.secret"), []byte("x"), 0o600); err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
d := parse(t, fmt.Sprintf(`{"declaration":1,"resources":[
|
|
{"id":"app.state","type":"directory","path":%q,"mode":"0700","owner":"%d:%d"}]}`, dir, me+4242, myGroup+4242))
|
|
// H-A-752: only what the pass found running is stopped and started again, services before timers, then
|
|
// containers; a run-once step's service, a scheduled process's service off its timer, a service and a container
|
|
// found stopped stay stopped.
|
|
m := unitsMachine(map[string]*fakeUnit{
|
|
"app.service": {active: "active", enabled: "enabled"}, "app.timer": {active: "active", enabled: "enabled"},
|
|
"once.service": {active: "inactive", enabled: "disabled"},
|
|
"sched.timer": {active: "active", enabled: "enabled"},
|
|
"sched.service": {active: "inactive", enabled: "disabled"},
|
|
"idle.service": {active: "inactive", enabled: "enabled"},
|
|
})
|
|
m.containers = map[string]*fakeContainer{"app-box": {id: "a", running: true}, "app-off": {id: "b"}}
|
|
var events []string
|
|
run := func(ctx context.Context, name string, args ...string) (string, error) {
|
|
out, err := m.run(ctx, name, args...)
|
|
if name == "systemctl" {
|
|
args = oldShape(name, args)
|
|
if args[0] == "stop" || args[0] == "start" {
|
|
events = append(events, args[0]+" "+args[len(args)-1])
|
|
}
|
|
}
|
|
if name == "docker" && (args[0] == "stop" || args[0] == "start") {
|
|
events = append(events, "docker "+args[0]+" "+args[len(args)-1])
|
|
}
|
|
return out, err
|
|
}
|
|
wasContent, wasOwn, wasF := lchownContent, ownDirectory, fchownDirectory
|
|
t.Cleanup(func() { lchownContent, ownDirectory, fchownDirectory = wasContent, wasOwn, wasF })
|
|
lchownContent = func(path string, uid, gid int) error {
|
|
if fails {
|
|
return syscall.EPERM
|
|
}
|
|
events = append(events, "give "+filepath.Base(path))
|
|
return nil
|
|
}
|
|
ownDirectory = func(string, string) error { events = append(events, "own directory"); return nil }
|
|
fchownDirectory = func(int, int, int) error { events = append(events, "own directory"); return nil }
|
|
known := store.State{}
|
|
for _, r := range []store.Applied{
|
|
{ID: "app.run", Type: "service", Target: "app.service"}, {ID: "app.tick", Type: "service", Target: "app.timer"},
|
|
{ID: "app.once", Type: "service", Target: "once.service"}, {ID: "app.sched", Type: "process", Target: "sched"},
|
|
{ID: "app.idle", Type: "service", Target: "idle.service"},
|
|
{ID: "app.box", Type: "container", Target: "app-box"}, {ID: "app.off", Type: "container", Target: "app-off"},
|
|
{ID: "app.state", Type: "directory", Target: dir},
|
|
} {
|
|
known.Record(r)
|
|
}
|
|
ConvergeDirectories(context.Background(), archHost(t), d, known, run)
|
|
stops := "stop app.timer,stop sched.timer,stop app.service,docker stop app-box"
|
|
want := stops + ",give webhook.secret,own directory,start app.service,start app.timer,start sched.timer,docker start app-box"
|
|
if fails {
|
|
want = stops
|
|
}
|
|
if got := strings.Join(events, ","); got != want {
|
|
t.Fatalf("events %q, want %q", got, want)
|
|
}
|
|
})
|
|
}
|
|
}
|