| 1 | package release |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "errors" |
| 7 | "os" |
| 8 | "strconv" |
| 9 | "time" |
| 10 | ) |
| 11 | |
| 12 | func HistoryReconstructionKey(acct, app, identity string, revision uint64) string { |
| 13 | return appPrefix(acct, app) + "release-history/" + identity + "/" + strconv.FormatUint(revision, 10) + ".json" |
| 14 | } |
| 15 | |
| 16 | func (h *RootHistory) ReceiveReconstruction(ctx context.Context, store Store, pub ed25519.PublicKey, b HistoryBinding, now time.Time, resolve func(HistoryBinding) (string, error)) error { |
| 17 | if !b.valid() || store == nil { |
| 18 | return os.ErrPermission |
| 19 | } |
| 20 | resumeErr := h.ResumeReconstruction(resolve) |
| 21 | status, err := h.ReconstructionStatus() |
| 22 | if err != nil { |
| 23 | return err |
| 24 | } |
| 25 | wire, _, err := store.Get(ctx, HistoryReconstructionKey(b.Account, b.App, status.Identity, status.Revision)) |
| 26 | if errors.Is(err, os.ErrNotExist) { |
| 27 | return resumeErr |
| 28 | } |
| 29 | if err != nil { |
| 30 | return err |
| 31 | } |
| 32 | return h.Reconstruct(pub, wire, b.Account, b.App, now, resolve) |
| 33 | } |
| 34 | |
| 35 | func HistoryRecoveryRetirementKey(b HistoryBinding, identity, outcomeDigest string) string { |
| 36 | return appPrefix(b.Account, b.App) + "release-history/" + identity + "/recovery/" + outcomeDigest + ".json" |
| 37 | } |
| 38 | |
| 39 | func (h *RootHistory) ReceiveRecoveryRetirements(ctx context.Context, store Store, pub ed25519.PublicKey, b HistoryBinding, now time.Time, resolve func(HistoryBinding) (string, error)) error { |
| 40 | if !b.valid() || store == nil { |
| 41 | return os.ErrPermission |
| 42 | } |
| 43 | status, err := h.ReconstructionStatus() |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if status.Pending { |
| 48 | return os.ErrPermission |
| 49 | } |
| 50 | outcomes, err := h.recoveryOutcomes() |
| 51 | if err != nil { |
| 52 | return err |
| 53 | } |
| 54 | count := 0 |
| 55 | for _, r := range outcomes { |
| 56 | if r.Binding != b { |
| 57 | continue |
| 58 | } |
| 59 | count++ |
| 60 | if count > historyLimit { |
| 61 | return os.ErrPermission |
| 62 | } |
| 63 | if !r.Settled || (r.Verified && !r.Failed && !r.Recovering) { |
| 64 | continue |
| 65 | } |
| 66 | retired, err := h.recoveryRetired(r, outcomes) |
| 67 | if err != nil { |
| 68 | return err |
| 69 | } |
| 70 | if retired { |
| 71 | continue |
| 72 | } |
| 73 | wire, _, err := store.Get(ctx, HistoryRecoveryRetirementKey(b, status.Identity, historyRecoveryDigest(r))) |
| 74 | if errors.Is(err, os.ErrNotExist) { |
| 75 | continue |
| 76 | } |
| 77 | if err != nil { |
| 78 | return err |
| 79 | } |
| 80 | if err := h.RetireRecovery(pub, wire, b, now, resolve); err != nil { |
| 81 | return err |
| 82 | } |
| 83 | } |
| 84 | return nil |
| 85 | } |
| 86 | |