mirror of
https://github.com/slackhq/nebula.git
synced 2026-09-30 09:36:37 +02:00
Every diagnostic command nebula has was reachable through exactly one door:
the built-in ssh debug server. That server is off by default, and turning it
on means generating a host key, writing an sshd block with authorized public
keys, and SIGHUPing the daemon. That is a lot of ceremony to answer "what
version is this node running".
Nebula now serves the same commands over a local unix socket, enabled by
default, and `nebula ctl <command>` runs them. The socket lives in a 0700
directory so filesystem permissions are the access control; no keys, nothing
on the network. Failing to create it is logged and never blocks startup.
The command registry was already transport neutral, so this is mostly new
transport rather than new commands:
- diag/ holds the registry, dispatch, writer and wire protocol, moved out
of sshd because none of it was ever about ssh. sshd and ctl.go dispatch
against one shared registry.
- commands.go holds every command implementation, moved out of ssh.go
(which was 85% not ssh) and renamed off the ssh prefix. Adding a command
there makes it available over both transports.
- ssh.go keeps only host keys, authorized users, and the listen address.
- ctl.go supervises the socket, following the statsServer lifecycle shape.
The wire protocol frames the response rather than terminating it, because
print-cert -raw and list-hostmap -json both emit arbitrary bytes that no
sentinel could safely delimit. argv travels as a list so quoting survives.
Exit statuses are real: 0, 2 for usage, 127 for an unknown command.
Two things fall out. The ssh console now reports a real exit status instead
of a hardcoded zero, so `ssh host list-hostmap` is scriptable too. And eight
command callbacks that silently returned nil on a flags type mismatch now
report it, which the exit status makes visible.
Windows is a stub returning a clear "not supported" until it gets a named
pipe with a security descriptor; iOS and Android are never enabled, having no
daemon for a CLI to attach to.
Breaking for embedders of the sshd package: NewSSHServer takes a
*diag.Registry, SSHServer.RegisterCommand is gone in favor of registering on
that registry, and the command types live in diag rather than sshd.
Co-Authored-By: Claude Opus 5 (1M context) <noreply@anthropic.com>
Claude-Session: https://claude.ai/code/session_014fya5fTXGiwX72FUmoL9y3
145 lines
4.5 KiB
Go
145 lines
4.5 KiB
Go
package diag
|
|
|
|
import (
|
|
"bufio"
|
|
"bytes"
|
|
"encoding/binary"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
func TestRequestRoundTrip(t *testing.T) {
|
|
t.Run("argv survives a round trip, spaces and all", func(t *testing.T) {
|
|
buf := &bytes.Buffer{}
|
|
args := []string{"start-cpu-profile", "/tmp/a path.pb.gz", "-json"}
|
|
require.NoError(t, writeRequest(buf, args))
|
|
|
|
req, err := readRequest(bufio.NewReader(buf))
|
|
require.NoError(t, err)
|
|
assert.Equal(t, ProtoVersion, req.Version)
|
|
assert.Equal(t, args, req.Args)
|
|
})
|
|
|
|
t.Run("an unknown version is refused by name", func(t *testing.T) {
|
|
r := bufio.NewReader(strings.NewReader(`{"version":99,"args":["version"]}` + "\n"))
|
|
|
|
_, err := readRequest(r)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "unsupported protocol version 99")
|
|
})
|
|
|
|
t.Run("malformed json is refused", func(t *testing.T) {
|
|
r := bufio.NewReader(strings.NewReader("not json\n"))
|
|
|
|
_, err := readRequest(r)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "malformed request")
|
|
})
|
|
|
|
t.Run("a line without a newline is bounded rather than buffered forever", func(t *testing.T) {
|
|
r := bufio.NewReader(strings.NewReader(strings.Repeat("a", maxRequest+10)))
|
|
|
|
_, err := readRequest(r)
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "without a newline")
|
|
})
|
|
}
|
|
|
|
func TestResponseRoundTrip(t *testing.T) {
|
|
t.Run("output and status survive a round trip", func(t *testing.T) {
|
|
wire := &bytes.Buffer{}
|
|
w := bufio.NewWriterSize(&frameWriter{w: wire}, outputBuffer)
|
|
require.NoError(t, NewWriter(w).WriteLine("hello"))
|
|
require.NoError(t, w.Flush())
|
|
require.NoError(t, writeEnd(wire, StatusOK, ""))
|
|
|
|
out := &bytes.Buffer{}
|
|
status, err := readResponse(wire, out)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, StatusOK, status)
|
|
assert.Equal(t, "hello\n", out.String())
|
|
})
|
|
|
|
// print-cert -raw and list-hostmap -json both emit arbitrary bytes, so a payload larger
|
|
// than one frame has to reassemble exactly.
|
|
t.Run("a payload larger than one frame reassembles byte for byte", func(t *testing.T) {
|
|
big := bytes.Repeat([]byte("nebula"), maxFrame)
|
|
|
|
wire := &bytes.Buffer{}
|
|
fw := &frameWriter{w: wire}
|
|
n, err := fw.Write(big)
|
|
require.NoError(t, err)
|
|
require.Equal(t, len(big), n)
|
|
require.NoError(t, writeEnd(wire, StatusOK, ""))
|
|
|
|
out := &bytes.Buffer{}
|
|
status, err := readResponse(wire, out)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, StatusOK, status)
|
|
assert.Equal(t, big, out.Bytes())
|
|
})
|
|
|
|
t.Run("a non-zero status carries its message", func(t *testing.T) {
|
|
wire := &bytes.Buffer{}
|
|
require.NoError(t, writeEnd(wire, StatusError, "it went wrong"))
|
|
|
|
status, err := readResponse(wire, &bytes.Buffer{})
|
|
require.Error(t, err)
|
|
assert.Equal(t, StatusError, status)
|
|
assert.Contains(t, err.Error(), "it went wrong")
|
|
})
|
|
|
|
// This is how the CLI notices a nebula that died mid-command rather than silently
|
|
// reporting whatever partial output it managed to read.
|
|
t.Run("a stream ending without an end frame is truncated, not successful", func(t *testing.T) {
|
|
wire := &bytes.Buffer{}
|
|
_, err := (&frameWriter{w: wire}).Write([]byte("partial"))
|
|
require.NoError(t, err)
|
|
|
|
out := &bytes.Buffer{}
|
|
_, err = readResponse(wire, out)
|
|
assert.ErrorIs(t, err, ErrTruncated)
|
|
})
|
|
|
|
t.Run("a truncated frame header is truncated, not successful", func(t *testing.T) {
|
|
_, err := readResponse(bytes.NewReader([]byte{frameOutput, 0x00}), &bytes.Buffer{})
|
|
assert.ErrorIs(t, err, ErrTruncated)
|
|
})
|
|
|
|
t.Run("an oversized frame is refused rather than allocated", func(t *testing.T) {
|
|
var hdr [5]byte
|
|
hdr[0] = frameOutput
|
|
binary.BigEndian.PutUint32(hdr[1:], maxFrame+1)
|
|
|
|
_, err := readResponse(bytes.NewReader(hdr[:]), &bytes.Buffer{})
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "exceeds")
|
|
})
|
|
|
|
// A reserved frame an older client does not understand must not break it.
|
|
t.Run("a reserved frame type is skipped", func(t *testing.T) {
|
|
wire := &bytes.Buffer{}
|
|
require.NoError(t, writeFrame(wire, frameStderr, []byte("future")))
|
|
require.NoError(t, writeFrame(wire, frameOutput, []byte("now")))
|
|
require.NoError(t, writeEnd(wire, StatusOK, ""))
|
|
|
|
out := &bytes.Buffer{}
|
|
status, err := readResponse(wire, out)
|
|
require.NoError(t, err)
|
|
assert.Equal(t, StatusOK, status)
|
|
assert.Equal(t, "now", out.String())
|
|
})
|
|
|
|
t.Run("an unknown frame type is an error", func(t *testing.T) {
|
|
wire := &bytes.Buffer{}
|
|
require.NoError(t, writeFrame(wire, 0x7f, nil))
|
|
|
|
_, err := readResponse(wire, &bytes.Buffer{})
|
|
require.Error(t, err)
|
|
assert.Contains(t, err.Error(), "unknown frame type")
|
|
})
|
|
}
|