| 1 | // Package approval is an approval's signed statement: what an approver's |
| 2 | // key signs to say that one recorded head of a change may land on a |
| 3 | // target, how the statement is written, signed and sealed in a note, and |
| 4 | // how a reader verifies it against the approval keys it holds. |
| 5 | // |
| 6 | // A statement is written in the attestation's line form: the statement |
| 7 | // type on the first line, then one key, one space and one value per line, |
| 8 | // in a fixed order, with no escapes and a closing newline. |
| 9 | // |
| 10 | // the-valley/approval/v1 |
| 11 | // instance 5f0c… |
| 12 | // project app |
| 13 | // target main |
| 14 | // change nxvpwlkmqtsrzyoukklnpmqrstuvwxyz |
| 15 | // version 2 |
| 16 | // head git-sha1:2222… |
| 17 | // record git-sha1:3333… |
| 18 | // principal operator |
| 19 | // issued 2026-11-06T00:43:26Z |
| 20 | // |
| 21 | // The issue time is for the record. A statement carries no expiry: whether |
| 22 | // an approval still lands is decided by what a landing checks, never by a |
| 23 | // clock. |
| 24 | // |
| 25 | // The package depends on the standard library and this repository alone. |
| 26 | package approval |
| 27 | |
| 28 | import ( |
| 29 | "bytes" |
| 30 | "crypto/sha256" |
| 31 | "encoding/hex" |
| 32 | "errors" |
| 33 | "fmt" |
| 34 | "regexp" |
| 35 | "slices" |
| 36 | "strconv" |
| 37 | "strings" |
| 38 | "time" |
| 39 | |
| 40 | "the-valley/change" |
| 41 | "the-valley/note" |
| 42 | ) |
| 43 | |
| 44 | const ( |
| 45 | // Type is the first line of every approval statement. The signature |
| 46 | // covers the text alone, so this line is what keeps an approval key's |
| 47 | // signature over an attestation, or anything else, from reading as an |
| 48 | // approval. |
| 49 | Type = "the-valley/approval/v1" |
| 50 | |
| 51 | // MaxStatement is the most bytes a statement may hold. It is the bound |
| 52 | // the hardware signer is held to, so a statement is refused before any |
| 53 | // device is asked to sign it. |
| 54 | MaxStatement = 1024 |
| 55 | |
| 56 | // MaxNote is the most bytes an approval note may hold: a statement and |
| 57 | // one signature line named by its principal, which the statement |
| 58 | // already holds. |
| 59 | MaxNote = 2 * MaxStatement |
| 60 | |
| 61 | // TimeLayout is how the issue time is written: UTC, to the second. |
| 62 | TimeLayout = "2006-01-02T15:04:05Z" |
| 63 | |
| 64 | // MaxProject is the most bytes in a project name, as the registry |
| 65 | // schema bounds it. |
| 66 | MaxProject = 64 |
| 67 | |
| 68 | instanceType = "valley-instance-v1" |
| 69 | ) |
| 70 | |
| 71 | // keys is the written form's line order, after the type line. |
| 72 | var keys = []string{"instance", "project", "target", "change", "version", "head", "record", "principal", "issued"} |
| 73 | |
| 74 | // name is a project's or a principal's name, as the schemas spell one. |
| 75 | var name = regexp.MustCompile(`^[a-zA-Z0-9][a-zA-Z0-9._-]*$`) |
| 76 | |
| 77 | // Statement is what an approver signs. |
| 78 | type Statement struct { |
| 79 | // Instance is the instance's identity, InstanceID of its repository. |
| 80 | Instance string |
| 81 | // Project and Target are where the change lands. |
| 82 | Project string |
| 83 | Target string |
| 84 | // Change and Version are the change and the version of it whose head |
| 85 | // was approved; Head is that head commit, as git-sha1:<hex>. |
| 86 | Change string |
| 87 | Version int |
| 88 | Head string |
| 89 | // Record is the change-record commit the approver read. |
| 90 | Record string |
| 91 | // Principal is who approves: the name their approval key is listed |
| 92 | // under. |
| 93 | Principal string |
| 94 | // Issued is when the statement was made, to the second. |
| 95 | Issued time.Time |
| 96 | } |
| 97 | |
| 98 | // Encode writes the statement in its written form, after holding every |
| 99 | // field to what the form allows. |
| 100 | func (s Statement) Encode() ([]byte, error) { |
| 101 | if err := s.check(); err != nil { |
| 102 | return nil, err |
| 103 | } |
| 104 | var b bytes.Buffer |
| 105 | b.WriteString(Type + "\n") |
| 106 | for i, v := range s.values() { |
| 107 | b.WriteString(keys[i] + " " + v + "\n") |
| 108 | } |
| 109 | if b.Len() > MaxStatement { |
| 110 | return nil, fmt.Errorf("the statement is %d bytes; an approval statement holds at most %d, the most the signers support", b.Len(), MaxStatement) |
| 111 | } |
| 112 | return b.Bytes(), nil |
| 113 | } |
| 114 | |
| 115 | func (s Statement) values() []string { |
| 116 | return []string{s.Instance, s.Project, s.Target, s.Change, strconv.Itoa(s.Version), |
| 117 | s.Head, s.Record, s.Principal, s.Issued.UTC().Format(TimeLayout)} |
| 118 | } |
| 119 | |
| 120 | func (s Statement) check() error { |
| 121 | switch { |
| 122 | case !isHex(s.Instance, sha256.Size*2): |
| 123 | return fmt.Errorf("the instance %q is not 64 lowercase hex digits", s.Instance) |
| 124 | case !name.MatchString(s.Project) || len(s.Project) > MaxProject: |
| 125 | return fmt.Errorf("the project %q is not a project name of at most %d bytes", s.Project, MaxProject) |
| 126 | case !change.ValidBranch(s.Target): |
| 127 | return fmt.Errorf("the target %q is not a branch name", s.Target) |
| 128 | case !change.ValidID(s.Change): |
| 129 | return fmt.Errorf("the change %q is not a change ID: one is %d letters from k to z", s.Change, change.IDLength) |
| 130 | case s.Version < 1 || s.Version > change.MaxVersions: |
| 131 | return fmt.Errorf("the version %d is outside 1 to %d", s.Version, change.MaxVersions) |
| 132 | case !isCommit(s.Head): |
| 133 | return fmt.Errorf("the head %q is not %s and 40 lowercase hex digits", s.Head, change.HeadPrefix) |
| 134 | case !isCommit(s.Record): |
| 135 | return fmt.Errorf("the record %q is not %s and 40 lowercase hex digits", s.Record, change.HeadPrefix) |
| 136 | case !name.MatchString(s.Principal): |
| 137 | return fmt.Errorf("the principal %q is not a principal name", s.Principal) |
| 138 | case s.Issued.IsZero(): |
| 139 | return errors.New("the statement has no issue time") |
| 140 | case s.Issued.Nanosecond() != 0 || s.Issued.UTC().Year() < 1 || s.Issued.UTC().Year() > 9999: |
| 141 | return fmt.Errorf("the issue time %s is not a whole second from year 1 to 9999", s.Issued) |
| 142 | } |
| 143 | return nil |
| 144 | } |
| 145 | |
| 146 | // Parse reads a statement's written form. Text is accepted only when it is |
| 147 | // exactly what Encode writes for the statement it says: every line in its |
| 148 | // place, no line missing, unknown or repeated, and every value in its one |
| 149 | // spelling. |
| 150 | func Parse(text []byte) (Statement, error) { |
| 151 | if len(text) > MaxStatement { |
| 152 | return Statement{}, fmt.Errorf("the statement is %d bytes; an approval statement holds at most %d", len(text), MaxStatement) |
| 153 | } |
| 154 | if err := note.CheckText(text); err != nil { |
| 155 | return Statement{}, err |
| 156 | } |
| 157 | lines := strings.Split(strings.TrimSuffix(string(text), "\n"), "\n") |
| 158 | if lines[0] != Type { |
| 159 | return Statement{}, fmt.Errorf("is not an approval: its text opens with %q, not %q", lines[0], Type) |
| 160 | } |
| 161 | if len(lines) != 1+len(keys) { |
| 162 | return Statement{}, fmt.Errorf("holds %d lines; an approval statement holds %d", len(lines), 1+len(keys)) |
| 163 | } |
| 164 | v := make([]string, len(keys)) |
| 165 | for i, key := range keys { |
| 166 | k, value, ok := strings.Cut(lines[i+1], " ") |
| 167 | if !ok || k != key { |
| 168 | return Statement{}, fmt.Errorf("line %d is %q, where the %s line belongs", i+2, lines[i+1], key) |
| 169 | } |
| 170 | v[i] = value |
| 171 | } |
| 172 | version, err := strconv.Atoi(v[4]) |
| 173 | if err != nil { |
| 174 | return Statement{}, fmt.Errorf("the version %q is not a number", v[4]) |
| 175 | } |
| 176 | issued, err := time.Parse(TimeLayout, v[8]) |
| 177 | if err != nil { |
| 178 | return Statement{}, fmt.Errorf("the issue time %q is not written %s", v[8], TimeLayout) |
| 179 | } |
| 180 | s := Statement{Instance: v[0], Project: v[1], Target: v[2], Change: v[3], Version: version, |
| 181 | Head: v[5], Record: v[6], Principal: v[7], Issued: issued} |
| 182 | written, err := s.Encode() |
| 183 | if err != nil { |
| 184 | return Statement{}, err |
| 185 | } |
| 186 | if !bytes.Equal(written, text) { |
| 187 | return Statement{}, errors.New("is not in the statement's one written form") |
| 188 | } |
| 189 | return s, nil |
| 190 | } |
| 191 | |
| 192 | // InstanceID is an instance's identity, derived from its repository's |
| 193 | // history: SHA-256 over the line valley-instance-v1, the line |
| 194 | // object-format <format>, and one line root <id> for each root commit of |
| 195 | // the instance repository's integrated tip, sorted bytewise. Repositories |
| 196 | // sharing that history share the identity. |
| 197 | func InstanceID(format string, roots []string) (string, error) { |
| 198 | size := map[string]int{"sha1": 40, "sha256": 64}[format] |
| 199 | if size == 0 { |
| 200 | return "", fmt.Errorf("the object format %q is not sha1 or sha256", format) |
| 201 | } |
| 202 | if len(roots) == 0 { |
| 203 | return "", errors.New("an instance identity needs at least one root commit") |
| 204 | } |
| 205 | sorted := slices.Clone(roots) |
| 206 | slices.Sort(sorted) |
| 207 | var b strings.Builder |
| 208 | b.WriteString(instanceType + "\nobject-format " + format + "\n") |
| 209 | for i, r := range sorted { |
| 210 | if !isHex(r, size) { |
| 211 | return "", fmt.Errorf("the root %q is not a %s object ID", r, format) |
| 212 | } |
| 213 | if i > 0 && r == sorted[i-1] { |
| 214 | return "", fmt.Errorf("the root %s is listed twice", r) |
| 215 | } |
| 216 | b.WriteString("root " + r + "\n") |
| 217 | } |
| 218 | sum := sha256.Sum256([]byte(b.String())) |
| 219 | return hex.EncodeToString(sum[:]), nil |
| 220 | } |
| 221 | |
| 222 | func isCommit(s string) bool { |
| 223 | id, ok := strings.CutPrefix(s, change.HeadPrefix) |
| 224 | return ok && isHex(id, 40) |
| 225 | } |
| 226 | |
| 227 | func isHex(s string, n int) bool { |
| 228 | if len(s) != n { |
| 229 | return false |
| 230 | } |
| 231 | for _, c := range []byte(s) { |
| 232 | if (c < '0' || c > '9') && (c < 'a' || c > 'f') { |
| 233 | return false |
| 234 | } |
| 235 | } |
| 236 | return true |
| 237 | } |