oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package provider
2
3import (
4 "encoding/json"
5 "os"
6 "path/filepath"
7 "strings"
8 "testing"
9)
10
11func TestCheckGoPathPins(t *testing.T) {
12 for _, target := range []string{"root", "modules"} {
13 t.Run(target, func(t *testing.T) {
14 root, mods := t.TempDir(), t.TempDir()
15 in := CheckPolicyInputs{Workspace: t.TempDir(), Bwrap: "/usr/bin/true", GoRoot: root, ModuleCache: mods, Argv: []string{"/usr/bin/true"}}
16 p, err := BuildCheckPolicy(in)
17 if err != nil {
18 t.Fatal(err)
19 }
20 l := Launch{AttemptKey: "check", Workspace: in.Workspace, Policy: p}
21 if _, err = AttestLaunch(l, in.Argv); err != nil {
22 t.Fatal(err)
23 }
24 dir := root
25 if target == "modules" {
26 dir = mods
27 }
28 if err = os.WriteFile(filepath.Join(dir, "changed"), []byte("changed"), 0600); err != nil {
29 t.Fatal(err)
30 }
31 if _, err = AttestLaunch(l, in.Argv); err != nil {
32 t.Fatal("trusted host cache update refused", err)
33 }
34 })
35 }
36}
37
38func TestEngineeringGoScope(t *testing.T) {
39 for _, action := range []string{"inspect", "converse", "research", "answer", ""} {
40 t.Run(action, func(t *testing.T) {
41 b, in := policyFixture(t)
42 in.Action = action
43 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: t.TempDir(), Target: "/opt/go", ReadOnly: true})
44 if _, err := BuildPolicy(b, in); err == nil {
45 t.Fatal("Go trees admitted outside Engineering")
46 }
47 })
48 }
49 for _, target := range []string{"/opt/go", "/opt/go-mod"} {
50 for _, source := range []string{"workspace", "scratch", "account", "ancestor", "symlink", "internal-symlink", "writable", "owner"} {
51 t.Run(filepath.Base(target)+"/"+source, func(t *testing.T) {
52 b, in := policyFixture(t)
53 in.Action = "implement"
54 path := t.TempDir()
55 ro := true
56 switch source {
57 case "workspace":
58 path = in.Workspace
59 case "scratch":
60 path = in.Scratch
61 case "account":
62 path = b.CredentialHome
63 case "ancestor":
64 path = filepath.Dir(b.CredentialHome)
65 case "symlink":
66 link := filepath.Join(t.TempDir(), "link")
67 if err := os.Symlink(path, link); err != nil {
68 t.Fatal(err)
69 }
70 path = link
71 case "owner":
72 if target == "/opt/go" {
73 t.Skip("Go may be installed by root")
74 }
75 if os.Getuid() == 0 {
76 t.Skip("needs a directory owned by another user")
77 }
78 path = "/usr/share"
79 case "internal-symlink":
80 if target == "/opt/go-mod" {
81 t.Skip("module cache is path pinned")
82 }
83 writeFake(t, filepath.Join(path, "VERSION"), "fixture")
84 if err := os.Symlink("VERSION", filepath.Join(path, "link")); err != nil {
85 t.Fatal(err)
86 }
87 case "writable":
88 ro = false
89 }
90 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: path, Target: target, ReadOnly: ro})
91 if _, err := BuildPolicy(b, in); err == nil {
92 t.Fatal("unsafe Go tree admitted")
93 }
94 })
95 }
96 }
97
98}
99
100func TestEngineeringGitEnvironment(t *testing.T) {
101 b, in := policyFixture(t)
102 in.Action = "implement"
103 in.RuntimeFiles = append(in.RuntimeFiles, Mount{Source: t.TempDir(), Target: "/opt/go", ReadOnly: true})
104 p := mustPolicy(t, b, in)
105 for k, want := range map[string]string{"GIT_CONFIG_GLOBAL": "/dev/null", "GIT_CONFIG_NOSYSTEM": "1", "PATH": "/opt/go/bin:/usr/bin:/bin:/opt"} {
106 if p.Env[k] != want {
107 t.Errorf("%s=%q, want %q", k, p.Env[k], want)
108 }
109 }
110 if _, ok := p.Env["XDG_CONFIG_HOME"]; ok {
111 t.Error("provider receives writable Git config home")
112 }
113}
114
115func TestEngineeringGoPinReuse(t *testing.T) {
116 b, in := policyFixture(t)
117 in.Action = "implement"
118 root, mods := t.TempDir(), t.TempDir()
119 in.RuntimeFiles = append(in.RuntimeFiles, Mount{root, "/opt/go", true}, Mount{mods, "/opt/go-mod", true})
120 p := mustPolicy(t, b, in)
121 var manifest effectiveManifest
122 if err := json.Unmarshal(p.Manifest, &manifest); err != nil {
123 t.Fatal(err)
124 }
125 for _, f := range manifest.Files {
126 if f.Mount.Target == "/opt/go-mod" && f.SHA256 != "" {
127 t.Fatal("module cache was content hashed")
128 }
129 }
130 // Trusted host edits do not trigger another content scan in this launch.
131 writeFake(t, filepath.Join(root, "new"), "host update")
132 writeFake(t, filepath.Join(mods, "new"), "host update")
133 if err := p.ValidateSources(b); err != nil {
134 t.Fatal("rehashed launch sources", err)
135 }
136 if q := mustPolicy(t, b, in); q.ManifestHash != p.ManifestHash {
137 t.Fatal("rehashed policy rebuild")
138 }
139 held := b.provenance.goPins[in.RuntimeFiles[len(in.RuntimeFiles)-2]].file
140 if err := b.provenance.Close(); err != nil {
141 t.Fatal(err)
142 }
143 if _, err := held.Stat(); err == nil {
144 t.Fatal("lease leaked its Go descriptor")
145 }
146 if f, err := p.OpenGoTree(b, in.RuntimeFiles[len(in.RuntimeFiles)-2]); err == nil {
147 f.Close()
148 t.Fatal("closed lease supplied a Go tree")
149 }
150
151}
152
153func TestEngineeringGoManifestTampering(t *testing.T) {
154 for _, field := range []string{"PATH", "digest"} {
155 t.Run(field, func(t *testing.T) {
156 b, in := policyFixture(t)
157 in.Action = "implement"
158 in.RuntimeFiles = append(in.RuntimeFiles, Mount{t.TempDir(), "/opt/go", true})
159 p := mustPolicy(t, b, in)
160 var m effectiveManifest
161 if err := json.Unmarshal(p.Manifest, &m); err != nil {
162 t.Fatal(err)
163 }
164 if field == "PATH" {
165 m.Env["PATH"] = "/workspace:/usr/bin"
166 p.Env["PATH"] = m.Env["PATH"]
167 } else {
168 for i := range m.Files {
169 if m.Files[i].Mount.Target == "/opt/go" {
170 m.Files[i].SHA256 = strings.Repeat("0", 64)
171 }
172 }
173 }
174 p.Manifest, p.ManifestHash, _ = hashJSON(m)
175 _, p.Hash, _ = hashJSON(policyTemplate(m))
176 l := Launch{AttemptKey: "tamper", Workspace: in.Workspace, Binding: b, Selection: Selection{Provider: b.Provider, AccountID: b.AccountID}, Policy: p}
177 if _, err := AttestLaunch(l, in.Argv); err == nil {
178 t.Fatal("forged " + field + " accepted")
179 }
180 if field == "digest" {
181 if f, err := p.OpenGoTree(b, in.RuntimeFiles[len(in.RuntimeFiles)-1]); err == nil {
182 f.Close()
183 t.Fatal("forged descriptor digest accepted")
184 }
185 }
186 })
187 }
188}
189