carlos / platform Public

Clone
git clone https://amadan.net/carlos/platform

Plain git — no account needed to clone.

Download

Download this file

1package host
2
3import (
4 "context"
5 "errors"
6 "os"
7
8 "github.com/carlosframework/platform/internal/moves"
9 "github.com/carlosframework/platform/internal/release"
10 "github.com/carlosframework/platform/internal/seeds"
11)
12
13type rootSeedsPublication struct {
14 Schema int
15 Deployment, Purpose string
16 Floor moves.Floor
17}
18
19func verifyRootSeedsPublication(raw []byte, id rootFloorIdentity) (*moves.Floor, error) {
20 pub, err := release.ParsePublicKey(os.Getenv("CARLOS_RELEASE_PUBKEY"), "CARLOS_RELEASE_PUBKEY")
21 if err != nil {
22 return nil, err
23 }
24 var p rootSeedsPublication
25 if release.VerifyRootEnvelope(pub, raw, &p) != nil || p.Schema != 1 || p.Deployment != id.Deployment || p.Purpose != seeds.FloorKey || !validRootFloor(&p.Floor) {
26 return nil, errRootFloorHistory
27 }
28 return &p.Floor, nil
29}
30func readRootSeedsBound(opt string, id rootFloorIdentity) (*moves.Floor, error) {
31 f, err := openRootProtocolFloors(rootSeedsFloorDir(opt), id, false)
32 if errors.Is(err, os.ErrNotExist) {
33 return nil, nil
34 }
35 if err != nil {
36 return nil, err
37 }
38 defer f.close()
39 raw, err := f.public.Read("publication.json")
40 if errors.Is(err, os.ErrNotExist) {
41 return nil, nil
42 }
43 if err != nil {
44 return nil, err
45 }
46 published, err := verifyRootSeedsPublication(raw, id)
47 if err != nil {
48 return nil, err
49 }
50 bound, err := f.localBound()
51 if err != nil {
52 return nil, err
53 }
54 capped, err := f.capLegacy([]*moves.Floor{bound})
55 if len(capped) > 0 {
56 published = floorMaximum(published, capped[0])
57 }
58 return published, err
59}
60func acceptRootSeedsPublication(ctx context.Context, f rootMovesFloors, raw []byte) error {
61 bound, err := verifyRootSeedsPublication(raw, f.identity)
62 if err != nil {
63 return err
64 }
65 unlock, err := f.private.Lock(ctx, "floor.lock")
66 if err != nil {
67 return err
68 }
69 defer unlock()
70 previous, e := f.public.Read("publication.json")
71 if e == nil {
72 old, e := verifyRootSeedsPublication(previous, f.identity)
73 if e != nil {
74 return e
75 }
76 if floorRegression(old, bound) != nil {
77 return errRootFloorHistory
78 }
79 } else if !errors.Is(e, os.ErrNotExist) {
80 return e
81 }
82 return f.public.Write("publication.json", raw)
83}
84