oficina / equipo Public

Clone
git clone https://amadan.net/oficina/equipo

Plain git — no account needed to clone.

Download

Download this file

1package webread
2
3import (
4 "context"
5 "debug/elf"
6 "encoding/json"
7 "io/fs"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "runtime"
12 "strings"
13 "testing"
14 "time"
15
16 "amadan.net/oficina/equipo/internal/testci"
17)
18
19type probeReport struct {
20 Paths []string
21 Env []string
22 Home bool
23}
24
25// probeMain runs inside the fetcher's sandbox in place of the fetcher and
26// reports what it can see.
27func probeMain(args []string) int {
28 if len(args) != 1 {
29 return 2
30 }
31 var r probeReport
32 filepath.WalkDir("/", func(p string, d fs.DirEntry, err error) error {
33 if err != nil {
34 return nil
35 }
36 if p == "/proc" || p == "/sys" || p == "/dev" {
37 return fs.SkipDir
38 }
39 if r.Paths = append(r.Paths, p); len(r.Paths) > 10000 {
40 return fs.SkipAll
41 }
42 return nil
43 })
44 r.Env = os.Environ()
45 _, err := os.Stat(os.Getenv("HOME"))
46 r.Home = os.Getenv("HOME") != "" && err == nil
47 b, _ := json.Marshal(r)
48 if writeNew(filepath.Join(args[0], "probe.json"), b) != nil {
49 return 1
50 }
51 return 0
52}
53
54func usableBwrap(t *testing.T) (bwrap, self string) {
55 t.Helper()
56 if runtime.GOOS != "linux" {
57 t.Skip("the fetcher runs only on Linux")
58 }
59 testci.Tools(t, "bwrap")
60 bwrap, err := exec.LookPath("bwrap")
61 if err != nil {
62 testci.Absent(t, "bwrap is not installed")
63 }
64 if out, err := exec.Command(bwrap, "--unshare-all", "--share-net", "--ro-bind", "/", "/", "true").CombinedOutput(); err != nil {
65 testci.Absent(t, "bwrap cannot create namespaces here: "+strings.TrimSpace(string(out)))
66 }
67 self, err = os.Executable()
68 if err != nil {
69 t.Fatal(err)
70 }
71 if self, err = filepath.EvalSymlinks(self); err != nil {
72 t.Fatal(err)
73 }
74 f, err := elf.Open(self)
75 if err != nil {
76 t.Fatal(err)
77 }
78 defer f.Close()
79 for _, p := range f.Progs {
80 if p.Type == elf.PT_INTERP {
81 t.Skip("the test binary is dynamically linked; run with CGO_ENABLED=0 as make does")
82 }
83 }
84 return bwrap, self
85}
86
87func outDir(t *testing.T) string {
88 t.Helper()
89 root, err := filepath.EvalSymlinks(t.TempDir())
90 if err != nil {
91 t.Fatal(err)
92 }
93 dir := filepath.Join(root, "out")
94 if err = os.Mkdir(dir, 0700); err != nil {
95 t.Fatal(err)
96 }
97 return dir
98}
99
100// The fetcher's sandbox holds its binary, the CA bundle, resolv.conf and
101// the output directory: no runner state, home, credentials or host /etc.
102func TestLaunchHoldsNoRunnerState(t *testing.T) {
103 bwrap, self := usableBwrap(t)
104 state := t.TempDir()
105 os.WriteFile(filepath.Join(state, "forge.json"), []byte(`{"token":"secret"}`), 0600)
106 out := outDir(t)
107 ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
108 defer cancel()
109 cfg := LaunchConfig{Bwrap: bwrap, Binary: self, OutDir: out}
110 if err := launch(ctx, cfg, plan(t, "https://shop.example.com/"), "webread-probe"); err != nil {
111 t.Fatal(err)
112 }
113 raw, err := os.ReadFile(filepath.Join(out, "probe.json"))
114 if err != nil {
115 t.Fatal(err)
116 }
117 var r probeReport
118 if err = json.Unmarshal(raw, &r); err != nil {
119 t.Fatal(err)
120 }
121 allowed := map[string]bool{"/": true, "/webread": true, "/etc": true, "/etc/ssl": true, "/etc/ssl/certs": true, "/etc/ssl/certs/ca-certificates.crt": true, "/etc/resolv.conf": true, "/out": true, "/out/probe.json": true}
122 for _, p := range r.Paths {
123 if !allowed[p] {
124 t.Errorf("the sandbox shows %s", p)
125 }
126 }
127 // bubblewrap sets PWD itself after --chdir; nothing else may be there.
128 if len(r.Env) > 1 || len(r.Env) == 1 && r.Env[0] != "PWD=/" || r.Home {
129 t.Errorf("environment %v, home %v", r.Env, r.Home)
130 }
131}
132
133// The real subcommand, under bubblewrap, writes a bundle the runner reads.
134// The only source is the instance's own host, refused before any lookup,
135// so the test needs no network.
136func TestLaunchWritesABundle(t *testing.T) {
137 bwrap, self := usableBwrap(t)
138 out := outDir(t)
139 p := plan(t, "https://shop.example.com/")
140 p.Instance = "shop.example.com"
141 ctx, cancel := context.WithTimeout(context.Background(), time.Minute)
142 defer cancel()
143 if err := Launch(ctx, LaunchConfig{Bwrap: bwrap, Binary: self, OutDir: out}, p); err != nil {
144 t.Fatal(err)
145 }
146 b, err := ReadBundle(out)
147 if err != nil {
148 t.Fatal(err)
149 }
150 if len(b.Log) != 1 || b.Log[0].Reason != ReasonHostRefused {
151 t.Fatalf("log %+v", b.Log)
152 }
153}
154
155func TestLaunchRefusesItsInputs(t *testing.T) {
156 if runtime.GOOS != "linux" {
157 if err := Launch(context.Background(), LaunchConfig{}, plan(t, "https://shop.example.com/")); err != ErrUnsupported {
158 t.Fatalf("Launch off Linux: %v", err)
159 }
160 return
161 }
162 ok := plan(t, "https://shop.example.com/")
163 self, _ := os.Executable()
164 used := outDir(t)
165 os.WriteFile(filepath.Join(used, "x"), nil, 0600)
166 open := outDir(t)
167 os.Chmod(open, 0755)
168 link := filepath.Join(filepath.Dir(outDir(t)), "link")
169 os.Symlink(outDir(t), link)
170 for name, c := range map[string]struct {
171 cfg LaunchConfig
172 plan Plan
173 }{
174 "bad plan": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: outDir(t)}, Plan{}},
175 "relative out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: "out"}, ok},
176 "used out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: used}, ok},
177 "open out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: open}, ok},
178 "symlinked out": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: link}, ok},
179 "missing binary": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: "/nonexistent/equipo-runner", OutDir: outDir(t)}, ok},
180 "missing CA": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: self, OutDir: outDir(t), CABundle: "/nonexistent.crt"}, ok},
181 "relative binary": {LaunchConfig{Bwrap: "/usr/bin/bwrap", Binary: "equipo-runner", OutDir: outDir(t)}, ok},
182 "relative bwrap": {LaunchConfig{Bwrap: "bwrap", Binary: self, OutDir: outDir(t)}, ok},
183 } {
184 if err := Launch(context.Background(), c.cfg, c.plan); err == nil {
185 t.Errorf("%s: launched", name)
186 }
187 }
188}
189