| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "testing" |
| 6 | |
| 7 | "github.com/carlosframework/platform/internal/hostguard" |
| 8 | "github.com/carlosframework/platform/internal/instancectl" |
| 9 | ) |
| 10 | |
| 11 | type plan14NamedLaunchUnits struct { |
| 12 | *launchUnits |
| 13 | host, unit string |
| 14 | } |
| 15 | |
| 16 | func (u plan14NamedLaunchUnits) Placement(host string) (string, string, error) { |
| 17 | if host != u.host { |
| 18 | return "", "", instancectl.ErrClaimUnit |
| 19 | } |
| 20 | return u.unit, instancectl.BoundClaimUnitControlGroup(host, u.unit), nil |
| 21 | } |
| 22 | |
| 23 | func TestPlan14NamedLaunchBindingGate(t *testing.T) { |
| 24 | for _, attack := range []string{"none", "missing", "canonical before", "canonical at exec", "owner", "retiring", "wrong group"} { |
| 25 | t.Run(attack, func(t *testing.T) { |
| 26 | f, _, _ := generationLaunchFixture(t) |
| 27 | unit := "plan14-bound.service" |
| 28 | f.units.started.Unit = unit |
| 29 | f.units.started.ControlGroup = instancectl.BoundClaimUnitControlGroup(f.target.Host, unit) |
| 30 | f.req.Reservation.Group.RelativePath = f.units.started.ControlGroup |
| 31 | f.root.Units = plan14NamedLaunchUnits{f.units, f.target.Host, unit} |
| 32 | b, _, _ := plan14UnitBindingFixture(t, unit, "", "") |
| 33 | in, err := f.root.Guard.Root.Installation(t.Context(), f.target.Host, hostguard.Captured{Incarnation: f.target.Incarnation, Stamp: f.target.Stamp}) |
| 34 | if err != nil { |
| 35 | t.Fatal(err) |
| 36 | } |
| 37 | shape := claimUnitShape{Host: in.Host, Account: in.Account, App: in.App, Kind: "instance", Unit: unit, Addr: "127.0.0.1:8090"} |
| 38 | b.installation = func(context.Context, string) (hostguard.RootInstallation, error) { return in, nil } |
| 39 | b.canonical = func(context.Context, string) (claimUnitShape, error) { return shape, nil } |
| 40 | saved, err := b.establish(t.Context(), in.Host) |
| 41 | if err != nil { |
| 42 | t.Fatal(err) |
| 43 | } |
| 44 | f.root.unitBindings = b |
| 45 | switch attack { |
| 46 | case "missing": |
| 47 | f.root.unitBindings = nil |
| 48 | case "canonical before": |
| 49 | shape.Unit = "replacement.service" |
| 50 | case "canonical at exec": |
| 51 | f.units.beforeCheck = func() { shape.Unit = "replacement.service" } |
| 52 | case "owner": |
| 53 | in.Stamp++ |
| 54 | case "retiring": |
| 55 | saved.Retiring = true |
| 56 | if err := b.write(saved); err != nil { |
| 57 | t.Fatal(err) |
| 58 | } |
| 59 | case "wrong group": |
| 60 | f.req.Reservation.Group.RelativePath = instancectl.ClaimUnitControlGroup(f.target.Host) |
| 61 | } |
| 62 | _, err = f.root.Serve(t.Context(), f.peer, f.req) |
| 63 | started, _ := f.units.counts() |
| 64 | if attack == "none" { |
| 65 | if err != nil || started != 1 { |
| 66 | t.Fatal("bound launch", err, started) |
| 67 | } |
| 68 | } else if err == nil || started != 0 { |
| 69 | t.Fatal("authority attack executed", err, started) |
| 70 | } |
| 71 | }) |
| 72 | } |
| 73 | } |
| 74 | |