| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "crypto/sha256" |
| 5 | "encoding/hex" |
| 6 | "errors" |
| 7 | "strings" |
| 8 | "testing" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/hostclaim" |
| 11 | ) |
| 12 | |
| 13 | func TestPlan15OriginCandidateBoundary(t *testing.T) { |
| 14 | w, raw, _ := testWriter(t) |
| 15 | body := []byte(`{"v":1,"host":"x.example","count":1,"origin":{"incarnation":"` + strings.Repeat("a", 64) + `","location":"instances/x.example","region":"ie","generation":"g1"}}`) |
| 16 | sum := sha256.Sum256(append([]byte("carlos-admitted-origin-v1\x00"), body...)) |
| 17 | key := "control/host-origins/x.example/" + hex.EncodeToString(sum[:]) + ".json" |
| 18 | for _, tc := range []struct { |
| 19 | name string |
| 20 | target MutationTarget |
| 21 | }{ |
| 22 | {"candidate", MutationTarget{Method: "Put", Key: key, After: body}}, |
| 23 | {"overwrite", MutationTarget{Method: "Put", Key: key, After: body, ETag: "v"}}, |
| 24 | {"delete", MutationTarget{Method: "Delete", Key: key}}, |
| 25 | {"wrong host", MutationTarget{Method: "Put", Key: strings.Replace(key, "x.example", "other.example", 1), After: body}}, |
| 26 | {"wrong digest", MutationTarget{Method: "Put", Key: key, After: append(append([]byte(nil), body...), ' ')}}, |
| 27 | {"unknown field", MutationTarget{Method: "Put", Key: key, After: []byte(`{"bogus":true}`)}}, |
| 28 | {"oversize", MutationTarget{Method: "Put", Key: key, After: make([]byte, 4097)}}, |
| 29 | } { |
| 30 | t.Run(tc.name, func(t *testing.T) { |
| 31 | c, err := ClassifyMutation(t.Context(), raw, w, tc.target) |
| 32 | if !errors.Is(err, ErrUnclassified) { |
| 33 | t.Fatalf("classification %+v %v", c, err) |
| 34 | } |
| 35 | }) |
| 36 | } |
| 37 | } |
| 38 | |
| 39 | func TestPlan15LifecycleGrantCannotRewriteOrigins(t *testing.T) { |
| 40 | w, o, clock := wakeResetFixture(t) |
| 41 | ctx := registerFor(t, w, o, "create2", "create", o.Claim.Holder.Incarnation, clock) |
| 42 | step := claimStep(t, o, func(c *hostclaim.Claim) error { |
| 43 | c.Origins = &hostclaim.OriginHead{Digest: strings.Repeat("a", 64), Count: 1} |
| 44 | return nil |
| 45 | }) |
| 46 | if err := authorizeClaimWrite(ctx, w.raw, o.Host, step); err == nil { |
| 47 | t.Fatal("general app grant manufactured origin authority") |
| 48 | } |
| 49 | if err := authorizeClaimWrite(ctx, w.raw, o.Host, claimStep(t, o, func(*hostclaim.Claim) error { return nil })); err != nil { |
| 50 | t.Fatalf("unchanged head refused: %v", err) |
| 51 | } |
| 52 | o.Claim.Origins = &hostclaim.OriginHead{Digest: strings.Repeat("b", 64), Count: 2} |
| 53 | for _, head := range []*hostclaim.OriginHead{nil, {Digest: strings.Repeat("a", 64), Count: 1}} { |
| 54 | step = claimStep(t, o, func(c *hostclaim.Claim) error { c.Origins = head; return nil }) |
| 55 | if err := authorizeClaimWrite(ctx, w.raw, o.Host, step); err == nil { |
| 56 | t.Fatal("grant erased or rolled back origin history") |
| 57 | } |
| 58 | } |
| 59 | } |
| 60 | |