mirror of
https://github.com/slackhq/nebula.git
synced 2026-09-30 03:16:38 +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
168 lines
5.6 KiB
Go
168 lines
5.6 KiB
Go
package diag
|
|
|
|
import (
|
|
"bytes"
|
|
"flag"
|
|
"strings"
|
|
"testing"
|
|
|
|
"github.com/stretchr/testify/assert"
|
|
"github.com/stretchr/testify/require"
|
|
)
|
|
|
|
type testFlags struct {
|
|
Json bool
|
|
}
|
|
|
|
// testCommand builds a command carrying a flag set, recording what the callback was actually
|
|
// handed so a test can assert on it.
|
|
func testCommand(name string, seen *any, args *[]string) *Command {
|
|
return &Command{
|
|
Name: name,
|
|
ShortDescription: name + " short description",
|
|
Flags: func() (*flag.FlagSet, any) {
|
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
f := &testFlags{}
|
|
fl.BoolVar(&f.Json, "json", false, "outputs json")
|
|
return fl, f
|
|
},
|
|
Callback: func(fs any, a []string, w StringWriter) error {
|
|
if seen != nil {
|
|
*seen = fs
|
|
}
|
|
if args != nil {
|
|
*args = a
|
|
}
|
|
return w.WriteLine("ran " + name)
|
|
},
|
|
}
|
|
}
|
|
|
|
func newTestRegistry(t *testing.T) (*Registry, *bytes.Buffer, StringWriter) {
|
|
t.Helper()
|
|
buf := &bytes.Buffer{}
|
|
return NewRegistry(), buf, NewWriter(buf)
|
|
}
|
|
|
|
func TestRegistryDispatch(t *testing.T) {
|
|
t.Run("a new registry knows help and nothing else", func(t *testing.T) {
|
|
r, buf, w := newTestRegistry(t)
|
|
|
|
require.NoError(t, r.DispatchArgs([]string{"help"}, w))
|
|
assert.Contains(t, buf.String(), "help -")
|
|
})
|
|
|
|
t.Run("empty args dump the command list, matching an empty line on the console", func(t *testing.T) {
|
|
r, buf, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", nil, nil))
|
|
|
|
require.NoError(t, r.DispatchArgs(nil, w))
|
|
assert.Contains(t, buf.String(), "Available commands:")
|
|
assert.Contains(t, buf.String(), "do-thing - do-thing short description")
|
|
})
|
|
|
|
t.Run("an unknown command reports ErrUnknownCommand and still tells the user", func(t *testing.T) {
|
|
r, buf, w := newTestRegistry(t)
|
|
|
|
err := r.DispatchArgs([]string{"nope"}, w)
|
|
require.ErrorIs(t, err, ErrUnknownCommand)
|
|
assert.Contains(t, buf.String(), "Did not understand: nope")
|
|
assert.Contains(t, buf.String(), "Available commands:")
|
|
})
|
|
|
|
// This is the hazard the ctl transport has to preserve: every callback in ssh.go begins by
|
|
// type asserting fs to its own concrete flags struct. Reach a callback without going
|
|
// through Command.Flags and every one of them fails.
|
|
t.Run("a callback is handed the concrete struct its Flags callback returned", func(t *testing.T) {
|
|
var seen any
|
|
r, _, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", &seen, nil))
|
|
|
|
require.NoError(t, r.DispatchArgs([]string{"do-thing", "-json"}, w))
|
|
|
|
flags, ok := seen.(*testFlags)
|
|
require.True(t, ok, "callback was handed %T, not *testFlags", seen)
|
|
assert.True(t, flags.Json)
|
|
})
|
|
|
|
t.Run("positional arguments survive flag parsing", func(t *testing.T) {
|
|
var args []string
|
|
r, _, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", nil, &args))
|
|
|
|
require.NoError(t, r.DispatchArgs([]string{"do-thing", "-json", "10.0.0.1"}, w))
|
|
assert.Equal(t, []string{"10.0.0.1"}, args)
|
|
})
|
|
|
|
// Documents stdlib flag behaviour rather than endorsing it: parsing stops at the first
|
|
// positional, so a flag written after one is silently a positional too.
|
|
t.Run("a flag after a positional is not parsed as a flag", func(t *testing.T) {
|
|
var seen any
|
|
var args []string
|
|
r, _, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", &seen, &args))
|
|
|
|
require.NoError(t, r.DispatchArgs([]string{"do-thing", "10.0.0.1", "-json"}, w))
|
|
assert.False(t, seen.(*testFlags).Json)
|
|
assert.Equal(t, []string{"10.0.0.1", "-json"}, args)
|
|
})
|
|
|
|
t.Run("a bad flag reports ErrUsage and writes the usage text", func(t *testing.T) {
|
|
r, buf, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", nil, nil))
|
|
|
|
err := r.DispatchArgs([]string{"do-thing", "-nope"}, w)
|
|
require.ErrorIs(t, err, ErrUsage)
|
|
assert.Contains(t, buf.String(), "flag provided but not defined")
|
|
})
|
|
|
|
t.Run("-h anywhere routes to help instead of running the command", func(t *testing.T) {
|
|
var seen any
|
|
r, buf, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", &seen, nil))
|
|
|
|
require.NoError(t, r.DispatchArgs([]string{"do-thing", "-h"}, w))
|
|
assert.Nil(t, seen, "the callback should not have run")
|
|
assert.Contains(t, buf.String(), "do-thing - do-thing short description")
|
|
assert.Contains(t, buf.String(), "-json")
|
|
})
|
|
|
|
t.Run("Dispatch splits a line the way a shell would", func(t *testing.T) {
|
|
var args []string
|
|
r, _, w := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("do-thing", nil, &args))
|
|
|
|
require.NoError(t, r.Dispatch(`do-thing "/tmp/a path.pb.gz"`, w))
|
|
assert.Equal(t, []string{"/tmp/a path.pb.gz"}, args)
|
|
})
|
|
|
|
t.Run("Match returns names by prefix for tab completion", func(t *testing.T) {
|
|
r, _, _ := newTestRegistry(t)
|
|
r.RegisterCommand(testCommand("print-cert", nil, nil))
|
|
r.RegisterCommand(testCommand("print-tunnel", nil, nil))
|
|
r.RegisterCommand(testCommand("version", nil, nil))
|
|
|
|
assert.Equal(t, []string{"print-cert", "print-tunnel"}, r.Match("print-"))
|
|
})
|
|
}
|
|
|
|
// A clone is what keeps the ssh session's `logout` command from being visible to every other
|
|
// session, and to nebula ctl.
|
|
func TestRegistryCloneIsolation(t *testing.T) {
|
|
parent, _, w := newTestRegistry(t)
|
|
parent.RegisterCommand(testCommand("shared", nil, nil))
|
|
|
|
child := parent.Clone()
|
|
child.RegisterCommand(testCommand("logout", nil, nil))
|
|
|
|
require.NoError(t, child.DispatchArgs([]string{"logout"}, w))
|
|
|
|
buf := &bytes.Buffer{}
|
|
err := parent.DispatchArgs([]string{"logout"}, NewWriter(buf))
|
|
assert.ErrorIs(t, err, ErrUnknownCommand)
|
|
|
|
buf.Reset()
|
|
require.NoError(t, child.DispatchArgs([]string{"shared"}, NewWriter(buf)))
|
|
assert.True(t, strings.HasPrefix(buf.String(), "ran shared"))
|
|
}
|