oficina / equipo Public

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

Plain git — no account needed to clone.

Download

Download this file

1package release
2
3import (
4 "crypto/sha256"
5 "encoding/hex"
6 "os"
7 "path/filepath"
8 "strings"
9 "testing"
10)
11
12// The rigs' gitleaks, and the line that links its hash into their stub
13// runners, as the release build links it into equipo-runner.
14const scannerBytes = "scanner"
15
16var linkedScanner = func() string {
17 sum := sha256.Sum256([]byte(scannerBytes))
18 return "# gitleaks " + hex.EncodeToString(sum[:]) + "\n"
19}()
20
21// scriptRig is one production script's rig, seen the same way: where the
22// staged runner release is, where the script installs it, and how to plan
23// and run it.
24type scriptRig struct {
25 release, installed, log string
26 plan, execute func() (string, error)
27}
28
29func scannerRigs(t *testing.T) map[string]func() scriptRig {
30 return map[string]func() scriptRig{
31 "upgrade-thin.sh": func() scriptRig {
32 r := newUpgradeRig(t)
33 release := filepath.Join(filepath.Dir(r.script), "..", "releases", "equipo-runner-linux-amd64-"+upgradeTo)
34 return scriptRig{release, filepath.Join(r.runner, "releases", upgradeTo), r.log,
35 func() (string, error) { return r.run(upgradeTo) }, func() (string, error) { return r.run(upgradeTo, "--execute") }}
36 },
37 "cutover-thin.sh": func() scriptRig {
38 r := newCutoverRig(t)
39 release := filepath.Join(filepath.Dir(r.script), "..", "releases", "equipo-runner-linux-amd64-"+cutoverVersion)
40 return scriptRig{release, filepath.Join(r.runner, "releases", cutoverVersion), r.log,
41 func() (string, error) { return r.run(cutoverVersion) }, func() (string, error) { return r.run(cutoverVersion, "--execute") }}
42 },
43 "cutover-finish-thin.sh": func() scriptRig {
44 r := newFinishRig(t)
45 release := filepath.Join(r.repo, "releases", "equipo-runner-linux-amd64-"+finishVersion)
46 return scriptRig{release, filepath.Join(r.runner, "releases", finishVersion), r.log,
47 func() (string, error) { return r.run("cutover-finish-thin.sh") }, func() (string, error) { return r.run("cutover-finish-thin.sh", "--execute") }}
48 },
49 }
50}
51
52// The runner scans every preview with the gitleaks beside its own
53// executable; without it every preview fails (preview_scanner). Each
54// script that installs a production runner checks the staged scanner in
55// its preflight and installs it beside the runner, 0700 like the others.
56func TestThinScriptsInstallTheScannerBesideTheRunner(t *testing.T) {
57 for name, rig := range scannerRigs(t) {
58 t.Run(name, func(t *testing.T) {
59 r := rig()
60 out, err := r.plan()
61 if err != nil {
62 t.Fatalf("plan: %v\n%s", err, out)
63 }
64 inOrder(t, out, "== ", filepath.Join(r.release, "gitleaks")+": executable, and its sha256 is linked into "+filepath.Join(r.release, "equipo-runner"),
65 "install -m 0700 "+filepath.Join(r.release, "equipo-runner")+" "+filepath.Join(r.release, "equipo-tool-proxy")+" "+filepath.Join(r.release, "gitleaks")+" "+r.installed+"/",
66 "verify-installed")
67 if out, err = r.execute(); err != nil {
68 t.Fatalf("execute: %v\n%s", err, out)
69 }
70 info, err := os.Stat(filepath.Join(r.installed, "gitleaks"))
71 if err != nil || info.Mode().Perm() != 0700 {
72 t.Fatalf("gitleaks not installed 0700 beside the runner: %v %v", info, err)
73 }
74 if raw, _ := os.ReadFile(filepath.Join(r.installed, "gitleaks")); string(raw) != scannerBytes {
75 t.Fatalf("installed another scanner: %q", raw)
76 }
77 })
78 }
79}
80
81// A staged release without the scanner, with another one, or with one
82// that cannot run, is refused in the preflight, before anything changes.
83func TestThinScriptsRefuseWithoutTheLinkedScanner(t *testing.T) {
84 for name, rig := range scannerRigs(t) {
85 for fault, set := range map[string]func(scanner string) error{
86 "missing": os.Remove,
87 "other bytes": func(s string) error { return os.WriteFile(s, []byte("another scanner"), 0700) },
88 "not executable": func(s string) error { return os.Chmod(s, 0600) },
89 } {
90 t.Run(name+"/"+fault, func(t *testing.T) {
91 r := rig()
92 if err := set(filepath.Join(r.release, "gitleaks")); err != nil {
93 t.Fatal(err)
94 }
95 out, err := r.execute()
96 if err == nil || !strings.Contains(out, "refused: ") || !strings.Contains(out, "gitleaks") {
97 t.Fatalf("not refused for the scanner: %v\n%s", err, out)
98 }
99 raw, _ := os.ReadFile(r.log)
100 for _, change := range []string{"db pull", "env set", "env unset", "daemon-reload", "deploy", "enrol", "restart", "stop"} {
101 if strings.Contains(string(raw), change) {
102 t.Fatalf("changed something (%s) before refusing:\n%s", change, raw)
103 }
104 }
105 if _, err := os.Stat(r.installed); !os.IsNotExist(err) {
106 t.Fatalf("installed something before refusing: %v", err)
107 }
108 })
109 }
110 }
111}
112