| index ff381a412..f2c225474 100644 |
| --- a/internal/host/egress_fixtures_rootrunner_linux_test.go |
| +++ b/internal/host/egress_fixtures_rootrunner_linux_test.go |
| @@ -18,6 +18,7 @@ import ( |
| "net/url" |
| "os" |
| "os/exec" |
| + "os/user" |
| "path/filepath" |
| "runtime" |
| "slices" |
| @@ -25,13 +26,16 @@ import ( |
| "strings" |
| "sync" |
| "sync/atomic" |
| + "syscall" |
| "testing" |
| "time" |
| |
| "golang.org/x/sys/unix" |
| |
| "github.com/carlosframework/platform/internal/cgroup" |
| + "github.com/carlosframework/platform/internal/controlsock" |
| "github.com/carlosframework/platform/internal/instancectl" |
| + "github.com/carlosframework/platform/internal/instanceexec" |
| "github.com/carlosframework/platform/internal/provision" |
| "github.com/carlosframework/platform/internal/registry" |
| "github.com/carlosframework/platform/internal/release" |
| @@ -138,7 +142,14 @@ func egressIP(t *testing.T, args ...string) { |
| // them, which is why the tenants here are unit files, never systemd-run). |
| func egressTemplateDeny(t *testing.T) string { |
| t.Helper() |
| - raw, err := unitsFS.ReadFile("units/carlos-instance@.service") |
| + return egressUnitDeny(t, "carlos-instance@.service") |
| +} |
| + |
| +// egressUnitDeny is the IPAddressDeny= list of an embedded unit (one of |
| +// unitsFS's units/), verbatim. |
| +func egressUnitDeny(t *testing.T, name string) string { |
| + t.Helper() |
| + raw, err := unitsFS.ReadFile("units/" + name) |
| if err != nil { |
| t.Fatal(err) |
| } |
| @@ -146,13 +157,13 @@ func egressTemplateDeny(t *testing.T) string { |
| for _, line := range strings.Split(string(raw), "\n") { |
| if v, ok := strings.CutPrefix(line, "IPAddressDeny="); ok { |
| if deny != "" { |
| - t.Fatal("template carries more than one IPAddressDeny= line") |
| + t.Fatal(name, "carries more than one IPAddressDeny= line") |
| } |
| deny = v |
| } |
| } |
| if deny == "" { |
| - t.Fatal("template carries no IPAddressDeny= line") |
| + t.Fatal(name, "carries no IPAddressDeny= line") |
| } |
| return deny |
| } |
| @@ -187,6 +198,23 @@ func egressTargetNetns(t *testing.T) map[string]string { |
| // (generationMounts) refuses any nsfs mount on purpose. Calling close, or |
| // the test's end, tears it down once, last-made first. |
| func egressTargetNetnsClosable(t *testing.T) (map[string]string, func()) { |
| + t.Helper() |
| + return egressTargetNetnsAt(t, egressNetnsTask13) |
| +} |
| + |
| +// egressNetnsNames names a target fixture's namespace and veth pair, so |
| +// two tasks' witnesses never share one: link names are box-wide, and each |
| +// task removes only what it made. |
| +type egressNetnsNames struct { |
| + ns, hostLink, nsLink string |
| +} |
| + |
| +// egressNetnsTask13 is Task 13's fixture (carlos-egr, carlos-egr1/1n). |
| +var egressNetnsTask13 = egressNetnsNames{ns: egressNetns, hostLink: egressHostLink, nsLink: egressNsLink} |
| + |
| +// egressTargetNetnsAt is egressTargetNetnsClosable with the fixture's |
| +// names given. |
| +func egressTargetNetnsAt(t *testing.T, n egressNetnsNames) (map[string]string, func()) { |
| t.Helper() |
| var steps []func() |
| var once sync.Once |
| @@ -205,64 +233,64 @@ func egressTargetNetnsClosable(t *testing.T) (map[string]string, func()) { |
| if _, err := exec.LookPath("ip"); err != nil { |
| t.Fatal("needs ip", err) |
| } |
| - nsPath := filepath.Join("/run/netns", egressNetns) |
| + nsPath := filepath.Join("/run/netns", n.ns) |
| if _, err := os.Lstat(nsPath); !errors.Is(err, fs.ErrNotExist) { |
| t.Fatal("network namespace already exists", nsPath, err) |
| } |
| - if _, err := net.InterfaceByName(egressHostLink); err == nil { |
| - t.Fatal("link already exists", egressHostLink) |
| + if _, err := net.InterfaceByName(n.hostLink); err == nil { |
| + t.Fatal("link already exists", n.hostLink) |
| } |
| |
| - egressIP(t, "netns", "add", egressNetns) |
| + egressIP(t, "netns", "add", n.ns) |
| cleanup(func() { |
| - if out, err := exec.Command("ip", "netns", "del", egressNetns).CombinedOutput(); err != nil { |
| - t.Errorf("ip netns del %s: %v: %s", egressNetns, err, out) |
| + if out, err := exec.Command("ip", "netns", "del", n.ns).CombinedOutput(); err != nil { |
| + t.Errorf("ip netns del %s: %v: %s", n.ns, err, out) |
| } |
| if _, err := os.Lstat(nsPath); !errors.Is(err, fs.ErrNotExist) { |
| t.Errorf("network namespace %s left behind: %v", nsPath, err) |
| } |
| }) |
| - egressIP(t, "link", "add", egressHostLink, "type", "veth", "peer", "name", egressNsLink, "netns", egressNetns) |
| + egressIP(t, "link", "add", n.hostLink, "type", "veth", "peer", "name", n.nsLink, "netns", n.ns) |
| cleanup(func() { |
| // Deleting one end removes the pair. The namespace's own deletion |
| // would too, but only once nothing holds the namespace, so the |
| // pair goes first and explicitly. |
| - if _, err := net.InterfaceByName(egressHostLink); err == nil { |
| - if out, err := exec.Command("ip", "link", "del", egressHostLink).CombinedOutput(); err != nil { |
| - t.Errorf("ip link del %s: %v: %s", egressHostLink, err, out) |
| + if _, err := net.InterfaceByName(n.hostLink); err == nil { |
| + if out, err := exec.Command("ip", "link", "del", n.hostLink).CombinedOutput(); err != nil { |
| + t.Errorf("ip link del %s: %v: %s", n.hostLink, err, out) |
| } |
| } |
| for deadline := time.Now().Add(5 * time.Second); ; time.Sleep(100 * time.Millisecond) { |
| - if _, err := net.InterfaceByName(egressHostLink); err != nil { |
| + if _, err := net.InterfaceByName(n.hostLink); err != nil { |
| break |
| } |
| if time.Now().After(deadline) { |
| - t.Errorf("link %s left behind", egressHostLink) |
| + t.Errorf("link %s left behind", n.hostLink) |
| break |
| } |
| } |
| }) |
| - egressIP(t, "addr", "add", "10.0.5.1/32", "dev", egressHostLink) |
| - egressIP(t, "-6", "addr", "add", "fd00:5::1/128", "dev", egressHostLink, "nodad") |
| - egressIP(t, "link", "set", egressHostLink, "up") |
| - egressIP(t, "route", "add", "10.0.5.20/32", "dev", egressHostLink, "src", "10.0.5.1") |
| - egressIP(t, "route", "add", "10.0.5.21/32", "dev", egressHostLink, "src", "10.0.5.1") |
| - egressIP(t, "-6", "route", "add", "fd00:5::20/128", "dev", egressHostLink, "src", "fd00:5::1") |
| - |
| - in := func(args ...string) { t.Helper(); egressIP(t, append([]string{"-n", egressNetns}, args...)...) } |
| + egressIP(t, "addr", "add", "10.0.5.1/32", "dev", n.hostLink) |
| + egressIP(t, "-6", "addr", "add", "fd00:5::1/128", "dev", n.hostLink, "nodad") |
| + egressIP(t, "link", "set", n.hostLink, "up") |
| + egressIP(t, "route", "add", "10.0.5.20/32", "dev", n.hostLink, "src", "10.0.5.1") |
| + egressIP(t, "route", "add", "10.0.5.21/32", "dev", n.hostLink, "src", "10.0.5.1") |
| + egressIP(t, "-6", "route", "add", "fd00:5::20/128", "dev", n.hostLink, "src", "fd00:5::1") |
| + |
| + in := func(args ...string) { t.Helper(); egressIP(t, append([]string{"-n", n.ns}, args...)...) } |
| in("link", "set", "lo", "up") |
| - in("addr", "add", "10.0.5.20/32", "dev", egressNsLink) |
| - in("addr", "add", "10.0.5.21/32", "dev", egressNsLink) |
| - in("-6", "addr", "add", "fd00:5::20/128", "dev", egressNsLink, "nodad") |
| - in("link", "set", egressNsLink, "up") |
| - in("route", "add", "10.0.5.1/32", "dev", egressNsLink) |
| - in("-6", "route", "add", "fd00:5::1/128", "dev", egressNsLink) |
| + in("addr", "add", "10.0.5.20/32", "dev", n.nsLink) |
| + in("addr", "add", "10.0.5.21/32", "dev", n.nsLink) |
| + in("-6", "addr", "add", "fd00:5::20/128", "dev", n.nsLink, "nodad") |
| + in("link", "set", n.nsLink, "up") |
| + in("route", "add", "10.0.5.1/32", "dev", n.nsLink) |
| + in("-6", "route", "add", "fd00:5::1/128", "dev", n.nsLink) |
| |
| addrs := map[string]string{} |
| for _, a := range egressTargets { |
| ln, err := egressListenIn(nsPath, net.JoinHostPort(a, "0")) |
| if err != nil { |
| - t.Fatalf("listen on %s in %s: %v", a, egressNetns, err) |
| + t.Fatalf("listen on %s in %s: %v", a, n.ns, err) |
| } |
| cleanup(func() { ln.Close() }) |
| go func() { |
| @@ -625,7 +653,12 @@ func egressSliceProperties(t *testing.T, slice string) { |
| // egressNestedRow is an activator-managed instance row at the nested |
| // defaults under dataDir: the row the pre-claim rail counts as nested. |
| func egressNestedRow(dataDir, host string) registry.Route { |
| - return registry.Route{Host: host, Kind: "instance", Account: "abc", App: "shop", Channel: "stable", Hibernate: true, Status: "ready", |
| + return egressNestedAppRow(dataDir, host, "shop") |
| +} |
| + |
| +// egressNestedAppRow is egressNestedRow for the app named. |
| +func egressNestedAppRow(dataDir, host, app string) registry.Route { |
| + return registry.Route{Host: host, Kind: "instance", Account: "abc", App: app, Channel: "stable", Hibernate: true, Status: "ready", |
| DBPath: provision.NestedDBPath(dataDir, host), Socket: provision.NestedSocket(host)} |
| } |
| |
| @@ -678,10 +711,30 @@ type egressBox struct { |
| reg *registry.Registry |
| egress *instancectl.EgressAsserter |
| ctl *egressFailingCtl |
| + srv *instancectl.Server |
| client *instancectl.Client |
| staged instancectl.StagedExe |
| } |
| |
| +// egressBoxOptions widens the pre-claim box for Task 14. The zero value is |
| +// Task 13's box. |
| +type egressBoxOptions struct { |
| + // netns names the target fixture; zero is Task 13's. |
| + netns egressNetnsNames |
| + // others are more nested hosts on the box, each another app's (account |
| + // abc), with no policy: a registry row, a staged build behind the |
| + // host's live link, the run role's launch file and log socket, and the |
| + // host slice's cleanup. They are started through POST /start. |
| + others []egressOtherHost |
| + // probe installs the readiness probe (carlos-instance-probe.service |
| + // from unitsFS) and its fixtures, and gives the control service the |
| + // probe's production fields, for Server.RunProbe. |
| + probe bool |
| +} |
| + |
| +// egressOtherHost is another app's host on the box. |
| +type egressOtherHost struct{ host, app string } |
| + |
| // The templates the pre-claim start paths use. |
| var egressTemplates = []string{ |
| "carlos-instance@.service", |
| @@ -694,6 +747,11 @@ var egressTemplates = []string{ |
| const egressGen = 1 |
| |
| func newEgressPreClaimBox(t *testing.T) *egressBox { |
| + t.Helper() |
| + return newEgressPreClaimBoxWith(t, egressBoxOptions{}) |
| +} |
| + |
| +func newEgressPreClaimBoxWith(t *testing.T, o egressBoxOptions) *egressBox { |
| t.Helper() |
| egressRootPrereqs(t) |
| uid, gid := generationRunnerRegistryUser(t) |
| @@ -708,6 +766,9 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| instancectl.UnitName(instancectl.RoleCmd, host), |
| instancectl.GenUnitName(host, egressGen), |
| } |
| + for _, other := range o.others { |
| + units = append(units, instancectl.UnitName(instancectl.RoleRun, other.host)) |
| + } |
| for _, u := range units { |
| if state := egressShow(t, u, "LoadState"); state != "not-found" { |
| t.Fatal("unit already present", u, state) |
| @@ -732,7 +793,11 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| } |
| }) |
| } |
| - b.targets = egressTargetNetns(t) |
| + if o.netns == (egressNetnsNames{}) { |
| + b.targets = egressTargetNetns(t) |
| + } else { |
| + b.targets, _ = egressTargetNetnsAt(t, o.netns) |
| + } |
| |
| // The build: the test binary, as the platform binary the templates |
| // run (instance-exec, instance-exit) and as the app's staged build, |
| @@ -763,18 +828,34 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| if err := os.Symlink(staged, release.LiveLink("/opt/carlos", host)); err != nil { |
| t.Fatal(err) |
| } |
| + for _, other := range o.others { |
| + otherStaged := release.VersionPath("/opt/carlos", "abc", "v1", other.app) |
| + if err := os.MkdirAll(filepath.Dir(otherStaged), 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(otherStaged, image, 0o755); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.Symlink(otherStaged, release.LiveLink("/opt/carlos", other.host)); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| |
| // The instance's two directories, the carlos user's, and the log |
| // sockets PID 1 connects each unit's output to. |
| if err := os.Chown("/data", uid, gid); err != nil { |
| t.Fatal(err) |
| } |
| - for _, d := range []string{provision.InstanceDir("/data", host), provision.InstanceRunDir(host), filepath.Join(provision.SocketDir, "logs")} { |
| + instanceDirs := []string{provision.InstanceDir("/data", host), provision.InstanceRunDir(host)} |
| + for _, other := range o.others { |
| + instanceDirs = append(instanceDirs, provision.InstanceDir("/data", other.host), provision.InstanceRunDir(other.host)) |
| + } |
| + for _, d := range append([]string{filepath.Join(provision.SocketDir, "logs")}, instanceDirs...) { |
| if err := os.MkdirAll(d, 0o755); err != nil { |
| t.Fatal(err) |
| } |
| } |
| - for _, d := range []string{provision.InstanceDir("/data", host), provision.InstanceRunDir(host)} { |
| + for _, d := range instanceDirs { |
| if err := os.Chown(d, uid, gid); err != nil { |
| t.Fatal(err) |
| } |
| @@ -787,10 +868,14 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| logs.mu.Unlock() |
| } |
| }) |
| - for _, sock := range []string{ |
| + logSockets := []string{ |
| provision.LogSocket(host, "run"), provision.LogSocket(host, "seed"), provision.LogSocket(host, "sidecar"), |
| provision.LogSocket(host, "cmd"), provision.LogSocket(host, "cmd-err"), instancectl.GenLogSocket(host, egressGen), |
| - } { |
| + } |
| + for _, other := range o.others { |
| + logSockets = append(logSockets, provision.LogSocket(other.host, "run")) |
| + } |
| + for _, sock := range logSockets { |
| ln, err := net.Listen("unix", sock) |
| if err != nil { |
| t.Fatal(err) |
| @@ -840,8 +925,11 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| }) |
| } |
| egressMustCtl(t, "daemon-reload") |
| - // The slice's drop-ins come off after its units have stopped. |
| + // The slices' drop-ins come off after their units have stopped. |
| egressSliceProperties(t, b.slice) |
| + for _, other := range o.others { |
| + egressSliceProperties(t, instancectl.SliceName(other.host)) |
| + } |
| t.Cleanup(func() { |
| for _, u := range units { |
| if out, _ := egressCtl("show", "-p", "LoadState", "--value", u); out != "not-found" { |
| @@ -869,6 +957,11 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| if err := reg.SetPolicy(host, "10.0.5.20", "", egressExpiry()); err != nil { |
| t.Fatal(err) |
| } |
| + for _, other := range o.others { |
| + if err := reg.Upsert(egressNestedAppRow("/data", other.host, other.app)); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| b.reg = reg |
| |
| // The launch files the agent writes before each start. Serving roles |
| @@ -895,11 +988,24 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| t.Fatal(err) |
| } |
| } |
| + // Each other host's run role serves plan14-tenant the same way. |
| + for _, other := range o.others { |
| + raw, err := json.Marshal(instancectl.Launch{Schema: instancectl.LaunchSchema, Host: other.host, |
| + Argv: []string{filepath.Join(instancectl.LiveDir, other.host), "plan14-tenant"}, |
| + Env: []string{"PLAN14_TENANT_SOCKET=" + filepath.Join(provision.InstanceRunDir(other.host), "run.sock")}}) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := os.WriteFile(filepath.Join(provision.InstanceDir("/data", other.host), instancectl.LaunchFileName(instancectl.RoleRun)), raw, 0o644); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| |
| // The production pre-claim asserter (RunInstanceControl's !claims |
| // branch), its systemctl behind a pass-through the revoke witness can |
| // arm, and the control service as runWithClaim builds it, minus the |
| - // socket activation, the probe and the disk floors. |
| + // socket activation, the probe loop and the disk floors (with |
| + // o.probe, the probe's fields are runWithClaim's, for RunProbe). |
| b.egress = newEgressAsserter(registryEgressSource("/data", time.Now), registryEgressHosts("/data")) |
| b.ctl = &egressFailingCtl{next: b.egress.Systemctl, slice: b.slice} |
| b.egress.Systemctl = b.ctl.run |
| @@ -919,6 +1025,14 @@ func newEgressPreClaimBox(t *testing.T) *egressBox { |
| DataRoot: "/data/instances", |
| Egress: b.egress, |
| } |
| + if o.probe { |
| + egressProbeFixture(t, uid, gid) |
| + srv.ProbeOut = egressProbeOut |
| + srv.FixtureOK = egressProbeFixturesOK |
| + srv.ResolvConf = "/etc/resolv.conf" |
| + srv.ResolvedConf = instancectl.ResolvedConf |
| + } |
| + b.srv = srv |
| b.client = egressServe(t, srv, filepath.Join(t.TempDir(), "control.sock")) |
| return b |
| } |
| @@ -984,3 +1098,187 @@ func (b *egressBox) startRun(t *testing.T) *http.Client { |
| } |
| return c |
| } |
| + |
| +// The readiness probe, for the isolation witness (Task 14): its unit |
| +// runs this test binary as /opt/carlos/carlos instance-probe, which |
| +// prints the probe's report (instanceexec.RunProbe) as the real binary |
| +// does (main.go). |
| +func init() { |
| + if len(os.Args) >= 2 && os.Args[1] == "instance-probe" { |
| + instanceexec.RunProbe(os.Args[2:]) |
| + os.Exit(0) |
| + } |
| +} |
| + |
| +// egressProbeUnit is the readiness probe's unit (instancectl's probeUnit). |
| +const egressProbeUnit = "carlos-instance-probe.service" |
| + |
| +// egressProbeOut is where the probe unit's StandardOutput=truncate: lands: |
| +// runWithClaim's ProbeOut. |
| +var egressProbeOut = filepath.Join(controlsock.Dir, "probe.out") |
| + |
| +// egressProbeFixture installs the probe for Server.RunProbe on the |
| +// pre-claim box (its fixture roots already made): |
| +// |
| +// - the unit from unitsFS into /run/systemd/system, with its mount |
| +// dependencies (Requires=, After=) dropped as the box drops the |
| +// templates', everything else verbatim (its IPAddressDeny= and Slice= |
| +// among it); |
| +// - the two fixtures tmpfiles stages (bootstrap.go): /etc/carlos/probe-secret |
| +// root:carlos 0640 and /dev/shm/carlos-probe, root's; |
| +// - the probe's own instance directories, its bind sources; |
| +// - carlos-instances.slice's properties, refused if already set and |
| +// reset on cleanup: RunProbe writes the resolver allow there. |
| +// |
| +// Everything is removed on cleanup. |
| +func egressProbeFixture(t *testing.T, uid, gid int) { |
| + t.Helper() |
| + if state := egressShow(t, egressProbeUnit, "LoadState"); state != "not-found" { |
| + t.Fatal("unit already present", egressProbeUnit, state) |
| + } |
| + for _, dir := range []string{"/run/systemd/system", "/etc/systemd/system"} { |
| + if _, err := os.Lstat(filepath.Join(dir, egressProbeUnit)); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatal("probe unit already installed", dir, err) |
| + } |
| + } |
| + raw, err := unitsFS.ReadFile("units/" + egressProbeUnit) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + if !strings.Contains(string(raw), "\nStandardOutput=truncate:"+egressProbeOut+"\n") { |
| + t.Fatalf("the probe unit does not write its report to %s", egressProbeOut) |
| + } |
| + |
| + secret := "/etc/carlos/probe-secret" |
| + if err := os.WriteFile(secret, []byte("probe"), 0o640); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := errors.Join(os.Chown(secret, 0, gid), os.Chmod(secret, 0o640)); err != nil { |
| + t.Fatal(err) |
| + } |
| + shm := "/dev/shm/carlos-probe" |
| + f, err := os.OpenFile(shm, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644) |
| + if err != nil { |
| + t.Fatal("probe shm fixture:", err) |
| + } |
| + t.Cleanup(func() { |
| + if err := os.Remove(shm); err != nil { |
| + t.Error(err) |
| + } |
| + }) |
| + _, werr := f.WriteString("probe") |
| + if err := errors.Join(werr, f.Close()); err != nil { |
| + t.Fatal(err) |
| + } |
| + for _, d := range []struct { |
| + path string |
| + mode fs.FileMode |
| + }{ |
| + {filepath.Join("/data/instances", instancectl.ProbeDirName), 0o700}, |
| + {filepath.Join(provision.SocketDir, "instances", instancectl.ProbeDirName), 0o750 | fs.ModeSetgid}, |
| + } { |
| + if err := os.Mkdir(d.path, 0o700); err != nil { |
| + t.Fatal(err) |
| + } |
| + if err := errors.Join(os.Chown(d.path, uid, gid), os.Chmod(d.path, d.mode)); err != nil { |
| + t.Fatal(err) |
| + } |
| + } |
| + |
| + egressParentSliceProperties(t) |
| + |
| + var lines []string |
| + for _, line := range strings.Split(string(raw), "\n") { |
| + if strings.HasPrefix(line, "Requires=") || strings.HasPrefix(line, "After=") { |
| + continue |
| + } |
| + lines = append(lines, line) |
| + } |
| + path := filepath.Join("/run/systemd/system", egressProbeUnit) |
| + uf, err := os.OpenFile(path, os.O_CREATE|os.O_EXCL|os.O_WRONLY, 0o644) |
| + if err != nil { |
| + t.Fatal(err) |
| + } |
| + t.Cleanup(func() { |
| + if out, _ := egressCtl("show", "-p", "LoadState", "--value", egressProbeUnit); out != "not-found" { |
| + if out, err := egressCtl("stop", egressProbeUnit); err != nil { |
| + t.Errorf("stop %s: %v: %s", egressProbeUnit, err, out) |
| + } |
| + } |
| + egressCtl("reset-failed", egressProbeUnit) |
| + if err := os.Remove(path); err != nil { |
| + t.Error(err) |
| + } |
| + if out, err := egressCtl("daemon-reload"); err != nil { |
| + t.Errorf("daemon-reload: %v: %s", err, out) |
| + } |
| + if out, _ := egressCtl("show", "-p", "LoadState", "--value", egressProbeUnit); out != "not-found" { |
| + t.Errorf("unit %s left behind: %s", egressProbeUnit, out) |
| + } |
| + }) |
| + _, werr = uf.WriteString(strings.Join(lines, "\n")) |
| + if err := errors.Join(werr, uf.Close()); err != nil { |
| + t.Fatal(err) |
| + } |
| + egressMustCtl(t, "daemon-reload") |
| +} |
| + |
| +// egressProbeFixturesOK is the control service's FixtureOK, made here as |
| +// instancectl's probeFixturesOK makes it (unexported there): both probe |
| +// fixtures present, the secret root:carlos 0640 and the shm file a |
| +// root-owned regular file. Either one missing would make its probe line |
| +// pass vacuously. |
| +func egressProbeFixturesOK() error { |
| + shm, err := os.Lstat("/dev/shm/carlos-probe") |
| + if err != nil { |
| + return fmt.Errorf("probe shm fixture absent: %v", err) |
| + } |
| + if st, ok := shm.Sys().(*syscall.Stat_t); !ok || st.Uid != 0 || !shm.Mode().IsRegular() { |
| + return errors.New("probe shm fixture: not a root-owned regular file") |
| + } |
| + fi, err := os.Stat("/etc/carlos/probe-secret") |
| + if err != nil { |
| + return fmt.Errorf("probe fixture absent: %v", err) |
| + } |
| + st, ok := fi.Sys().(*syscall.Stat_t) |
| + if !ok { |
| + return errors.New("probe fixture: no stat") |
| + } |
| + g, err := user.LookupGroup("carlos") |
| + if err != nil { |
| + return err |
| + } |
| + gid, _ := strconv.Atoi(g.Gid) |
| + if st.Uid != 0 || int(st.Gid) != gid || fi.Mode().Perm() != 0o640 { |
| + return fmt.Errorf("probe fixture is uid %d gid %d mode %o, want root:carlos 0640", st.Uid, st.Gid, fi.Mode().Perm()) |
| + } |
| + return nil |
| +} |
| + |
| +// egressParentSliceProperties refuses a carlos-instances.slice that |
| +// already carries set-property drop-ins or an allow, and on cleanup resets |
| +// its allow and removes the runtime drop-ins the test's writes leave (the |
| +// resolver allow RunProbe writes, egressParentAllow's stand-in). |
| +func egressParentSliceProperties(t *testing.T) { |
| + t.Helper() |
| + parent := instancectl.InstancesSlice |
| + for _, root := range []string{"/run/systemd/system.control", "/etc/systemd/system.control"} { |
| + if _, err := os.Lstat(filepath.Join(root, parent+".d")); !errors.Is(err, fs.ErrNotExist) { |
| + t.Fatal("parent slice already has set-property drop-ins", root, err) |
| + } |
| + } |
| + if got := egressShow(t, parent, "IPAddressAllow"); got != "" { |
| + t.Fatal("parent slice already carries an allow", got) |
| + } |
| + t.Cleanup(func() { |
| + if out, err := egressCtl("set-property", "--runtime", parent, "IPAddressAllow="); err != nil { |
| + t.Errorf("reset %s: %v: %s", parent, err, out) |
| + } |
| + if err := os.RemoveAll(filepath.Join("/run/systemd/system.control", parent+".d")); err != nil { |
| + t.Error(err) |
| + } |
| + if out, err := egressCtl("daemon-reload"); err != nil { |
| + t.Errorf("daemon-reload: %v: %s", err, out) |
| + } |
| + }) |
| +} |