package host import ( "bytes" "context" "crypto/sha256" "encoding/hex" "encoding/json" "github.com/carlosframework/platform/internal/hostactors" "github.com/carlosframework/platform/internal/hostbox" "github.com/carlosframework/platform/internal/hostclaim" "github.com/carlosframework/platform/internal/hostevidence" "github.com/carlosframework/platform/internal/release" ) // RelocationReleaseReceipts discharges only the stop obligation. The region // worker still owns the journal and executor transfer after this CAS. type RelocationReleaseReceipts struct { Store release.Store Directory hostactors.Directory } func (r RelocationReleaseReceipts) Complete(ctx context.Context, observed hostclaim.Observed, key, digest string) (hostclaim.Observed, error) { if ctx == nil || r.Store == nil || observed.Absent || observed.Claim == nil || observed.Claim.Holder == nil || observed.ETag == "" { return hostclaim.Observed{}, ErrClaimStopIdentity } c := observed.Claim if c.Op.Kind != hostclaim.KindRelocating || c.Op.Phase != "releasing" || c.Op.ID == "" || c.Op.Exec != c.Op.Intent["source_box"] || c.Op.Exec == "" { return hostclaim.Observed{}, ErrClaimStopIdentity } raw, _, err := r.Store.Get(ctx, key) if err != nil { return hostclaim.Observed{}, err } sum := sha256.Sum256(raw) if hex.EncodeToString(sum[:]) != digest { return hostclaim.Observed{}, ErrClaimStopReceipt } var receipt StopReceipt if json.Unmarshal(raw, &receipt) != nil { return hostclaim.Observed{}, ErrClaimStopReceipt } canonical, err := json.Marshal(receipt) if err != nil || !bytes.Equal(raw, canonical) { return hostclaim.Observed{}, ErrClaimStopReceipt } h := c.Holder if receipt.V != 1 || key != stopEvidenceKey(c.Host, c.Op.ID, c.Op.Exec, receipt.ID) || receipt.Host != c.Host || receipt.Incarnation != h.Incarnation || receipt.InstallationStamp != h.Stamp || receipt.WriterSession != h.Session || receipt.OperationID != c.Op.ID || receipt.Phase != c.Op.Phase || receipt.ObligationID != "relocation-release" || receipt.Box != c.Op.Exec || receipt.Region != c.Op.Intent["region"] || receipt.Enrollment != c.Op.Intent["source_enrollment"] || receipt.EvidenceAt.IsZero() || receipt.RequestLimitNS != int64(hostbox.RequestLimit) || receipt.ClaimGen == 0 || len(receipt.CgroupIdentities) == 0 || !stopDigest(receipt.InventorySHA256) || !stopDigest(receipt.LocalJournalSHA256) { return hostclaim.Observed{}, ErrClaimStopReceipt } if h.Session == 0 { replayed := c.Op.Journal["release_stop_receipt"] == key && c.Op.Journal["release_stop_sha256"] == digest if receipt.Kind != StopReceiptNoProcess || receipt.FenceID != "" || receipt.ProducerDrainDigest != "" || receipt.ClaimETag == "" || (!replayed && receipt.ClaimETag != observed.ETag) || h.WriterBox != "" || h.Generation != "" || len(h.Admissions) != 0 { return hostclaim.Observed{}, ErrClaimStopReceipt } } else if receipt.Kind != "stopped" || receipt.FenceID == "" || !stopDigest(receipt.ProducerDrainDigest) { return hostclaim.Observed{}, ErrClaimStopReceipt } roster, err := r.Directory.Read(ctx) if err != nil { return hostclaim.Observed{}, err } box, ok := roster.Roster.Boxes[receipt.Box] if !ok || box.Enrollment != receipt.Enrollment || box.Region != receipt.Region { return hostclaim.Observed{}, ErrClaimStopIdentity } signature, _, err := r.Store.Get(ctx, key+".sig") if err != nil { return hostclaim.Observed{}, err } if err = hostevidence.Verify(box.CensusPublicKey, key, raw, signature); err != nil { return hostclaim.Observed{}, err } if c.Op.Journal["release_stop_receipt"] == key && c.Op.Journal["release_stop_sha256"] == digest { for _, ob := range c.Obligations { if ob.ID == receipt.ObligationID && ob.Op == receipt.OperationID { return hostclaim.Observed{}, ErrClaimStopReceipt } } return observed, nil } if c.Op.Journal["release_stop_receipt"] != "" || c.Op.Journal["release_stop_sha256"] != "" { return hostclaim.Observed{}, ErrClaimStopReceipt } return hostclaim.Update(ctx, r.Store, observed, func(next *hostclaim.Claim) error { for i, ob := range next.Obligations { if ob.ID == receipt.ObligationID && ob.Op == receipt.OperationID && ob.Kind == "relocation-release" && ob.Park == nil && ob.Box == receipt.Box && ob.Region == receipt.Region && ob.Owner == "region:"+receipt.Region { if next.Op.Journal == nil { next.Op.Journal = map[string]string{} } next.Op.Journal["release_stop_receipt"] = key next.Op.Journal["release_stop_sha256"] = digest next.Obligations = append(next.Obligations[:i], next.Obligations[i+1:]...) return nil } } return hostclaim.ErrWrongState }) }