gunk-dev/the-valley

signerprojects.go

source0cb9a251c595main's published tip history
3.1 KiB
1package note
2
3// The signer projects file restricts signers to projects. It sits beside a
4// known-signers file: the known-signers file says which keys may sign, and
5// this file says, for the signers it names, in which projects their
6// evidence counts. It is how a check worker's key can be known to every
7// controller that shares the known-signers file while its evidence counts
8// only in the projects it builds for.
9//
10// Each line names one signer, then the projects its evidence counts in,
11// separated by spaces:
12//
13// worker/attestations the-valley app
14//
15// A signer the file names counts only in the projects on its line, and a
16// line with a name and no project lets it count nowhere. A signer the file
17// does not name is not restricted. Blank lines and lines that open with #
18// are skipped.
19//
20// Every name and project is a word of printable ASCII: no space, no other
21// whitespace, no control character and nothing outside ASCII. The host
22// module holds what it renders to the same rule, so the file reads back as
23// it was declared. A name is also one a verifier key can carry, so it
24// holds no plus.
25
26import (
27 "fmt"
28 "strings"
29)
30
31// SignerProjects maps each restricted signer's name to the projects its
32// evidence counts in. A nil SignerProjects restricts nobody.
33type SignerProjects map[string]map[string]bool
34
35// ParseSignerProjects reads a signer projects file's contents. A word that
36// is not printable ASCII, a name a verifier key cannot carry, or a signer
37// named on two lines fails the whole read, naming the line. Two lines for
38// one signer would leave which of them restricts it to the reader's guess.
39func ParseSignerProjects(data []byte) (SignerProjects, error) {
40 s := SignerProjects{}
41 for i, line := range strings.Split(string(data), "\n") {
42 var words []string
43 for _, w := range strings.Split(line, " ") {
44 if w != "" {
45 words = append(words, w)
46 }
47 }
48 if len(words) == 0 || strings.HasPrefix(words[0], "#") {
49 continue
50 }
51 for _, w := range words {
52 if err := checkWord(w); err != nil {
53 return nil, fmt.Errorf("line %d: %w", i+1, err)
54 }
55 }
56 name := words[0]
57 if err := CheckName(name); err != nil {
58 return nil, fmt.Errorf("line %d: %w", i+1, err)
59 }
60 if _, dup := s[name]; dup {
61 return nil, fmt.Errorf("line %d: %s is named on an earlier line too", i+1, name)
62 }
63 s[name] = map[string]bool{}
64 for _, project := range words[1:] {
65 s[name][project] = true
66 }
67 }
68 return s, nil
69}
70
71// checkWord holds a name or a project to printable ASCII.
72func checkWord(w string) error {
73 for _, c := range []byte(w) {
74 if c <= ' ' || c > '~' {
75 return fmt.Errorf("%q holds %#02x, and a name or project is printable ASCII with no space", w, c)
76 }
77 }
78 return nil
79}
80
81// Counts says whether evidence signed under name counts in project.
82func (s SignerProjects) Counts(name, project string) bool {
83 projects, restricted := s[name]
84 return !restricted || projects[project]
85}
86
87// Restricts says whether the file names name at all. The controllers that
88// read the file never sign under a name it names.
89func (s SignerProjects) Restricts(name string) bool {
90 _, restricted := s[name]
91 return restricted
92}