| 1 | package hostactors |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "github.com/carlosframework/platform/internal/hostclaim" |
| 6 | "github.com/carlosframework/platform/internal/hostfloor" |
| 7 | "github.com/carlosframework/platform/internal/release" |
| 8 | "slices" |
| 9 | "strings" |
| 10 | "time" |
| 11 | ) |
| 12 | |
| 13 | func (d Directory) ReleaseActivation(ctx context.Context, sg release.Signer, id string, now time.Time) error { |
| 14 | o, err := d.Read(ctx) |
| 15 | if err != nil { |
| 16 | return err |
| 17 | } |
| 18 | h, ok := o.Roster.Activations[id] |
| 19 | if !ok { |
| 20 | return nil |
| 21 | } |
| 22 | floor, err := hostfloor.Read(ctx, d.Store, d.PublicKey, d.Bucket) |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | evidence := ExactEvidence{PublicKey: d.PublicKey, Store: d.Store, Boxes: d.PrivateBoxReport} |
| 27 | for actor, enrollment := range h.Actors { |
| 28 | ref, ok := strings.CutPrefix(actor, "box:") |
| 29 | if !ok { |
| 30 | return ErrRoster |
| 31 | } |
| 32 | box, ok := o.Roster.Boxes[ref] |
| 33 | if !ok || box.Enrollment != enrollment { |
| 34 | return ErrRoster |
| 35 | } |
| 36 | excluded, err := d.activationExcluded(ctx, o, ref, box, now) |
| 37 | if err != nil { |
| 38 | return err |
| 39 | } |
| 40 | if excluded { |
| 41 | continue |
| 42 | } |
| 43 | r, err := d.PrivateBoxReport(ctx, ref, box) |
| 44 | if err != nil { |
| 45 | return err |
| 46 | } |
| 47 | if err = VerifyReport(ctx, evidence, d.PublicKey, floor, r, enrollment, box.Cloud.InstanceID, now); err != nil { |
| 48 | return err |
| 49 | } |
| 50 | if r.ActivationID != id || !r.ActivationComplete || !slices.Contains(h.Hashes[actor], r.BinarySHA256) { |
| 51 | return ErrRoster |
| 52 | } |
| 53 | } |
| 54 | next := Clone(o.Roster) |
| 55 | delete(next.Activations, id) |
| 56 | _, err = d.Replace(ctx, sg, o, next) |
| 57 | return err |
| 58 | } |
| 59 | |
| 60 | // activationExcluded applies D7's only exclusions to a reserved actor, the |
| 61 | // same ones the census applies: a valid enrollment-qualified termination |
| 62 | // fence, or durable quarantine or retirement authority whose root-signed |
| 63 | // loss records bind this exact enrollment at its current revision. A |
| 64 | // silent active actor is never excluded, and any read or validation |
| 65 | // failure refuses, leaving the reservation conservatively held. |
| 66 | func (d Directory) activationExcluded(ctx context.Context, o Observed, ref string, b Box, now time.Time) (bool, error) { |
| 67 | fenced, err := d.TerminationFence(ctx, o, ref, now) |
| 68 | if err != nil { |
| 69 | return false, err |
| 70 | } |
| 71 | if fenced { |
| 72 | return true, nil |
| 73 | } |
| 74 | if b.State == "active" { |
| 75 | return false, nil |
| 76 | } |
| 77 | if len(b.LossIDs) == 0 || b.QuarantineRevision == 0 { |
| 78 | return false, ErrRoster |
| 79 | } |
| 80 | found := false |
| 81 | for _, id := range b.LossIDs { |
| 82 | loss, err := hostclaim.ReadLossRecord(ctx, d.Store, d.PublicKey, id) |
| 83 | if err != nil { |
| 84 | return false, err |
| 85 | } |
| 86 | if loss.Record.Box != ref || loss.Record.Enrollment != b.Enrollment { |
| 87 | return false, ErrRoster |
| 88 | } |
| 89 | if loss.Record.QuarantineRevision == b.QuarantineRevision { |
| 90 | found = true |
| 91 | } |
| 92 | } |
| 93 | if !found { |
| 94 | return false, ErrRoster |
| 95 | } |
| 96 | return true, nil |
| 97 | } |
| 98 | |