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.

Serve the front door from vole serve

c5f2c5d49be1d35a0a54fd70029ac9311647194e · Keenan Nemetz · 16d ago · parent e075258f

Serve the front door from vole serve

The forced command had nothing to talk to. This gives `vole serve` the local
listener, the token it consumes, and the host key a lease now carries.

`ssh_listen` decides what serves SSH and is the only switch: "tcp" is the
built-in listener on `ssh_addr` (the default, and unchanged), "socket" is the
system sshd in front, "none" serves nothing. The socket path stays in
`ssh_socket`, which #304 already added, so no value lives in two places. In
socket mode `ssh_addr` only names the host and port clone URLs carry, which
validation now enforces by refusing another port: the point of the mode is a
clone URL with no port, and a leftover 2222 would silently break every link.

The local listener speaks the framed protocol from `internal/sshwire` and turns
it back into the Session the built-in listener produces, so `handleLocal` calls
the same `exec` with the account and key from the token row and the same ACL,
credential-scoping, and refusal code runs. A test proves it: every kind of
handler — whoami, roster, both git pack services, the admin API, the run queue,
release channels, a refused verb, and a job credential that may do less than its
account — is driven over both transports and compared byte for byte, status
included.

A front-door instance has no signer, so `New` no longer creates a private host
key it would never present: it reads sshd's public key from
`ssh_public_host_key_file` into the same field a lease carries. A missing or
unreadable file fails startup rather than handing a job a key that cannot verify
its host, and `Serve` refuses outright when there is no host key.

The transport's own details: a write larger than a frame is split rather than
refused, stdout and stderr share one socket under one lock, expired tokens are
swept every thirty seconds and once at startup so a mode switch leaves nothing
behind, and the socket is created 0660 with a stale file replaced only after
checking that nothing answers on it.

`ssh_user` replaces the hardcoded `git` in generated clone URLs, which is what
lets a host with another forge's `git` account keep both.

Packaging and the sshd drop-in are #306; the documentation and migration are
#307.

server/ssh/server.go +82−13

@@ -12,6 +12,7 @@ import (
12 12 "crypto/rand"
13 13 "crypto/x509"
14 14 "encoding/pem"
15 "errors"
15 16 "fmt"
16 17 "io"
17 18 "log/slog"
@@ -35,6 +36,7 @@ import (
35 36 "rodent.software/vole/forge/channel"
36 37 "rodent.software/vole/forge/identity"
37 38 "rodent.software/vole/forge/repo"
39 "rodent.software/vole/forge/sshsession"
38 40 )
39 41
40 42 const serverVersion = "SSH-2.0-vole"
@@ -119,13 +121,23 @@ type Config struct {
119 121 // HandshakeTimeout bounds how long a connection may take to complete the SSH
120 122 // handshake. Zero uses the default.
121 123 HandshakeTimeout time.Duration
124 // Sessions is the table of one-use tokens the front door consumes. Nil means
125 // this server takes no local requests, which is the direct mode's answer.
126 Sessions *sshsession.Store
127 // PublicHostKeyPath is sshd's public host key, in the front door's mode. When
128 // it is set, a lease carries that key and this process creates no private host
129 // key of its own: there is no listener here to present one on.
130 PublicHostKeyPath string
122 131 }
123 132
124 133 type Server struct {
125 134 cfg Config
126 135 sshCfg *xssh.ServerConfig
127 136 mu sync.Mutex
128 ln net.Listener
137 // listeners are the sockets this server accepts on: one, unless a caller is
138 // running the built-in listener and the front door side by side. Close ends
139 // all of them, and removes the file behind a Unix socket.
140 listeners []net.Listener
129 141
130 142 // conns holds one token per open connection, and gitSlots one per running git
131 143 // process. Both are channels rather than counters because taking a token is
@@ -147,6 +159,9 @@ type Server struct {
147 159 // entry it needs without trusting whatever answers a scan.
148 160 hostKey string
149 161
162 // sweepOnce keeps one token sweeper per server, however many listeners ask
163 // for one.
164 sweepOnce sync.Once
150 165 // refused throttles the "listener is full" warning. A flood is exactly when the
151 166 // log must not become the next unbounded resource.
152 167 refusedMu sync.Mutex
@@ -185,22 +200,39 @@ func (c Config) maxGitProcesses() int {
185 200 }
186 201
187 202 // New loads or creates the host key and prepares the SSH config.
203 //
204 // A front-door instance (PublicHostKeyPath set) has no SSH listener of its own:
205 // sshd authenticates, and the forced command reaches this process over a Unix
206 // socket that speaks a local protocol. In that mode the host key is sshd's
207 // public half, read here so a lease can hand a job the key it will actually see.
208 // Creating a private key nobody presents would be a secret with no purpose.
188 209 func New(cfg Config) (*Server, error) {
189 210 if cfg.Log == nil {
190 211 cfg.Log = slog.Default()
191 212 }
192 signer, err := loadOrCreateHostKey(cfg.HostKeyPath, cfg.Log)
193 if err != nil {
194 return nil, err
195 }
196 213 s := &Server{
197 214 cfg: cfg,
198 215 conns: make(chan struct{}, cfg.maxConnections()),
199 216 gitSlots: make(chan struct{}, cfg.maxGitProcesses()),
200 217 done: make(chan struct{}),
201 218 live: make(map[net.Conn]struct{}),
202 hostKey: strings.TrimSpace(string(xssh.MarshalAuthorizedKey(signer.PublicKey()))),
203 219 }
220 var signer xssh.Signer
221 if cfg.PublicHostKeyPath != "" {
222 line, err := readPublicHostKey(cfg.PublicHostKeyPath)
223 if err != nil {
224 return nil, err
225 }
226 s.hostKey = line
227 } else {
228 var err error
229 signer, err = loadOrCreateHostKey(cfg.HostKeyPath, cfg.Log)
230 if err != nil {
231 return nil, err
232 }
233 s.hostKey = strings.TrimSpace(string(xssh.MarshalAuthorizedKey(signer.PublicKey())))
234 }
235
204 236 s.sshCfg = &xssh.ServerConfig{
205 237 ServerVersion: serverVersion,
206 238 MaxAuthTries: 6,
@@ -230,10 +262,30 @@ func New(cfg Config) (*Server, error) {
230 262 return &xssh.Permissions{Extensions: ext}, nil
231 263 },
232 264 }
233 s.sshCfg.AddHostKey(signer)
265 if signer != nil {
266 s.sshCfg.AddHostKey(signer)
267 } else {
268 s.sshCfg = nil
269 }
234 270 return s, nil
235 271 }
236 272
273 // readPublicHostKey turns sshd's public host key file into the one
274 // authorized_keys line a lease carries. A file that is missing or unreadable is a
275 // startup failure: a job handed a key that cannot verify its host fails at its
276 // first push, which is the worst place to learn it.
277 func readPublicHostKey(path string) (string, error) {
278 data, err := os.ReadFile(path)
279 if err != nil {
280 return "", fmt.Errorf("ssh: read the public host key: %w", err)
281 }
282 pub, _, _, _, err := xssh.ParseAuthorizedKey(data)
283 if err != nil {
284 return "", fmt.Errorf("ssh: %s is not an SSH public key: %w", path, err)
285 }
286 return strings.TrimSpace(string(xssh.MarshalAuthorizedKey(pub))), nil
287 }
288
237 289 func (s *Server) ListenAndServe(addr string) error {
238 290 ln, err := net.Listen("tcp", addr)
239 291 if err != nil {
@@ -242,10 +294,20 @@ func (s *Server) ListenAndServe(addr string) error {
242 294 return s.Serve(ln)
243 295 }
244 296
245 // Serve accepts connections until the listener is closed.
297 // Serve accepts SSH connections until the listener is closed.
246 298 func (s *Server) Serve(ln net.Listener) error {
299 if s.sshCfg == nil {
300 return errors.New("ssh: this instance serves the front door and accepts no SSH connections")
301 }
302 return s.accept(ln, s.handleConn)
303 }
304
305 // accept is the listener loop both transports share: the connection cap, the
306 // per-connection accounting, and the shutdown wait are the same work whether a
307 // connection starts with an SSH handshake or a framed request.
308 func (s *Server) accept(ln net.Listener, handle func(net.Conn)) error {
247 309 s.mu.Lock()
248 s.ln = ln
310 s.listeners = append(s.listeners, ln)
249 311 s.mu.Unlock()
250 312 for {
251 313 conn, err := ln.Accept()
@@ -275,7 +337,7 @@ func (s *Server) Serve(ln net.Listener) error {
275 337 defer func() { <-s.conns }()
276 338 defer s.handlers.Done()
277 339 defer s.forget(c)
278 s.handleConn(c)
340 handle(c)
279 341 }(conn)
280 342 }
281 343 }
@@ -356,7 +418,7 @@ func (s *Server) Close() error {
356 418 return nil
357 419 }
358 420 s.closing = true
359 ln := s.ln
421 listeners := append([]net.Listener(nil), s.listeners...)
360 422 live := make([]net.Conn, 0, len(s.live))
361 423 for c := range s.live {
362 424 live = append(live, c)
@@ -365,8 +427,15 @@ func (s *Server) Close() error {
365 427
366 428 close(s.done)
367 429 var err error
368 if ln != nil {
369 err = ln.Close()
430 for _, ln := range listeners {
431 if cerr := ln.Close(); cerr != nil && err == nil {
432 err = cerr
433 }
434 // net.Listen leaves a Unix socket file behind, and the next start has to
435 // find the path free.
436 if ua, ok := ln.Addr().(*net.UnixAddr); ok {
437 _ = os.Remove(ua.Name)
438 }
370 439 }
371 440 for _, c := range live {
372 441 _ = c.Close()