| 1 | package hostbox |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "errors" |
| 6 | "testing" |
| 7 | |
| 8 | "github.com/carlosframework/platform/internal/hostclaim" |
| 9 | "github.com/carlosframework/platform/internal/hostwrite" |
| 10 | ) |
| 11 | |
| 12 | func TestPlan11PolicyRefusalsCannotMutateLaterChecks(t *testing.T) { |
| 13 | c := plan11Relocating("probing") |
| 14 | c.Op.Journal = map[string]string{"owner": "original"} |
| 15 | refused := errors.New("later refusal") |
| 16 | policy := Policy{Held: func(context.Context, string) (string, error) { return "", nil }, Relocation: func(context.Context, *hostclaim.Claim, Selection, hostwrite.Identity) error { return nil }} |
| 17 | policy = policy.WithRefusals( |
| 18 | func(_ context.Context, copy *hostclaim.Claim, _ Selection, _ hostwrite.Identity) error { |
| 19 | copy.Op.Exec = "intruder" |
| 20 | copy.Holder.Session = 99 |
| 21 | copy.Op.Intent["target_box"] = "intruder" |
| 22 | copy.Op.Journal["owner"] = "changed" |
| 23 | return nil |
| 24 | }, |
| 25 | func(_ context.Context, copy *hostclaim.Claim, _ Selection, _ hostwrite.Identity) error { |
| 26 | if copy.Op.Exec == "target" && copy.Holder.Session == 7 && copy.Op.Intent["target_box"] == "target" && copy.Op.Journal["owner"] == "original" { |
| 27 | return refused |
| 28 | } |
| 29 | return nil |
| 30 | }, |
| 31 | ) |
| 32 | s := Selection{Host: "x.example", Box: "target", Op: "r1", Action: "probe", Phase: "probing", Session: 7} |
| 33 | if err := policy.Check(context.Background(), c, s, hostwrite.Identity{}); !errors.Is(err, refused) { |
| 34 | t.Fatalf("earlier callback removed later refusal: %v", err) |
| 35 | } |
| 36 | if c.Op.Exec != "target" || c.Holder.Session != 7 || c.Op.Intent["target_box"] != "target" || c.Op.Journal["owner"] != "original" { |
| 37 | t.Fatal("policy mutated authority") |
| 38 | } |
| 39 | } |
| 40 | |
| 41 | func TestPlan11RelocationPolicyCannotMutateAuthority(t *testing.T) { |
| 42 | c := plan11Relocating("probing") |
| 43 | policy := Policy{Held: func(context.Context, string) (string, error) { return "", nil }, Relocation: func(_ context.Context, copy *hostclaim.Claim, _ Selection, _ hostwrite.Identity) error { |
| 44 | copy.Op.Exec = "intruder" |
| 45 | return nil |
| 46 | }} |
| 47 | s := Selection{Host: "x.example", Box: "target", Op: "r1", Action: "probe", Phase: "probing", Session: 7} |
| 48 | if err := policy.Check(context.Background(), c, s, hostwrite.Identity{}); err != nil { |
| 49 | t.Fatal(err) |
| 50 | } |
| 51 | if c.Op.Exec != "target" { |
| 52 | t.Fatal("relocation policy mutated checked authority") |
| 53 | } |
| 54 | } |
| 55 | |