Download this file
| 1 | package host |
| 2 | |
| 3 | import ( |
| 4 | "bytes" |
| 5 | "encoding/json" |
| 6 | "io" |
| 7 | "os" |
| 8 | |
| 9 | "github.com/carlosframework/platform/internal/rootstate" |
| 10 | ) |
| 11 | |
| 12 | func readClaimUnitShapes(path string) (map[string]claimUnitShape, error) { |
| 13 | f, err := rootstate.OpenAuthorityFile(path) |
| 14 | if err != nil { |
| 15 | return nil, err |
| 16 | } |
| 17 | defer f.Close() |
| 18 | raw, err := io.ReadAll(io.LimitReader(f, rootstate.MaxRecord+1)) |
| 19 | if err != nil { |
| 20 | return nil, err |
| 21 | } |
| 22 | return decodeClaimUnitShapes(raw) |
| 23 | } |
| 24 | |
| 25 | func decodeClaimUnitShapes(raw []byte) (map[string]claimUnitShape, error) { |
| 26 | var config struct { |
| 27 | Schema int |
| 28 | Units []claimUnitShape |
| 29 | } |
| 30 | dec := json.NewDecoder(bytes.NewReader(raw)) |
| 31 | dec.DisallowUnknownFields() |
| 32 | if len(raw) > rootstate.MaxRecord || dec.Decode(&config) != nil || dec.Decode(new(any)) != io.EOF || config.Schema != 1 || len(config.Units) > 4096 { |
| 33 | return nil, os.ErrPermission |
| 34 | } |
| 35 | hosts, units := map[string]claimUnitShape{}, map[string]bool{} |
| 36 | for _, s := range config.Units { |
| 37 | if !s.valid() || hosts[s.Host].Host != "" || units[s.Unit] { |
| 38 | return nil, os.ErrPermission |
| 39 | } |
| 40 | hosts[s.Host], units[s.Unit] = s, true |
| 41 | } |
| 42 | return hosts, nil |
| 43 | } |
| 44 | |