| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/ed25519" |
| 6 | "os" |
| 7 | "time" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/historyreadback" |
| 10 | "github.com/carlosframework/platform/internal/hostactors" |
| 11 | "github.com/carlosframework/platform/internal/hostfloor" |
| 12 | "github.com/carlosframework/platform/internal/release" |
| 13 | ) |
| 14 | |
| 15 | type historyReadbackPublisher struct { |
| 16 | directory hostactors.Directory |
| 17 | enrollment hostfloor.Enrollment |
| 18 | key func() (ed25519.PrivateKey, error) |
| 19 | now func() time.Time |
| 20 | } |
| 21 | |
| 22 | func (p historyReadbackPublisher) publish(ctx context.Context, b release.HistoryBinding, snapshot func(context.Context) (release.HistoryMigrationStatus, error)) error { |
| 23 | observed, err := p.directory.Read(ctx) |
| 24 | if err != nil { |
| 25 | return err |
| 26 | } |
| 27 | box, ok := observed.Roster.Boxes[p.enrollment.Box] |
| 28 | if !ok || box.Enrollment != p.enrollment.Enrollment || p.enrollment.InstanceID == "" || box.Cloud.InstanceID != p.enrollment.InstanceID || p.key == nil || p.now == nil { |
| 29 | return os.ErrPermission |
| 30 | } |
| 31 | key, err := p.key() |
| 32 | if err != nil { |
| 33 | return err |
| 34 | } |
| 35 | scope := historyreadback.Scope{Bucket: p.directory.Bucket, Box: p.enrollment.Box, Enrollment: p.enrollment.Enrollment, Binding: b} |
| 36 | return historyreadback.Respond(ctx, p.directory, scope, key, p.now(), snapshot) |
| 37 | } |
| 38 | |
| 39 | func publishRootHistoryReadback(ctx context.Context, store release.Store, b release.HistoryBinding, snapshot func(context.Context) (release.HistoryMigrationStatus, error)) error { |
| 40 | ctx, cancel := context.WithTimeout(ctx, 5*time.Second) |
| 41 | defer cancel() |
| 42 | rr, err := readProductionRosterRow(ctx, store, os.Getenv) |
| 43 | if err != nil { |
| 44 | return err |
| 45 | } |
| 46 | p := historyReadbackPublisher{directory: rr.directory, enrollment: rr.enrollment, key: func() (ed25519.PrivateKey, error) { return readClaimEvidenceKey("/etc/carlos/host-evidence.key") }, now: time.Now} |
| 47 | return p.publish(ctx, b, snapshot) |
| 48 | } |
| 49 | |