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 HTTPS on macOS with socket activation
54ccf66e63032dd8e5afd80f609d6e6be183e5f5 · Keenan Nemetz · 3d ago · parent 75478a9d
Serve HTTPS on macOS with socket activation Let vole serve accept listeners the service manager already owns, so a launchd LaunchDaemon can own ports 80/443 as root and hand the ready sockets to an unprivileged serve for ACME. Reads LISTEN_FDS/LISTEN_PID (and launchd's Listeners), defaults the platform contract to the host, and falls back to binding http_addr/https_addr when no descriptors are inherited. Add unit tests for the env contract and fd wrapping, plus an integration test that execs the real binary with an inherited fd and completes a request. Document the socket-activation choice in the macOS deploy guide.
server/socketactivation_test.go +88
| @@ -0,0 +1,88 @@ | |||
| 1 | package server | ||
| 2 | |||
| 3 | import ( | ||
| 4 | "net" | ||
| 5 | "os" | ||
| 6 | "strconv" | ||
| 7 | "strings" | ||
| 8 | "testing" | ||
| 9 | ) | ||
| 10 | |||
| 11 | func fakeEnv(vars map[string]string) func(string) string { | ||
| 12 | return func(k string) string { return vars[k] } | ||
| 13 | } | ||
| 14 | |||
| 15 | func TestActivationEnvReadsBothContracts(t *testing.T) { | ||
| 16 | cases := []struct { | ||
| 17 | name string | ||
| 18 | env map[string]string | ||
| 19 | want int | ||
| 20 | }{ | ||
| 21 | {"no activation", map[string]string{}, 0}, | ||
| 22 | {"systemd one fd", map[string]string{"LISTEN_PID": "42", "LISTEN_FDS": "1"}, 1}, | ||
| 23 | {"systemd two fds", map[string]string{"LISTEN_PID": "42", "LISTEN_FDS": "2"}, 2}, | ||
| 24 | {"launchd socket var", map[string]string{"LAUNCHD_SOCKET": "42", "LISTEN_FDS": "2"}, 2}, | ||
| 25 | {"no owner guard", map[string]string{"LISTEN_FDS": "2"}, 2}, | ||
| 26 | {"foreign owner", map[string]string{"LISTEN_PID": "7", "LISTEN_FDS": "2"}, 0}, | ||
| 27 | {"garbage count", map[string]string{"LISTEN_PID": "42", "LISTEN_FDS": "x"}, 0}, | ||
| 28 | {"negative count", map[string]string{"LISTEN_PID": "42", "LISTEN_FDS": "-1"}, 0}, | ||
| 29 | } | ||
| 30 | for _, tc := range cases { | ||
| 31 | if got := activationEnv(fakeEnv(tc.env), 42); got != tc.want { | ||
| 32 | t.Errorf("%s: activationEnv = %d, want %d", tc.name, got, tc.want) | ||
| 33 | } | ||
| 34 | } | ||
| 35 | } | ||
| 36 | |||
| 37 | func TestInheritedListenerWrapsARealDescriptor(t *testing.T) { | ||
| 38 | ln, err := net.Listen("tcp", "127.0.0.1:0") | ||
| 39 | if err != nil { | ||
| 40 | t.Fatal(err) | ||
| 41 | } | ||
| 42 | defer ln.Close() | ||
| 43 | file, err := ln.(*net.TCPListener).File() | ||
| 44 | if err != nil { | ||
| 45 | t.Fatal(err) | ||
| 46 | } | ||
| 47 | // File() gives us a descriptor with an arbitrary number; inheritedListener is | ||
| 48 | // agnostic to the number, so exercise it directly without touching fd 3. | ||
| 49 | wrapped, err := inheritedListener(file.Fd(), "http") | ||
| 50 | if err != nil { | ||
| 51 | t.Fatalf("inheritedListener: %v", err) | ||
| 52 | } | ||
| 53 | defer wrapped.Close() | ||
| 54 | |||
| 55 | conn, err := net.Dial("tcp", wrapped.Addr().String()) | ||
| 56 | if err != nil { | ||
| 57 | t.Fatalf("dial wrapped listener %s: %v", wrapped.Addr(), err) | ||
| 58 | } | ||
| 59 | conn.Close() | ||
| 60 | } | ||
| 61 | |||
| 62 | func TestHTTPListenersBindWhenNotActivated(t *testing.T) { | ||
| 63 | httpLn, httpsLn, err := httpListeners(fakeEnv(nil), os.Getpid(), "127.0.0.1:0", "", false) | ||
| 64 | if err != nil { | ||
| 65 | t.Fatalf("bind fallback: %v", err) | ||
| 66 | } | ||
| 67 | defer httpLn.Close() | ||
| 68 | if httpsLn != nil { | ||
| 69 | t.Fatalf("no-TLS fallback returned an https listener: %v", httpsLn) | ||
| 70 | } | ||
| 71 | if !strings.Contains(httpLn.Addr().String(), "127.0.0.1") { | ||
| 72 | t.Errorf("fallback listener bound to %q, want the configured address", httpLn.Addr()) | ||
| 73 | } | ||
| 74 | } | ||
| 75 | |||
| 76 | func TestHTTPListenersIgnoreForeignPIDs(t *testing.T) { | ||
| 77 | // LISTEN_PID names another process; serve must not touch the descriptors it | ||
| 78 | // was not handed, so it falls back to binding its own address. | ||
| 79 | env := map[string]string{"LISTEN_PID": strconv.Itoa(os.Getpid() + 1), "LISTEN_FDS": "2"} | ||
| 80 | httpLn, _, err := httpListeners(fakeEnv(env), os.Getpid(), "127.0.0.1:0", "", false) | ||
| 81 | if err != nil { | ||
| 82 | t.Fatalf("foreign owner should fall back to binding: %v", err) | ||
| 83 | } | ||
| 84 | defer httpLn.Close() | ||
| 85 | if !strings.Contains(httpLn.Addr().String(), "127.0.0.1") { | ||
| 86 | t.Errorf("foreign-owner listener bound to %q, want a fresh bind", httpLn.Addr()) | ||
| 87 | } | ||
| 88 | } | ||