197 lines
8.3 KiB
Go
197 lines
8.3 KiB
Go
package main
|
|
|
|
import (
|
|
"context"
|
|
"encoding/json/jsontext"
|
|
"fmt"
|
|
"slices"
|
|
"strings"
|
|
"testing"
|
|
"time"
|
|
|
|
connectorv1 "github.com/samcharles93/archipelago/sdk/connector/v1"
|
|
)
|
|
|
|
type fakeIncus struct {
|
|
execs [][]string
|
|
realpaths map[string]string
|
|
snaps map[string][]snapshot
|
|
created []string
|
|
failSnap bool
|
|
}
|
|
|
|
func (f *fakeIncus) Instances(context.Context, string) ([]instance, error) {
|
|
return []instance{{"web-1", "Running"}, {"other", "Stopped"}}, nil
|
|
}
|
|
|
|
func (f *fakeIncus) Snapshots(_ context.Context, project, name string) ([]snapshot, error) {
|
|
return f.snaps[key(project, name)], nil
|
|
}
|
|
|
|
func (f *fakeIncus) Snapshot(_ context.Context, project, name, snap string) error {
|
|
f.created = append(f.created, snap)
|
|
f.snaps[key(project, name)] = append(f.snaps[key(project, name)], snapshot{Name: snap, Created: time.Now()})
|
|
if f.failSnap {
|
|
return fmt.Errorf("connection reset")
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func (f *fakeIncus) Exec(_ context.Context, _, _ string, argv []string) (execResult, error) {
|
|
f.execs = append(f.execs, argv)
|
|
if argv[0] == "realpath" {
|
|
p := argv[len(argv)-1]
|
|
if r, ok := f.realpaths[p]; ok {
|
|
p = r
|
|
}
|
|
return execResult{Stdout: p + "\n"}, nil
|
|
}
|
|
return execResult{Stdout: "ok"}, nil
|
|
}
|
|
|
|
func newTestConnector(t *testing.T, f *fakeIncus) *incusConnector {
|
|
t.Helper()
|
|
c, err := configure(map[string]string{
|
|
"containers": `[{"project":"default","name":"web-1","environment":"prod","cache_paths":["/var/cache/app","/srv/tmp/"]},{"project":"default","name":"app-1","cache_paths":["/var/cache/app"]}]`,
|
|
"commands": `[{"name":"disk_usage","argv":["/usr/bin/df","-h"]}]`,
|
|
"default_environment": "staging",
|
|
"snapshot_window": "24h",
|
|
}, f)
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
return c
|
|
}
|
|
|
|
// TestConfinement guards the two security boundaries: a command runs only
|
|
// when allow-listed with its fixed argv, and a deletion only removes what is
|
|
// strictly under the container's cache paths, after symlinks are resolved.
|
|
func TestConfinement(t *testing.T) {
|
|
tests := []struct {
|
|
name string
|
|
lookup string // a lookup, else the delete_cache action
|
|
input string
|
|
real map[string]string
|
|
err string // empty means allowed
|
|
want []string
|
|
}{
|
|
{name: "delete under cache path", input: `{"project":"default","container":"web-1","path":"/var/cache/app/pkg"}`,
|
|
want: []string{"rm", "-rf", "--one-file-system", "--", "/var/cache/app/pkg"}},
|
|
{name: "path cleaned before use", input: `{"project":"default","container":"web-1","path":"/var/cache/app//x/./y/"}`,
|
|
want: []string{"rm", "-rf", "--one-file-system", "--", "/var/cache/app/x/y"}},
|
|
{name: "second path with trailing slash root", input: `{"project":"default","container":"web-1","path":"/srv/tmp/a"}`,
|
|
want: []string{"rm", "-rf", "--one-file-system", "--", "/srv/tmp/a"}},
|
|
{name: "allowed command", lookup: "run_command", input: `{"project":"default","container":"web-1","command":"disk_usage"}`,
|
|
want: []string{"/usr/bin/df", "-h"}},
|
|
|
|
{name: "outside every cache path", input: `{"project":"default","container":"web-1","path":"/etc/passwd"}`, err: "outside"},
|
|
{name: "dot-dot escape", input: `{"project":"default","container":"web-1","path":"/var/cache/app/../../etc"}`, err: "outside"},
|
|
{name: "sibling sharing a prefix", input: `{"project":"default","container":"web-1","path":"/var/cache/app-evil/x"}`, err: "outside"},
|
|
{name: "the cache path itself", input: `{"project":"default","container":"web-1","path":"/var/cache/app"}`, err: "outside"},
|
|
{name: "relative path", input: `{"project":"default","container":"web-1","path":"var/cache/app/x"}`, err: "absolute"},
|
|
{name: "control character", input: "{\"project\":\"default\",\"container\":\"web-1\",\"path\":\"/var/cache/app/x\\ny\"}", err: "control"},
|
|
{name: "another container's path", input: `{"project":"default","container":"app-1","path":"/srv/tmp/a"}`, err: "outside"},
|
|
{name: "symlink leading out", input: `{"project":"default","container":"web-1","path":"/var/cache/app/link"}`,
|
|
real: map[string]string{"/var/cache/app/link": "/etc"}, err: "resolves outside"},
|
|
{name: "undeclared container", input: `{"project":"default","container":"db-1","path":"/var/cache/app/x"}`, err: "isn't configured"},
|
|
{name: "undeclared project", input: `{"project":"other","container":"web-1","path":"/var/cache/app/x"}`, err: "isn't configured"},
|
|
{name: "command off the allow-list", lookup: "run_command", input: `{"project":"default","container":"web-1","command":"cat_shadow"}`, err: "allow-list"},
|
|
{name: "command argv smuggled in", lookup: "run_command", input: `{"project":"default","container":"web-1","command":"disk_usage","argv":["sh"]}`, err: "input"},
|
|
{name: "command on undeclared container", lookup: "run_command", input: `{"project":"default","container":"db-1","command":"disk_usage"}`, err: "isn't configured"},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := &fakeIncus{realpaths: tt.real, snaps: map[string][]snapshot{}}
|
|
c := newTestConnector(t, f)
|
|
var err error
|
|
if tt.lookup != "" {
|
|
_, _, err = c.Lookup(t.Context(), tt.lookup, jsontext.Value(tt.input))
|
|
} else {
|
|
_, _, err = c.Invoke(t.Context(), "delete_cache", jsontext.Value(tt.input), "k1")
|
|
_, rerr := c.Resolve(t.Context(), "delete_cache", jsontext.Value(tt.input))
|
|
if tt.real == nil && (err == nil) != (rerr == nil) {
|
|
t.Errorf("Invoke err = %v but Resolve err = %v", err, rerr)
|
|
}
|
|
}
|
|
if tt.err != "" {
|
|
if err == nil || !strings.Contains(err.Error(), tt.err) {
|
|
t.Fatalf("error = %v, want one containing %q", err, tt.err)
|
|
}
|
|
for _, argv := range f.execs {
|
|
if argv[0] != "realpath" {
|
|
t.Fatalf("refused call still ran %v", argv)
|
|
}
|
|
}
|
|
return
|
|
}
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if got := f.execs[len(f.execs)-1]; !slices.Equal(got, tt.want) {
|
|
t.Errorf("ran %v, want %v", got, tt.want)
|
|
}
|
|
})
|
|
}
|
|
}
|
|
|
|
// TestSnapshotIdempotent guards a retried run taking a second snapshot, or
|
|
// reporting a failure for one that landed.
|
|
func TestSnapshotIdempotent(t *testing.T) {
|
|
in := jsontext.Value(`{"project":"default","container":"web-1"}`)
|
|
f := &fakeIncus{snaps: map[string][]snapshot{}}
|
|
c := newTestConnector(t, f)
|
|
first, _, err := c.Invoke(t.Context(), "snapshot_container", in, "key-1")
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
second, _, err := c.Invoke(t.Context(), "snapshot_container", in, "key-1")
|
|
if err != nil || string(first) != string(second) || len(f.created) != 1 {
|
|
t.Fatalf("retry: %s %v, snapshots created %v", second, err, f.created)
|
|
}
|
|
if _, _, err := c.Invoke(t.Context(), "snapshot_container", in, "key-2"); err != nil || len(f.created) != 2 {
|
|
t.Fatalf("a new key must take a new snapshot: %v %v", err, f.created)
|
|
}
|
|
|
|
f = &fakeIncus{snaps: map[string][]snapshot{}, failSnap: true}
|
|
c = newTestConnector(t, f)
|
|
if _, unknown, err := c.Invoke(t.Context(), "snapshot_container", in, "key-3"); err != nil || unknown {
|
|
t.Errorf("a create that landed before the error: unknown=%v err=%v", unknown, err)
|
|
}
|
|
}
|
|
|
|
// TestResolve guards the judged declaration: the target, its environment, and
|
|
// that only a recent snapshot makes a deletion recoverable.
|
|
func TestResolve(t *testing.T) {
|
|
del := jsontext.Value(`{"project":"default","container":"%s","path":"/var/cache/app/x"}`)
|
|
tests := []struct {
|
|
name string
|
|
container string
|
|
age time.Duration // zero means no snapshot
|
|
env string
|
|
rev connectorv1.Reversibility
|
|
}{
|
|
{"recent snapshot", "web-1", time.Hour, "prod", connectorv1.Reversibility_REVERSIBILITY_RECOVERABLE},
|
|
{"stale snapshot", "web-1", 48 * time.Hour, "prod", connectorv1.Reversibility_REVERSIBILITY_IRREVERSIBLE},
|
|
{"no snapshot, default environment", "app-1", 0, "staging", connectorv1.Reversibility_REVERSIBILITY_IRREVERSIBLE},
|
|
}
|
|
for _, tt := range tests {
|
|
t.Run(tt.name, func(t *testing.T) {
|
|
f := &fakeIncus{snaps: map[string][]snapshot{}}
|
|
if tt.age > 0 {
|
|
f.snaps[key("default", tt.container)] = []snapshot{{Name: "s", Created: time.Now().Add(-tt.age)}}
|
|
}
|
|
c := newTestConnector(t, f)
|
|
r, err := c.Resolve(t.Context(), "delete_cache", jsontext.Value(fmt.Sprintf(string(del), tt.container)))
|
|
if err != nil {
|
|
t.Fatal(err)
|
|
}
|
|
if r.Target.Kind != "container" || r.Target.Id != "default/"+tt.container || r.Target.Environment != tt.env || r.Target.Disposable {
|
|
t.Errorf("target = %v", r.Target)
|
|
}
|
|
if r.Effect.Reversibility != tt.rev {
|
|
t.Errorf("reversibility = %v, want %v", r.Effect.Reversibility, tt.rev)
|
|
}
|
|
})
|
|
}
|
|
}
|