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/server.go +106−16

@@ -8,6 +8,7 @@ import (
8 8 "log/slog"
9 9 "net"
10 10 "net/http"
11 "os"
11 12 "strconv"
12 13 "strings"
13 14 "time"
@@ -24,6 +25,7 @@ import (
24 25 "rodent.software/vole/forge/repo"
25 26 "rodent.software/vole/forge/secret"
26 27 "rodent.software/vole/forge/session"
28 "rodent.software/vole/forge/sshsession"
27 29 "rodent.software/vole/internal/compress"
28 30 "rodent.software/vole/server/githttp"
29 31 "rodent.software/vole/server/pages"
@@ -52,10 +54,13 @@ type Server struct {
52 54 Pages *pages.Server
53 55 Proxy *githttp.Proxy
54 56 SSH *ssh.Server
55 Web *web.Web
56 Log *slog.Logger
57 mux *http.ServeMux
58 acme *acmeSetup
57 // SSHSessions is the one-use token table behind the front door. It shares the
58 // instance database with the roster.
59 SSHSessions *sshsession.Store
60 Web *web.Web
61 Log *slog.Logger
62 mux *http.ServeMux
63 acme *acmeSetup
59 64 // metadataWake nudges the metadata event loop after a browser write. Pushes
60 65 // deliver the same information through the spool; this is the path a write made
61 66 // without a push uses.
@@ -113,7 +118,23 @@ func New(cfg *config.Config, binPath string, log *slog.Logger) (*Server, error)
113 118 ClientIPHeader: cfg.GitHTTPClientIPHeader,
114 119 RequestTimeout: time.Duration(cfg.GitHTTPTimeoutSeconds) * time.Second,
115 120 })
121 // The front door's tokens live in the instance database, beside the roster
122 // they answer for. Opening it here, rather than inside the SSH server, keeps
123 // one owner for the file and one place to close it.
124 sshSessions, err := sshsession.Open(cfg.InstanceDB())
125 if err != nil {
126 inst.Close()
127 return nil, fmt.Errorf("open ssh session tokens: %w", err)
128 }
129 // Tokens that expired while the front door was off are still rows. Dropping
130 // them at startup is what keeps a mode switch from leaving them behind, and it
131 // costs one indexed delete.
132 if _, err := sshSessions.Prune(time.Now().UTC()); err != nil {
133 log.Warn("cannot drop expired ssh session tokens", "err", err)
134 }
116 135 ssh, err := ssh.New(ssh.Config{
136 Sessions: sshSessions,
137 PublicHostKeyPath: cfg.SSHPublicHostKeyFile,
117 138 Identity: inst.Identity,
118 139 Repos: inst.Repos,
119 140 Instance: inst,
@@ -172,7 +193,7 @@ func New(cfg *config.Config, binPath string, log *slog.Logger) (*Server, error)
172 193 Cfg: cfg, Repos: inst.Repos, Identity: inst.Identity, Sessions: inst.Sessions,
173 194 Runs: inst.Runs, Index: inst.Index, Channels: inst.Channels, acme: acme,
174 195 LFS: inst.LFS, Locks: inst.Locks, PageViews: inst.PageViews, Secrets: inst.Secrets,
175 Proxy: proxy, SSH: ssh, Web: wp, Log: log,
196 Proxy: proxy, SSH: ssh, SSHSessions: sshSessions, Web: wp, Log: log,
176 197 metadataWake: make(chan struct{}, 1),
177 198 InstanceRepo: inst.InstanceRepo,
178 199 }
@@ -286,13 +307,18 @@ func (s *Server) drain() {
286 307 // HTTPS, the HTTP address only answers ACME challenges and redirects, and the
287 308 // forge is served over TLS on HTTPSAddr.
288 309 func (s *Server) Run(ctx context.Context) error {
289 sshLn, err := net.Listen("tcp", s.Cfg.SSHAddr)
310 sshLn, err := s.sshListener()
290 311 if err != nil {
291 return fmt.Errorf("ssh listen %s: %w", s.Cfg.SSHAddr, err)
312 return err
313 }
314 closeSSH := func() {
315 if sshLn != nil {
316 sshLn.Close()
317 }
292 318 }
293 319 httpLn, err := net.Listen("tcp", s.Cfg.HTTPAddr)
294 320 if err != nil {
295 sshLn.Close()
321 closeSSH()
296 322 return fmt.Errorf("http listen %s: %w", s.Cfg.HTTPAddr, err)
297 323 }
298 324
@@ -318,7 +344,7 @@ func (s *Server) Run(ctx context.Context) error {
318 344 httpsLn, err = net.Listen("tcp", s.Cfg.HTTPSAddr)
319 345 if err != nil {
320 346 httpLn.Close()
321 sshLn.Close()
347 closeSSH()
322 348 return fmt.Errorf("https listen %s: %w", s.Cfg.HTTPSAddr, err)
323 349 }
324 350 httpsSrv = &http.Server{
@@ -332,11 +358,19 @@ func (s *Server) Run(ctx context.Context) error {
332 358 }
333 359
334 360 errCh := make(chan error, 3)
335 go func() {
336 if err := s.SSH.Serve(sshLn); err != nil {
337 errCh <- fmt.Errorf("ssh serve: %w", err)
338 }
339 }()
361 if sshLn != nil {
362 go func() {
363 // The front door takes the same server and the same handlers; only the
364 // transport and the authentication in front of it differ.
365 serve := s.SSH.Serve
366 if s.Cfg.SSHListen == config.SSHListenSocket {
367 serve = s.SSH.ServeLocal
368 }
369 if err := serve(sshLn); err != nil {
370 errCh <- fmt.Errorf("ssh serve: %w", err)
371 }
372 }()
373 }
340 374 go func() {
341 375 if err := httpSrv.Serve(httpLn); err != nil && !errors.Is(err, http.ErrServerClosed) {
342 376 errCh <- fmt.Errorf("http serve: %w", err)
@@ -350,11 +384,15 @@ func (s *Server) Run(ctx context.Context) error {
350 384 }()
351 385 }
352 386
387 sshDesc := "none"
388 if sshLn != nil {
389 sshDesc = sshLn.Addr().String()
390 }
353 391 if httpsSrv != nil {
354 s.Log.Info("listening", "ssh", sshLn.Addr().String(), "http", httpLn.Addr().String(),
392 s.Log.Info("listening", "ssh", sshDesc, "ssh_mode", s.Cfg.SSHListen, "http", httpLn.Addr().String(),
355 393 "https", httpsLn.Addr().String(), "data", s.Cfg.DataDir, "users", s.Identity.Snapshot().Count)
356 394 } else {
357 s.Log.Info("listening", "ssh", sshLn.Addr().String(), "http", httpLn.Addr().String(),
395 s.Log.Info("listening", "ssh", sshDesc, "ssh_mode", s.Cfg.SSHListen, "http", httpLn.Addr().String(),
358 396 "data", s.Cfg.DataDir, "users", s.Identity.Snapshot().Count)
359 397 }
360 398
@@ -381,6 +419,55 @@ func (s *Server) Run(ctx context.Context) error {
381 419 }
382 420 }
383 421
422 // sshListener opens the socket the configured SSH mode binds: the built-in
423 // listener on ssh_addr, the front door's Unix socket, or nothing at all.
424 func (s *Server) sshListener() (net.Listener, error) {
425 switch s.Cfg.SSHListen {
426 case config.SSHListenNone:
427 return nil, nil
428 case config.SSHListenSocket:
429 return listenFrontDoor(s.Cfg.SSHSocket)
430 default:
431 ln, err := net.Listen("tcp", s.Cfg.SSHAddr)
432 if err != nil {
433 return nil, fmt.Errorf("ssh listen %s: %w", s.Cfg.SSHAddr, err)
434 }
435 return ln, nil
436 }
437 }
438
439 // listenFrontDoor opens the Unix socket the forced command connects to.
440 //
441 // A socket file outlives the process that made it, so a stale one is removed —
442 // but only after checking that nothing answers on it, because removing a live
443 // socket would leave a running instance unreachable.
444 func listenFrontDoor(path string) (net.Listener, error) {
445 if st, err := os.Lstat(path); err == nil {
446 if st.Mode()&os.ModeSocket == 0 {
447 return nil, fmt.Errorf("ssh socket %s exists and is not a socket", path)
448 }
449 conn, dialErr := net.DialTimeout("unix", path, time.Second)
450 if dialErr == nil {
451 conn.Close()
452 return nil, fmt.Errorf("ssh socket %s is in use by another vole serve", path)
453 }
454 if err := os.Remove(path); err != nil {
455 return nil, fmt.Errorf("ssh socket %s is stale and cannot be removed: %w", path, err)
456 }
457 }
458 ln, err := net.Listen("unix", path)
459 if err != nil {
460 return nil, fmt.Errorf("ssh socket %s: %w", path, err)
461 }
462 // The service account creates it; the `git` group sshd runs the forced command
463 // as connects to it. Nobody else on the host gets either.
464 if err := os.Chmod(path, 0o660); err != nil {
465 ln.Close()
466 return nil, fmt.Errorf("ssh socket %s: %w", path, err)
467 }
468 return ln, nil
469 }
470
384 471 func (s *Server) watchRoster(ctx context.Context) {
385 472 t := time.NewTicker(5 * time.Second)
386 473 defer t.Stop()
@@ -422,6 +509,9 @@ func (s *Server) Close() error {
422 509 return err
423 510 }
424 511 }
512 if s.SSHSessions != nil {
513 _ = s.SSHSessions.Close()
514 }
425 515 if s.Sessions != nil {
426 516 _ = s.Sessions.Close()
427 517 }