| 1 | package console |
| 2 | |
| 3 | import ( |
| 4 | "context" |
| 5 | "crypto/rand" |
| 6 | "encoding/hex" |
| 7 | "errors" |
| 8 | "log" |
| 9 | "time" |
| 10 | |
| 11 | "github.com/carlosframework/platform/internal/hostactors" |
| 12 | "github.com/carlosframework/platform/internal/hostops" |
| 13 | ) |
| 14 | |
| 15 | func claimOperationID() string { |
| 16 | var b [16]byte |
| 17 | if _, err := rand.Read(b[:]); err != nil { |
| 18 | return "" |
| 19 | } |
| 20 | return hex.EncodeToString(b[:]) |
| 21 | } |
| 22 | |
| 23 | // Called after NewServer, before background workers. It starts no goroutine. |
| 24 | func (s *Server) ConfigureClaimBackfill() error { |
| 25 | if !s.cfg.HostClaim { |
| 26 | return nil |
| 27 | } |
| 28 | if s.cfg.Releases == nil || s.cfg.Bucket == "" || len(s.cfg.ReleasePub) == 0 { |
| 29 | s.cfg.HostClaimOps = nil |
| 30 | log.Printf("console: claim recovery: %v", hostops.ErrContract) |
| 31 | return nil |
| 32 | } |
| 33 | if s.cfg.HostClaimOps == nil { |
| 34 | d := hostactors.Directory{Store: s.cfg.Releases, PublicKey: s.cfg.ReleasePub, Bucket: s.cfg.Bucket} |
| 35 | c := hostops.NewBackfillController(s.cfg.Releases, d, time.Now, claimOperationID) |
| 36 | s.cfg.HostClaimOps = &c |
| 37 | } |
| 38 | return nil |
| 39 | } |
| 40 | func (s *Server) ReconcileHostClaims(ctx context.Context) error { |
| 41 | if !s.cfg.HostClaim { |
| 42 | return nil |
| 43 | } |
| 44 | if s.cfg.HostClaimOps == nil { |
| 45 | return hostops.ErrContract |
| 46 | } |
| 47 | return hostops.ReconcileClaims(ctx, *s.cfg.HostClaimOps, s.adoptHostRecord) |
| 48 | } |
| 49 | |
| 50 | // Exported for the moves console pass; construction and startup remain separate. |
| 51 | func (s *Server) RunClaimRecoveryPass(ctx context.Context) error { |
| 52 | if !s.cfg.HostClaim { |
| 53 | return nil |
| 54 | } |
| 55 | if a := s.cfg.ClaimAuthority; a != nil && a.Control != nil { |
| 56 | return a.Control.RunRecovery(ctx, s.runClaimRecoveryPassBody) |
| 57 | } |
| 58 | return s.runClaimRecoveryPassBody(ctx) |
| 59 | } |
| 60 | |
| 61 | func (s *Server) runClaimRecoveryPassBody(ctx context.Context) error { |
| 62 | var errs []error |
| 63 | if err := s.ReconcileHostClaims(ctx); err != nil { |
| 64 | errs = append(errs, err) |
| 65 | } |
| 66 | if s.cfg.HostClaimRecovery != nil { |
| 67 | if err := s.cfg.HostClaimRecovery.Pass(ctx); err != nil { |
| 68 | errs = append(errs, err) |
| 69 | } |
| 70 | } |
| 71 | if s.cfg.ClaimAuthority != nil { |
| 72 | errs = append(errs, s.SweepClaimHolds(ctx)) |
| 73 | } |
| 74 | return errors.Join(errs...) |
| 75 | } |
| 76 | |