carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1//go:build linux && rootrunner
2
3package host
4
5import (
6 "encoding/json"
7 "fmt"
8 "os"
9 "os/exec"
10 "path/filepath"
11 "strings"
12 "testing"
13
14 "github.com/carlosframework/platform/internal/registry"
15 "github.com/carlosframework/platform/internal/rollout"
16 "github.com/carlosframework/platform/internal/rootparticipation"
17 "github.com/carlosframework/platform/internal/rootstate"
18)
19
20func TestPlan14RootRetryHardenedController(t *testing.T) {
21 if os.Getenv("PLAN14_RETRY_HARDENED") == "1" {
22 s, err := rootstate.Open(rollout.RootFailuresDir, false, false)
23 if err != nil {
24 t.Fatal(err)
25 }
26 defer s.Close()
27 b, err := rollout.OpenRootFailures(s, "box/en/1")
28 if err != nil {
29 t.Fatal(err)
30 }
31 p := rootUpdateSource{book: b, publish: publishRootRetry}
32 if err := p.publishRetry(rootUpdateRecord{Route: registry.Route{Host: "h.example.com", Account: "bab", App: "km"}}, "v1"); err != nil {
33 t.Fatal(err)
34 }
35 return
36 }
37 if os.Geteuid() != 0 {
38 t.Fatal("root required")
39 }
40 for _, path := range []string{"/var/lib/carlos-claims", "/var/lib/carlos-release", rootRetryProjection, "/run/carlos-control", "/run/carlos"} {
41 if err := os.Mkdir(path, 0755); err != nil {
42 t.Fatal("existing fixture", path, err)
43 }
44 path := path
45 t.Cleanup(func() { os.RemoveAll(path) })
46 }
47 state, err := rootstate.Open(rootparticipation.Namespace, true, false)
48 if err != nil {
49 t.Fatal(err)
50 }
51 raw, _ := json.Marshal(rootparticipation.Census{Schema: 1, Box: "box", Enrollment: "en", Migration: 1, Revision: 1, LaunchRevision: 1, Complete: true})
52 if err = state.Write("census.json", raw); err != nil {
53 t.Fatal(err)
54 }
55 state.Close()
56 bookState, err := rootstate.Open(rollout.RootFailuresDir, true, false)
57 if err != nil {
58 t.Fatal(err)
59 }
60 book, err := rollout.InitializeRootFailures(bookState, "box/en/1", nil)
61 if err != nil {
62 t.Fatal(err)
63 }
64 if _, err = book.Record(rollout.FailureRecord{Account: "bab", App: "km", Host: "h.example.com", Build: "v1", HeldDown: []string{"h.example.com"}}); err != nil {
65 t.Fatal(err)
66 }
67 id := book.ID()
68 bookState.Close()
69 dir := generationRootRunnerDir(t)
70 config := filepath.Join(dir, "host.env")
71 if err := os.WriteFile(config, []byte("CARLOS_HOST_CLAIM=1\nCARLOS_DATA_DIR="+dir+"\nCARLOS_OPT_DIR="+dir+"\n"), 0600); err != nil {
72 t.Fatal(err)
73 }
74 settings, err := readClaimUnitSettings(config)
75 if err != nil || !settings.rootRetry {
76 t.Fatal("production settings omitted retry scope", settings, err)
77 }
78 binary, err := os.ReadFile(os.Args[0])
79 if err != nil {
80 t.Fatal(err)
81 }
82 runner := filepath.Join(dir, "runner.test")
83 if err = os.WriteFile(runner, binary, 0755); err != nil {
84 t.Fatal(err)
85 }
86 raw, err = unitsFS.ReadFile("units/" + claimControlUnit)
87 if err != nil {
88 t.Fatal(err)
89 }
90 var lines []string
91 for _, line := range strings.Split(string(claimUnitContent(claimControlUnit, raw, settings)), "\n") {
92 if strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") || strings.HasPrefix(line, "ExecStart=") {
93 continue
94 }
95 lines = append(lines, line)
96 }
97 const unit = "plan14-p3-retry.service"
98 path := filepath.Join("/run/systemd/system", unit)
99 file, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0644)
100 if err != nil {
101 t.Fatal(err)
102 }
103 content := strings.Join(lines, "\n") + fmt.Sprintf("\nType=oneshot\nEnvironment=PLAN14_RETRY_HARDENED=1\nExecStart=%s -test.run=^TestPlan14RootRetryHardenedController$ -test.v\nTimeoutStartSec=60\nIPAddressDeny=any\n", runner)
104 if _, err = file.WriteString(content); err != nil {
105 t.Fatal(err)
106 }
107 file.Close()
108 t.Cleanup(func() {
109 exec.Command("systemctl", "stop", unit).Run()
110 os.Remove(path)
111 exec.Command("systemctl", "daemon-reload").Run()
112 })
113 for _, args := range [][]string{{"daemon-reload"}, {"start", unit}} {
114 if out, err := exec.Command("systemctl", args...).CombinedOutput(); err != nil {
115 logs, _ := exec.Command("journalctl", "-u", unit, "--no-pager").CombinedOutput()
116 t.Fatal(err, string(out), string(logs))
117 }
118 }
119 s, err := rootstate.Open(rootRetryProjection, false, true)
120 if err != nil {
121 t.Fatal(err)
122 }
123 defer s.Close()
124 raw, err = s.Read("h.example.com.json")
125 var report rootRetryStatusRecord
126 if err != nil || json.Unmarshal(raw, &report) != nil || report.HeldBook != id || report.HeldEpoch == 0 {
127 t.Fatal("hardened controller failed delivery", string(raw), err)
128 }
129}
130