| 1 | package main |
| 2 | |
| 3 | // The signer projects file restricts signers to projects. It is how a |
| 4 | // worker's key can be known to every integrator that shares the |
| 5 | // known-signers file while its evidence counts only in the projects it |
| 6 | // builds for. |
| 7 | // |
| 8 | // Each line names one signer, then the projects whose evidence it may |
| 9 | // sign, separated by spaces: |
| 10 | // |
| 11 | // laddie-worker/attestations the-valley cosmo |
| 12 | // |
| 13 | // A signer the file names counts only in the projects on its line. In any |
| 14 | // other project the integrator classifies its notes as it classifies a |
| 15 | // stranger's: signed by no signer it holds. A line with a name and no |
| 16 | // project lets that signer count nowhere. A signer the file does not name |
| 17 | // is not restricted. Blank lines and lines that open with # are skipped. |
| 18 | |
| 19 | import ( |
| 20 | "fmt" |
| 21 | "os" |
| 22 | "strings" |
| 23 | |
| 24 | "the-valley/note" |
| 25 | ) |
| 26 | |
| 27 | // signerProjects maps each restricted signer's name to the projects its |
| 28 | // evidence counts in. A nil map restricts nobody. |
| 29 | type signerProjects map[string]map[string]bool |
| 30 | |
| 31 | // readSignerProjects reads the file at path. An empty path is no file, and |
| 32 | // restricts nobody. A file that cannot be read, or does not read as a |
| 33 | // signer projects file, is an error, so a controller given a broken file |
| 34 | // judges nothing rather than judging without it. |
| 35 | func readSignerProjects(path string) (signerProjects, error) { |
| 36 | if path == "" { |
| 37 | return nil, nil |
| 38 | } |
| 39 | data, err := os.ReadFile(path) |
| 40 | if err != nil { |
| 41 | return nil, fmt.Errorf("reading the signer projects file: %w", err) |
| 42 | } |
| 43 | s, err := parseSignerProjects(data) |
| 44 | if err != nil { |
| 45 | return nil, fmt.Errorf("the signer projects file %s does not read: %w", path, err) |
| 46 | } |
| 47 | return s, nil |
| 48 | } |
| 49 | |
| 50 | // parseSignerProjects reads a signer projects file's contents. A line |
| 51 | // whose first word is not a name a verifier key can carry, or that names |
| 52 | // a signer an earlier line already named, fails the whole read, naming |
| 53 | // the line. Two lines for one signer would leave which of them restricts |
| 54 | // it to the reader's guess. |
| 55 | func parseSignerProjects(data []byte) (signerProjects, error) { |
| 56 | s := signerProjects{} |
| 57 | for i, line := range strings.Split(string(data), "\n") { |
| 58 | fields := strings.Fields(line) |
| 59 | if len(fields) == 0 || strings.HasPrefix(fields[0], "#") { |
| 60 | continue |
| 61 | } |
| 62 | name := fields[0] |
| 63 | if err := note.CheckName(name); err != nil { |
| 64 | return nil, fmt.Errorf("line %d: %w", i+1, err) |
| 65 | } |
| 66 | if _, dup := s[name]; dup { |
| 67 | return nil, fmt.Errorf("line %d: %s is named on an earlier line too", i+1, name) |
| 68 | } |
| 69 | s[name] = map[string]bool{} |
| 70 | for _, project := range fields[1:] { |
| 71 | s[name][project] = true |
| 72 | } |
| 73 | } |
| 74 | return s, nil |
| 75 | } |
| 76 | |
| 77 | // counts says whether evidence signed under name counts in project. |
| 78 | func (s signerProjects) counts(name, project string) bool { |
| 79 | projects, restricted := s[name] |
| 80 | return !restricted || projects[project] |
| 81 | } |