mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-15 10:16:52 +01:00
smoke-extra / freebsd-amd64 (push) Failing after 14s
smoke-extra / linux-amd64-ipv6disable (push) Failing after 13s
smoke-extra / netbsd-amd64 (push) Failing after 14s
smoke-extra / openbsd-amd64 (push) Failing after 15s
smoke-extra / linux-386 (push) Failing after 14s
smoke / Run multi node smoke test (push) Failing after 1m46s
Build and test / Static checks (push) Successful in 2m38s
Build and test / Test linux (push) Failing after 1m12s
Build and test / Test linux-pkcs11 (push) Failing after 2m6s
Build and test / Test linux-boringcrypto (push) Failing after 3m0s
Build and test / Test linux-fips140 (push) Failing after 2m56s
Build and test / Cross-build linux-arm (push) Successful in 3m10s
Build and test / Cross-build linux-mips (push) Successful in 3m55s
Build and test / Cross-build linux-other (push) Successful in 3m15s
Build and test / Cross-build windows (push) Successful in 1m5s
Build and test / Cross-build freebsd (push) Successful in 1m37s
Build and test / Cross-build netbsd (push) Successful in 1m36s
Build and test / Cross-build openbsd (push) Successful in 1m35s
Build and test / Cross-build mobile (push) Successful in 3m32s
smoke-extra / Run windows smoke test (push) Canceled after 0s
smoke / Run self traffic smoke test on macOS (push) Canceled after 0s
Build and test / Test macos (push) Canceled after 0s
Build and test / Test windows (push) Canceled after 0s
Build and test / CI status (push) Canceled after 0s
Report ssh command failures to the surface (#1959) The ssh debug commands told the user when something went wrong but never told the surface that ran them, so `ssh host cmd` always exited 0 and a few failures were not reported at all. A failed command now returns its error, a failure the user has already heard about is a commands.Failure, and the ssh exec path exits 1 for either. The command bodies also lose their ssh prefixes and the duplication they carried over from ssh.go; both goldens pin that nothing else changed. - Dispatch returns the callback's error, or shlex's; the ssh session logs it at debug, and not at all for a Failure the user already saw - commands.Fail writes a message and returns it as a Failure in one step; an unknown command, `help` for a command that is not there, a failed reload and an unbalanced quote are failures - ssh exec exit-status is 1 for a command, flag or failure error, 0 otherwise; the e2e golden records the exit status for every case - Clone rebinds help to the copy, so an ssh session's help lists logout and `help logout` and `logout -h` answer; sshd.LogoutCommand is the session's logout, exported for tests - "did not understand" names the first word only, the one looked up; DispatchArgs takes just the tokens - log-format writes its refusal to the user, as log-level does - print-relays emits the relay error as a string and omits the key when there is none, where it printed {} or "Error":null - list-hostmap and list-lighthouse-addrmap -json return the encode error; print-cert reports a certificate that will not marshal - a flags type that is not the one registered is an internal error, not a silent success - command functions and flag types are <name>Command and <name>Flags; parseVpnAddr and listHostMapFlagSet replace the repeated parse block and flag set; close-tunnel sends through sendCloseTunnel Co-Authored-By: Claude Fable 5.1 <noreply@anthropic.com>
182 lines
4.6 KiB
Go
182 lines
4.6 KiB
Go
package commands
|
|
|
|
import (
|
|
"cmp"
|
|
"fmt"
|
|
"maps"
|
|
"slices"
|
|
"strings"
|
|
"sync"
|
|
|
|
"github.com/anmitsu/go-shlex"
|
|
)
|
|
|
|
// Registry holds a set of commands. New is the only way to make one.
|
|
//
|
|
// It is safe for concurrent use. Commands may be registered while a surface is serving
|
|
// from the registry: the ssh server clones it per session, and a clone may overlap a
|
|
// Register.
|
|
type Registry struct {
|
|
mu sync.RWMutex
|
|
commands map[Name]*Command
|
|
}
|
|
|
|
// New returns an empty Registry with only `help` available.
|
|
func New() *Registry {
|
|
r := &Registry{commands: map[Name]*Command{}}
|
|
r.registerHelp()
|
|
return r
|
|
}
|
|
|
|
func (r *Registry) registerHelp() {
|
|
r.Register(&Command{
|
|
Name: Help,
|
|
ShortDescription: "prints available commands or help <command> for specific usage info",
|
|
Callback: func(a any, args []string, w StringWriter) error {
|
|
return r.help(args, w)
|
|
},
|
|
})
|
|
}
|
|
|
|
// Register adds a command that can be run by a user. A command registered under a name
|
|
// already held replaces it; see Clone for what that means for Help.
|
|
func (r *Registry) Register(c *Command) {
|
|
r.mu.Lock()
|
|
defer r.mu.Unlock()
|
|
r.commands[c.Name] = c
|
|
}
|
|
|
|
// Get returns the command registered under name, or nil if there is no such command.
|
|
func (r *Registry) Get(name Name) *Command {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
return r.commands[name]
|
|
}
|
|
|
|
// Match returns the names of every command starting with prefix, sorted, as strings for
|
|
// a prompt to complete and print.
|
|
func (r *Registry) Match(prefix string) []string {
|
|
cmds := make([]string, 0)
|
|
r.mu.RLock()
|
|
for n := range r.commands {
|
|
if strings.HasPrefix(string(n), prefix) {
|
|
cmds = append(cmds, string(n))
|
|
}
|
|
}
|
|
r.mu.RUnlock()
|
|
slices.Sort(cmds)
|
|
return cmds
|
|
}
|
|
|
|
// All returns every registered command sorted by name, which is the order help lists them.
|
|
func (r *Registry) All() []*Command {
|
|
r.mu.RLock()
|
|
defer r.mu.RUnlock()
|
|
|
|
cmds := slices.Collect(maps.Values(r.commands))
|
|
slices.SortFunc(cmds, func(a, b *Command) int { return cmp.Compare(a.Name, b.Name) })
|
|
return cmds
|
|
}
|
|
|
|
// Clone returns a copy holding the same commands, with help bound to the copy: a
|
|
// command registered on the clone alone is listed and described there, and one the
|
|
// clone withholds is not. That rebinding replaces whatever was registered under Help on
|
|
// the source, built-in or not.
|
|
func (r *Registry) Clone() *Registry {
|
|
r.mu.RLock()
|
|
c := &Registry{commands: maps.Clone(r.commands)}
|
|
r.mu.RUnlock()
|
|
c.registerHelp()
|
|
return c
|
|
}
|
|
|
|
// Dispatch parses a command line and runs it, reporting problems to w. The error is for
|
|
// the caller to log, not to show; see CommandCallback.
|
|
func (r *Registry) Dispatch(line string, w StringWriter) error {
|
|
// A line shlex cannot split, an unbalanced quote.
|
|
args, err := shlex.Split(line, true)
|
|
if err != nil {
|
|
return Fail(w, "could not parse the command line: "+err.Error())
|
|
}
|
|
|
|
return r.DispatchArgs(args, w)
|
|
}
|
|
|
|
// DispatchArgs runs an already tokenized command line. The error is as for Dispatch.
|
|
func (r *Registry) DispatchArgs(args []string, w StringWriter) error {
|
|
if len(args) == 0 {
|
|
r.dumpCommands(w)
|
|
return nil
|
|
}
|
|
|
|
c := r.Get(Name(args[0]))
|
|
if c == nil {
|
|
// Only the first word was looked up, so only it is named.
|
|
msg := fmt.Sprintf("did not understand: %s", args[0])
|
|
if err := w.WriteLine(msg); err != nil {
|
|
return err
|
|
}
|
|
|
|
r.dumpCommands(w)
|
|
return &Failure{Msg: msg}
|
|
}
|
|
|
|
if checkHelpArgs(args) {
|
|
// Through the registered help command, so -h and `help <name>` are one path.
|
|
return execCommand(r.Get(Help), []string{string(c.Name)}, w)
|
|
}
|
|
|
|
return execCommand(c, args[1:], w)
|
|
}
|
|
|
|
// dumpCommands writes the list of available commands to w.
|
|
func (r *Registry) dumpCommands(w StringWriter) {
|
|
err := w.WriteLine("Available commands:")
|
|
if err != nil {
|
|
return
|
|
}
|
|
|
|
// In the order All gives, which is the display order.
|
|
cmds := make([]string, 0)
|
|
for _, l := range r.All() {
|
|
cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
|
|
}
|
|
|
|
_ = w.Write(strings.Join(cmds, "\n") + "\n\n")
|
|
}
|
|
|
|
// help writes the command list, or usage for the single command named in a.
|
|
func (r *Registry) help(a []string, w StringWriter) error {
|
|
// Just typed help
|
|
if len(a) == 0 {
|
|
r.dumpCommands(w)
|
|
return nil
|
|
}
|
|
|
|
// We are printing a specific commands help text
|
|
cmd := r.Get(Name(a[0]))
|
|
if cmd == nil {
|
|
return Fail(w, "Command not available "+a[0])
|
|
}
|
|
|
|
if err := w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription)); err != nil {
|
|
return err
|
|
}
|
|
|
|
if cmd.Help != "" {
|
|
if err := w.WriteLine(fmt.Sprintf(" %s", cmd.Help)); err != nil {
|
|
return err
|
|
}
|
|
}
|
|
|
|
if cmd.Flags != nil {
|
|
fs, _ := cmd.Flags()
|
|
if fs != nil {
|
|
fs.SetOutput(w.GetWriter())
|
|
fs.PrintDefaults()
|
|
}
|
|
}
|
|
|
|
return nil
|
|
}
|