Sign in

Search issue titles and bodies, change requests and comments. Private repositories you cannot read are not included.

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

server/channels_test.go +232

@@ -0,0 +1,232 @@
1 package server_test
2
3 // Channels are a mutable alias to an immutable release: the page redirects, the
4 // tree serves the target's files, and moving the pointer is a maintainer action
5 // with an audit line.
6
7 import (
8 "fmt"
9 "io"
10 "net/http"
11 "os"
12 "path/filepath"
13 "strings"
14 "testing"
15 "time"
16 )
17
18 func channelDoc(t *testing.T, f *fixture, tag, by, description string) string {
19 t.Helper()
20 ttag, err := mustRepo(t, f).Git().Tag(tag)
21 if err != nil || !ttag.Annotated {
22 t.Fatalf("tag %s: %v (annotated=%v)", tag, err, ttag.Annotated)
23 }
24 return fmt.Sprintf("---\nrelease: %s\nrelease_object: %s\nupdated_by: %s\nupdated_at: %s\n---\n%s\n",
25 tag, ttag.OID, by, time.Now().UTC().Format(time.RFC3339), description)
26 }
27
28 func TestChannelPageAndTree(t *testing.T) {
29 t.Parallel()
30 f := releaseFixture(t)
31 data := []byte("Package: vole\n")
32 attachArtifact(t, f, "v1.0.0", "apt/Packages", data)
33
34 meta := f.newMetaRepo(t)
35 if err := meta.apply(map[string]string{
36 "channels/stable.md": channelDoc(t, f, "v1.0.0", "keenan", "Recommended for production."),
37 }, "channel: stable -> v1.0.0"); err != nil {
38 t.Fatal(err)
39 }
40
41 code, page := f.get("/" + f.repo + "/channels")
42 if code != 200 {
43 t.Fatalf("channels page = %d", code)
44 }
45 for _, want := range []string{"stable", "v1.0.0", "Recommended for production."} {
46 if !strings.Contains(page, want) {
47 t.Fatalf("channels page does not show %q:\n%s", want, page)
48 }
49 }
50
51 // The release page carries the channel badge, so the pointer is discoverable
52 // from the release it names.
53 if code, release := f.get("/" + f.repo + "/releases/v1.0.0"); code != 200 ||
54 !strings.Contains(release, "/channels/stable") {
55 t.Fatalf("release page = %d, badge missing:\n%s", code, release)
56 }
57
58 // The stable page is a redirect to the canonical release page.
59 req, _ := http.NewRequest(http.MethodGet, f.http.URL+"/"+f.repo+"/channels/stable", nil)
60 resp, err := noRedirect.Do(req)
61 if err != nil {
62 t.Fatal(err)
63 }
64 resp.Body.Close()
65 if resp.StatusCode != http.StatusFound || resp.Header.Get("Location") != "/"+f.repo+"/releases/v1.0.0" {
66 t.Fatalf("channel redirect = %d %q", resp.StatusCode, resp.Header.Get("Location"))
67 }
68
69 // The tree is served from the target release, under the channel name.
70 resp, err = http.Get(f.http.URL + "/" + f.repo + "/channels/stable/apt/Packages")
71 if err != nil {
72 t.Fatal(err)
73 }
74 body, _ := io.ReadAll(resp.Body)
75 resp.Body.Close()
76 if resp.StatusCode != 200 || string(body) != string(data) {
77 t.Fatalf("channel file = %d %q", resp.StatusCode, body)
78 }
79 if ct := resp.Header.Get("Content-Type"); ct != "application/octet-stream" {
80 t.Fatalf("channel Content-Type = %q, want octet-stream", ct)
81 }
82 if cd := resp.Header.Get("Content-Disposition"); !strings.Contains(cd, `filename="Packages"`) {
83 t.Fatalf("channel Content-Disposition = %q", cd)
84 }
85 if cc := resp.Header.Get("Cache-Control"); !strings.Contains(cc, "must-revalidate") {
86 t.Fatalf("channel Cache-Control = %q", cc)
87 }
88 if resp.Header.Get("X-Content-Type-Options") != "nosniff" {
89 t.Fatal("channel file is served without nosniff")
90 }
91
92 if code, _ := f.get("/" + f.repo + "/channels/stable/missing"); code != 404 {
93 t.Fatalf("missing channel file = %d, want 404", code)
94 }
95 if code, _ := f.get("/" + f.repo + "/channels/missing"); code != 404 {
96 t.Fatalf("missing channel = %d, want 404", code)
97 }
98 }
99
100 func TestChannelDanglingTarget(t *testing.T) {
101 t.Parallel()
102 f := releaseFixture(t)
103 raw := fmt.Sprintf("---\nrelease: v9.9.9\nrelease_object: %s\nupdated_by: keenan\nupdated_at: %s\n---\n",
104 strings.Repeat("a", 40), time.Now().UTC().Format(time.RFC3339))
105 meta := f.newMetaRepo(t)
106 if err := meta.apply(map[string]string{"channels/stable.md": raw}, "channel: stable -> v9.9.9"); err != nil {
107 t.Fatal(err)
108 }
109
110 code, page := f.get("/" + f.repo + "/channels")
111 if code != 200 || !strings.Contains(page, "dangling") || !strings.Contains(page, "v9.9.9") {
112 t.Fatalf("channels page = %d, want a dangling marker:\n%s", code, page)
113 }
114 if code, _ := f.get("/" + f.repo + "/channels/stable"); code != 404 {
115 t.Fatalf("dangling channel = %d, want 404", code)
116 }
117 if code, _ := f.get("/" + f.repo + "/channels/stable/apt/Packages"); code != 404 {
118 t.Fatalf("dangling channel file = %d, want 404", code)
119 }
120 }
121
122 func TestChannelMoveRequiresMaintain(t *testing.T) {
123 t.Parallel()
124 f := releaseFixture(t)
125 raw := channelDoc(t, f, "v1.0.0", "carol", "move")
126
127 pub, priv := keypair(t, filepath.Join(f.cfg.DataDir, "id_contrib"))
128 contrib, err := f.srv.Identity.Create("carol", pub, "test")
129 if err != nil {
130 t.Fatal(err)
131 }
132 m := mustRepo(t, f)
133 a, err := m.ACL()
134 if err != nil {
135 t.Fatal(err)
136 }
137 if err := a.SetEntry(contrib.Name, "contribute"); err != nil {
138 t.Fatal(err)
139 }
140 if err := m.SetACL(a, "test"); err != nil {
141 t.Fatal(err)
142 }
143
144 if out, err := pushMetaAs(t, f, priv, map[string]string{"channels/stable.md": raw}, nil, "channel: stable"); err == nil ||
145 !strings.Contains(out, "maintain") {
146 t.Fatalf("contributor channel move = %v: %s, want a maintain refusal", err, out)
147 }
148
149 if _, err := pushMetaAs(t, f, f.addWriter(t), map[string]string{"channels/stable.md": raw}, nil, "channel: stable"); err != nil {
150 t.Fatalf("maintainer channel move: %v", err)
151 }
152 audit, err := os.ReadFile(f.cfg.AuditLog())
153 if err != nil {
154 t.Fatal(err)
155 }
156 if !strings.Contains(string(audit), "channel.set") || !strings.Contains(string(audit), "stable -> v1.0.0") {
157 t.Fatalf("audit log has no channel move:\n%s", audit)
158 }
159 if code, page := f.get("/" + f.repo + "/channels"); code != 200 || !strings.Contains(page, "v1.0.0") {
160 t.Fatalf("channel after move = %d:\n%s", code, page)
161 }
162 }
163
164 func TestChannelFileIsNotPublicOnAPrivateRepo(t *testing.T) {
165 t.Parallel()
166 f := newFixture(t)
167 work := workRepo(t, map[string]string{"readme.md": "secret\n"})
168 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "main:refs/heads/main"); err != nil {
169 t.Fatalf("push: %s", errs)
170 }
171 if _, errs, err := runGit(t, work, "tag", "-a", "v1.0.0", "-m", "notes\n"); err != nil {
172 t.Fatalf("tag: %s", errs)
173 }
174 if _, errs, err := f.git(work, "push", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil {
175 t.Fatalf("push tag: %s", errs)
176 }
177 attachArtifact(t, f, "v1.0.0", "apt/Packages", []byte("Package: vole\n"))
178 meta := f.newMetaRepo(t)
179 if err := meta.apply(map[string]string{
180 "channels/stable.md": channelDoc(t, f, "v1.0.0", "keenan", ""),
181 }, "channel: stable -> v1.0.0"); err != nil {
182 t.Fatal(err)
183 }
184 if code, _ := f.get("/" + f.repo + "/channels/stable/apt/Packages"); code != 404 {
185 t.Fatalf("private channel file = %d, want 404", code)
186 }
187 _, cookie := f.browserSession(t, f.ownerID)
188 resp, body := f.browserGet(t, "/"+f.repo+"/channels/stable/apt/Packages", cookie)
189 if resp.StatusCode != 200 || body != "Package: vole\n" {
190 t.Fatalf("owner channel file = %d %q", resp.StatusCode, body)
191 }
192 if cc := resp.Header.Get("Cache-Control"); strings.Contains(cc, "public") {
193 t.Fatalf("private channel Cache-Control = %q", cc)
194 }
195 }
196
197 func TestChannelCLI(t *testing.T) {
198 t.Parallel()
199 f := releaseFixture(t)
200 work := workRepo(t, map[string]string{"readme.md": "cli\n"})
201 if _, errs, err := runGit(t, work, "remote", "add", "origin", f.sshURL(f.repo)); err != nil {
202 t.Fatalf("remote: %v: %s", err, errs)
203 }
204 if _, errs, err := f.git(work, "fetch", f.sshURL(f.repo), "refs/tags/v1.0.0:refs/tags/v1.0.0"); err != nil {
205 t.Fatalf("fetch tag: %v: %s", err, errs)
206 }
207
208 out, errOut, err := f.cli(work, "release", "channel", "set", "stable", "v1.0.0", "-m", "CLI channel")
209 if err != nil {
210 t.Fatalf("channel set: %v\n%s%s", err, out, errOut)
211 }
212 if !strings.Contains(out, "stable -> v1.0.0") {
213 t.Fatalf("channel set output = %q", out)
214 }
215 if code, page := f.get("/" + f.repo + "/channels"); code != 200 || !strings.Contains(page, "CLI channel") {
216 t.Fatalf("channels page after CLI set = %d:\n%s", code, page)
217 }
218
219 out, errOut, err = f.cli(work, "release", "channel", "list")
220 if err != nil || !strings.Contains(out, "stable") || !strings.Contains(out, "v1.0.0") {
221 t.Fatalf("channel list = %v %q %q", err, out, errOut)
222 }
223
224 out, errOut, err = f.cli(work, "release", "channel", "clear", "stable")
225 if err != nil {
226 t.Fatalf("channel clear: %v\n%s%s", err, out, errOut)
227 }
228 out, _, err = f.cli(work, "release", "channel", "list")
229 if err != nil || !strings.Contains(out, "no channels") {
230 t.Fatalf("channel list after clear = %v %q", err, out)
231 }
232 }