100 lines
3.1 KiB
Go
100 lines
3.1 KiB
Go
|
|
//go:build linux || darwin
|
||
|
|
|
||
|
|
package artifactcache
|
||
|
|
|
||
|
|
import (
|
||
|
|
"context"
|
||
|
|
"errors"
|
||
|
|
"os"
|
||
|
|
"path/filepath"
|
||
|
|
"syscall"
|
||
|
|
"testing"
|
||
|
|
|
||
|
|
"gitea.maximumdirect.net/eric/notarius/internal/framework/contracts"
|
||
|
|
)
|
||
|
|
|
||
|
|
func TestFilesystemCacheUsesDeploymentCreationPermissions(t *testing.T) {
|
||
|
|
for _, test := range []struct {
|
||
|
|
name string
|
||
|
|
umask int
|
||
|
|
directory, file os.FileMode
|
||
|
|
}{
|
||
|
|
{"private", 0o077, 0o700, 0o600},
|
||
|
|
{"group", 0o007, 0o770, 0o660},
|
||
|
|
} {
|
||
|
|
t.Run(test.name, func(t *testing.T) {
|
||
|
|
previous := syscall.Umask(test.umask)
|
||
|
|
defer syscall.Umask(previous)
|
||
|
|
root := filepath.Join(t.TempDir(), "cache")
|
||
|
|
if err := newStore(t, root).Store(context.Background(), contracts.CacheAuto, "task", []byte("{}")); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
for path, expected := range map[string]os.FileMode{root: test.directory, filepath.Join(root, "task.json"): test.file} {
|
||
|
|
info, err := os.Stat(path)
|
||
|
|
if err != nil || info.Mode().Perm() != expected {
|
||
|
|
t.Fatalf("%s: info %v, error %v; want mode %o", path, info, err, expected)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
})
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFilesystemCacheDoesNotConvergeProvisionedRootPermissions(t *testing.T) {
|
||
|
|
root := t.TempDir()
|
||
|
|
if err := os.Chmod(root, os.ModeSetgid|0o751); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
before, err := os.Stat(root)
|
||
|
|
if err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
previous := syscall.Umask(0o007)
|
||
|
|
defer syscall.Umask(previous)
|
||
|
|
if err := newStore(t, root).Store(context.Background(), contracts.CacheRefresh, "task", []byte("{}")); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
after, err := os.Stat(root)
|
||
|
|
if err != nil || before.Mode() != after.Mode() {
|
||
|
|
t.Fatalf("provisioned root changed: before %v, after %v, error %v", before, after, err)
|
||
|
|
}
|
||
|
|
file, err := os.Stat(filepath.Join(root, "task.json"))
|
||
|
|
if err != nil || file.Mode().Perm() != 0o660 || file.Sys().(*syscall.Stat_t).Gid != after.Sys().(*syscall.Stat_t).Gid {
|
||
|
|
t.Fatalf("new artifact did not inherit deployment policy: %v, %v", file, err)
|
||
|
|
}
|
||
|
|
}
|
||
|
|
|
||
|
|
func TestFilesystemCachePermissionFailuresPreservePriorAcceptance(t *testing.T) {
|
||
|
|
if os.Geteuid() == 0 {
|
||
|
|
t.Skip("root bypasses ordinary Unix permission failures")
|
||
|
|
}
|
||
|
|
ctx := context.Background()
|
||
|
|
root := t.TempDir()
|
||
|
|
store := newStore(t, root)
|
||
|
|
want := []byte(`{"accepted":"before"}`)
|
||
|
|
if err := store.Store(ctx, contracts.CacheAuto, "task", want); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
entry := filepath.Join(root, "task.json")
|
||
|
|
if err := os.Chmod(entry, 0); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
t.Cleanup(func() { _ = os.Chmod(entry, 0o600) })
|
||
|
|
if got, err := store.Lookup(ctx, contracts.CacheAuto, "task"); got != nil || !errors.Is(err, os.ErrPermission) {
|
||
|
|
t.Fatalf("unreadable artifact = %q, %v", got, err)
|
||
|
|
}
|
||
|
|
if err := os.Chmod(entry, 0o600); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
if err := os.Chmod(root, 0o500); err != nil {
|
||
|
|
t.Fatal(err)
|
||
|
|
}
|
||
|
|
t.Cleanup(func() { _ = os.Chmod(root, 0o700) })
|
||
|
|
if err := store.Store(ctx, contracts.CacheRefresh, "task", []byte(`{"accepted":"after"}`)); !errors.Is(err, os.ErrPermission) {
|
||
|
|
t.Fatalf("unwritable cache = %v", err)
|
||
|
|
}
|
||
|
|
got, err := store.Lookup(ctx, contracts.CacheAuto, "task")
|
||
|
|
if err != nil || string(got) != string(want) {
|
||
|
|
t.Fatalf("failed refresh changed acceptance: %q, %v", got, err)
|
||
|
|
}
|
||
|
|
}
|