A module moved to an account of its own (runs-as) has files below its directories that it wrote as the account it ran as before (gitea's webhook secret, 0600, as the operator's account); the node-engine changed only the directory's owner, so the new account could not read them. Now applyDirectory and applyMeshDirectory give the new owner to what the previous owner held below the directory, but never a path the declaration or another module's composition places (and what lies below it), never an entry another account owns, never through a link (Lchown) and never on another file system. Builds: design 47 H2 H-A-545 (2), H-A-550
96 lines
3.5 KiB
Go
96 lines
3.5 KiB
Go
package apply
|
|
|
|
import (
|
|
"os"
|
|
"path/filepath"
|
|
"sort"
|
|
"strings"
|
|
"testing"
|
|
|
|
"git.novox.be/novox/mesh-sdk/go/declaration"
|
|
)
|
|
|
|
// 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
|
|
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 account owns is left", from: [2]int{me + 1, myGroup}},
|
|
{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 })
|
|
lchownContent = func(path string, uid, gid int) error {
|
|
if uid != to[0] || gid != to[1] {
|
|
t.Errorf("%s given to %d:%d, want %d:%d", path, uid, gid, to[0], to[1])
|
|
}
|
|
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)
|
|
}
|
|
}
|