Add a local control socket and nebula ctl (#1975)

Add support for accessing the previously ssh-only debug commands via
`nebula ctl <command>.`

- unix socket (named pipe on windows) for communication
- sockets scoped to the user + an optional group
- in cmd/nebula and cmd/nebula-service, but not nebula.Main

Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
This commit is contained in:
Matt RichardsonandClaude Fable 5.1 authored and GitHub committed 2026-10-21 11:16:00 -04:00
1 parent 9a5bdb508e
commit 0f767ce529
54 files changed
+7581 -925

No files matched your search

+22 -1
View File
@@ -8,7 +8,9 @@ import (
"strings"
"github.com/slackhq/nebula"
"github.com/slackhq/nebula/cmd/internal/ctl"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/localapi"
"github.com/slackhq/nebula/logging"
"github.com/slackhq/nebula/util"
)
@@ -32,6 +34,11 @@ func init() {
}
func main() {
// commands are either `nebula ctl ...` or `nebula -<flag> ...`
if code, done := ctl.Subcommand(os.Args); done {
os.Exit(code)
}
serviceFlag := flag.String("service", "", "Control the system service.")
configPath := flag.String("config", "", "Path to either a file or directory to load configuration from")
configTest := flag.Bool("test", false, "Test the config and print the end result. Non zero exit indicates a faulty config")
@@ -40,6 +47,12 @@ func main() {
flag.Parse()
// A word after the daemon's flags is `ctl` in the other place an operator puts it,
// or a mistyped subcommand, and either way not a daemon to start; see Trailing.
if code, done := ctl.Trailing(flag.Args(), *configPath); done {
os.Exit(code)
}
if *printVersion {
fmt.Printf("Version: %s\n", Build)
os.Exit(0)
@@ -104,9 +117,17 @@ func main() {
os.Exit(1)
}
ctl := localapi.Wire(ctrl.Context(), l, c, ctrl.Commands())
go ctrl.ShutdownBlock()
if err := ctrl.Wait(); err != nil {
err := ctrl.Wait()
// os.Exit skips deferred calls, so the socket is removed here, before either exit
// below, rather than left to a deferred Stop that would never run.
ctl.Stop()
if err != nil {
l.Error("Nebula stopped due to fatal error", "error", err)
os.Exit(2)
}
+13
View File
@@ -8,6 +8,7 @@ import (
"github.com/kardianos/service"
"github.com/slackhq/nebula"
"github.com/slackhq/nebula/config"
"github.com/slackhq/nebula/localapi"
"github.com/slackhq/nebula/logging"
)
@@ -17,6 +18,7 @@ type program struct {
configPath *string
build string
control *nebula.Control
ctl *localapi.Server
}
func (p *program) Start(s service.Service) error {
@@ -50,10 +52,16 @@ func (p *program) Start(s service.Service) error {
return err
}
// Now that we're started, stand up the local control socket.
p.ctl = localapi.Wire(p.control.Context(), l, c, p.control.Commands())
// Nebula can stop itself on a fatal packet reader error, make sure to log it if it happens.
go func() {
if err := p.control.Wait(); err != nil {
logger.Error(fmt.Sprintf("Nebula stopped due to fatal error: %v", err))
// os.Exit skips deferred calls and races the socket's own context watcher,
// so the socket is removed here, as cmd/nebula does.
p.ctl.Stop()
os.Exit(2)
}
}()
@@ -63,6 +71,11 @@ func (p *program) Start(s service.Service) error {
func (p *program) Stop(s service.Service) error {
logger.Info("Nebula service stopping.")
if p.ctl != nil {
p.ctl.Stop()
}
if p.control == nil {
return nil
}