| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "crypto/ed25519" |
| 5 | "errors" |
| 6 | "time" |
| 7 | ) |
| 8 | |
| 9 | // HostRetryGrant is the console's signed `carlos instances retry <host>` |
| 10 | // (zero-downtime spec §8.4, §11; Paul's ruling on plan 6 Q4, amadan #272): |
| 11 | // a box clears a host's failure hold only on a grant the deployment root |
| 12 | // signed, naming that account, app, host and the build that restricts it. |
| 13 | // |
| 14 | // The host-retry log it rides (HostRetryKey; the restart-scope log before |
| 15 | // readiness round 2, whose legacy entries boxes still honour) is writable |
| 16 | // by every box (Astra impl-6 finding 5), so an unsigned entry is a request |
| 17 | // anyone with a box could have written, and it clears nothing. The signature closes forgery; Epoch |
| 18 | // and Book close replay (plan review finding 13; the crash-epoch hard limit, |
| 19 | // readiness round 2 Task 1): Epoch is the app's restriction generation as |
| 20 | // the box published it (rollout.FailureBook.PublishedEpoch), which the box |
| 21 | // publishes only once it is on disk and never hands out twice, so a grant |
| 22 | // clears the restriction state the member saw and nothing that changed |
| 23 | // after, even across an agent restart. |
| 24 | type HostRetryGrant struct { |
| 25 | V int `json:"v"` |
| 26 | Account string `json:"account"` |
| 27 | App string `json:"app"` |
| 28 | Host string `json:"host"` |
| 29 | // Build is the build whose restriction the grant lifts: the failure |
| 30 | // record of that build for Host, and Host's hold-ran marker when it |
| 31 | // names that build (plan review finding 5). It is not the build the |
| 32 | // host should move to. |
| 33 | Build string `json:"build"` |
| 34 | // Epoch is the app's restriction generation as published, which the |
| 35 | // console read from the box's report (status.HostRollout.HeldEpoch): |
| 36 | // every change to any restriction of the app moves it, so the grant |
| 37 | // clears only while nothing of the app's restrictions changed since. |
| 38 | // Zero is an older box that reported none, or a box whose book owes a |
| 39 | // save: the console still signs it, and no box accepts it. |
| 40 | Epoch uint64 `json:"epoch"` |
| 41 | // Book is the identity of the failure book Epoch counts in, as the box |
| 42 | // reported it (status.HostRollout.HeldBook): epochs are a counter every |
| 43 | // book starts alike, so a grant binds the restriction only with both |
| 44 | // (final review round 1, finding 3). Empty is a console or a box older |
| 45 | // than the identity: the console still signs it, and no box accepts it. |
| 46 | Book string `json:"book"` |
| 47 | IssuedAt string `json:"issued_at"` // RFC 3339, UTC, whole seconds |
| 48 | } |
| 49 | |
| 50 | // HostRetryGrantTTL is how long a box accepts a grant after it was issued. |
| 51 | // It is a backstop only: the epoch is what binds a grant to one failure. |
| 52 | const HostRetryGrantTTL = 24 * time.Hour |
| 53 | |
| 54 | // hostRetrySkew is how far ahead of a box's clock a grant's IssuedAt may be. |
| 55 | const hostRetrySkew = time.Minute |
| 56 | |
| 57 | // ErrHostRetryGrant is a grant that verified but is not for this request. |
| 58 | var ErrHostRetryGrant = errors.New("host retry grant: not valid for this request") |
| 59 | |
| 60 | // SignHostRetry signs a grant with the deployment root. A scoped release |
| 61 | // key is refused (SignRootEnvelope): a channel delegate has no say over |
| 62 | // another instance's failure hold. |
| 63 | func SignHostRetry(sg Signer, acct, app, host, build, book string, epoch uint64, now time.Time) (string, error) { |
| 64 | g := HostRetryGrant{V: 1, Account: acct, App: app, Host: host, Build: build, Epoch: epoch, Book: book, |
| 65 | IssuedAt: now.UTC().Truncate(time.Second).Format(time.RFC3339)} |
| 66 | wire, err := SignRootEnvelope(sg, g) |
| 67 | return string(wire), err |
| 68 | } |
| 69 | |
| 70 | // VerifyHostRetry checks wire against the root key and the entry it rode in: |
| 71 | // the same account, app and host, a build and a non-zero epoch, issued no |
| 72 | // later than now plus a minute of skew and no earlier than now minus |
| 73 | // HostRetryGrantTTL. Whether the epoch is still the restriction's, and the |
| 74 | // book the box's, is the failure book's check, made where the clear is. |
| 75 | func VerifyHostRetry(pub ed25519.PublicKey, wire, acct, app, host string, now time.Time) (HostRetryGrant, error) { |
| 76 | var g HostRetryGrant |
| 77 | if err := VerifyRootEnvelope(pub, []byte(wire), &g); err != nil { |
| 78 | return HostRetryGrant{}, err |
| 79 | } |
| 80 | at, err := time.Parse(time.RFC3339, g.IssuedAt) |
| 81 | if err != nil || g.V != 1 || g.Account != acct || g.App != app || g.Host != host || g.Build == "" || g.Epoch == 0 || |
| 82 | at.After(now.Add(hostRetrySkew)) || at.Before(now.Add(-HostRetryGrantTTL)) { |
| 83 | return HostRetryGrant{}, ErrHostRetryGrant |
| 84 | } |
| 85 | return g, nil |
| 86 | } |
| 87 | |