vole
Site
Clone
Read only, no credentials:
https://rodent.software/vole.git
Write over SSH with a registered key:
git clone ssh://git@rodent.software/vole.git
Self-hosted forge that lives in your Git repo.
Add named release channels and a generic channel file route
24019cb30892b9fc0118697e8b89cd61e9fab576 · Keenan Nemetz · 19d ago · parent 4f8a3da8
Add named release channels and a generic channel file route A channel is a mutable pointer, channels/<name>.md, to one immutable annotated release. The target tag object is recorded so a rewritten tag is detected rather than silently followed. - metadata: channel parse/render/validate, closed-layout and size checks, set/clear writers - CLI: vole release channel set|list|clear; a dangling target is reported - server: maintain/admin path gate and audit for channel.set/clear - index: release_channels table, incremental refresh, per-tag badge query - web: /channels list, /channels/<name> redirect, and /channels/<name>/<path...> serving the target release's tree with OID ETags, Range, revalidation, and octet-stream responses - tests: metadata round trips, index moves, hook role/audit, page/tree/dangling/ private, and the CLI end to end No apt, yum, or other package-manager code is involved: a package layout is a directory in the release tree. Refs: 01M7BPHANWMSW5HK2GT0PY74QZ
metadata/channels.go +272
| @@ -0,0 +1,272 @@ | |||
| 1 | package metadata | ||
| 2 | |||
| 3 | // A channel is a mutable pointer to one immutable release: a stable URL that | ||
| 4 | // follows what maintainers promote, without moving the tag it points at. | ||
| 5 | // | ||
| 6 | // The pointer is a file, channels/<name>.md, for the same reasons the rest of the | ||
| 7 | // metadata branch is files: a move is a commit with an author and a time, it | ||
| 8 | // travels with a clone, and the reader needs no second store. The target tag is | ||
| 9 | // the release; the tag object sha is recorded so a rewritten tag is detected | ||
| 10 | // rather than silently followed. | ||
| 11 | // | ||
| 12 | // A channel names a release, not a package manager. The files a channel serves | ||
| 13 | // are the files attached to the target release, so a package layout is just a | ||
| 14 | // tree of paths; nothing here knows about apt, yum, or any other client. | ||
| 15 | |||
| 16 | import ( | ||
| 17 | "encoding/hex" | ||
| 18 | "fmt" | ||
| 19 | "regexp" | ||
| 20 | "sort" | ||
| 21 | "strings" | ||
| 22 | "time" | ||
| 23 | |||
| 24 | "rodent.software/vole/core/git" | ||
| 25 | ) | ||
| 26 | |||
| 27 | const ( | ||
| 28 | // ChannelsDir is the channel-pointer subtree. One file per channel. | ||
| 29 | ChannelsDir = "channels" | ||
| 30 | // maxChannelNameLen bounds the URL segment. | ||
| 31 | maxChannelNameLen = 32 | ||
| 32 | // maxChannelDescription bounds the prose a channel page carries. A channel is | ||
| 33 | // a pointer and a short note, not a second release page. | ||
| 34 | maxChannelDescription = 8 << 10 | ||
| 35 | // maxChannelTagLen is generous for a tag name and still bounds a file. | ||
| 36 | maxChannelTagLen = 200 | ||
| 37 | ) | ||
| 38 | |||
| 39 | var channelNameRE = regexp.MustCompile(`^[a-z0-9][a-z0-9-]{0,31}$`) | ||
| 40 | |||
| 41 | // Channel is one mutable alias for a release. | ||
| 42 | type Channel struct { | ||
| 43 | Name string | ||
| 44 | Release string | ||
| 45 | ReleaseObject string | ||
| 46 | UpdatedBy string | ||
| 47 | UpdatedAt time.Time | ||
| 48 | Description string | ||
| 49 | } | ||
| 50 | |||
| 51 | // ValidChannelName checks the URL segment. Channels are lowercase so the URL and | ||
| 52 | // the metadata filename cannot differ by case on a case-insensitive filesystem. | ||
| 53 | func ValidChannelName(name string) error { | ||
| 54 | switch { | ||
| 55 | case name == "": | ||
| 56 | return fmt.Errorf("a channel needs a name") | ||
| 57 | case len(name) > maxChannelNameLen: | ||
| 58 | return fmt.Errorf("channel name is %d bytes, limit %d", len(name), maxChannelNameLen) | ||
| 59 | case !channelNameRE.MatchString(name): | ||
| 60 | return fmt.Errorf("channel name %q must be lowercase letters, digits, and dashes, starting with a letter or digit", name) | ||
| 61 | } | ||
| 62 | return nil | ||
| 63 | } | ||
| 64 | |||
| 65 | // ChannelPath is where one channel's pointer lives on the metadata branch. | ||
| 66 | func ChannelPath(name string) string { return ChannelsDir + "/" + name + ".md" } | ||
| 67 | |||
| 68 | // ChannelNameFromPath returns the channel named by a metadata path, if the path | ||
| 69 | // is exactly channels/<name>.md. A nested directory or another suffix is not a | ||
| 70 | // channel, and the validator refuses it at push time. | ||
| 71 | func ChannelNameFromPath(path string) (string, bool) { | ||
| 72 | rest, ok := strings.CutPrefix(path, ChannelsDir+"/") | ||
| 73 | if !ok || !strings.HasSuffix(rest, ".md") || strings.Contains(rest, "/") { | ||
| 74 | return "", false | ||
| 75 | } | ||
| 76 | name := strings.TrimSuffix(rest, ".md") | ||
| 77 | if ValidChannelName(name) != nil { | ||
| 78 | return "", false | ||
| 79 | } | ||
| 80 | return name, true | ||
| 81 | } | ||
| 82 | |||
| 83 | // ValidChannelTag checks the target tag name as it is stored in the file. Git | ||
| 84 | // validates the ref itself; this keeps a hand edit from putting a control | ||
| 85 | // character or an unbounded value into the metadata tree. | ||
| 86 | func ValidChannelTag(tag string) error { | ||
| 87 | switch { | ||
| 88 | case tag == "": | ||
| 89 | return fmt.Errorf("a channel needs a release tag") | ||
| 90 | case len(tag) > maxChannelTagLen: | ||
| 91 | return fmt.Errorf("release tag is %d bytes, limit %d", len(tag), maxChannelTagLen) | ||
| 92 | } | ||
| 93 | return ValidText(tag) | ||
| 94 | } | ||
| 95 | |||
| 96 | func validObjectID(id string) error { | ||
| 97 | if len(id) != 40 && len(id) != 64 { | ||
| 98 | return fmt.Errorf("release_object %q is not a 40- or 64-character object id", id) | ||
| 99 | } | ||
| 100 | if _, err := hex.DecodeString(id); err != nil { | ||
| 101 | return fmt.Errorf("release_object %q is not hexadecimal", id) | ||
| 102 | } | ||
| 103 | return nil | ||
| 104 | } | ||
| 105 | |||
| 106 | func validateChannel(ch *Channel) error { | ||
| 107 | var problems []string | ||
| 108 | if err := ValidChannelName(ch.Name); err != nil { | ||
| 109 | problems = append(problems, err.Error()) | ||
| 110 | } | ||
| 111 | if err := ValidChannelTag(ch.Release); err != nil { | ||
| 112 | problems = append(problems, err.Error()) | ||
| 113 | } | ||
| 114 | if err := validObjectID(ch.ReleaseObject); err != nil { | ||
| 115 | problems = append(problems, err.Error()) | ||
| 116 | } | ||
| 117 | if strings.TrimSpace(ch.UpdatedBy) == "" { | ||
| 118 | problems = append(problems, "updated_by is required") | ||
| 119 | } else if err := ValidText(ch.UpdatedBy); err != nil { | ||
| 120 | problems = append(problems, "updated_by: "+err.Error()) | ||
| 121 | } | ||
| 122 | if ch.UpdatedAt.IsZero() { | ||
| 123 | problems = append(problems, "updated_at is required") | ||
| 124 | } | ||
| 125 | if len(ch.Description) > maxChannelDescription { | ||
| 126 | problems = append(problems, fmt.Sprintf("description is %d bytes, limit %d", | ||
| 127 | len(ch.Description), maxChannelDescription)) | ||
| 128 | } | ||
| 129 | if len(problems) > 0 { | ||
| 130 | return &ValidationError{Problems: problems} | ||
| 131 | } | ||
| 132 | return nil | ||
| 133 | } | ||
| 134 | |||
| 135 | // renderChannel writes the frontmatter and body. The target object is a value, | ||
| 136 | // not a reference to the tag's current state: a tag rewritten after the pointer | ||
| 137 | // was written must not silently move the channel with it. | ||
| 138 | func renderChannel(ch *Channel) ([]byte, error) { | ||
| 139 | d := NewDoc() | ||
| 140 | d.Word("release", ch.Release) | ||
| 141 | d.Word("release_object", ch.ReleaseObject) | ||
| 142 | d.Word("updated_by", ch.UpdatedBy) | ||
| 143 | d.Word("updated_at", ch.UpdatedAt.UTC().Format(time.RFC3339)) | ||
| 144 | return d.Bytes(strings.TrimRight(ch.Description, "\n")) | ||
| 145 | } | ||
| 146 | |||
| 147 | // ParseChannel reads one channel file. The name comes from the path, not from a | ||
| 148 | // field, so a renamed file is a different channel. | ||
| 149 | func ParseChannel(name string, raw []byte) (*Channel, error) { | ||
| 150 | head, body, err := Split(raw) | ||
| 151 | if err != nil { | ||
| 152 | return nil, err | ||
| 153 | } | ||
| 154 | var fields struct { | ||
| 155 | Release string `yaml:"release"` | ||
| 156 | ReleaseObject string `yaml:"release_object"` | ||
| 157 | UpdatedBy string `yaml:"updated_by"` | ||
| 158 | UpdatedAt string `yaml:"updated_at"` | ||
| 159 | } | ||
| 160 | if err := Decode(head, &fields); err != nil { | ||
| 161 | return nil, err | ||
| 162 | } | ||
| 163 | at, err := time.Parse(time.RFC3339, strings.TrimSpace(fields.UpdatedAt)) | ||
| 164 | if err != nil { | ||
| 165 | return nil, fmt.Errorf("updated_at: %w", err) | ||
| 166 | } | ||
| 167 | ch := &Channel{ | ||
| 168 | Name: name, | ||
| 169 | Release: strings.TrimSpace(fields.Release), | ||
| 170 | ReleaseObject: strings.TrimSpace(fields.ReleaseObject), | ||
| 171 | UpdatedBy: strings.TrimSpace(fields.UpdatedBy), | ||
| 172 | UpdatedAt: at.UTC(), | ||
| 173 | Description: strings.TrimSpace(body), | ||
| 174 | } | ||
| 175 | if err := validateChannel(ch); err != nil { | ||
| 176 | return nil, err | ||
| 177 | } | ||
| 178 | return ch, nil | ||
| 179 | } | ||
| 180 | |||
| 181 | // LoadChannelsAt reads every channel on the metadata branch at commit, sorted by | ||
| 182 | // name. A malformed file is skipped: the push validator refuses it, and a reader | ||
| 183 | // must not fail the whole page because a hand edit landed in one file. The index | ||
| 184 | // and the web use this; the push boundary uses ChannelProblems. | ||
| 185 | func LoadChannelsAt(g *git.Repo, commit string) ([]Channel, error) { | ||
| 186 | if commit == "" || commit == git.ZeroSHA { | ||
| 187 | return nil, nil | ||
| 188 | } | ||
| 189 | files, err := readMetadata(g, commit, ChannelsDir) | ||
| 190 | if err != nil { | ||
| 191 | return nil, err | ||
| 192 | } | ||
| 193 | out := make([]Channel, 0, len(files)) | ||
| 194 | for path, raw := range files { | ||
| 195 | name, ok := ChannelNameFromPath(path) | ||
| 196 | if !ok { | ||
| 197 | continue | ||
| 198 | } | ||
| 199 | ch, err := ParseChannel(name, []byte(raw)) | ||
| 200 | if err != nil { | ||
| 201 | continue | ||
| 202 | } | ||
| 203 | out = append(out, *ch) | ||
| 204 | } | ||
| 205 | sort.Slice(out, func(i, j int) bool { return out[i].Name < out[j].Name }) | ||
| 206 | return out, nil | ||
| 207 | } | ||
| 208 | |||
| 209 | // ChannelProblems validates every channels/ file at commit. The layout is closed: | ||
| 210 | // exactly channels/<name>.md, with a valid pointer. A target tag is not required | ||
| 211 | // to exist here, because a tag and the pointer that names it may arrive in one | ||
| 212 | // push or in either order; the reader resolves the target and reports a dangling | ||
| 213 | // channel. | ||
| 214 | func ChannelProblems(g *git.Repo, commit string) []string { | ||
| 215 | if commit == "" || commit == git.ZeroSHA { | ||
| 216 | return nil | ||
| 217 | } | ||
| 218 | files, err := readMetadata(g, commit, ChannelsDir) | ||
| 219 | if err != nil { | ||
| 220 | return []string{err.Error()} | ||
| 221 | } | ||
| 222 | var problems []string | ||
| 223 | for path, raw := range files { | ||
| 224 | name, ok := ChannelNameFromPath(path) | ||
| 225 | if !ok { | ||
| 226 | problems = append(problems, fmt.Sprintf("%s: channels are %s/<name>.md", path, ChannelsDir)) | ||
| 227 | continue | ||
| 228 | } | ||
| 229 | if _, err := ParseChannel(name, []byte(raw)); err != nil { | ||
| 230 | if ve, ok := err.(*ValidationError); ok { | ||
| 231 | for _, p := range ve.Problems { | ||
| 232 | problems = append(problems, path+": "+p) | ||
| 233 | } | ||
| 234 | continue | ||
| 235 | } | ||
| 236 | problems = append(problems, path+": "+err.Error()) | ||
| 237 | } | ||
| 238 | } | ||
| 239 | sort.Strings(problems) | ||
| 240 | return problems | ||
| 241 | } | ||
| 242 | |||
| 243 | // WriteChannel records one pointer on the metadata branch. The write is a normal | ||
| 244 | // commit-files compare-and-swap, so a move races like every other metadata write | ||
| 245 | // and re-applies on the new tip. | ||
| 246 | func WriteChannel(g *git.Repo, ref string, ch *Channel, message, author string) error { | ||
| 247 | if err := validateChannel(ch); err != nil { | ||
| 248 | return err | ||
| 249 | } | ||
| 250 | raw, err := renderChannel(ch) | ||
| 251 | if err != nil { | ||
| 252 | return err | ||
| 253 | } | ||
| 254 | _, err = git.CommitFiles(g, ref, func(files map[string][]byte) error { | ||
| 255 | files[ChannelPath(ch.Name)] = raw | ||
| 256 | return nil | ||
| 257 | }, message, author) | ||
| 258 | return err | ||
| 259 | } | ||
| 260 | |||
| 261 | // DeleteChannel removes one pointer. Removing a channel publishes nothing; the | ||
| 262 | // release it named keeps its files and its page. | ||
| 263 | func DeleteChannel(g *git.Repo, ref, name, message, author string) error { | ||
| 264 | if err := ValidChannelName(name); err != nil { | ||
| 265 | return err | ||
| 266 | } | ||
| 267 | _, err := git.CommitFiles(g, ref, func(files map[string][]byte) error { | ||
| 268 | delete(files, ChannelPath(name)) | ||
| 269 | return nil | ||
| 270 | }, message, author) | ||
| 271 | return err | ||
| 272 | } | ||