package hostfloor import ( "context" "crypto/ed25519" "encoding/json" "errors" "github.com/carlosframework/platform/internal/release" "github.com/carlosframework/platform/internal/rootparticipation" "io" "os" "path/filepath" ) type Disk struct { Participation *rootparticipation.Ledger Dir, Bucket string PublicKey ed25519.PublicKey } func (d Disk) Load() (Policy, Verified, error) { raw, err := os.ReadFile(filepath.Join(d.Dir, "policy.json")) if err != nil { return Policy{}, Verified{}, err } var p Policy if json.Unmarshal(raw, &p) != nil { return Policy{}, Verified{}, ErrFloor } f, err := p.Verify(d.PublicKey, d.Bucket) return p, f, err } func Durable(path string, raw []byte, mode os.FileMode) error { dir := filepath.Dir(path) f, err := os.CreateTemp(dir, ".guard-*") if err != nil { return err } name := f.Name() defer os.Remove(name) if err = f.Chmod(mode); err == nil { _, err = f.Write(raw) } if err == nil { err = f.Sync() } err = errors.Join(err, f.Close()) if err != nil { return err } if err = os.Rename(name, path); err != nil { return err } fd, err := os.Open(dir) if err != nil { return err } defer fd.Close() return fd.Sync() } // Persist never creates unpublished state implicitly. Bootstrap creates that // explicit root-owned file before installing the guarded unit drop-ins. func (d Disk) Persist(next Policy, identities map[string][]byte) error { _, nf, err := func() (Policy, Verified, error) { f, e := next.Verify(d.PublicKey, d.Bucket); return next, f, e }() if err != nil { return err } old, of, err := d.Load() if err != nil { return err } if old.Mode != "unpublished" { if next.Mode == "unpublished" || nf.Digest != of.Digest { return ErrFloor } if old.Mode == "enforced" && next.Mode != "enforced" { return ErrFloor } } for tag, raw := range identities { p, err := release.VerifySystemIdentity(d.PublicKey, raw) if err != nil || p.Current.ReleaseTag != tag { return ErrFloor } if err = Durable(filepath.Join(d.Dir, "identities", tag+".json"), raw, 0644); err != nil { return err } } raw, err := json.Marshal(next) if err != nil { return err } return Durable(filepath.Join(d.Dir, "policy.json"), raw, 0644) } func (d Disk) VerifyFile(f *os.File, platform string, roles ...string) (string, error) { component := "router" if len(roles) == 1 { component = roles[0] } else if len(roles) > 1 { return "", ErrFloor } p, floor, err := d.Load() if err != nil { return "", err } if p.Mode == "unpublished" { return "", nil } if _, err = f.Seek(0, io.SeekStart); err != nil { return "", err } hash, err := DigestReader(io.LimitReader(f, 512<<20)) if err != nil { return "", err } entries, err := os.ReadDir(filepath.Join(d.Dir, "identities")) if err != nil { return "", err } for _, entry := range entries { if entry.IsDir() { continue } wire, err := os.ReadFile(filepath.Join(d.Dir, "identities", entry.Name())) if err != nil { return "", err } identity, err := release.VerifySystemIdentity(d.PublicKey, wire) if err != nil { return "", err } if CheckComponent(d.PublicKey, floor, wire, identity.Current.Version, platform, hash, component) == nil { return hash, nil } } return "", ErrFloor } func (d Disk) Begin(ctx context.Context, id, dataDir string) error { if id == "" { return ErrFloor } marker := filepath.Join(d.Dir, "update-begun.json") if _, err := os.Stat(marker); err == nil { return ErrFloor } else if !errors.Is(err, os.ErrNotExist) { return err } raw, _ := json.Marshal(struct { ID string `json:"id"` }{id}) return d.WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { if err := checkLegacyParticipation(dataDir, false); err != nil { return err } return l.BeginUpdateWith(ctx, id, func() error { return Durable(marker, raw, 0644) }) }, func() error { if err := Durable(marker, raw, 0644); err != nil { return err } if err := checkLegacyParticipation(dataDir, true); err != nil { return errors.Join(err, RemoveDurable(marker)) } return nil }) } // CheckControllersIdle refuses while any controller state is present. The // directory must also be readable: a missing or unreadable directory cannot // prove that no controller is active. func CheckControllersIdle(ctx context.Context, dataDir string) error { return (Disk{}).ControllersIdle(ctx, dataDir) } func RemoveDurable(path string) error { if err := os.Remove(path); err != nil && !errors.Is(err, os.ErrNotExist) { return err } dir, err := os.Open(filepath.Dir(path)) if err != nil { return err } defer dir.Close() return dir.Sync() } func UpdateIdle(guardDir, journal string) error { for _, p := range []string{filepath.Join(guardDir, "update-begun.json"), journal} { if _, err := os.Lstat(p); err == nil { return ErrFloor } else if !errors.Is(err, os.ErrNotExist) { return err } } return nil } func (d Disk) WithParticipation(ctx context.Context, fn func(context.Context, rootparticipation.Ledger) error, legacy func() error) error { return d.withParticipation(ctx, fn, legacy, true) } func (d Disk) withParticipation(ctx context.Context, fn func(context.Context, rootparticipation.Ledger) error, legacy func() error, exclude bool) error { l := d.Participation if held, ok, err := rootparticipation.TransactionLedger(ctx); ok || err != nil { if err != nil { return err } if l != nil && l.State != held.State { return rootparticipation.ErrUnavailable } l = &held } var required bool var err error if l != nil { required, err = l.UpdateRequired() } else { var opened rootparticipation.Ledger opened, required, err = rootparticipation.OpenForUpdate() l = &opened if l.State != nil { defer l.State.Close() } } if err != nil { return err } if !required { return legacy() } if !exclude { return fn(ctx, *l) } return l.WithLaunchExclusion(ctx, func(ctx context.Context) error { return fn(ctx, *l) }) } func (d Disk) ParticipationRequired(ctx context.Context) (bool, error) { required := false err := d.withParticipation(ctx, func(context.Context, rootparticipation.Ledger) error { required = true; return nil }, func() error { return nil }, false) return required, err } func (d Disk) ControllersIdle(ctx context.Context, dataDir ...string) error { return d.WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { if err := l.Idle(ctx); err != nil { return err } if len(dataDir) == 1 { return checkLegacyParticipation(dataDir[0], false) } return nil }, func() error { if len(dataDir) != 1 { return ErrFloor } return checkLegacyParticipation(dataDir[0], true) }) } func (d Disk) BeginParticipation(ctx context.Context, id, dataDir string, commit, legacy func() error) error { return d.WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { if err := checkLegacyParticipation(dataDir, false); err != nil { return err } return l.BeginUpdateWith(ctx, id, commit) }, legacy) } func (d Disk) EndParticipation(ctx context.Context, id string, commit func() error) error { return d.WithParticipation(ctx, func(ctx context.Context, l rootparticipation.Ledger) error { if _, err := l.UpdateID(); errors.Is(err, os.ErrNotExist) { if err := l.BeginUpdate(ctx, id); err != nil { return err } } else if err != nil { return err } return l.EndUpdateWith(ctx, id, commit) }, func() error { if commit != nil { return commit() } return nil }) }