| 1 | package hostops |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "errors" |
| 7 | "testing" |
| 8 | "time" |
| 9 | |
| 10 | "github.com/carlosframework/platform/internal/hostclaim" |
| 11 | "github.com/carlosframework/platform/internal/release" |
| 12 | ) |
| 13 | |
| 14 | // M7: a reset that timed out is uncertain, then expires and is archived. The |
| 15 | // archive closes that request's id, but it must not pin the claim ETag for |
| 16 | // ever: a later issue gets a fresh request. Absence of a receipt never becomes |
| 17 | // proof, and an archived request with a valid root receipt is proof. |
| 18 | func TestPlan9M7TimedOutResetDoesNotPinClaimETag(t *testing.T) { |
| 19 | for _, proved := range []bool{false, true} { |
| 20 | name := "uncertain" |
| 21 | if proved { |
| 22 | name = "proved-before-archive" |
| 23 | } |
| 24 | t.Run(name, func(t *testing.T) { |
| 25 | tr, r, o, boxkey := resetTransportFixture(t) |
| 26 | now := tr.Now() |
| 27 | tr.Now = func() time.Time { return now } |
| 28 | w, err := NewControlWriter(tr.Store, tr.Directory.PublicKey, tr.Now) |
| 29 | if err != nil { |
| 30 | t.Fatal(err) |
| 31 | } |
| 32 | signer := release.RootSigner(ed25519.NewKeyFromSeed(make([]byte, 32))) |
| 33 | h := o.Claim.Holder |
| 34 | owner := r.Admission.Owner |
| 35 | // The fixture's live registration, renewed as its console would. |
| 36 | handle := &StartHandle{w: w, account: h.Account, app: h.App, appID: "app", id: "reset", owner: owner, epoch: 1, kind: RegistrationWakeRetryReset, incarnation: h.Incarnation} |
| 37 | grant := lifecycleGrant{kind: "app", account: h.Account, app: h.App, appID: "app", operation: "reset", owner: owner, epoch: 1, w: w, registration: RegistrationWakeRetryReset, incarnation: h.Incarnation} |
| 38 | ctx := withLifecycleGrant(t.Context(), grant) |
| 39 | issue := func() (WakeResetRequest, error) { |
| 40 | return w.RequestWakeReset(ctx, tr, signer, r.Console, r.ConsoleEnrollment, r.Namespace, o, r.Attempt) |
| 41 | } |
| 42 | first, err := issue() |
| 43 | if err != nil { |
| 44 | t.Fatal(err) |
| 45 | } |
| 46 | if proved { |
| 47 | publishResetProof(t, tr, first, boxkey) |
| 48 | } |
| 49 | for _, step := range []time.Duration{80 * time.Second, 80 * time.Second, 60 * time.Second, 25 * time.Second} { |
| 50 | now = now.Add(step) |
| 51 | if err := handle.Renew(t.Context(), now); err != nil { |
| 52 | t.Fatal(err) |
| 53 | } |
| 54 | } |
| 55 | if !proved { |
| 56 | if err := tr.Wait(t.Context(), first, time.Millisecond); !errors.Is(err, ErrWakeResetUncertain) { |
| 57 | t.Fatalf("expired without receipt = %v", err) |
| 58 | } |
| 59 | } |
| 60 | if err := w.SweepWakeResets(t.Context(), tr); err != nil { |
| 61 | t.Fatal(err) |
| 62 | } |
| 63 | if _, _, err := tr.Store.Get(t.Context(), wakeResetArchiveKey(first)); err != nil { |
| 64 | t.Fatalf("not archived: %v", err) |
| 65 | } |
| 66 | again, err := issue() |
| 67 | if err != nil { |
| 68 | t.Fatalf("archived request pinned the claim ETag: %v", err) |
| 69 | } |
| 70 | if proved { |
| 71 | if err := tr.Wait(t.Context(), again, time.Millisecond); err != nil { |
| 72 | t.Fatalf("archived proof lost: %v", err) |
| 73 | } |
| 74 | return |
| 75 | } |
| 76 | if again.ID == first.ID || !again.Expires.After(now) || again.ClaimETag != first.ClaimETag { |
| 77 | t.Fatalf("reissue reused the archived request: %+v", again) |
| 78 | } |
| 79 | if err := tr.Authorize(t.Context(), again); err != nil { |
| 80 | t.Fatalf("fresh request refused: %v", err) |
| 81 | } |
| 82 | if err := tr.Authorize(t.Context(), first); err == nil { |
| 83 | t.Fatal("archived request resurrected") |
| 84 | } |
| 85 | short, cancel := context.WithTimeout(t.Context(), 20*time.Millisecond) |
| 86 | defer cancel() |
| 87 | if err := tr.Wait(short, again, time.Millisecond); !errors.Is(err, ErrWakeResetUncertain) { |
| 88 | t.Fatalf("absence became proof: %v", err) |
| 89 | } |
| 90 | replay, err := issue() |
| 91 | if err != nil || replay != again { |
| 92 | t.Fatalf("replay=%+v err=%v", replay, err) |
| 93 | } |
| 94 | calls := 0 |
| 95 | stop := func(_ context.Context, _ hostclaim.Observed, a hostclaim.WakeRetryAttempt) error { |
| 96 | calls++ |
| 97 | publishResetProof(t, tr, again, boxkey) |
| 98 | return nil |
| 99 | } |
| 100 | if err := tr.Pass(t.Context(), "box", stop); err != nil || calls != 1 { |
| 101 | t.Fatalf("fresh request not dispatched: calls=%d %v", calls, err) |
| 102 | } |
| 103 | if err := tr.Wait(t.Context(), again, time.Millisecond); err != nil { |
| 104 | t.Fatal(err) |
| 105 | } |
| 106 | }) |
| 107 | } |
| 108 | } |
| 109 | |