| 1 | package hostactors |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "encoding/json" |
| 6 | "github.com/carlosframework/platform/internal/hostfloor" |
| 7 | "github.com/carlosframework/platform/internal/release" |
| 8 | "time" |
| 9 | ) |
| 10 | |
| 11 | // PublishFloor is called by the approved release workflow after the initial |
| 12 | // deployment inventory has been reconciled and every guarded launch path |
| 13 | // installed. It never discovers membership from reports. |
| 14 | func (d Directory) PublishFloor(ctx context.Context, sg release.Signer, floor hostfloor.Floor, e Evidence, now time.Time) error { |
| 15 | o, err := d.Read(ctx) |
| 16 | if err != nil { |
| 17 | return err |
| 18 | } |
| 19 | if len(o.Roster.Boxes) == 0 || len(o.Roster.Consoles) == 0 || len(o.Roster.Holds) != 0 || len(o.Roster.Activations) != 0 { |
| 20 | return ErrRoster |
| 21 | } |
| 22 | raw, err := json.Marshal(floor) |
| 23 | if err != nil { |
| 24 | return err |
| 25 | } |
| 26 | prospective := hostfloor.Verified{Floor: floor, Digest: hostfloor.Digest(raw)} |
| 27 | if o.Roster.Publication == "" { |
| 28 | next := Clone(o.Roster) |
| 29 | next.Publication = "preparing" |
| 30 | next.FloorSHA256 = prospective.Digest |
| 31 | o, err = d.Replace(ctx, sg, o, next) |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | } else if o.Roster.FloorSHA256 != prospective.Digest { |
| 36 | return ErrRoster |
| 37 | } |
| 38 | for ref, b := range o.Roster.Boxes { |
| 39 | // Initial publication requires every enrollment. A retained quarantine |
| 40 | // is resolved by the activation runbook rather than skipped here. |
| 41 | if b.State != "active" { |
| 42 | return ErrRoster |
| 43 | } |
| 44 | r, err := e.BoxReport(ctx, ref, b) |
| 45 | if err != nil { |
| 46 | return err |
| 47 | } |
| 48 | if err = VerifyReport(ctx, e, d.PublicKey, prospective, r, b.Enrollment, b.Cloud.InstanceID, now); err != nil { |
| 49 | return err |
| 50 | } |
| 51 | } |
| 52 | for id, c := range o.Roster.Consoles { |
| 53 | r, err := e.ConsoleReport(ctx, id, c) |
| 54 | if err != nil { |
| 55 | return err |
| 56 | } |
| 57 | if _, err := verifyCompute(ctx, o, e, id, c, r); err != nil { |
| 58 | return err |
| 59 | } |
| 60 | if err = VerifyReport(ctx, e, d.PublicKey, prospective, r, c.Enrollment, id, now); err != nil { |
| 61 | return err |
| 62 | } |
| 63 | } |
| 64 | // An inventory move during the scan is a failed publication attempt. |
| 65 | fresh, err := d.Read(ctx) |
| 66 | if err != nil { |
| 67 | return err |
| 68 | } |
| 69 | if fresh.ETag != o.ETag { |
| 70 | return ErrRoster |
| 71 | } |
| 72 | if err = hostfloor.Publish(ctx, d.Store, sg, d.Bucket, floor); err != nil { |
| 73 | return err |
| 74 | } |
| 75 | next := Clone(fresh.Roster) |
| 76 | next.Publication = "published" |
| 77 | _, err = d.Replace(ctx, sg, fresh, next) |
| 78 | return err |
| 79 | } |
| 80 | |