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.

Blame · server/ssh/channel.go

Line attribution on main · 199 line(s)

262e516e
Keenan Nemetz
19d ago
1 package ssh
2
3 // Release channels over the SSH verb.
4 //
5 // vole-channel list <repository>
6 // vole-channel set <repository> <name> <tag> (description on stdin)
7 // vole-channel clear <repository> <name>
8 //
9 // The channel store is server-side source state, so the CLI asks the instance
10 // over the same key it pushes with. The verbs re-check the repository role:
11 // listing is a read capability, moving a channel is maintainer work. A move is
12 // recorded in the audit log before the row is written.
13
14 import (
15 "encoding/json"
16 "fmt"
17 "io"
18 "strings"
19 "time"
20
21 "rodent.software/vole/core/git"
22 "rodent.software/vole/forge/access"
23 "rodent.software/vole/forge/channel"
24 "rodent.software/vole/internal/audit"
25 )
26
27 type channelLine struct {
28 Name string `json:"name"`
29 Release string `json:"release"`
30 ReleaseObject string `json:"release_object"`
31 UpdatedBy string `json:"updated_by"`
32 UpdatedAt time.Time `json:"updated_at"`
33 Description string `json:"description"`
34 Dangling bool `json:"dangling"`
35 }
36
5ff8dfc9
Keenan Nemetz
16d ago
37 func (s *Server) serveChannel(ch Session, argv []string, user string) int {
262e516e
Keenan Nemetz
19d ago
38 if s.cfg.Channels == nil {
39 fmt.Fprint(ch, "vole: this instance has no channel store\n")
40 return 1
41 }
42 if len(argv) < 3 {
43 fmt.Fprint(ch, "vole: usage: vole-channel list|set|clear <repository> [<name> <tag>]\n")
44 return 129
45 }
46 op := argv[1]
47 repo, ok := repoName(argv[2])
48 if !ok {
49 fmt.Fprint(ch, "vole: invalid repository name\n")
50 return 129
51 }
52 m, err := s.cfg.Repos.Open(repo)
53 if err != nil {
54 fmt.Fprintf(ch, "vole: repository %q not found\n", repo)
55 return 1
56 }
57 a, err := m.ACL()
58 if err != nil {
59 fmt.Fprintf(ch, "vole: cannot read access rules for %s\n", repo)
60 return 1
61 }
62 role := a.EffectiveRole(user, s.cfg.Identity != nil && s.cfg.Identity.IsAdmin(user))
63
64 switch op {
65 case "list":
66 if len(argv) != 3 {
67 fmt.Fprint(ch, "vole: usage: vole-channel list <repository>\n")
68 return 129
69 }
70 if !role.AtLeast(access.Read) {
71 fmt.Fprintf(ch, "vole: %s may not read channels in %s\n", user, repo)
72 return 1
73 }
74 list, err := s.cfg.Channels.List(m.ID)
75 if err != nil {
76 fmt.Fprintf(ch, "vole: cannot read channels: %v\n", err)
77 return 1
78 }
79 lines := make([]channelLine, 0, len(list))
80 for _, ch := range list {
81 lines = append(lines, channelLine{
82 Name: ch.Name,
83 Release: ch.Release,
84 ReleaseObject: ch.ReleaseObject,
85 UpdatedBy: ch.UpdatedBy,
86 UpdatedAt: ch.UpdatedAt,
87 Description: ch.Description,
88 Dangling: !channelTargetOK(m.Git(), ch),
89 })
90 }
91 if err := json.NewEncoder(ch).Encode(lines); err != nil {
92 fmt.Fprintf(ch, "vole: cannot write the channel list: %v\n", err)
93 return 1
94 }
95 return 0
96
97 case "set":
98 if len(argv) != 5 {
99 fmt.Fprint(ch, "vole: usage: vole-channel set <repository> <name> <tag>\n")
100 return 129
101 }
102 if !role.AtLeast(access.Maintain) {
103 fmt.Fprintf(ch, "vole: moving a release channel requires maintain access (role: %s)\n", role)
104 return 1
105 }
106 name := argv[3]
107 if err := channel.ValidName(name); err != nil {
108 fmt.Fprintf(ch, "vole: %v\n", err)
109 return 129
110 }
111 tag := argv[4]
112 t, err := m.Git().Tag(tag)
113 if err != nil {
114 fmt.Fprintf(ch, "vole: no tag %s in %s\n", tag, repo)
115 return 1
116 }
117 if !t.Annotated {
118 fmt.Fprintf(ch, "vole: %s is a lightweight tag; a channel names an annotated release\n", tag)
119 return 1
120 }
121 description, err := io.ReadAll(io.LimitReader(ch, channel.MaxDescription+1))
122 if err != nil {
123 fmt.Fprintf(ch, "vole: cannot read the channel description: %v\n", err)
124 return 1
125 }
126 if len(description) > channel.MaxDescription {
127 fmt.Fprintf(ch, "vole: the channel description is larger than %d bytes\n", channel.MaxDescription)
128 return 129
129 }
130 if err := audit.Append(s.cfg.AuditPath, audit.Record{
131 Action: "channel.set",
132 Actor: user,
133 Subject: repo,
134 Detail: name + " -> " + tag,
135 }); err != nil {
136 fmt.Fprintf(ch, "vole: cannot record the channel move, refusing it: %v\n", err)
137 return 1
138 }
139 if err := s.cfg.Channels.Set(m.ID, channel.Channel{
140 Name: name,
141 Release: tag,
142 ReleaseObject: t.OID,
143 UpdatedBy: user,
144 UpdatedAt: time.Now().UTC(),
145 Description: string(description),
146 }); err != nil {
147 fmt.Fprintf(ch, "vole: %v\n", err)
148 return 1
149 }
150 fmt.Fprintf(ch, "channel %s -> %s\n", name, tag)
151 return 0
152
153 case "clear":
154 if len(argv) != 4 {
155 fmt.Fprint(ch, "vole: usage: vole-channel clear <repository> <name>\n")
156 return 129
157 }
158 if !role.AtLeast(access.Maintain) {
159 fmt.Fprintf(ch, "vole: moving a release channel requires maintain access (role: %s)\n", role)
160 return 1
161 }
162 name := argv[3]
163 if err := channel.ValidName(name); err != nil {
164 fmt.Fprintf(ch, "vole: %v\n", err)
165 return 129
166 }
167 if _, found, err := s.cfg.Channels.Get(m.ID, name); err != nil {
168 fmt.Fprintf(ch, "vole: cannot read the channel: %v\n", err)
169 return 1
170 } else if !found {
171 fmt.Fprintf(ch, "vole: no channel %s\n", name)
172 return 1
173 }
174 if err := audit.Append(s.cfg.AuditPath, audit.Record{
175 Action: "channel.clear",
176 Actor: user,
177 Subject: repo,
178 Detail: name,
179 }); err != nil {
180 fmt.Fprintf(ch, "vole: cannot record the channel change, refusing it: %v\n", err)
181 return 1
182 }
183 if _, err := s.cfg.Channels.Clear(m.ID, name); err != nil {
184 fmt.Fprintf(ch, "vole: %v\n", err)
185 return 1
186 }
187 fmt.Fprintf(ch, "cleared channel %s\n", name)
188 return 0
189 }
190 fmt.Fprintf(ch, "vole: unknown channel command %q; use list, set, or clear\n", op)
191 return 129
192 }
193
194 // channelTargetOK is the server's dangling check: an annotated tag whose object
195 // id is still the one the row recorded.
196 func channelTargetOK(g *git.Repo, ch channel.Channel) bool {
197 t, err := g.Tag(ch.Release)
198 return err == nil && t.Annotated && strings.EqualFold(t.OID, ch.ReleaseObject)
199 }