mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 16:36:52 +01:00
Compare commits
10
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
357b129749 | ||
|
|
8a4bd8b10f | ||
|
|
b426843f88 | ||
|
|
cda3710d5a | ||
|
|
c26871fe08 | ||
|
|
7daacc6a48 | ||
|
|
428dad94b3 | ||
|
|
5bf666dd90 | ||
|
|
49207da077 | ||
|
|
bbc49669b7 |
No files matched your search
@@ -0,0 +1,2 @@
|
|||||||
|
# Goldens are compared byte for byte, so they must come out of git the way they went in.
|
||||||
|
*.golden -text
|
||||||
@@ -7,8 +7,15 @@ and this project adheres to [Semantic Versioning](https://semver.org/spec/v2.0.0
|
|||||||
|
|
||||||
## [Unreleased]
|
## [Unreleased]
|
||||||
|
|
||||||
|
### Removed
|
||||||
|
|
||||||
|
- The SSH debug server's `start-cpu-profile`, `stop-cpu-profile`, `save-heap-profile`, `save-mutex-profile`, and
|
||||||
|
`mutex-profile-fraction` commands, and `sshd.sandbox_dir`, which is now ignored if set.
|
||||||
|
|
||||||
### Fixed
|
### Fixed
|
||||||
|
|
||||||
|
- Signing a v2 certificate with an empty name, a name longer than 253 bytes, or an empty group now fails.
|
||||||
|
These certificates were signed successfully but could not be loaded.
|
||||||
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
- `create-tunnel -address <ip:port>` panicked on a nil RemoteList and, over the ssh debug server, took nebula
|
||||||
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
down with it. A hostinfo returned by StartHandshake had no RemoteList until the handshake loop's first
|
||||||
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
pass attached one, and `create-tunnel -address` set the remote before that. The list is now attached when
|
||||||
|
|||||||
+10
-1
@@ -389,7 +389,16 @@ func (c *certificateV2) fromTBSCertificate(t *TBSCertificate) error {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (c *certificateV2) validate() error {
|
func (c *certificateV2) validate() error {
|
||||||
// Empty names are allowed
|
// Mirror the name and group rules in unmarshalDetails so signing fails instead of producing an unparseable certificate
|
||||||
|
if c.details.name == "" || len(c.details.name) > MaxNameLength {
|
||||||
|
return NewErrInvalidCertificateProperties("name must be between 1 and %d bytes", MaxNameLength)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, group := range c.details.groups {
|
||||||
|
if group == "" {
|
||||||
|
return NewErrInvalidCertificateProperties("groups may not be empty")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
if len(c.publicKey) == 0 {
|
if len(c.publicKey) == 0 {
|
||||||
return ErrInvalidPublicKey
|
return ErrInvalidPublicKey
|
||||||
|
|||||||
@@ -6,6 +6,7 @@ import (
|
|||||||
"crypto/elliptic"
|
"crypto/elliptic"
|
||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
@@ -135,3 +136,47 @@ func TestCertificate_SignP256_AlwaysNormalized(t *testing.T) {
|
|||||||
assert.True(t, normie)
|
assert.True(t, normie)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestCertificateV2_SignRejectsUnparseable(t *testing.T) {
|
||||||
|
_, priv, err := ed25519.GenerateKey(rand.Reader)
|
||||||
|
require.NoError(t, err)
|
||||||
|
var invalid *ErrInvalidCertificateProperties
|
||||||
|
|
||||||
|
newTBS := func() TBSCertificate {
|
||||||
|
return TBSCertificate{
|
||||||
|
Version: Version2,
|
||||||
|
Name: "testing",
|
||||||
|
Networks: []netip.Prefix{mustParsePrefixUnmapped("10.1.1.1/24")},
|
||||||
|
Groups: []string{"test-group1"},
|
||||||
|
NotBefore: time.Now().Add(-time.Minute),
|
||||||
|
NotAfter: time.Now().Add(time.Minute),
|
||||||
|
PublicKey: []byte("1234567890abcedfghij1234567890ab"),
|
||||||
|
Curve: Curve_CURVE25519,
|
||||||
|
IsCA: true,
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
tbs := newTBS()
|
||||||
|
tbs.Name = strings.Repeat("a", MaxNameLength)
|
||||||
|
c, err := tbs.Sign(nil, Curve_CURVE25519, priv)
|
||||||
|
require.NoError(t, err)
|
||||||
|
b, err := c.Marshal()
|
||||||
|
require.NoError(t, err)
|
||||||
|
_, err = unmarshalCertificateV2(b, nil, Curve_CURVE25519)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
tbs = newTBS()
|
||||||
|
tbs.Name = strings.Repeat("a", MaxNameLength+1)
|
||||||
|
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
||||||
|
require.ErrorAs(t, err, &invalid)
|
||||||
|
|
||||||
|
tbs = newTBS()
|
||||||
|
tbs.Name = ""
|
||||||
|
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
||||||
|
require.ErrorAs(t, err, &invalid)
|
||||||
|
|
||||||
|
tbs = newTBS()
|
||||||
|
tbs.Groups = []string{"test-group1", ""}
|
||||||
|
_, err = tbs.Sign(nil, Curve_CURVE25519, priv)
|
||||||
|
require.ErrorAs(t, err, &invalid)
|
||||||
|
}
|
||||||
+710
@@ -0,0 +1,710 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"encoding/json"
|
||||||
|
"flag"
|
||||||
|
"fmt"
|
||||||
|
"log/slog"
|
||||||
|
"maps"
|
||||||
|
"net/netip"
|
||||||
|
"sort"
|
||||||
|
"strings"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/logging"
|
||||||
|
)
|
||||||
|
|
||||||
|
// The commands nebula serves. attachCommands builds the one registry every surface
|
||||||
|
// reads. See the commands package for what a Command is.
|
||||||
|
|
||||||
|
type listHostMapFlags struct {
|
||||||
|
Json bool
|
||||||
|
Pretty bool
|
||||||
|
ByIndex bool
|
||||||
|
}
|
||||||
|
|
||||||
|
// listHostMapFlagSet is the flag set list-hostmap and list-pending-hostmap share.
|
||||||
|
func listHostMapFlagSet() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := listHostMapFlags{}
|
||||||
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
||||||
|
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
||||||
|
return fl, &s
|
||||||
|
}
|
||||||
|
|
||||||
|
type printCertFlags struct {
|
||||||
|
Json bool
|
||||||
|
Pretty bool
|
||||||
|
Raw bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type printTunnelFlags struct {
|
||||||
|
Pretty bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type changeRemoteFlags struct {
|
||||||
|
Address string
|
||||||
|
}
|
||||||
|
|
||||||
|
type closeTunnelFlags struct {
|
||||||
|
LocalOnly bool
|
||||||
|
}
|
||||||
|
|
||||||
|
type createTunnelFlags struct {
|
||||||
|
Address string
|
||||||
|
}
|
||||||
|
|
||||||
|
type deviceInfoFlags struct {
|
||||||
|
Json bool
|
||||||
|
Pretty bool
|
||||||
|
}
|
||||||
|
|
||||||
|
func attachCommands(l *slog.Logger, c *config.C, reg *commands.Registry, f *Interface) {
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.ListHostmap,
|
||||||
|
ShortDescription: "List all known previously connected hosts",
|
||||||
|
Flags: listHostMapFlagSet,
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return listHostMapCommand(f.hostMap, fs, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.ListPendingHostmap,
|
||||||
|
ShortDescription: "List all handshaking hosts",
|
||||||
|
Flags: listHostMapFlagSet,
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return listHostMapCommand(f.handshakeManager, fs, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.ListLighthouseAddrmap,
|
||||||
|
ShortDescription: "List all lighthouse map entries",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := listHostMapFlags{}
|
||||||
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return listLighthouseAddrmapCommand(f.lightHouse, fs, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.Reload,
|
||||||
|
ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return reloadCommand(l, c, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.LogLevel,
|
||||||
|
ShortDescription: "Gets or sets the current log level",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return logLevelCommand(l, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.LogFormat,
|
||||||
|
ShortDescription: "Gets or sets the current log format",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return logFormatCommand(l, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.Version,
|
||||||
|
ShortDescription: "Prints the currently running version of nebula",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return versionCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.DeviceInfo,
|
||||||
|
ShortDescription: "Prints information about the network device.",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := deviceInfoFlags{}
|
||||||
|
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return deviceInfoCommand(f, fs, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.PrintCert,
|
||||||
|
ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn addr",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := printCertFlags{}
|
||||||
|
fl.BoolVar(&s.Json, "json", false, "outputs as json")
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
||||||
|
fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return printCertCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.PrintTunnel,
|
||||||
|
ShortDescription: "Prints json details about a tunnel for the provided vpn addr",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := printTunnelFlags{}
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return printTunnelCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.PrintRelays,
|
||||||
|
ShortDescription: "Prints json details about all relay info",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := printTunnelFlags{}
|
||||||
|
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return printRelaysCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.ChangeRemote,
|
||||||
|
ShortDescription: "Changes the remote address used in the tunnel for the provided vpn addr",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := changeRemoteFlags{}
|
||||||
|
fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return changeRemoteCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.CloseTunnel,
|
||||||
|
ShortDescription: "Closes a tunnel for the provided vpn addr",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := closeTunnelFlags{}
|
||||||
|
fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return closeTunnelCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.CreateTunnel,
|
||||||
|
ShortDescription: "Creates a tunnel for the provided vpn address",
|
||||||
|
Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
s := createTunnelFlags{}
|
||||||
|
fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
|
||||||
|
return fl, &s
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return createTunnelCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
reg.Register(&commands.Command{
|
||||||
|
Name: commands.QueryLighthouse,
|
||||||
|
ShortDescription: "Query the lighthouses for the provided vpn address",
|
||||||
|
Help: "This command is asynchronous. Only currently known udp addresses will be printed.",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return queryLighthouseCommand(f, fs, a, w)
|
||||||
|
},
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
func listHostMapCommand(hl controlHostLister, a any, w commands.StringWriter) error {
|
||||||
|
fs, ok := a.(*listHostMapFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *listHostMapFlags but was %T", a)
|
||||||
|
}
|
||||||
|
|
||||||
|
var hm []ControlHostInfo
|
||||||
|
if fs.ByIndex {
|
||||||
|
hm = listHostMapIndexes(hl)
|
||||||
|
} else {
|
||||||
|
hm = listHostMapHosts(hl)
|
||||||
|
}
|
||||||
|
|
||||||
|
sort.Slice(hm, func(i, j int) bool {
|
||||||
|
return hm[i].VpnAddrs[0].Compare(hm[j].VpnAddrs[0]) < 0
|
||||||
|
})
|
||||||
|
|
||||||
|
if fs.Json || fs.Pretty {
|
||||||
|
js := json.NewEncoder(w.GetWriter())
|
||||||
|
if fs.Pretty {
|
||||||
|
js.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := js.Encode(hm); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
for _, v := range hm {
|
||||||
|
err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddrs, v.RemoteAddrs))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func listLighthouseAddrmapCommand(lightHouse *LightHouse, a any, w commands.StringWriter) error {
|
||||||
|
fs, ok := a.(*listHostMapFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *listHostMapFlags but was %T", a)
|
||||||
|
}
|
||||||
|
|
||||||
|
type lighthouseInfo struct {
|
||||||
|
VpnAddr string `json:"vpnAddr"`
|
||||||
|
Addrs *CacheMap `json:"addrs"`
|
||||||
|
}
|
||||||
|
|
||||||
|
lightHouse.RLock()
|
||||||
|
addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
|
||||||
|
x := 0
|
||||||
|
for k, v := range lightHouse.addrMap {
|
||||||
|
addrMap[x] = lighthouseInfo{
|
||||||
|
VpnAddr: k.String(),
|
||||||
|
Addrs: v.CopyCache(),
|
||||||
|
}
|
||||||
|
x++
|
||||||
|
}
|
||||||
|
lightHouse.RUnlock()
|
||||||
|
|
||||||
|
sort.Slice(addrMap, func(i, j int) bool {
|
||||||
|
return strings.Compare(addrMap[i].VpnAddr, addrMap[j].VpnAddr) < 0
|
||||||
|
})
|
||||||
|
|
||||||
|
if fs.Json || fs.Pretty {
|
||||||
|
js := json.NewEncoder(w.GetWriter())
|
||||||
|
if fs.Pretty {
|
||||||
|
js.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := js.Encode(addrMap); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
} else {
|
||||||
|
for _, v := range addrMap {
|
||||||
|
b, err := json.Marshal(v.Addrs)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddr, string(b)))
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// parseVpnAddr parses the vpn address a command takes as its first operand, failing to w
|
||||||
|
// when there is none or it does not parse. noun is how the command's messages name it,
|
||||||
|
// "vpn address" or "vpn addr", which the output golden pins per command.
|
||||||
|
func parseVpnAddr(a []string, noun string, w commands.StringWriter) (netip.Addr, error) {
|
||||||
|
if len(a) == 0 {
|
||||||
|
return netip.Addr{}, commands.Fail(w, "No vpn address was provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnAddr, err := netip.ParseAddr(a[0])
|
||||||
|
if err != nil {
|
||||||
|
return netip.Addr{}, commands.Fail(w, fmt.Sprintf("The provided %s could not be parsed: %s", noun, a[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
return vpnAddr, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
func versionCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
return w.WriteLine(ifce.version)
|
||||||
|
}
|
||||||
|
|
||||||
|
func queryLighthouseCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
var cm *CacheMap
|
||||||
|
rl := ifce.lightHouse.Query(vpnAddr)
|
||||||
|
if rl != nil {
|
||||||
|
cm = rl.CopyCache()
|
||||||
|
}
|
||||||
|
return json.NewEncoder(w.GetWriter()).Encode(cm)
|
||||||
|
}
|
||||||
|
|
||||||
|
func closeTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
flags, ok := fs.(*closeTunnelFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *closeTunnelFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo == nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
if !flags.LocalOnly {
|
||||||
|
ifce.sendCloseTunnel(hostInfo)
|
||||||
|
}
|
||||||
|
|
||||||
|
ifce.closeTunnel(hostInfo)
|
||||||
|
return w.WriteLine("Closed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func createTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
flags, ok := fs.(*createTunnelFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *createTunnelFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo != nil {
|
||||||
|
return commands.Fail(w, "Tunnel already exists")
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo = ifce.handshakeManager.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo != nil {
|
||||||
|
return commands.Fail(w, "Tunnel already handshaking")
|
||||||
|
}
|
||||||
|
|
||||||
|
var addr netip.AddrPort
|
||||||
|
if flags.Address != "" {
|
||||||
|
addr, err = netip.ParseAddrPort(flags.Address)
|
||||||
|
if err != nil {
|
||||||
|
return commands.Fail(w, "Address could not be parsed")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Set the remote from the callback, which StartHandshake runs under the handshake manager lock
|
||||||
|
// before the hostinfo reaches the handshake loop or the static-host trigger, so the address is
|
||||||
|
// in the RemoteList before the first handshake is sent.
|
||||||
|
ifce.handshakeManager.StartHandshake(vpnAddr, func(hh *HandshakeHostInfo) {
|
||||||
|
if addr.IsValid() {
|
||||||
|
hh.hostinfo.SetRemote(addr)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
|
||||||
|
return w.WriteLine("Created")
|
||||||
|
}
|
||||||
|
|
||||||
|
func changeRemoteCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
flags, ok := fs.(*changeRemoteFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *changeRemoteFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn address", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
if flags.Address == "" {
|
||||||
|
return commands.Fail(w, "No address was provided")
|
||||||
|
}
|
||||||
|
|
||||||
|
addr, err := netip.ParseAddrPort(flags.Address)
|
||||||
|
if err != nil {
|
||||||
|
return commands.Fail(w, "Address could not be parsed")
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo == nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo.SetRemote(addr)
|
||||||
|
return w.WriteLine("Changed")
|
||||||
|
}
|
||||||
|
|
||||||
|
func logLevelCommand(l *slog.Logger, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
ctrl, ok := l.Handler().(interface {
|
||||||
|
GetLevel() slog.Level
|
||||||
|
SetLevel(slog.Level)
|
||||||
|
})
|
||||||
|
if !ok {
|
||||||
|
return commands.Fail(w, "Log level is not reconfigurable on this logger")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(a) == 0 {
|
||||||
|
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
||||||
|
}
|
||||||
|
|
||||||
|
level, err := logging.ParseLevel(strings.ToLower(a[0]))
|
||||||
|
if err != nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Unknown log level %s. Possible log levels: trace, debug, info, warn, error", a))
|
||||||
|
}
|
||||||
|
|
||||||
|
ctrl.SetLevel(level)
|
||||||
|
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
||||||
|
}
|
||||||
|
|
||||||
|
func logFormatCommand(l *slog.Logger, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
ctrl, ok := l.Handler().(interface {
|
||||||
|
GetFormat() string
|
||||||
|
SetFormat(string) error
|
||||||
|
})
|
||||||
|
if !ok {
|
||||||
|
return commands.Fail(w, "Log format is not reconfigurable on this logger")
|
||||||
|
}
|
||||||
|
|
||||||
|
if len(a) == 0 {
|
||||||
|
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
||||||
|
}
|
||||||
|
|
||||||
|
if err := ctrl.SetFormat(strings.ToLower(a[0])); err != nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Unable to set log format: %s", err))
|
||||||
|
}
|
||||||
|
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func printCertCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
args, ok := fs.(*printCertFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *printCertFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
cert := ifce.pki.getCertState().GetDefaultCertificate()
|
||||||
|
if len(a) > 0 {
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn addr", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo == nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
cert = hostInfo.GetCert().Certificate
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Json || args.Pretty {
|
||||||
|
b, err := cert.MarshalJSON()
|
||||||
|
if err != nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Pretty {
|
||||||
|
buf := new(bytes.Buffer)
|
||||||
|
if err := json.Indent(buf, b, "", " "); err != nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
||||||
|
}
|
||||||
|
b = buf.Bytes()
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.WriteBytes(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
if args.Raw {
|
||||||
|
b, err := cert.MarshalPEM()
|
||||||
|
if err != nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Unable to marshal the certificate: %s", err))
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.WriteBytes(b)
|
||||||
|
}
|
||||||
|
|
||||||
|
return w.WriteLine(cert.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
func printRelaysCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
args, ok := fs.(*printTunnelFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *printTunnelFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
ifce.hostMap.RLock()
|
||||||
|
relays := maps.Clone(ifce.hostMap.Relays)
|
||||||
|
ifce.hostMap.RUnlock()
|
||||||
|
|
||||||
|
type RelayFor struct {
|
||||||
|
Error string `json:",omitempty"`
|
||||||
|
Type string
|
||||||
|
State string
|
||||||
|
PeerAddr netip.Addr
|
||||||
|
LocalIndex uint32
|
||||||
|
RemoteIndex uint32
|
||||||
|
RelayedThrough []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
type RelayOutput struct {
|
||||||
|
NebulaAddr netip.Addr
|
||||||
|
RelayForAddrs []RelayFor
|
||||||
|
}
|
||||||
|
|
||||||
|
type CmdOutput struct {
|
||||||
|
Relays []*RelayOutput
|
||||||
|
}
|
||||||
|
|
||||||
|
co := CmdOutput{}
|
||||||
|
|
||||||
|
enc := json.NewEncoder(w.GetWriter())
|
||||||
|
|
||||||
|
if args.Pretty {
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
for k, v := range relays {
|
||||||
|
ro := RelayOutput{NebulaAddr: v.vpnAddrs[0]}
|
||||||
|
co.Relays = append(co.Relays, &ro)
|
||||||
|
relayHI := ifce.hostMap.QueryVpnAddr(v.vpnAddrs[0])
|
||||||
|
if relayHI == nil {
|
||||||
|
ro.RelayForAddrs = append(ro.RelayForAddrs, RelayFor{Error: "could not find hostinfo"})
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
for _, vpnAddr := range relayHI.relayState.CopyRelayForIps() {
|
||||||
|
rf := RelayFor{}
|
||||||
|
r, ok := relayHI.relayState.GetRelayForByAddr(vpnAddr)
|
||||||
|
if ok {
|
||||||
|
t := ""
|
||||||
|
switch r.Type {
|
||||||
|
case ForwardingType:
|
||||||
|
t = "forwarding"
|
||||||
|
case TerminalType:
|
||||||
|
t = "terminal"
|
||||||
|
default:
|
||||||
|
t = "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
s := ""
|
||||||
|
switch r.State {
|
||||||
|
case Requested:
|
||||||
|
s = "requested"
|
||||||
|
case Established:
|
||||||
|
s = "established"
|
||||||
|
default:
|
||||||
|
s = "unknown"
|
||||||
|
}
|
||||||
|
|
||||||
|
rf.LocalIndex = r.LocalIndex
|
||||||
|
rf.RemoteIndex = r.RemoteIndex
|
||||||
|
rf.PeerAddr = r.PeerAddr
|
||||||
|
rf.Type = t
|
||||||
|
rf.State = s
|
||||||
|
if rf.LocalIndex != k {
|
||||||
|
rf.Error = fmt.Sprintf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
relayedHI := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if relayedHI != nil {
|
||||||
|
rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
|
||||||
|
}
|
||||||
|
|
||||||
|
ro.RelayForAddrs = append(ro.RelayForAddrs, rf)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return enc.Encode(co)
|
||||||
|
}
|
||||||
|
|
||||||
|
func printTunnelCommand(ifce *Interface, fs any, a []string, w commands.StringWriter) error {
|
||||||
|
args, ok := fs.(*printTunnelFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected *printTunnelFlags but was %T", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
vpnAddr, err := parseVpnAddr(a, "vpn addr", w)
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
|
||||||
|
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
||||||
|
if hostInfo == nil {
|
||||||
|
return commands.Fail(w, fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
||||||
|
}
|
||||||
|
|
||||||
|
enc := json.NewEncoder(w.GetWriter())
|
||||||
|
if args.Pretty {
|
||||||
|
enc.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
|
||||||
|
}
|
||||||
|
|
||||||
|
func deviceInfoCommand(ifce *Interface, fs any, w commands.StringWriter) error {
|
||||||
|
flags, ok := fs.(*deviceInfoFlags)
|
||||||
|
if !ok {
|
||||||
|
return fmt.Errorf("internal error: expected flags to be deviceInfoFlags but was %+v", fs)
|
||||||
|
}
|
||||||
|
|
||||||
|
data := struct {
|
||||||
|
Name string `json:"name"`
|
||||||
|
Cidr []netip.Prefix `json:"cidr"`
|
||||||
|
}{
|
||||||
|
Name: ifce.inside.Name(),
|
||||||
|
Cidr: make([]netip.Prefix, len(ifce.inside.Networks())),
|
||||||
|
}
|
||||||
|
|
||||||
|
copy(data.Cidr, ifce.inside.Networks())
|
||||||
|
|
||||||
|
if flags.Json || flags.Pretty {
|
||||||
|
js := json.NewEncoder(w.GetWriter())
|
||||||
|
if flags.Pretty {
|
||||||
|
js.SetIndent("", " ")
|
||||||
|
}
|
||||||
|
|
||||||
|
return js.Encode(data)
|
||||||
|
} else {
|
||||||
|
return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// reloadCommand reloads the config and writes the result to the client. It reloads even if the client has gone away
|
||||||
|
func reloadCommand(l *slog.Logger, c *config.C, w commands.StringWriter) error {
|
||||||
|
writeErr := w.WriteLine("Reloading config")
|
||||||
|
res := c.ReloadConfig()
|
||||||
|
res.Log(l)
|
||||||
|
if err := w.WriteLine(res.String()); writeErr == nil {
|
||||||
|
writeErr = err
|
||||||
|
}
|
||||||
|
if writeErr != nil {
|
||||||
|
return writeErr
|
||||||
|
}
|
||||||
|
if res.Err != nil || res.Failed {
|
||||||
|
return &commands.Failure{Msg: res.String()}
|
||||||
|
}
|
||||||
|
return nil
|
||||||
|
}
|
||||||
@@ -0,0 +1,83 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
)
|
||||||
|
|
||||||
|
// CommandFlags is called before help or command execution to parse command line flags.
|
||||||
|
// It should return a flag.FlagSet instance and a pointer to the struct that will contain parsed flags
|
||||||
|
type CommandFlags func() (*flag.FlagSet, any)
|
||||||
|
|
||||||
|
// CommandCallback is called when your command should execute.
|
||||||
|
// fs will be a pointer to the struct provided by Command.Flags callback, if there was one. -h and -help are reserved
|
||||||
|
// and handled automatically for you.
|
||||||
|
// a will be any unconsumed arguments, if no Command.Flags was available this will be all the flags passed in.
|
||||||
|
// w is the writer to use when sending messages back to the client.
|
||||||
|
// An error returned by the callback is handed back to whoever dispatched the command, which reports a failure: exit
|
||||||
|
// status 1 over ssh. The error is not shown to the user. For a failure the user caused or can act on, write the
|
||||||
|
// message and return it as a *Failure, which Fail does in one step; the surface then reports the failure without
|
||||||
|
// logging what the user was already told. Any other error is logged by the surface as well.
|
||||||
|
type CommandCallback func(fs any, a []string, w StringWriter) error
|
||||||
|
|
||||||
|
type Command struct {
|
||||||
|
Name Name
|
||||||
|
ShortDescription string
|
||||||
|
Help string
|
||||||
|
|
||||||
|
Flags CommandFlags
|
||||||
|
Callback CommandCallback
|
||||||
|
}
|
||||||
|
|
||||||
|
// Failure is the error a command returns for a failure it has already explained to the user:
|
||||||
|
// the message it wrote is the whole story. A surface reports it as a failure and does not
|
||||||
|
// log it.
|
||||||
|
type Failure struct {
|
||||||
|
Msg string
|
||||||
|
}
|
||||||
|
|
||||||
|
func (f *Failure) Error() string {
|
||||||
|
return f.Msg
|
||||||
|
}
|
||||||
|
|
||||||
|
// Fail writes msg to w and returns it as a *Failure, so a command reports a failure to the
|
||||||
|
// user and to its surface in one line. If the write itself fails, that error is returned
|
||||||
|
// instead: the user was not told anything.
|
||||||
|
func Fail(w StringWriter, msg string) error {
|
||||||
|
if err := w.WriteLine(msg); err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
return &Failure{Msg: msg}
|
||||||
|
}
|
||||||
|
|
||||||
|
func execCommand(c *Command, args []string, w StringWriter) error {
|
||||||
|
var (
|
||||||
|
fl *flag.FlagSet
|
||||||
|
fs any
|
||||||
|
)
|
||||||
|
|
||||||
|
if c.Flags != nil {
|
||||||
|
fl, fs = c.Flags()
|
||||||
|
if fl != nil {
|
||||||
|
// SetOutput() here in case fl.Parse dumps usage.
|
||||||
|
fl.SetOutput(w.GetWriter())
|
||||||
|
err := fl.Parse(args)
|
||||||
|
if err != nil {
|
||||||
|
// fl.Parse has dumped error information to the user via the w writer.
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
args = fl.Args()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return c.Callback(fs, args, w)
|
||||||
|
}
|
||||||
|
|
||||||
|
func checkHelpArgs(args []string) bool {
|
||||||
|
for _, a := range args {
|
||||||
|
if a == "-h" || a == "-help" {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
return false
|
||||||
|
}
|
||||||
@@ -0,0 +1,28 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
// Name identifies a command. Every command nebula serves is a constant here, so a
|
||||||
|
// surface, a client and a test spell it the same way and are told at compile time when
|
||||||
|
// they do not. It is a string rather than an integer so a test can register a throwaway
|
||||||
|
// command without adding it here, and so the wire carries the name as the string it has
|
||||||
|
// always been.
|
||||||
|
type Name string
|
||||||
|
|
||||||
|
const (
|
||||||
|
ChangeRemote Name = "change-remote"
|
||||||
|
CloseTunnel Name = "close-tunnel"
|
||||||
|
CreateTunnel Name = "create-tunnel"
|
||||||
|
DeviceInfo Name = "device-info"
|
||||||
|
Help Name = "help"
|
||||||
|
ListHostmap Name = "list-hostmap"
|
||||||
|
ListLighthouseAddrmap Name = "list-lighthouse-addrmap"
|
||||||
|
ListPendingHostmap Name = "list-pending-hostmap"
|
||||||
|
LogFormat Name = "log-format"
|
||||||
|
LogLevel Name = "log-level"
|
||||||
|
Logout Name = "logout"
|
||||||
|
PrintCert Name = "print-cert"
|
||||||
|
PrintRelays Name = "print-relays"
|
||||||
|
PrintTunnel Name = "print-tunnel"
|
||||||
|
QueryLighthouse Name = "query-lighthouse"
|
||||||
|
Reload Name = "reload"
|
||||||
|
Version Name = "version"
|
||||||
|
)
|
||||||
@@ -0,0 +1,181 @@
|
|||||||
|
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
|
||||||
|
}
|
||||||
@@ -0,0 +1,176 @@
|
|||||||
|
package commands
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"errors"
|
||||||
|
"flag"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func testRegistry() *Registry {
|
||||||
|
r := New()
|
||||||
|
r.Register(&Command{
|
||||||
|
Name: "served",
|
||||||
|
ShortDescription: "a command this surface serves",
|
||||||
|
Callback: func(fs any, a []string, w StringWriter) error {
|
||||||
|
return w.WriteLine("read " + strings.Join(a, ","))
|
||||||
|
},
|
||||||
|
})
|
||||||
|
r.Register(&Command{
|
||||||
|
Name: "withheld",
|
||||||
|
ShortDescription: "a command this surface withholds",
|
||||||
|
Callback: func(fs any, a []string, w StringWriter) error {
|
||||||
|
return w.WriteLine("wrote")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFlagUsageGoesToTheWriter pins that a parse failure's usage lands on the command's
|
||||||
|
// writer.
|
||||||
|
func TestFlagUsageGoesToTheWriter(t *testing.T) {
|
||||||
|
r := New()
|
||||||
|
r.Register(&Command{
|
||||||
|
Name: "emit",
|
||||||
|
ShortDescription: "emits",
|
||||||
|
Flags: func() (*flag.FlagSet, any) {
|
||||||
|
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
||||||
|
fl.Bool("json", false, "emit json")
|
||||||
|
return fl, nil
|
||||||
|
},
|
||||||
|
Callback: func(fs any, a []string, w StringWriter) error { return nil },
|
||||||
|
})
|
||||||
|
|
||||||
|
merged := &bytes.Buffer{}
|
||||||
|
require.Error(t, r.DispatchArgs([]string{"emit", "-nope"}, NewStringWriter(merged)))
|
||||||
|
assert.Contains(t, merged.String(), "flag provided but not defined")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDispatchReportsUnparseableLine: a line shlex rejects, an unbalanced quote, is a
|
||||||
|
// failure the user is told about.
|
||||||
|
func TestDispatchReportsUnparseableLine(t *testing.T) {
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
err := testRegistry().Dispatch(`served "unterminated`, NewStringWriter(out))
|
||||||
|
|
||||||
|
var f *Failure
|
||||||
|
require.ErrorAs(t, err, &f)
|
||||||
|
assert.Equal(t, f.Msg+"\n", out.String())
|
||||||
|
assert.Contains(t, out.String(), "could not parse the command line: ")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestFailTellsTheUserAndTheSurface: Fail writes the message and returns it as a Failure;
|
||||||
|
// when the write fails, the user heard nothing, so the write error is what comes back.
|
||||||
|
func TestFailTellsTheUserAndTheSurface(t *testing.T) {
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
err := Fail(NewStringWriter(out), "no such tunnel")
|
||||||
|
var f *Failure
|
||||||
|
require.ErrorAs(t, err, &f)
|
||||||
|
assert.Equal(t, "no such tunnel", f.Msg)
|
||||||
|
assert.Equal(t, "no such tunnel\n", out.String())
|
||||||
|
|
||||||
|
err = Fail(NewStringWriter(failingWriter{}), "no such tunnel")
|
||||||
|
require.EqualError(t, err, "gone")
|
||||||
|
assert.NotErrorAs(t, err, &f)
|
||||||
|
}
|
||||||
|
|
||||||
|
type failingWriter struct{}
|
||||||
|
|
||||||
|
func (failingWriter) Write([]byte) (int, error) { return 0, errors.New("gone") }
|
||||||
|
|
||||||
|
// TestAllSortsByName: registration order does not matter.
|
||||||
|
func TestAllSortsByName(t *testing.T) {
|
||||||
|
r := New()
|
||||||
|
for _, n := range []Name{"zz", Version, "aa", ChangeRemote} {
|
||||||
|
r.Register(&Command{Name: n})
|
||||||
|
}
|
||||||
|
|
||||||
|
var got []Name
|
||||||
|
for _, c := range r.All() {
|
||||||
|
got = append(got, c.Name)
|
||||||
|
}
|
||||||
|
assert.Equal(t, []Name{"aa", ChangeRemote, Help, Version, "zz"}, got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestHelpFlagAgreesWithHelpCommand pins that -h answers through the registered help
|
||||||
|
// command, and that help on a clone describes the clone: a command registered only there
|
||||||
|
// is known to both -h and `help <name>`, and one withheld from it is known to neither.
|
||||||
|
func TestHelpFlagAgreesWithHelpCommand(t *testing.T) {
|
||||||
|
r := testRegistry()
|
||||||
|
c := r.Clone()
|
||||||
|
c.Register(&Command{
|
||||||
|
Name: Logout,
|
||||||
|
ShortDescription: "ends the session",
|
||||||
|
Callback: func(fs any, a []string, w StringWriter) error { return nil },
|
||||||
|
})
|
||||||
|
|
||||||
|
flag := &bytes.Buffer{}
|
||||||
|
c.Dispatch("logout -h", NewStringWriter(flag))
|
||||||
|
cmd := &bytes.Buffer{}
|
||||||
|
c.Dispatch("help logout", NewStringWriter(cmd))
|
||||||
|
assert.Equal(t, cmd.String(), flag.String())
|
||||||
|
assert.Equal(t, "logout - ends the session\n", flag.String())
|
||||||
|
|
||||||
|
// The source never learns of it.
|
||||||
|
src := &bytes.Buffer{}
|
||||||
|
r.Dispatch("help logout", NewStringWriter(src))
|
||||||
|
assert.Equal(t, "Command not available logout\n", src.String())
|
||||||
|
|
||||||
|
// And a command both hold is described either way.
|
||||||
|
flag.Reset()
|
||||||
|
c.Dispatch("served -h", NewStringWriter(flag))
|
||||||
|
assert.Equal(t, "served - a command this surface serves\n", flag.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCloneHelpListsTheClone: the command list help prints is the clone's, so a clone
|
||||||
|
// that withholds a command does not name it, and the source still does.
|
||||||
|
func TestCloneHelpListsTheClone(t *testing.T) {
|
||||||
|
r := testRegistry()
|
||||||
|
c := New()
|
||||||
|
c.Register(r.Get("served"))
|
||||||
|
c = c.Clone()
|
||||||
|
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
c.Dispatch("help", NewStringWriter(out))
|
||||||
|
assert.Equal(t, "Available commands:\n"+
|
||||||
|
"help - prints available commands or help <command> for specific usage info\n"+
|
||||||
|
"served - a command this surface serves\n\n", out.String())
|
||||||
|
|
||||||
|
out.Reset()
|
||||||
|
c.Dispatch("help withheld", NewStringWriter(out))
|
||||||
|
assert.Equal(t, "Command not available withheld\n", out.String())
|
||||||
|
|
||||||
|
out.Reset()
|
||||||
|
r.Dispatch("help withheld", NewStringWriter(out))
|
||||||
|
assert.Equal(t, "withheld - a command this surface withholds\n", out.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestDispatchReturnsTheCommandsError: what the callback returns reaches the caller, so
|
||||||
|
// a surface can log it.
|
||||||
|
func TestDispatchReturnsTheCommandsError(t *testing.T) {
|
||||||
|
r := New()
|
||||||
|
r.Register(&Command{
|
||||||
|
Name: "fails",
|
||||||
|
Callback: func(fs any, a []string, w StringWriter) error {
|
||||||
|
return errors.New("boom")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
require.EqualError(t, r.Dispatch("fails", NewStringWriter(out)), "boom")
|
||||||
|
assert.Empty(t, out.String(), "a command's own error is not shown; the command speaks for itself")
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestUnknownCommandNamesTheWord: only the first word is looked up, so only it is
|
||||||
|
// reported, followed by the command list, and the line is a failure to the surface.
|
||||||
|
func TestUnknownCommandNamesTheWord(t *testing.T) {
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
err := testRegistry().Dispatch(`nope "with args"`, NewStringWriter(out))
|
||||||
|
var f *Failure
|
||||||
|
require.ErrorAs(t, err, &f)
|
||||||
|
assert.Equal(t, "did not understand: nope", f.Msg)
|
||||||
|
assert.True(t, strings.HasPrefix(out.String(), "did not understand: nope\nAvailable commands:\n"), out.String())
|
||||||
|
}
|
||||||
@@ -1,7 +1,9 @@
|
|||||||
package sshd
|
package commands
|
||||||
|
|
||||||
import "io"
|
import "io"
|
||||||
|
|
||||||
|
// StringWriter is how a command talks back to whoever invoked it: one stream, which the
|
||||||
|
// ssh surface points at its channel.
|
||||||
type StringWriter interface {
|
type StringWriter interface {
|
||||||
WriteLine(string) error
|
WriteLine(string) error
|
||||||
Write(string) error
|
Write(string) error
|
||||||
@@ -13,6 +15,11 @@ type stringWriter struct {
|
|||||||
w io.Writer
|
w io.Writer
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// NewStringWriter returns a StringWriter that writes to w.
|
||||||
|
func NewStringWriter(w io.Writer) StringWriter {
|
||||||
|
return &stringWriter{w: w}
|
||||||
|
}
|
||||||
|
|
||||||
func (w *stringWriter) WriteLine(s string) error {
|
func (w *stringWriter) WriteLine(s string) error {
|
||||||
return w.Write(s + "\n")
|
return w.Write(s + "\n")
|
||||||
}
|
}
|
||||||
@@ -0,0 +1,67 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"io"
|
||||||
|
"net/netip"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
|
"github.com/slackhq/nebula/logging"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestLogFormatRefusalIsShown: a format the logger does not know is reported to the user
|
||||||
|
// as a failure, rather than returned bare, which over ssh was silence.
|
||||||
|
func TestLogFormatRefusalIsShown(t *testing.T) {
|
||||||
|
l := logging.NewLogger(io.Discard)
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
w := commands.NewStringWriter(out)
|
||||||
|
|
||||||
|
var f *commands.Failure
|
||||||
|
require.ErrorAs(t, logFormatCommand(l, nil, []string{"bogus"}, w), &f)
|
||||||
|
assert.Equal(t, "Unable to set log format: unknown log format `bogus`. possible formats: [text json]\n", out.String())
|
||||||
|
|
||||||
|
out.Reset()
|
||||||
|
require.NoError(t, logFormatCommand(l, nil, []string{"JSON"}, w))
|
||||||
|
assert.Equal(t, "Log format is: json\n", out.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestPrintRelaysReportsErrorsAsText: the Error field was an error value, which encodes
|
||||||
|
// as {} and lost the message. It is the message now, and absent when there is none.
|
||||||
|
func TestPrintRelaysReportsErrorsAsText(t *testing.T) {
|
||||||
|
l := test.NewLogger()
|
||||||
|
hm := newHostMap(l)
|
||||||
|
relay := netip.MustParseAddr("10.0.0.1")
|
||||||
|
peer := netip.MustParseAddr("10.0.0.2")
|
||||||
|
|
||||||
|
// A relay host that is in Relays but not in Hosts, so its hostinfo cannot be found.
|
||||||
|
hm.Relays[1] = &HostInfo{vpnAddrs: []netip.Addr{relay}}
|
||||||
|
ifce := &Interface{hostMap: hm}
|
||||||
|
|
||||||
|
out := &bytes.Buffer{}
|
||||||
|
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
||||||
|
assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Error":"could not find hostinfo","Type":"","State":"","PeerAddr":"","LocalIndex":0,"RemoteIndex":0,"RelayedThrough":null}]}]}`, out.String())
|
||||||
|
|
||||||
|
// The same relay host, found, relaying for a peer whose index matches: no Error key.
|
||||||
|
hi := hm.Relays[1]
|
||||||
|
r := &Relay{Type: ForwardingType, State: Established, PeerAddr: peer, LocalIndex: 1, RemoteIndex: 2}
|
||||||
|
hi.relayState = RelayState{
|
||||||
|
relayForByAddr: map[netip.Addr]*Relay{peer: r},
|
||||||
|
relayForByIdx: map[uint32]*Relay{r.LocalIndex: r},
|
||||||
|
}
|
||||||
|
hm.Hosts[relay] = hi
|
||||||
|
|
||||||
|
out.Reset()
|
||||||
|
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
||||||
|
assert.JSONEq(t, `{"Relays":[{"NebulaAddr":"10.0.0.1","RelayForAddrs":[{"Type":"forwarding","State":"established","PeerAddr":"10.0.0.2","LocalIndex":1,"RemoteIndex":2,"RelayedThrough":null}]}]}`, out.String())
|
||||||
|
|
||||||
|
// And an index mismatch names it.
|
||||||
|
hm.Relays[7] = hi
|
||||||
|
delete(hm.Relays, 1)
|
||||||
|
out.Reset()
|
||||||
|
require.NoError(t, printRelaysCommand(ifce, &printTunnelFlags{}, nil, commands.NewStringWriter(out)))
|
||||||
|
assert.Contains(t, out.String(), `"Error":"hostmap LocalIndex '7' does not match RelayState LocalIndex"`)
|
||||||
|
}
|
||||||
@@ -11,6 +11,7 @@ import (
|
|||||||
"syscall"
|
"syscall"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
"github.com/slackhq/nebula/header"
|
"github.com/slackhq/nebula/header"
|
||||||
"github.com/slackhq/nebula/overlay"
|
"github.com/slackhq/nebula/overlay"
|
||||||
)
|
)
|
||||||
@@ -47,6 +48,7 @@ type Control struct {
|
|||||||
|
|
||||||
f *Interface
|
f *Interface
|
||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
|
commands *commands.Registry
|
||||||
ctx context.Context
|
ctx context.Context
|
||||||
cancel context.CancelFunc
|
cancel context.CancelFunc
|
||||||
sshStart func()
|
sshStart func()
|
||||||
@@ -372,6 +374,13 @@ func (c *Control) CloseAllTunnels(excludeLighthouses bool) (closed int) {
|
|||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Commands returns the registry the daemon's surfaces serve from. It is the live
|
||||||
|
// registry, not a copy: a command registered on it is served to every ssh session that
|
||||||
|
// starts afterwards. Clone it to run commands without that reach.
|
||||||
|
func (c *Control) Commands() *commands.Registry {
|
||||||
|
return c.commands
|
||||||
|
}
|
||||||
|
|
||||||
func (c *Control) Device() overlay.Device {
|
func (c *Control) Device() overlay.Device {
|
||||||
return c.f.inside
|
return c.f.inside
|
||||||
}
|
}
|
||||||
|
|||||||
+49
-22
@@ -38,8 +38,8 @@ type dnsServer struct {
|
|||||||
|
|
||||||
serverMu sync.Mutex
|
serverMu sync.Mutex
|
||||||
server *dns.Server
|
server *dns.Server
|
||||||
// started is closed once `server` has finished binding (or after
|
// started is closed once `server` is serving (or after serve returns
|
||||||
// ListenAndServe returns on a bind failure). Stop waits on it before
|
// without ever starting). Stop waits on it before
|
||||||
// calling Shutdown to avoid the miekg/dns "server not started" race
|
// calling Shutdown to avoid the miekg/dns "server not started" race
|
||||||
// where a Shutdown that arrives before bind completes is silently
|
// where a Shutdown that arrives before bind completes is silently
|
||||||
// ignored, leaving the listener running forever.
|
// ignored, leaving the listener running forever.
|
||||||
@@ -84,7 +84,7 @@ func newDnsServerFromConfig(ctx context.Context, l *slog.Logger, pki *PKI, hostM
|
|||||||
}
|
}
|
||||||
|
|
||||||
// reload applies the latest config and reconciles the running state with it:
|
// reload applies the latest config and reconciles the running state with it:
|
||||||
// - enabled toggled on -> spawn a runner
|
// - enabled toggled on -> bind, then serve in the background
|
||||||
// - enabled toggled off -> stop the runner
|
// - enabled toggled off -> stop the runner
|
||||||
// - listen address changed (while running) -> restart on the new address
|
// - listen address changed (while running) -> restart on the new address
|
||||||
// - everything else -> no-op
|
// - everything else -> no-op
|
||||||
@@ -121,13 +121,20 @@ func (d *dnsServer) reload(c *config.C, initial bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if !running {
|
if running && !sameAddr {
|
||||||
// Was disabled (or never started); bring it up now.
|
// Stop clears the slot before shutting down, otherwise the listen below can find the dying server and refuse
|
||||||
go d.Start()
|
|
||||||
} else if !sameAddr {
|
|
||||||
// Stop clears the slot before shutting down, otherwise the Start below can find the dying server and refuse
|
|
||||||
d.Stop()
|
d.Stop()
|
||||||
go d.Start()
|
running = false
|
||||||
|
}
|
||||||
|
if !running {
|
||||||
|
// Bind here rather than in the background, so a reload that can't listen says so
|
||||||
|
server, started, err := d.listen()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if server != nil {
|
||||||
|
go d.serve(server, started)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// Refresh the self entry every enabled reload so cert renewals that change our name or VPN addresses are picked up.
|
// Refresh the self entry every enabled reload so cert renewals that change our name or VPN addresses are picked up.
|
||||||
@@ -156,30 +163,50 @@ func (d *dnsServer) shutdownServer(srv *dns.Server, started chan struct{}, reaso
|
|||||||
// Must be invoked after the tun device is active so that lighthouse.dns.host
|
// Must be invoked after the tun device is active so that lighthouse.dns.host
|
||||||
// may bind to a nebula IP.
|
// may bind to a nebula IP.
|
||||||
func (d *dnsServer) Start() {
|
func (d *dnsServer) Start() {
|
||||||
if !d.enabled.Load() {
|
server, started, err := d.listen()
|
||||||
|
if err != nil {
|
||||||
|
d.l.Warn("Failed to run the DNS responder", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if server != nil {
|
||||||
|
d.serve(server, started)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// listen binds the responder and claims the server slot for serve. It returns a nil server, and no error, when DNS is
|
||||||
|
// disabled or a server is already running.
|
||||||
|
func (d *dnsServer) listen() (*dns.Server, chan struct{}, error) {
|
||||||
|
if !d.enabled.Load() {
|
||||||
|
return nil, nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
started := make(chan struct{})
|
started := make(chan struct{})
|
||||||
d.serverMu.Lock()
|
d.serverMu.Lock()
|
||||||
|
defer d.serverMu.Unlock()
|
||||||
// Re-check enabled under the lock, a disable that raced our check above snapshots the slot under it too.
|
// Re-check enabled under the lock, a disable that raced our check above snapshots the slot under it too.
|
||||||
// Two reloads in quick succession can both spawn a Start, the loser would orphan the live listener past Stop
|
// A reload and Control.Start can both get here, the loser would orphan the live listener past Stop
|
||||||
if d.ctx.Err() != nil || d.server != nil || !d.enabled.Load() {
|
if d.ctx.Err() != nil || d.server != nil || !d.enabled.Load() {
|
||||||
d.serverMu.Unlock()
|
return nil, nil, nil
|
||||||
return
|
}
|
||||||
|
pc, err := net.ListenPacket("udp", d.addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, nil, err
|
||||||
}
|
}
|
||||||
addr := d.addr
|
|
||||||
server := &dns.Server{
|
server := &dns.Server{
|
||||||
Addr: addr,
|
Addr: d.addr,
|
||||||
Net: "udp",
|
PacketConn: pc,
|
||||||
Handler: d.mux,
|
Handler: d.mux,
|
||||||
NotifyStartedFunc: func() { close(started) },
|
NotifyStartedFunc: func() { close(started) },
|
||||||
}
|
}
|
||||||
d.server = server
|
d.server = server
|
||||||
d.started = started
|
d.started = started
|
||||||
d.serverMu.Unlock()
|
d.l.Info("Starting DNS responder", "dnsListener", pc.LocalAddr())
|
||||||
|
return server, started, nil
|
||||||
|
}
|
||||||
|
|
||||||
// Per-invocation ctx watcher. Exits when Start does, so we don't leak a
|
// serve answers queries on a server from listen until Stop or the context ends it.
|
||||||
|
func (d *dnsServer) serve(server *dns.Server, started chan struct{}) {
|
||||||
|
// Per-invocation ctx watcher. Exits when serve does, so we don't leak a
|
||||||
// watcher per reload-driven restart.
|
// watcher per reload-driven restart.
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
@@ -190,16 +217,16 @@ func (d *dnsServer) Start() {
|
|||||||
}
|
}
|
||||||
}()
|
}()
|
||||||
|
|
||||||
d.l.Info("Starting DNS responder", "dnsListener", addr)
|
err := server.ActivateAndServe()
|
||||||
err := server.ListenAndServe()
|
|
||||||
close(done)
|
close(done)
|
||||||
|
|
||||||
// If the listener never bound (bind error) NotifyStartedFunc never fires,
|
// If serving failed before it started NotifyStartedFunc never fires, so close started here to release any Stop
|
||||||
// so close started here to release any Stop caller waiting on it.
|
// caller waiting on it. ActivateAndServe doesn't close the socket on that path either
|
||||||
select {
|
select {
|
||||||
case <-started:
|
case <-started:
|
||||||
default:
|
default:
|
||||||
close(started)
|
close(started)
|
||||||
|
_ = server.PacketConn.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
// Release our slot, unless a reload already replaced us, so a dead listener can't block a future Start
|
// Release our slot, unless a reload already replaced us, so a dead listener can't block a future Start
|
||||||
|
|||||||
@@ -629,3 +629,22 @@ func TestDnsServer_Start_refusesWhenDisabledUnderLock(t *testing.T) {
|
|||||||
require.NoError(t, err, "an orphaned listener is still holding the port")
|
require.NoError(t, err, "an orphaned listener is still holding the port")
|
||||||
_ = pc.Close()
|
_ = pc.Close()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A reload that can't bind says so, rather than finding out in the background
|
||||||
|
func TestDnsServer_reload_busyPortFails(t *testing.T) {
|
||||||
|
busy, err := net.ListenPacket("udp", "127.0.0.1:0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = busy.Close() })
|
||||||
|
_, port, err := net.SplitHostPort(busy.LocalAddr().String())
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
ds, c := newTestDnsServer(t)
|
||||||
|
setDnsConfig(c, "127.0.0.1", port, false, true)
|
||||||
|
require.NoError(t, ds.reload(c, true))
|
||||||
|
|
||||||
|
setDnsConfig(c, "127.0.0.1", port, true, true)
|
||||||
|
require.Error(t, ds.reload(c, false))
|
||||||
|
ds.serverMu.Lock()
|
||||||
|
assert.Nil(t, ds.server, "a failed bind kept the slot")
|
||||||
|
ds.serverMu.Unlock()
|
||||||
|
}
|
||||||
@@ -0,0 +1,164 @@
|
|||||||
|
//go:build e2e_testing
|
||||||
|
|
||||||
|
package e2e
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"regexp"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/cert"
|
||||||
|
"github.com/slackhq/nebula/cert_test"
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
|
"github.com/slackhq/nebula/sshd"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// volatile lists everything in command output that legitimately differs between runs.
|
||||||
|
// Each run mints fresh certificates and indexes, so these are replaced with a marker
|
||||||
|
// before comparison. Keep the list short: anything added here is something the golden
|
||||||
|
// stops guarding.
|
||||||
|
var volatile = []struct {
|
||||||
|
re *regexp.Regexp
|
||||||
|
with string
|
||||||
|
}{
|
||||||
|
{regexp.MustCompile(`[0-9a-f]{40,}`), "<hex>"}, // fingerprints, keys, signatures
|
||||||
|
{regexp.MustCompile(`\d{4}-\d{2}-\d{2}T\d{2}:\d{2}:\d{2}(?:\.\d+)?(?:Z|[+-]\d{2}:\d{2})`), "<time>"}, // RFC 3339
|
||||||
|
{regexp.MustCompile(`\d{4}-\d{2}-\d{2} \d{2}:\d{2}:\d{2}(?:\.\d+)? [+-]\d{4} \w+`), "<time>"}, // time.Time.String()
|
||||||
|
{regexp.MustCompile(`(?s)(-----BEGIN [A-Z ]+-----\n).*?(\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // print-cert -raw
|
||||||
|
{regexp.MustCompile(`(-----BEGIN [A-Z ]+-----\\n).*?(\\n-----END [A-Z ]+-----)`), "${1}<pem>${2}"}, // the same, inside a JSON string
|
||||||
|
{regexp.MustCompile(`"(localIndex|remoteIndex|messageCounter)":\d+`), `"$1":<n>`},
|
||||||
|
{regexp.MustCompile(`(?m)^(\s*"?(?:LocalIndex|RemoteIndex|localIndex|remoteIndex|messageCounter)"?:?\s*)\d+`), "${1}<n>"}, // and the -pretty form
|
||||||
|
}
|
||||||
|
|
||||||
|
func normalise(s string) string {
|
||||||
|
for _, v := range volatile {
|
||||||
|
s = v.re.ReplaceAllString(s, v.with)
|
||||||
|
}
|
||||||
|
return s
|
||||||
|
}
|
||||||
|
|
||||||
|
// outputCase is one command line to run through the ssh dispatch path.
|
||||||
|
type outputCase struct {
|
||||||
|
name string
|
||||||
|
args []string
|
||||||
|
}
|
||||||
|
|
||||||
|
// TestCommandOutput pins the bytes every command produces, in every format, through the
|
||||||
|
// ssh dispatch path. It is the safety net for moving rendering out of the commands: a
|
||||||
|
// change here is a change someone at an ssh prompt would see. Regenerate with:
|
||||||
|
//
|
||||||
|
// go test -tags e2e_testing ./e2e -run TestCommandOutput -update
|
||||||
|
func TestCommandOutput(t *testing.T) {
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(
|
||||||
|
cert.Version1, cert.Curve_CURVE25519,
|
||||||
|
time.Now(), time.Now().Add(10*time.Minute),
|
||||||
|
nil, nil, []string{},
|
||||||
|
)
|
||||||
|
|
||||||
|
control, _, _, _ := newSimpleServer(cert.Version1, ca, caKey, "output-golden", "10.222.0.1/24", nil)
|
||||||
|
control.Start()
|
||||||
|
t.Cleanup(func() { control.Stop() })
|
||||||
|
|
||||||
|
// As an ssh session does: a clone of the daemon's registry with logout added, which
|
||||||
|
// help on the clone lists and describes. There is no session to end here.
|
||||||
|
reg := control.Commands().Clone()
|
||||||
|
reg.Register(sshd.LogoutCommand(func() {}))
|
||||||
|
|
||||||
|
// run dispatches as the ssh exec path does and returns the output and the error, which
|
||||||
|
// over ssh is exit status 1.
|
||||||
|
run := func(args ...string) (string, error) {
|
||||||
|
buf := &bytes.Buffer{}
|
||||||
|
err := reg.DispatchArgs(args, commands.NewStringWriter(buf))
|
||||||
|
return buf.String(), err
|
||||||
|
}
|
||||||
|
|
||||||
|
// Seed a pending tunnel so the listing and per-address commands have something to show.
|
||||||
|
seed, err := run("create-tunnel", "10.222.0.9")
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, "Created\n", seed, "seeding a tunnel should succeed before anything else is recorded")
|
||||||
|
|
||||||
|
cases := []outputCase{
|
||||||
|
{"version", nil},
|
||||||
|
{"device-info", nil},
|
||||||
|
{"list-hostmap", nil},
|
||||||
|
{"list-hostmap", []string{"-by-index"}},
|
||||||
|
{"list-pending-hostmap", nil},
|
||||||
|
{"list-pending-hostmap", []string{"-by-index"}},
|
||||||
|
{"list-lighthouse-addrmap", nil},
|
||||||
|
{"print-cert", nil},
|
||||||
|
{"print-cert", []string{"-raw"}},
|
||||||
|
{"print-cert", []string{"10.222.0.9"}},
|
||||||
|
{"print-cert", []string{"10.222.0.250"}},
|
||||||
|
{"print-cert", []string{"not-an-address"}},
|
||||||
|
{"print-tunnel", nil},
|
||||||
|
{"print-tunnel", []string{"10.222.0.9"}},
|
||||||
|
{"print-tunnel", []string{"10.222.0.250"}},
|
||||||
|
{"print-relays", nil},
|
||||||
|
{"query-lighthouse", nil},
|
||||||
|
{"query-lighthouse", []string{"10.222.0.9"}},
|
||||||
|
{"query-lighthouse", []string{"not-an-address"}},
|
||||||
|
// The test logger cannot change level or format, so these cases record the
|
||||||
|
// refusal each command prints on it, not a successful change.
|
||||||
|
{"log-level", nil},
|
||||||
|
{"log-level", []string{"debug"}},
|
||||||
|
{"log-level", []string{"bogus"}},
|
||||||
|
{"log-level", []string{"info"}},
|
||||||
|
{"log-format", nil},
|
||||||
|
{"log-format", []string{"json"}},
|
||||||
|
{"log-format", []string{"text"}},
|
||||||
|
{"change-remote", nil},
|
||||||
|
{"change-remote", []string{"10.222.0.9"}},
|
||||||
|
{"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.9"}},
|
||||||
|
{"change-remote", []string{"-address", "10.94.0.10:4242", "10.222.0.250"}},
|
||||||
|
{"create-tunnel", nil},
|
||||||
|
{"create-tunnel", []string{"10.222.0.9"}},
|
||||||
|
// A fresh address, so this exercises the handshake start with a remote attached
|
||||||
|
// rather than the "already handshaking" path above.
|
||||||
|
{"create-tunnel", []string{"-address", "10.94.0.10:4242", "10.222.0.10"}},
|
||||||
|
{"create-tunnel", []string{"not-an-address"}},
|
||||||
|
{"close-tunnel", nil},
|
||||||
|
{"close-tunnel", []string{"10.222.0.250"}},
|
||||||
|
{"close-tunnel", []string{"not-an-address"}},
|
||||||
|
{"help", nil},
|
||||||
|
{"help", []string{"print-cert"}},
|
||||||
|
{"help", []string{"logout"}},
|
||||||
|
{"logout", []string{"-h"}},
|
||||||
|
{"not-a-command", []string{"10.222.0.9"}},
|
||||||
|
// reload is last so that whatever a reload does to the daemon cannot leak into an
|
||||||
|
// earlier case. In this harness it fails: the config was loaded from a string and
|
||||||
|
// has no path to re-read, which is what the recorded result says.
|
||||||
|
{"reload", nil},
|
||||||
|
}
|
||||||
|
|
||||||
|
got := &strings.Builder{}
|
||||||
|
for _, c := range cases {
|
||||||
|
for _, fmtFlags := range [][]string{nil, {"-json"}, {"-pretty"}} {
|
||||||
|
args := append([]string{c.name}, fmtFlags...)
|
||||||
|
args = append(args, c.args...)
|
||||||
|
|
||||||
|
fmt.Fprintf(got, "$ %s\n", strings.Join(args, " "))
|
||||||
|
out, err := run(args...)
|
||||||
|
out = normalise(out)
|
||||||
|
got.WriteString(out)
|
||||||
|
// Output that does not end in a newline is recorded as such, the way diff marks
|
||||||
|
// it, so the golden sees that byte too and the next prompt still starts a line.
|
||||||
|
if out != "" && !strings.HasSuffix(out, "\n") {
|
||||||
|
got.WriteString("\\ No newline at end of output\n")
|
||||||
|
}
|
||||||
|
// A failed command is recorded as the exit status an ssh caller sees, so the
|
||||||
|
// golden pins which outcomes count as failures.
|
||||||
|
if err != nil {
|
||||||
|
got.WriteString("exit status 1\n")
|
||||||
|
}
|
||||||
|
got.WriteString("\n")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
test.Golden(t, filepath.Join("testdata", "commands_output.golden"), got.String())
|
||||||
|
}
|
||||||
Vendored
+720
@@ -0,0 +1,720 @@
|
|||||||
|
$ version
|
||||||
|
e2e-test
|
||||||
|
|
||||||
|
$ version -json
|
||||||
|
e2e-test
|
||||||
|
|
||||||
|
$ version -pretty
|
||||||
|
e2e-test
|
||||||
|
|
||||||
|
$ device-info
|
||||||
|
name= cidr=[10.222.0.1/24]
|
||||||
|
|
||||||
|
$ device-info -json
|
||||||
|
{"name":"","cidr":["10.222.0.1/24"]}
|
||||||
|
|
||||||
|
$ device-info -pretty
|
||||||
|
{
|
||||||
|
"name": "",
|
||||||
|
"cidr": [
|
||||||
|
"10.222.0.1/24"
|
||||||
|
]
|
||||||
|
}
|
||||||
|
|
||||||
|
$ list-hostmap
|
||||||
|
|
||||||
|
$ list-hostmap -json
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-hostmap -pretty
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-hostmap -by-index
|
||||||
|
|
||||||
|
$ list-hostmap -json -by-index
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-hostmap -pretty -by-index
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-pending-hostmap
|
||||||
|
[10.222.0.9]: []
|
||||||
|
|
||||||
|
$ list-pending-hostmap -json
|
||||||
|
[{"vpnAddrs":["10.222.0.9"],"localIndex":<n>,"remoteIndex":<n>,"remoteAddrs":[],"cert":null,"messageCounter":<n>,"currentRemote":"","currentRelaysToMe":[],"currentRelaysThroughMe":[]}]
|
||||||
|
|
||||||
|
$ list-pending-hostmap -pretty
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"vpnAddrs": [
|
||||||
|
"10.222.0.9"
|
||||||
|
],
|
||||||
|
"localIndex": <n>,
|
||||||
|
"remoteIndex": <n>,
|
||||||
|
"remoteAddrs": [],
|
||||||
|
"cert": null,
|
||||||
|
"messageCounter": <n>,
|
||||||
|
"currentRemote": "",
|
||||||
|
"currentRelaysToMe": [],
|
||||||
|
"currentRelaysThroughMe": []
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
$ list-pending-hostmap -by-index
|
||||||
|
|
||||||
|
$ list-pending-hostmap -json -by-index
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-pending-hostmap -pretty -by-index
|
||||||
|
[]
|
||||||
|
|
||||||
|
$ list-lighthouse-addrmap
|
||||||
|
10.222.0.9: {}
|
||||||
|
|
||||||
|
$ list-lighthouse-addrmap -json
|
||||||
|
[{"vpnAddr":"10.222.0.9","addrs":{}}]
|
||||||
|
|
||||||
|
$ list-lighthouse-addrmap -pretty
|
||||||
|
[
|
||||||
|
{
|
||||||
|
"vpnAddr": "10.222.0.9",
|
||||||
|
"addrs": {}
|
||||||
|
}
|
||||||
|
]
|
||||||
|
|
||||||
|
$ print-cert
|
||||||
|
{
|
||||||
|
"details": {
|
||||||
|
"curve": "CURVE25519",
|
||||||
|
"groups": [],
|
||||||
|
"isCa": false,
|
||||||
|
"issuer": "<hex>",
|
||||||
|
"name": "output-golden",
|
||||||
|
"networks": [
|
||||||
|
"10.222.0.1/24"
|
||||||
|
],
|
||||||
|
"notAfter": "<time>",
|
||||||
|
"notBefore": "<time>",
|
||||||
|
"publicKey": "<hex>",
|
||||||
|
"unsafeNetworks": []
|
||||||
|
},
|
||||||
|
"fingerprint": "<hex>",
|
||||||
|
"signature": "<hex>",
|
||||||
|
"version": 1
|
||||||
|
}
|
||||||
|
|
||||||
|
$ print-cert -json
|
||||||
|
{"details":{"curve":"CURVE25519","groups":[],"isCa":false,"issuer":"<hex>","name":"output-golden","networks":["10.222.0.1/24"],"notAfter":"<time>","notBefore":"<time>","publicKey":"<hex>","unsafeNetworks":[]},"fingerprint":"<hex>","signature":"<hex>","version":1}\ No newline at end of output
|
||||||
|
|
||||||
|
$ print-cert -pretty
|
||||||
|
{
|
||||||
|
"details": {
|
||||||
|
"curve": "CURVE25519",
|
||||||
|
"groups": [],
|
||||||
|
"isCa": false,
|
||||||
|
"issuer": "<hex>",
|
||||||
|
"name": "output-golden",
|
||||||
|
"networks": [
|
||||||
|
"10.222.0.1/24"
|
||||||
|
],
|
||||||
|
"notAfter": "<time>",
|
||||||
|
"notBefore": "<time>",
|
||||||
|
"publicKey": "<hex>",
|
||||||
|
"unsafeNetworks": []
|
||||||
|
},
|
||||||
|
"fingerprint": "<hex>",
|
||||||
|
"signature": "<hex>",
|
||||||
|
"version": 1
|
||||||
|
}\ No newline at end of output
|
||||||
|
|
||||||
|
$ print-cert -raw
|
||||||
|
-----BEGIN NEBULA CERTIFICATE-----
|
||||||
|
<pem>
|
||||||
|
-----END NEBULA CERTIFICATE-----
|
||||||
|
|
||||||
|
$ print-cert -json -raw
|
||||||
|
{"details":{"curve":"CURVE25519","groups":[],"isCa":false,"issuer":"<hex>","name":"output-golden","networks":["10.222.0.1/24"],"notAfter":"<time>","notBefore":"<time>","publicKey":"<hex>","unsafeNetworks":[]},"fingerprint":"<hex>","signature":"<hex>","version":1}\ No newline at end of output
|
||||||
|
|
||||||
|
$ print-cert -pretty -raw
|
||||||
|
{
|
||||||
|
"details": {
|
||||||
|
"curve": "CURVE25519",
|
||||||
|
"groups": [],
|
||||||
|
"isCa": false,
|
||||||
|
"issuer": "<hex>",
|
||||||
|
"name": "output-golden",
|
||||||
|
"networks": [
|
||||||
|
"10.222.0.1/24"
|
||||||
|
],
|
||||||
|
"notAfter": "<time>",
|
||||||
|
"notBefore": "<time>",
|
||||||
|
"publicKey": "<hex>",
|
||||||
|
"unsafeNetworks": []
|
||||||
|
},
|
||||||
|
"fingerprint": "<hex>",
|
||||||
|
"signature": "<hex>",
|
||||||
|
"version": 1
|
||||||
|
}\ No newline at end of output
|
||||||
|
|
||||||
|
$ print-cert 10.222.0.9
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -json 10.222.0.9
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -pretty 10.222.0.9
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert 10.222.0.250
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -json 10.222.0.250
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -pretty 10.222.0.250
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert not-an-address
|
||||||
|
The provided vpn addr could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -json not-an-address
|
||||||
|
The provided vpn addr could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-cert -pretty not-an-address
|
||||||
|
The provided vpn addr could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -json
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -pretty
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel 10.222.0.9
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -json 10.222.0.9
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -pretty 10.222.0.9
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel 10.222.0.250
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -json 10.222.0.250
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-tunnel -pretty 10.222.0.250
|
||||||
|
Could not find tunnel for vpn addr: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-relays
|
||||||
|
{"Relays":null}
|
||||||
|
|
||||||
|
$ print-relays -json
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ print-relays -pretty
|
||||||
|
{
|
||||||
|
"Relays": null
|
||||||
|
}
|
||||||
|
|
||||||
|
$ query-lighthouse
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse -json
|
||||||
|
The provided vpn address could not be parsed: -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse -pretty
|
||||||
|
The provided vpn address could not be parsed: -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse 10.222.0.9
|
||||||
|
{}
|
||||||
|
|
||||||
|
$ query-lighthouse -json 10.222.0.9
|
||||||
|
The provided vpn address could not be parsed: -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse -pretty 10.222.0.9
|
||||||
|
The provided vpn address could not be parsed: -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse not-an-address
|
||||||
|
The provided vpn address could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse -json not-an-address
|
||||||
|
The provided vpn address could not be parsed: -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ query-lighthouse -pretty not-an-address
|
||||||
|
The provided vpn address could not be parsed: -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -json
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -pretty
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level debug
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -json debug
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -pretty debug
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level bogus
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -json bogus
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -pretty bogus
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level info
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -json info
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-level -pretty info
|
||||||
|
Log level is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -json
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -pretty
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format json
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -json json
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -pretty json
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format text
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -json text
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ log-format -pretty text
|
||||||
|
Log format is not reconfigurable on this logger
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -json
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -pretty
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote 10.222.0.9
|
||||||
|
No address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -json 10.222.0.9
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -pretty 10.222.0.9
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -address 10.94.0.10:4242 10.222.0.9
|
||||||
|
Could not find tunnel for vpn address: 10.222.0.9
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -json -address 10.94.0.10:4242 10.222.0.9
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -pretty -address 10.94.0.10:4242 10.222.0.9
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -address 10.94.0.10:4242 10.222.0.250
|
||||||
|
Could not find tunnel for vpn address: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -json -address 10.94.0.10:4242 10.222.0.250
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ change-remote -pretty -address 10.94.0.10:4242 10.222.0.250
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -json
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -pretty
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel 10.222.0.9
|
||||||
|
Tunnel already handshaking
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -json 10.222.0.9
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -pretty 10.222.0.9
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -address 10.94.0.10:4242 10.222.0.10
|
||||||
|
Created
|
||||||
|
|
||||||
|
$ create-tunnel -json -address 10.94.0.10:4242 10.222.0.10
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -pretty -address 10.94.0.10:4242 10.222.0.10
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel not-an-address
|
||||||
|
The provided vpn address could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -json not-an-address
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ create-tunnel -pretty not-an-address
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel
|
||||||
|
No vpn address was provided
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -json
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -pretty
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel 10.222.0.250
|
||||||
|
Could not find tunnel for vpn address: 10.222.0.250
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -json 10.222.0.250
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -pretty 10.222.0.250
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel not-an-address
|
||||||
|
The provided vpn address could not be parsed: not-an-address
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -json not-an-address
|
||||||
|
flag provided but not defined: -json
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ close-tunnel -pretty not-an-address
|
||||||
|
flag provided but not defined: -pretty
|
||||||
|
Usage:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help
|
||||||
|
Available commands:
|
||||||
|
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
||||||
|
close-tunnel - Closes a tunnel for the provided vpn addr
|
||||||
|
create-tunnel - Creates a tunnel for the provided vpn address
|
||||||
|
device-info - Prints information about the network device.
|
||||||
|
help - prints available commands or help <command> for specific usage info
|
||||||
|
list-hostmap - List all known previously connected hosts
|
||||||
|
list-lighthouse-addrmap - List all lighthouse map entries
|
||||||
|
list-pending-hostmap - List all handshaking hosts
|
||||||
|
log-format - Gets or sets the current log format
|
||||||
|
log-level - Gets or sets the current log level
|
||||||
|
logout - Ends the current session
|
||||||
|
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
print-relays - Prints json details about all relay info
|
||||||
|
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
||||||
|
query-lighthouse - Query the lighthouses for the provided vpn address
|
||||||
|
reload - Reloads configuration from disk, same as sending HUP to the process
|
||||||
|
version - Prints the currently running version of nebula
|
||||||
|
|
||||||
|
|
||||||
|
$ help -json
|
||||||
|
Command not available -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help -pretty
|
||||||
|
Command not available -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help print-cert
|
||||||
|
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
-json
|
||||||
|
outputs as json
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
-raw
|
||||||
|
raw prints the PEM encoded certificate, not compatible with -json or -pretty
|
||||||
|
|
||||||
|
$ help -json print-cert
|
||||||
|
Command not available -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help -pretty print-cert
|
||||||
|
Command not available -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help logout
|
||||||
|
logout - Ends the current session
|
||||||
|
|
||||||
|
$ help -json logout
|
||||||
|
Command not available -json
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ help -pretty logout
|
||||||
|
Command not available -pretty
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ logout -h
|
||||||
|
logout - Ends the current session
|
||||||
|
|
||||||
|
$ logout -json -h
|
||||||
|
logout - Ends the current session
|
||||||
|
|
||||||
|
$ logout -pretty -h
|
||||||
|
logout - Ends the current session
|
||||||
|
|
||||||
|
$ not-a-command 10.222.0.9
|
||||||
|
did not understand: not-a-command
|
||||||
|
Available commands:
|
||||||
|
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
||||||
|
close-tunnel - Closes a tunnel for the provided vpn addr
|
||||||
|
create-tunnel - Creates a tunnel for the provided vpn address
|
||||||
|
device-info - Prints information about the network device.
|
||||||
|
help - prints available commands or help <command> for specific usage info
|
||||||
|
list-hostmap - List all known previously connected hosts
|
||||||
|
list-lighthouse-addrmap - List all lighthouse map entries
|
||||||
|
list-pending-hostmap - List all handshaking hosts
|
||||||
|
log-format - Gets or sets the current log format
|
||||||
|
log-level - Gets or sets the current log level
|
||||||
|
logout - Ends the current session
|
||||||
|
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
print-relays - Prints json details about all relay info
|
||||||
|
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
||||||
|
query-lighthouse - Query the lighthouses for the provided vpn address
|
||||||
|
reload - Reloads configuration from disk, same as sending HUP to the process
|
||||||
|
version - Prints the currently running version of nebula
|
||||||
|
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ not-a-command -json 10.222.0.9
|
||||||
|
did not understand: not-a-command
|
||||||
|
Available commands:
|
||||||
|
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
||||||
|
close-tunnel - Closes a tunnel for the provided vpn addr
|
||||||
|
create-tunnel - Creates a tunnel for the provided vpn address
|
||||||
|
device-info - Prints information about the network device.
|
||||||
|
help - prints available commands or help <command> for specific usage info
|
||||||
|
list-hostmap - List all known previously connected hosts
|
||||||
|
list-lighthouse-addrmap - List all lighthouse map entries
|
||||||
|
list-pending-hostmap - List all handshaking hosts
|
||||||
|
log-format - Gets or sets the current log format
|
||||||
|
log-level - Gets or sets the current log level
|
||||||
|
logout - Ends the current session
|
||||||
|
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
print-relays - Prints json details about all relay info
|
||||||
|
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
||||||
|
query-lighthouse - Query the lighthouses for the provided vpn address
|
||||||
|
reload - Reloads configuration from disk, same as sending HUP to the process
|
||||||
|
version - Prints the currently running version of nebula
|
||||||
|
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ not-a-command -pretty 10.222.0.9
|
||||||
|
did not understand: not-a-command
|
||||||
|
Available commands:
|
||||||
|
change-remote - Changes the remote address used in the tunnel for the provided vpn addr
|
||||||
|
close-tunnel - Closes a tunnel for the provided vpn addr
|
||||||
|
create-tunnel - Creates a tunnel for the provided vpn address
|
||||||
|
device-info - Prints information about the network device.
|
||||||
|
help - prints available commands or help <command> for specific usage info
|
||||||
|
list-hostmap - List all known previously connected hosts
|
||||||
|
list-lighthouse-addrmap - List all lighthouse map entries
|
||||||
|
list-pending-hostmap - List all handshaking hosts
|
||||||
|
log-format - Gets or sets the current log format
|
||||||
|
log-level - Gets or sets the current log level
|
||||||
|
logout - Ends the current session
|
||||||
|
print-cert - Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
print-relays - Prints json details about all relay info
|
||||||
|
print-tunnel - Prints json details about a tunnel for the provided vpn addr
|
||||||
|
query-lighthouse - Query the lighthouses for the provided vpn address
|
||||||
|
reload - Reloads configuration from disk, same as sending HUP to the process
|
||||||
|
version - Prints the currently running version of nebula
|
||||||
|
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ reload
|
||||||
|
Reloading config
|
||||||
|
Config reload failed, the new config did not load: no config files found at
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ reload -json
|
||||||
|
Reloading config
|
||||||
|
Config reload failed, the new config did not load: no config files found at
|
||||||
|
exit status 1
|
||||||
|
|
||||||
|
$ reload -pretty
|
||||||
|
Reloading config
|
||||||
|
Config reload failed, the new config did not load: no config files found at
|
||||||
|
exit status 1
|
||||||
|
|
||||||
+1
-7
@@ -8,6 +8,7 @@ pki:
|
|||||||
cert: /etc/nebula/host.crt
|
cert: /etc/nebula/host.crt
|
||||||
key: /etc/nebula/host.key
|
key: /etc/nebula/host.key
|
||||||
# blocklist is a list of certificate fingerprints that we will refuse to talk to
|
# blocklist is a list of certificate fingerprints that we will refuse to talk to
|
||||||
|
# Entries are the hex fingerprints shown by `nebula-cert print`, an invalid entry fails startup and reload.
|
||||||
#blocklist:
|
#blocklist:
|
||||||
# - c99d4e650533b92061b09918e838a5a0a6aaee21eed1d12fd937682865936c72
|
# - c99d4e650533b92061b09918e838a5a0a6aaee21eed1d12fd937682865936c72
|
||||||
# disconnect_invalid is a toggle to force a client to be disconnected if the certificate is expired or invalid.
|
# disconnect_invalid is a toggle to force a client to be disconnected if the certificate is expired or invalid.
|
||||||
@@ -232,13 +233,6 @@ punchy:
|
|||||||
# Trusted SSH CA public keys. These are the public keys of the CAs that are allowed to sign SSH keys for access.
|
# Trusted SSH CA public keys. These are the public keys of the CAs that are allowed to sign SSH keys for access.
|
||||||
#trusted_cas:
|
#trusted_cas:
|
||||||
#- "ssh public key string"
|
#- "ssh public key string"
|
||||||
# sandbox_dir restricts file paths for profiling commands (start-cpu-profile, save-heap-profile,
|
|
||||||
# save-mutex-profile) to the specified directory. Relative paths will be resolved within this directory,
|
|
||||||
# and absolute paths outside of it will be rejected. Default is $TMP/nebula-debug.
|
|
||||||
# The directory is NOT automatically created.
|
|
||||||
# Overriding this to "" is the same as "/" and will allow overwriting any path on the host.
|
|
||||||
# Not reloadable.
|
|
||||||
#sandbox_dir: /var/tmp/nebula-debug
|
|
||||||
|
|
||||||
# EXPERIMENTAL: relay support for networks that can't establish direct connections.
|
# EXPERIMENTAL: relay support for networks that can't establish direct connections.
|
||||||
relay:
|
relay:
|
||||||
|
|||||||
@@ -7,7 +7,6 @@ require (
|
|||||||
filippo.io/bigmod v0.1.0
|
filippo.io/bigmod v0.1.0
|
||||||
github.com/DefinedNet/netlink v1.3.2-dn.1
|
github.com/DefinedNet/netlink v1.3.2-dn.1
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be
|
||||||
github.com/armon/go-radix v1.0.0
|
|
||||||
github.com/flynn/noise v1.1.0
|
github.com/flynn/noise v1.1.0
|
||||||
github.com/gaissmai/bart v0.29.0
|
github.com/gaissmai/bart v0.29.0
|
||||||
github.com/gogo/protobuf v1.3.2
|
github.com/gogo/protobuf v1.3.2
|
||||||
|
|||||||
@@ -12,8 +12,6 @@ github.com/alecthomas/units v0.0.0-20190717042225-c3de453c63f4/go.mod h1:ybxpYRF
|
|||||||
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
github.com/alecthomas/units v0.0.0-20190924025748-f65c72e2690d/go.mod h1:rBZYJk541a8SKzHPHnH3zbiI+7dagKZ0cgpgrD7Fyho=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be h1:9AeTilPcZAjCFIImctFaOjnTIavg87rW78vTPkQqLI8=
|
||||||
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
github.com/anmitsu/go-shlex v0.0.0-20200514113438-38f4b401e2be/go.mod h1:ySMOLuWl6zY27l47sB3qLNK6tF2fkHG55UZxx8oIVo4=
|
||||||
github.com/armon/go-radix v1.0.0 h1:F4z6KzEeeQIMeLFa97iZU6vupzoecKdU5TX24SNppXI=
|
|
||||||
github.com/armon/go-radix v1.0.0/go.mod h1:ufUuZ+zHj4x4TnLV4JWEpy2hxWSpsRywHrMgIH9cCH8=
|
|
||||||
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
github.com/beorn7/perks v0.0.0-20180321164747-3a771d992973/go.mod h1:Dwedo/Wpr24TaqPxmxbtue+5NUziq4I4S80YR8gNf3Q=
|
||||||
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
github.com/beorn7/perks v1.0.0/go.mod h1:KWe93zE9D1o94FZ5RNwFwVgaQK1VOXiVxmqh+CedLV8=
|
||||||
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
github.com/beorn7/perks v1.0.1 h1:VlbKKnNfV8bJzeqoa4cOKqO6bYr3WgKZxO8Z16+hsOM=
|
||||||
|
|||||||
@@ -12,6 +12,7 @@ import (
|
|||||||
"strings"
|
"strings"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/cpupick"
|
"github.com/slackhq/nebula/cpupick"
|
||||||
"github.com/slackhq/nebula/noiseutil"
|
"github.com/slackhq/nebula/noiseutil"
|
||||||
@@ -71,7 +72,11 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
}
|
}
|
||||||
l.Info("Firewall started", "firewallHashes", fw.GetRuleHashes())
|
l.Info("Firewall started", "firewallHashes", fw.GetRuleHashes())
|
||||||
|
|
||||||
ssh, err := sshd.NewSSHServer(ctx, l.With("subsystem", "sshd"))
|
// The command registry is shared by every surface that exposes these commands, so a
|
||||||
|
// command cannot reach one of them without reaching the others.
|
||||||
|
cmdRegistry := commands.New()
|
||||||
|
|
||||||
|
ssh, err := sshd.NewSSHServer(ctx, l.With("subsystem", "sshd"), cmdRegistry)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, util.ContextualizeIfNeeded("Error while creating SSH server", err)
|
return nil, util.ContextualizeIfNeeded("Error while creating SSH server", err)
|
||||||
}
|
}
|
||||||
@@ -343,13 +348,14 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
return nil, nil
|
return nil, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
attachCommands(l, c, ssh, ifce)
|
attachCommands(l, c, cmdRegistry, ifce)
|
||||||
|
|
||||||
networkChanges := udp.NewNetworkChangeMonitor(ctx, l, c)
|
networkChanges := udp.NewNetworkChangeMonitor(ctx, l, c)
|
||||||
|
|
||||||
return &Control{
|
return &Control{
|
||||||
state: StateReady,
|
state: StateReady,
|
||||||
f: ifce,
|
f: ifce,
|
||||||
|
commands: cmdRegistry,
|
||||||
l: l,
|
l: l,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
|
|||||||
+3
-1
@@ -476,7 +476,9 @@ func parseV4(data []byte, incoming bool, fp *firewall.ParsedPacket) error {
|
|||||||
func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, messageCounter uint64, out []byte, rxc *rxContext) {
|
func (f *Interface) handleOutsideMessagePacket(hostinfo *HostInfo, messageCounter uint64, out []byte, rxc *rxContext) {
|
||||||
err := newPacket(out, true, rxc.fwPacket)
|
err := newPacket(out, true, rxc.fwPacket)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
hostinfo.logger(f.l).Warn("Error while validating inbound packet", "error", err, "packet", out)
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
|
hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
|
||||||
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -0,0 +1,121 @@
|
|||||||
|
//go:build !ios && !e2e_testing
|
||||||
|
|
||||||
|
package overlay
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"os"
|
||||||
|
"syscall"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
func TestTun_WriteFraming(t *testing.T) {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer r.Close()
|
||||||
|
defer w.Close()
|
||||||
|
tn := &tun{f: w}
|
||||||
|
|
||||||
|
for _, tc := range []struct {
|
||||||
|
name string
|
||||||
|
pkt []byte
|
||||||
|
af byte
|
||||||
|
}{
|
||||||
|
// Literal families rather than syscall.AF_*, so a wrong constant in Write can't also fix the expectation.
|
||||||
|
{"ipv4", []byte{0x45, 1, 2, 3, 4, 5}, 2},
|
||||||
|
{"ipv6", []byte{0x60, 1, 2, 3, 4, 5}, 30},
|
||||||
|
} {
|
||||||
|
t.Run(tc.name, func(t *testing.T) {
|
||||||
|
n, err := tn.Write(tc.pkt)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, len(tc.pkt), n)
|
||||||
|
|
||||||
|
got := make([]byte, 64)
|
||||||
|
n, err = r.Read(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, append([]byte{0, 0, 0, tc.af}, tc.pkt...), got[:n])
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
|
_, err = tn.Write([]byte{0x10, 1, 2, 3})
|
||||||
|
require.Error(t, err)
|
||||||
|
_, err = tn.Write([]byte{})
|
||||||
|
require.ErrorIs(t, err, syscall.EIO)
|
||||||
|
|
||||||
|
// The rejected writes must not have put anything on the fd, so the next read sees only this packet.
|
||||||
|
pkt := []byte{0x45, 9, 9}
|
||||||
|
_, err = tn.Write(pkt)
|
||||||
|
require.NoError(t, err)
|
||||||
|
got := make([]byte, 64)
|
||||||
|
n, err := r.Read(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, append([]byte{0, 0, 0, 2}, pkt...), got[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTun_ReadStripsHeader(t *testing.T) {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer r.Close()
|
||||||
|
defer w.Close()
|
||||||
|
tn := &tun{f: r}
|
||||||
|
|
||||||
|
pkt := []byte{0x45, 1, 2, 3, 4, 5}
|
||||||
|
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
got := make([]byte, 64)
|
||||||
|
n, err := tn.Read(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, pkt, got[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestTun_ReadShort(t *testing.T) {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer r.Close()
|
||||||
|
defer w.Close()
|
||||||
|
tn := &tun{f: r}
|
||||||
|
|
||||||
|
// Fewer bytes than the protocol header is not a packet.
|
||||||
|
_, err = w.Write([]byte{0, 0})
|
||||||
|
require.NoError(t, err)
|
||||||
|
n, err := tn.Read(make([]byte, 64))
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Zero(t, n)
|
||||||
|
|
||||||
|
// The next packet reads cleanly only if the short read consumed exactly what was written.
|
||||||
|
pkt := []byte{0x45, 1, 2, 3}
|
||||||
|
_, err = w.Write(append([]byte{0, 0, 0, syscall.AF_INET}, pkt...))
|
||||||
|
require.NoError(t, err)
|
||||||
|
got := make([]byte, 64)
|
||||||
|
n, err = tn.Read(got)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.Equal(t, pkt, got[:n])
|
||||||
|
}
|
||||||
|
|
||||||
|
// A heap allocation per packet rarely shows in a short throughput run, only across GC pauses, so
|
||||||
|
// this pins Read and Write to zero allocations directly.
|
||||||
|
func TestTun_ReadWriteDoNotAllocate(t *testing.T) {
|
||||||
|
r, w, err := os.Pipe()
|
||||||
|
require.NoError(t, err)
|
||||||
|
defer r.Close()
|
||||||
|
defer w.Close()
|
||||||
|
tw := &tun{f: w}
|
||||||
|
tr := &tun{f: r}
|
||||||
|
|
||||||
|
pkts := [][]byte{bytes.Repeat([]byte{0x45}, 1300), bytes.Repeat([]byte{0x60}, 1300)}
|
||||||
|
buf := make([]byte, 1500)
|
||||||
|
i := 0
|
||||||
|
allocs := testing.AllocsPerRun(1000, func() {
|
||||||
|
i++
|
||||||
|
if _, err := tw.Write(pkts[i%len(pkts)]); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
if _, err := tr.Read(buf); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
})
|
||||||
|
require.Zero(t, allocs)
|
||||||
|
}
|
||||||
@@ -2,7 +2,9 @@ package nebula
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/fips140"
|
"crypto/fips140"
|
||||||
|
"crypto/sha256"
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
|
"encoding/hex"
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -233,12 +235,14 @@ func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
|||||||
|
|
||||||
func (p *PKI) reloadCAPool(c *config.C) *util.ContextualError {
|
func (p *PKI) reloadCAPool(c *config.C) *util.ContextualError {
|
||||||
caPool, err := loadCAPoolFromConfig(p.l, c)
|
caPool, err := loadCAPoolFromConfig(p.l, c)
|
||||||
|
if caPool != nil {
|
||||||
|
p.caPool.Store(caPool)
|
||||||
|
p.l.Debug("Trusted CA fingerprints", "fingerprints", caPool.GetFingerprints())
|
||||||
|
}
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return util.NewContextualError("Failed to load ca from config", nil, err)
|
return util.NewContextualError("Failed to load ca from config", nil, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
p.caPool.Store(caPool)
|
|
||||||
p.l.Debug("Trusted CA fingerprints", "fingerprints", caPool.GetFingerprints())
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -613,11 +617,22 @@ func loadCAPoolFromConfig(l *slog.Logger, c *config.C) (*cert.CAPool, error) {
|
|||||||
|
|
||||||
bl := c.GetStringSlice("pki.blocklist", []string{})
|
bl := c.GetStringSlice("pki.blocklist", []string{})
|
||||||
if len(bl) > 0 {
|
if len(bl) > 0 {
|
||||||
for _, fp := range bl {
|
var bad []string
|
||||||
|
for i, raw := range bl {
|
||||||
|
// Fingerprints are compared as lowercase hex strings, a malformed entry would silently block nothing
|
||||||
|
fp := strings.ToLower(strings.TrimSpace(raw))
|
||||||
|
if b, err := hex.DecodeString(fp); err != nil || len(b) != sha256.Size {
|
||||||
|
bad = append(bad, fmt.Sprintf("entry %d %q", i+1, raw))
|
||||||
|
continue
|
||||||
|
}
|
||||||
caPool.BlocklistFingerprint(fp)
|
caPool.BlocklistFingerprint(fp)
|
||||||
}
|
}
|
||||||
|
|
||||||
l.Info("Blocklisted certificates", "fingerprintCount", len(bl))
|
l.Info("Blocklisted certificates", "fingerprintCount", len(bl)-len(bad))
|
||||||
|
if len(bad) > 0 {
|
||||||
|
// The valid entries are still returned so a failed reload blocks them, startup refuses the error
|
||||||
|
return caPool, fmt.Errorf("pki.blocklist entries are not hex certificate fingerprints: %s", strings.Join(bad, ", "))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
return caPool, nil
|
return caPool, nil
|
||||||
|
|||||||
+43
@@ -217,3 +217,46 @@ func TestPKIReloadChachapolyInFIPSOnlyFails(t *testing.T) {
|
|||||||
assert.True(t, res.Failed)
|
assert.True(t, res.Failed)
|
||||||
assert.False(t, res.RestartRequired)
|
assert.False(t, res.RestartRequired)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestLoadCAPoolBlocklist(t *testing.T) {
|
||||||
|
ca, _, caKey, caPEM := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Time{}, time.Time{}, nil, nil, nil)
|
||||||
|
crt, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")}, nil, nil)
|
||||||
|
fp, err := crt.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
load := func(entry string) (*cert.CAPool, error) {
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
require.NoError(t, c.LoadString(fmt.Sprintf("pki:\n ca: %q\n blocklist:\n - %q\n", caPEM, entry)))
|
||||||
|
return loadCAPoolFromConfig(test.NewLogger(), c)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range []string{fp, strings.ToUpper(fp), " " + fp + "\t"} {
|
||||||
|
pool, err := load(entry)
|
||||||
|
require.NoError(t, err, entry)
|
||||||
|
assert.True(t, pool.IsBlocklisted(fp), entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, entry := range []string{fp[1:], fp + "00", "sha256:" + fp, "zz" + fp[2:], ""} {
|
||||||
|
_, err := load(entry)
|
||||||
|
require.Error(t, err, entry)
|
||||||
|
}
|
||||||
|
|
||||||
|
// A bad entry on reload fails it, the valid entries are still blocked
|
||||||
|
_, _, keyPEM, certPEM := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, "me", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.1/24")}, nil, nil)
|
||||||
|
pkiConfig := func(entry string) string {
|
||||||
|
return fmt.Sprintf("pki:\n ca: %q\n cert: %q\n key: %q\n blocklist:\n - %q\n", caPEM, certPEM, keyPEM, entry)
|
||||||
|
}
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
require.NoError(t, c.LoadString(pkiConfig(fp)))
|
||||||
|
p, err := NewPKIFromConfig(test.NewLogger(), c)
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
other, _, _, _ := cert_test.NewTestCert(cert.Version2, cert.Curve_CURVE25519, ca, caKey, "other", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.3/24")}, nil, nil)
|
||||||
|
otherFp, err := other.Fingerprint()
|
||||||
|
require.NoError(t, err)
|
||||||
|
|
||||||
|
res := c.ReloadConfigString(pkiConfig(fp) + fmt.Sprintf(" - %q\n - %q\n", otherFp, fp[1:]))
|
||||||
|
assert.True(t, res.Failed)
|
||||||
|
assert.True(t, p.GetCAPool().IsBlocklisted(fp))
|
||||||
|
assert.True(t, p.GetCAPool().IsBlocklisted(otherFp))
|
||||||
|
}
|
||||||
+4
-2
@@ -36,11 +36,13 @@ func (l *tcpListener) Accept() (net.Conn, error) {
|
|||||||
|
|
||||||
func (l *tcpListener) Close() error {
|
func (l *tcpListener) Close() error {
|
||||||
l.closeOnce.Do(func() {
|
l.closeOnce.Do(func() {
|
||||||
|
// Close before leaving the map so a concurrent closeListeners that
|
||||||
|
// no longer sees this listener knows Accept is already released.
|
||||||
|
close(l.closed)
|
||||||
|
|
||||||
l.s.mu.Lock()
|
l.s.mu.Lock()
|
||||||
delete(l.s.mu.listeners, l.port)
|
delete(l.s.mu.listeners, l.port)
|
||||||
l.s.mu.Unlock()
|
l.s.mu.Unlock()
|
||||||
|
|
||||||
close(l.closed)
|
|
||||||
})
|
})
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|||||||
+70
-7
@@ -32,13 +32,18 @@ import (
|
|||||||
const nicID = 1
|
const nicID = 1
|
||||||
|
|
||||||
type Service struct {
|
type Service struct {
|
||||||
eg *errgroup.Group
|
|
||||||
control *nebula.Control
|
control *nebula.Control
|
||||||
ipstack *stack.Stack
|
ipstack *stack.Stack
|
||||||
|
|
||||||
|
// done is closed once the forwarding goroutines have exited and the
|
||||||
|
// netstack is torn down, at which point err holds the first fatal error.
|
||||||
|
done chan struct{}
|
||||||
|
err error
|
||||||
|
|
||||||
mu struct {
|
mu struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
|
closed bool
|
||||||
listeners map[uint16]*tcpListener
|
listeners map[uint16]*tcpListener
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -65,7 +70,6 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
ctx := control.Context()
|
ctx := control.Context()
|
||||||
eg, ctx := errgroup.WithContext(ctx)
|
eg, ctx := errgroup.WithContext(ctx)
|
||||||
s := Service{
|
s := Service{
|
||||||
eg: eg,
|
|
||||||
control: control,
|
control: control,
|
||||||
}
|
}
|
||||||
s.mu.listeners = map[uint16]*tcpListener{}
|
s.mu.listeners = map[uint16]*tcpListener{}
|
||||||
@@ -74,6 +78,12 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
NetworkProtocols: []stack.NetworkProtocolFactory{ipv4.NewProtocol, ipv6.NewProtocol},
|
||||||
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
TransportProtocols: []stack.TransportProtocolFactory{tcp.NewProtocol, udp.NewProtocol, icmp.NewProtocol4, icmp.NewProtocol6},
|
||||||
})
|
})
|
||||||
|
// The teardown goroutine only owns the stack once New succeeds
|
||||||
|
defer func() {
|
||||||
|
if reterr != nil {
|
||||||
|
s.ipstack.Destroy()
|
||||||
|
}
|
||||||
|
}()
|
||||||
sackEnabledOpt := tcpip.TCPSACKEnabled(true) // TCP SACK is disabled by default
|
sackEnabledOpt := tcpip.TCPSACKEnabled(true) // TCP SACK is disabled by default
|
||||||
tcpipErr := s.ipstack.SetTransportProtocolOption(tcp.ProtocolNumber, &sackEnabledOpt)
|
tcpipErr := s.ipstack.SetTransportProtocolOption(tcp.ProtocolNumber, &sackEnabledOpt)
|
||||||
if tcpipErr != nil {
|
if tcpipErr != nil {
|
||||||
@@ -123,6 +133,12 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
// this will read exactly one packet
|
// this will read exactly one packet
|
||||||
n, err := reader.Read(buf)
|
n, err := reader.Read(buf)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
// An error after shutdown began is noise from the device pipes
|
||||||
|
// closing, anything else is fatal and must stop nebula too
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.control.Stop()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
packetBuf := stack.NewPacketBuffer(stack.PacketBufferOptions{
|
||||||
@@ -130,8 +146,8 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
})
|
})
|
||||||
linkEP.InjectInbound(header.IPv4ProtocolNumber, packetBuf)
|
linkEP.InjectInbound(header.IPv4ProtocolNumber, packetBuf)
|
||||||
|
|
||||||
if err := ctx.Err(); err != nil {
|
if ctx.Err() != nil {
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
@@ -139,13 +155,18 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
for {
|
for {
|
||||||
packet := linkEP.ReadContext(ctx)
|
packet := linkEP.ReadContext(ctx)
|
||||||
if packet == nil {
|
if packet == nil {
|
||||||
if err := ctx.Err(); err != nil {
|
if ctx.Err() != nil {
|
||||||
return err
|
return nil
|
||||||
}
|
}
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
bufView := packet.ToView()
|
bufView := packet.ToView()
|
||||||
if _, err := bufView.WriteTo(writer); err != nil {
|
if _, err := bufView.WriteTo(writer); err != nil {
|
||||||
|
// Same shutdown noise handling as the inbound reader
|
||||||
|
if ctx.Err() != nil {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
s.control.Stop()
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
bufView.Release()
|
bufView.Release()
|
||||||
@@ -158,6 +179,22 @@ func New(control *nebula.Control) (_ *Service, reterr error) {
|
|||||||
return control.Wait()
|
return control.Wait()
|
||||||
})
|
})
|
||||||
|
|
||||||
|
// Tear down here rather than in Close so it also happens when nebula stops
|
||||||
|
// on its own. The forwarding goroutines must exit before the stack they feed
|
||||||
|
// is destroyed.
|
||||||
|
s.done = make(chan struct{})
|
||||||
|
go func() {
|
||||||
|
s.err = eg.Wait()
|
||||||
|
s.closeListeners()
|
||||||
|
// Destroy aborts the endpoints that exist when it starts, then waits on
|
||||||
|
// every endpoint, so a Dial registering in between would hold Wait until
|
||||||
|
// its connect timed out. Without the NIC new dials fail route lookup.
|
||||||
|
// RemoveNIC also closes linkEP.
|
||||||
|
_ = s.ipstack.RemoveNIC(nicID)
|
||||||
|
s.ipstack.Destroy()
|
||||||
|
close(s.done)
|
||||||
|
}()
|
||||||
|
|
||||||
return &s, nil
|
return &s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -237,6 +274,9 @@ func (s *Service) Listen(network, address string) (net.Listener, error) {
|
|||||||
s.mu.Lock()
|
s.mu.Lock()
|
||||||
defer s.mu.Unlock()
|
defer s.mu.Unlock()
|
||||||
|
|
||||||
|
if s.mu.closed {
|
||||||
|
return nil, net.ErrClosed
|
||||||
|
}
|
||||||
if _, ok := s.mu.listeners[port]; ok {
|
if _, ok := s.mu.listeners[port]; ok {
|
||||||
return nil, fmt.Errorf("already listening on port %d", port)
|
return nil, fmt.Errorf("already listening on port %d", port)
|
||||||
}
|
}
|
||||||
@@ -245,15 +285,38 @@ func (s *Service) Listen(network, address string) (net.Listener, error) {
|
|||||||
return l, nil
|
return l, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Wait blocks until the service has fully stopped, either via Close or a fatal
|
||||||
|
// error, and returns the first fatal error if there was one.
|
||||||
func (s *Service) Wait() error {
|
func (s *Service) Wait() error {
|
||||||
return s.eg.Wait()
|
<-s.done
|
||||||
|
return s.err
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close closes all listeners and stops nebula. Use Wait to block until
|
||||||
|
// shutdown has completed.
|
||||||
func (s *Service) Close() error {
|
func (s *Service) Close() error {
|
||||||
|
s.closeListeners()
|
||||||
s.control.Stop()
|
s.control.Stop()
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// closeListeners unblocks pending Accept calls and rejects new listeners.
|
||||||
|
func (s *Service) closeListeners() {
|
||||||
|
// Every caller closes every listener rather than returning early once
|
||||||
|
// closed is set, so none returns while another is still closing them.
|
||||||
|
s.mu.Lock()
|
||||||
|
s.mu.closed = true
|
||||||
|
listeners := make([]*tcpListener, 0, len(s.mu.listeners))
|
||||||
|
for _, l := range s.mu.listeners {
|
||||||
|
listeners = append(listeners, l)
|
||||||
|
}
|
||||||
|
s.mu.Unlock()
|
||||||
|
|
||||||
|
for _, l := range listeners {
|
||||||
|
_ = l.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *Service) tcpHandler(r *tcp.ForwarderRequest) {
|
func (s *Service) tcpHandler(r *tcp.ForwarderRequest) {
|
||||||
endpointID := r.ID()
|
endpointID := r.ID()
|
||||||
|
|
||||||
|
|||||||
@@ -327,3 +327,162 @@ func TestListenerAcceptAfterClose(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// Close must release blocked Accept calls and let Wait report a clean shutdown.
|
||||||
|
func TestServiceClose(t *testing.T) {
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
||||||
|
"static_host_map": m{},
|
||||||
|
"listen": m{
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
ln, err := a.Listen("tcp", ":1234")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
accepted := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
_, err := ln.Accept()
|
||||||
|
accepted <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
if err := a.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-accepted:
|
||||||
|
if !errors.Is(err, net.ErrClosed) {
|
||||||
|
t.Fatalf("expected net.ErrClosed from Accept, got %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Accept still blocked after Close")
|
||||||
|
}
|
||||||
|
|
||||||
|
waited := make(chan error, 1)
|
||||||
|
go func() { waited <- a.Wait() }()
|
||||||
|
select {
|
||||||
|
case err := <-waited:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Wait still blocked after Close")
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.ipstack.HasNIC(nicID) {
|
||||||
|
t.Fatal("netstack not torn down after Wait")
|
||||||
|
}
|
||||||
|
if err := a.Close(); err != nil {
|
||||||
|
t.Fatalf("second close: %v", err)
|
||||||
|
}
|
||||||
|
if _, err := a.Listen("tcp", ":1235"); !errors.Is(err, net.ErrClosed) {
|
||||||
|
t.Fatalf("expected net.ErrClosed from Listen after Close, got %v", err)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// If nebula stops without Close being called, the service must still release
|
||||||
|
// Accept and tear down the netstack.
|
||||||
|
func TestServiceNebulaStopped(t *testing.T) {
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
||||||
|
"static_host_map": m{},
|
||||||
|
"listen": m{
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
ln, err := a.Listen("tcp", ":1234")
|
||||||
|
if err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
accepted := make(chan error, 1)
|
||||||
|
go func() {
|
||||||
|
_, err := ln.Accept()
|
||||||
|
accepted <- err
|
||||||
|
}()
|
||||||
|
|
||||||
|
a.control.Stop()
|
||||||
|
|
||||||
|
waited := make(chan error, 1)
|
||||||
|
go func() { waited <- a.Wait() }()
|
||||||
|
select {
|
||||||
|
case err := <-waited:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Wait still blocked after nebula stopped")
|
||||||
|
}
|
||||||
|
|
||||||
|
select {
|
||||||
|
case err := <-accepted:
|
||||||
|
if !errors.Is(err, net.ErrClosed) {
|
||||||
|
t.Fatalf("expected net.ErrClosed from Accept, got %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Accept still blocked after nebula stopped")
|
||||||
|
}
|
||||||
|
|
||||||
|
if a.ipstack.HasNIC(nicID) {
|
||||||
|
t.Fatal("netstack not torn down after Wait")
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// Dials racing Close must not hold Wait until their connect times out.
|
||||||
|
func TestServiceDialDuringClose(t *testing.T) {
|
||||||
|
ca, _, caKey, _ := cert_test.NewTestCaCert(cert.Version2, cert.Curve_CURVE25519, time.Now(), time.Now().Add(10*time.Minute), nil, nil, []string{})
|
||||||
|
a := newSimpleService(ca, caKey, "a", netip.MustParseAddr("10.0.0.1"), m{
|
||||||
|
"static_host_map": m{},
|
||||||
|
"listen": m{
|
||||||
|
"host": "127.0.0.1",
|
||||||
|
"port": 0,
|
||||||
|
},
|
||||||
|
})
|
||||||
|
|
||||||
|
stop := make(chan struct{})
|
||||||
|
var eg errgroup.Group
|
||||||
|
for range 8 {
|
||||||
|
eg.Go(func() error {
|
||||||
|
for {
|
||||||
|
select {
|
||||||
|
case <-stop:
|
||||||
|
return nil
|
||||||
|
default:
|
||||||
|
}
|
||||||
|
// 10.0.0.2 has no tunnel, so each dial sits in SYN-SENT until aborted
|
||||||
|
if c, err := a.Dial("tcp", "10.0.0.2:80"); err == nil {
|
||||||
|
_ = c.Close()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
})
|
||||||
|
}
|
||||||
|
time.Sleep(50 * time.Millisecond)
|
||||||
|
|
||||||
|
if err := a.Close(); err != nil {
|
||||||
|
t.Fatal(err)
|
||||||
|
}
|
||||||
|
waited := make(chan error, 1)
|
||||||
|
go func() { waited <- a.Wait() }()
|
||||||
|
select {
|
||||||
|
case err := <-waited:
|
||||||
|
if err != nil {
|
||||||
|
t.Fatalf("expected clean shutdown from Wait, got %v", err)
|
||||||
|
}
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Wait blocked behind a dial that raced Close")
|
||||||
|
}
|
||||||
|
|
||||||
|
close(stop)
|
||||||
|
dialersDone := make(chan error, 1)
|
||||||
|
go func() { dialersDone <- eg.Wait() }()
|
||||||
|
select {
|
||||||
|
case <-dialersDone:
|
||||||
|
case <-time.After(5 * time.Second):
|
||||||
|
t.Fatal("Dial still blocked after Wait")
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -1,67 +1,19 @@
|
|||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"bytes"
|
|
||||||
"encoding/json"
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"maps"
|
|
||||||
"net"
|
"net"
|
||||||
"net/netip"
|
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
|
||||||
"runtime"
|
|
||||||
"runtime/pprof"
|
|
||||||
"slices"
|
"slices"
|
||||||
"sort"
|
|
||||||
"strconv"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/header"
|
|
||||||
"github.com/slackhq/nebula/logging"
|
|
||||||
"github.com/slackhq/nebula/sshd"
|
"github.com/slackhq/nebula/sshd"
|
||||||
)
|
)
|
||||||
|
|
||||||
type sshListHostMapFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
ByIndex bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshPrintCertFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
Raw bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshPrintTunnelFlags struct {
|
|
||||||
Pretty bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshChangeRemoteFlags struct {
|
|
||||||
Address string
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshCloseTunnelFlags struct {
|
|
||||||
LocalOnly bool
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshCreateTunnelFlags struct {
|
|
||||||
Address string
|
|
||||||
}
|
|
||||||
|
|
||||||
type sshDeviceInfoFlags struct {
|
|
||||||
Json bool
|
|
||||||
Pretty bool
|
|
||||||
}
|
|
||||||
|
|
||||||
func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
// sshd.sandbox_dir is read once, when the commands are attached
|
|
||||||
r.RestartRequiredIfChanged("sshd.sandbox_dir")
|
|
||||||
if c.GetBool("sshd.enabled", false) {
|
if c.GetBool("sshd.enabled", false) {
|
||||||
// Restarting the server drops every open session, the one that asked for this reload included. Only restart
|
// Restarting the server drops every open session, the one that asked for this reload included. Only restart
|
||||||
// when it isn't running or its config changed. The host key file can change under the same config, new
|
// when it isn't running or its config changed. The host key file can change under the same config, new
|
||||||
@@ -78,23 +30,27 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sshRun, err := configSSH(l, ssh, c, r)
|
if _, err := configSSH(l, ssh, c, r); err != nil {
|
||||||
if err != nil {
|
|
||||||
l.Error("Failed to reconfigure the sshd", "error", err)
|
l.Error("Failed to reconfigure the sshd", "error", err)
|
||||||
ssh.Stop()
|
ssh.Stop()
|
||||||
r.Failed()
|
r.Failed()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
if sshRun != nil {
|
// Bind here rather than in the background, so a reload that can't listen says so
|
||||||
go sshRun()
|
listener, err := ssh.Listen(c.GetString("sshd.listen", ""))
|
||||||
|
if err != nil {
|
||||||
|
l.Error("Failed to run the SSH server", "error", err)
|
||||||
|
r.Failed()
|
||||||
|
return
|
||||||
}
|
}
|
||||||
|
go ssh.Serve(listener)
|
||||||
} else {
|
} else {
|
||||||
ssh.Stop()
|
ssh.Stop()
|
||||||
}
|
}
|
||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// sshdServerKeys are what configSSH reads. sshd.sandbox_dir isn't one, it's read once at startup and restarting the
|
// sshdServerKeys are what configSSH reads
|
||||||
// server doesn't apply it
|
|
||||||
var sshdServerKeys = []string{"sshd.enabled", "sshd.listen", "sshd.host_key", "sshd.trusted_cas", "sshd.authorized_users"}
|
var sshdServerKeys = []string{"sshd.enabled", "sshd.listen", "sshd.host_key", "sshd.trusted_cas", "sshd.authorized_users"}
|
||||||
|
|
||||||
// loadSSHHostKey returns sshd.host_key, read from its file when it names one.
|
// loadSSHHostKey returns sshd.host_key, read from its file when it names one.
|
||||||
@@ -234,871 +190,3 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
|
|
||||||
return runner, nil
|
return runner, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func attachCommands(l *slog.Logger, c *config.C, ssh *sshd.SSHServer, f *Interface) {
|
|
||||||
// sandboxDir defaults to a dir in temp. The intention is that end user will
|
|
||||||
// create this dir as needed. Overriding this config value to "" allows
|
|
||||||
// writing to anywhere in the system.
|
|
||||||
defaultDir := filepath.Join(os.TempDir(), "nebula-debug")
|
|
||||||
sandboxDir := c.GetString("sshd.sandbox_dir", defaultDir)
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "list-hostmap",
|
|
||||||
ShortDescription: "List all known previously connected hosts",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshListHostMapFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshListHostMap(f.hostMap, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "list-pending-hostmap",
|
|
||||||
ShortDescription: "List all handshaking hosts",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshListHostMapFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
fl.BoolVar(&s.ByIndex, "by-index", false, "gets all hosts in the hostmap from the index table")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshListHostMap(f.handshakeManager, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "list-lighthouse-addrmap",
|
|
||||||
ShortDescription: "List all lighthouse map entries",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshListHostMapFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshListLighthouseMap(f.lightHouse, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "reload",
|
|
||||||
ShortDescription: "Reloads configuration from disk, same as sending HUP to the process",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshReload(l, c, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "start-cpu-profile",
|
|
||||||
ShortDescription: "Starts a cpu profile and write output to the provided file, ex: `cpu-profile.pb.gz`",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshStartCpuProfile(sandboxDir, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "stop-cpu-profile",
|
|
||||||
ShortDescription: "Stops a cpu profile and writes output to the previously provided file",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
pprof.StopCPUProfile()
|
|
||||||
return w.WriteLine("If a CPU profile was running it is now stopped")
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "save-heap-profile",
|
|
||||||
ShortDescription: "Saves a heap profile to the provided path, ex: `heap-profile.pb.gz`",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshGetHeapProfile(sandboxDir, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "mutex-profile-fraction",
|
|
||||||
ShortDescription: "Gets or sets runtime.SetMutexProfileFraction",
|
|
||||||
Callback: sshMutexProfileFraction,
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "save-mutex-profile",
|
|
||||||
ShortDescription: "Saves a mutex profile to the provided path, ex: `mutex-profile.pb.gz`",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshGetMutexProfile(sandboxDir, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "log-level",
|
|
||||||
ShortDescription: "Gets or sets the current log level",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshLogLevel(l, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "log-format",
|
|
||||||
ShortDescription: "Gets or sets the current log format",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshLogFormat(l, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "version",
|
|
||||||
ShortDescription: "Prints the currently running version of nebula",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshVersion(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "device-info",
|
|
||||||
ShortDescription: "Prints information about the network device.",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshDeviceInfoFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json with more information")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshDeviceInfo(f, fs, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "print-cert",
|
|
||||||
ShortDescription: "Prints the current certificate being used or the certificate for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshPrintCertFlags{}
|
|
||||||
fl.BoolVar(&s.Json, "json", false, "outputs as json")
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json, assumes -json")
|
|
||||||
fl.BoolVar(&s.Raw, "raw", false, "raw prints the PEM encoded certificate, not compatible with -json or -pretty")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshPrintCert(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "print-tunnel",
|
|
||||||
ShortDescription: "Prints json details about a tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshPrintTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshPrintTunnel(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "print-relays",
|
|
||||||
ShortDescription: "Prints json details about all relay info",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshPrintTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.Pretty, "pretty", false, "pretty prints json")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshPrintRelays(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "change-remote",
|
|
||||||
ShortDescription: "Changes the remote address used in the tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshChangeRemoteFlags{}
|
|
||||||
fl.StringVar(&s.Address, "address", "", "The new remote address, ip:port")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshChangeRemote(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "close-tunnel",
|
|
||||||
ShortDescription: "Closes a tunnel for the provided vpn addr",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshCloseTunnelFlags{}
|
|
||||||
fl.BoolVar(&s.LocalOnly, "local-only", false, "Disables notifying the remote that the tunnel is shutting down")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshCloseTunnel(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "create-tunnel",
|
|
||||||
ShortDescription: "Creates a tunnel for the provided vpn address",
|
|
||||||
Help: "The lighthouses will be queried for real addresses but you can provide one as well.",
|
|
||||||
Flags: func() (*flag.FlagSet, any) {
|
|
||||||
fl := flag.NewFlagSet("", flag.ContinueOnError)
|
|
||||||
s := sshCreateTunnelFlags{}
|
|
||||||
fl.StringVar(&s.Address, "address", "", "Optionally provide a real remote address, ip:port ")
|
|
||||||
return fl, &s
|
|
||||||
},
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshCreateTunnel(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
ssh.RegisterCommand(&sshd.Command{
|
|
||||||
Name: "query-lighthouse",
|
|
||||||
ShortDescription: "Query the lighthouses for the provided vpn address",
|
|
||||||
Help: "This command is asynchronous. Only currently known udp addresses will be printed.",
|
|
||||||
Callback: func(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return sshQueryLighthouse(f, fs, a, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshListHostMap(hl controlHostLister, a any, w sshd.StringWriter) error {
|
|
||||||
fs, ok := a.(*sshListHostMapFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
var hm []ControlHostInfo
|
|
||||||
if fs.ByIndex {
|
|
||||||
hm = listHostMapIndexes(hl)
|
|
||||||
} else {
|
|
||||||
hm = listHostMapHosts(hl)
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Slice(hm, func(i, j int) bool {
|
|
||||||
return hm[i].VpnAddrs[0].Compare(hm[j].VpnAddrs[0]) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if fs.Json || fs.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if fs.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := js.Encode(hm)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for _, v := range hm {
|
|
||||||
err := w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddrs, v.RemoteAddrs))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshListLighthouseMap(lightHouse *LightHouse, a any, w sshd.StringWriter) error {
|
|
||||||
fs, ok := a.(*sshListHostMapFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
type lighthouseInfo struct {
|
|
||||||
VpnAddr string `json:"vpnAddr"`
|
|
||||||
Addrs *CacheMap `json:"addrs"`
|
|
||||||
}
|
|
||||||
|
|
||||||
lightHouse.RLock()
|
|
||||||
addrMap := make([]lighthouseInfo, len(lightHouse.addrMap))
|
|
||||||
x := 0
|
|
||||||
for k, v := range lightHouse.addrMap {
|
|
||||||
addrMap[x] = lighthouseInfo{
|
|
||||||
VpnAddr: k.String(),
|
|
||||||
Addrs: v.CopyCache(),
|
|
||||||
}
|
|
||||||
x++
|
|
||||||
}
|
|
||||||
lightHouse.RUnlock()
|
|
||||||
|
|
||||||
sort.Slice(addrMap, func(i, j int) bool {
|
|
||||||
return strings.Compare(addrMap[i].VpnAddr, addrMap[j].VpnAddr) < 0
|
|
||||||
})
|
|
||||||
|
|
||||||
if fs.Json || fs.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if fs.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
err := js.Encode(addrMap)
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
} else {
|
|
||||||
for _, v := range addrMap {
|
|
||||||
b, err := json.Marshal(v.Addrs)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
err = w.WriteLine(fmt.Sprintf("%s: %s", v.VpnAddr, string(b)))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// sshSanitizeFilePath validates that the given file path is within the sandbox directory.
|
|
||||||
// If sandboxDir is empty, the path is returned as-is for backwards compatibility.
|
|
||||||
func sshSanitizeFilePath(sandboxDir, filePath string) (string, error) {
|
|
||||||
if sandboxDir == "" {
|
|
||||||
return filePath, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// Clean and resolve the path relative to the sandbox directory
|
|
||||||
if !filepath.IsAbs(filePath) {
|
|
||||||
filePath = filepath.Join(sandboxDir, filePath)
|
|
||||||
}
|
|
||||||
cleaned := filepath.Clean(filePath)
|
|
||||||
|
|
||||||
// Ensure the resolved path is within the sandbox directory
|
|
||||||
cleanedSandbox := filepath.Clean(sandboxDir)
|
|
||||||
if cleaned == cleanedSandbox {
|
|
||||||
return "", fmt.Errorf("path %q resolves to the sandbox directory itself %q", filePath, sandboxDir)
|
|
||||||
}
|
|
||||||
if !strings.HasPrefix(cleaned, cleanedSandbox+string(filepath.Separator)) {
|
|
||||||
return "", fmt.Errorf("path %q is outside the sandbox directory %q", filePath, sandboxDir)
|
|
||||||
}
|
|
||||||
|
|
||||||
return cleaned, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshStartCpuProfile(sandboxDir string, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
if len(a) == 0 {
|
|
||||||
err := w.WriteLine("No path to write profile provided")
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath, err := sshSanitizeFilePath(sandboxDir, a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(filePath)
|
|
||||||
if err != nil {
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pprof.StartCPUProfile(file)
|
|
||||||
if err != nil {
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Unable to start cpu profile: %s", err))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Started cpu profile, issue stop-cpu-profile to write the output to %s", a))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshVersion(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
return w.WriteLine(fmt.Sprintf("%s", ifce.version))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshQueryLighthouse(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
var cm *CacheMap
|
|
||||||
rl := ifce.lightHouse.Query(vpnAddr)
|
|
||||||
if rl != nil {
|
|
||||||
cm = rl.CopyCache()
|
|
||||||
}
|
|
||||||
return json.NewEncoder(w.GetWriter()).Encode(cm)
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshCloseTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
flags, ok := fs.(*sshCloseTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !flags.LocalOnly {
|
|
||||||
ifce.send(
|
|
||||||
header.CloseTunnel,
|
|
||||||
0,
|
|
||||||
hostInfo.ConnectionState,
|
|
||||||
hostInfo,
|
|
||||||
[]byte{},
|
|
||||||
make([]byte, 12, 12),
|
|
||||||
make([]byte, mtu),
|
|
||||||
)
|
|
||||||
}
|
|
||||||
|
|
||||||
ifce.closeTunnel(hostInfo)
|
|
||||||
return w.WriteLine("Closed")
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshCreateTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
flags, ok := fs.(*sshCreateTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Tunnel already exists"))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo = ifce.handshakeManager.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Tunnel already handshaking"))
|
|
||||||
}
|
|
||||||
|
|
||||||
var addr netip.AddrPort
|
|
||||||
if flags.Address != "" {
|
|
||||||
addr, err = netip.ParseAddrPort(flags.Address)
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine("Address could not be parsed")
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// Set the remote from the callback, which StartHandshake runs under the handshake manager lock
|
|
||||||
// before the hostinfo reaches the handshake loop or the static-host trigger, so the address is
|
|
||||||
// in the RemoteList before the first handshake is sent.
|
|
||||||
ifce.handshakeManager.StartHandshake(vpnAddr, func(hh *HandshakeHostInfo) {
|
|
||||||
if addr.IsValid() {
|
|
||||||
hh.hostinfo.SetRemote(addr)
|
|
||||||
}
|
|
||||||
})
|
|
||||||
|
|
||||||
return w.WriteLine("Created")
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshChangeRemote(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
flags, ok := fs.(*sshChangeRemoteFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
if flags.Address == "" {
|
|
||||||
return w.WriteLine("No address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
addr, err := netip.ParseAddrPort(flags.Address)
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine("Address could not be parsed")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn address could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn address: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo.SetRemote(addr)
|
|
||||||
return w.WriteLine("Changed")
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshGetHeapProfile(sandboxDir string, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No path to write profile provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath, err := sshSanitizeFilePath(sandboxDir, a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(filePath)
|
|
||||||
if err != nil {
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = pprof.WriteHeapProfile(file)
|
|
||||||
if err != nil {
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
err = w.WriteLine(fmt.Sprintf("Mem profile created at %s", a))
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshMutexProfileFraction(fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
if len(a) == 0 {
|
|
||||||
rate := runtime.SetMutexProfileFraction(-1)
|
|
||||||
return w.WriteLine(fmt.Sprintf("Current value: %d", rate))
|
|
||||||
}
|
|
||||||
|
|
||||||
newRate, err := strconv.Atoi(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Invalid argument: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
oldRate := runtime.SetMutexProfileFraction(newRate)
|
|
||||||
return w.WriteLine(fmt.Sprintf("New value: %d. Old value: %d", newRate, oldRate))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshGetMutexProfile(sandboxDir string, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No path to write profile provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
filePath, err := sshSanitizeFilePath(sandboxDir, a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(err.Error())
|
|
||||||
}
|
|
||||||
|
|
||||||
file, err := os.Create(filePath)
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Unable to create profile file: %s", err))
|
|
||||||
}
|
|
||||||
defer file.Close()
|
|
||||||
|
|
||||||
mutexProfile := pprof.Lookup("mutex")
|
|
||||||
if mutexProfile == nil {
|
|
||||||
return w.WriteLine("Unable to get pprof.Lookup(\"mutex\")")
|
|
||||||
}
|
|
||||||
|
|
||||||
err = mutexProfile.WriteTo(file, 0)
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Unable to write profile: %s", err))
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteLine(fmt.Sprintf("Mutex profile created at %s", a))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshLogLevel(l *slog.Logger, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
ctrl, ok := l.Handler().(interface {
|
|
||||||
GetLevel() slog.Level
|
|
||||||
SetLevel(slog.Level)
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return w.WriteLine("Log level is not reconfigurable on this logger")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
|
||||||
}
|
|
||||||
|
|
||||||
level, err := logging.ParseLevel(strings.ToLower(a[0]))
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Unknown log level %s. Possible log levels: trace, debug, info, warn, error", a))
|
|
||||||
}
|
|
||||||
|
|
||||||
ctrl.SetLevel(level)
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log level is: %s", logging.LevelName(ctrl.GetLevel())))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshLogFormat(l *slog.Logger, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
ctrl, ok := l.Handler().(interface {
|
|
||||||
GetFormat() string
|
|
||||||
SetFormat(string) error
|
|
||||||
})
|
|
||||||
if !ok {
|
|
||||||
return w.WriteLine("Log format is not reconfigurable on this logger")
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
|
||||||
}
|
|
||||||
|
|
||||||
if err := ctrl.SetFormat(strings.ToLower(a[0])); err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return w.WriteLine(fmt.Sprintf("Log format is: %s", ctrl.GetFormat()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshPrintCert(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
args, ok := fs.(*sshPrintCertFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
cert := ifce.pki.getCertState().GetDefaultCertificate()
|
|
||||||
if len(a) > 0 {
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
cert = hostInfo.GetCert().Certificate
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Json || args.Pretty {
|
|
||||||
b, err := cert.MarshalJSON()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Pretty {
|
|
||||||
buf := new(bytes.Buffer)
|
|
||||||
err := json.Indent(buf, b, "", " ")
|
|
||||||
b = buf.Bytes()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteBytes(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
if args.Raw {
|
|
||||||
b, err := cert.MarshalPEM()
|
|
||||||
if err != nil {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteBytes(b)
|
|
||||||
}
|
|
||||||
|
|
||||||
return w.WriteLine(cert.String())
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshPrintRelays(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
args, ok := fs.(*sshPrintTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
w.WriteLine(fmt.Sprintf("sshPrintRelays failed to convert args type"))
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
relays := map[uint32]*HostInfo{}
|
|
||||||
ifce.hostMap.Lock()
|
|
||||||
maps.Copy(relays, ifce.hostMap.Relays)
|
|
||||||
ifce.hostMap.Unlock()
|
|
||||||
|
|
||||||
type RelayFor struct {
|
|
||||||
Error error
|
|
||||||
Type string
|
|
||||||
State string
|
|
||||||
PeerAddr netip.Addr
|
|
||||||
LocalIndex uint32
|
|
||||||
RemoteIndex uint32
|
|
||||||
RelayedThrough []netip.Addr
|
|
||||||
}
|
|
||||||
|
|
||||||
type RelayOutput struct {
|
|
||||||
NebulaAddr netip.Addr
|
|
||||||
RelayForAddrs []RelayFor
|
|
||||||
}
|
|
||||||
|
|
||||||
type CmdOutput struct {
|
|
||||||
Relays []*RelayOutput
|
|
||||||
}
|
|
||||||
|
|
||||||
co := CmdOutput{}
|
|
||||||
|
|
||||||
enc := json.NewEncoder(w.GetWriter())
|
|
||||||
|
|
||||||
if args.Pretty {
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
for k, v := range relays {
|
|
||||||
ro := RelayOutput{NebulaAddr: v.vpnAddrs[0]}
|
|
||||||
co.Relays = append(co.Relays, &ro)
|
|
||||||
relayHI := ifce.hostMap.QueryVpnAddr(v.vpnAddrs[0])
|
|
||||||
if relayHI == nil {
|
|
||||||
ro.RelayForAddrs = append(ro.RelayForAddrs, RelayFor{Error: errors.New("could not find hostinfo")})
|
|
||||||
continue
|
|
||||||
}
|
|
||||||
for _, vpnAddr := range relayHI.relayState.CopyRelayForIps() {
|
|
||||||
rf := RelayFor{Error: nil}
|
|
||||||
r, ok := relayHI.relayState.GetRelayForByAddr(vpnAddr)
|
|
||||||
if ok {
|
|
||||||
t := ""
|
|
||||||
switch r.Type {
|
|
||||||
case ForwardingType:
|
|
||||||
t = "forwarding"
|
|
||||||
case TerminalType:
|
|
||||||
t = "terminal"
|
|
||||||
default:
|
|
||||||
t = "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
s := ""
|
|
||||||
switch r.State {
|
|
||||||
case Requested:
|
|
||||||
s = "requested"
|
|
||||||
case Established:
|
|
||||||
s = "established"
|
|
||||||
default:
|
|
||||||
s = "unknown"
|
|
||||||
}
|
|
||||||
|
|
||||||
rf.LocalIndex = r.LocalIndex
|
|
||||||
rf.RemoteIndex = r.RemoteIndex
|
|
||||||
rf.PeerAddr = r.PeerAddr
|
|
||||||
rf.Type = t
|
|
||||||
rf.State = s
|
|
||||||
if rf.LocalIndex != k {
|
|
||||||
rf.Error = fmt.Errorf("hostmap LocalIndex '%v' does not match RelayState LocalIndex", k)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
relayedHI := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if relayedHI != nil {
|
|
||||||
rf.RelayedThrough = append(rf.RelayedThrough, relayedHI.relayState.CopyRelayIps()...)
|
|
||||||
}
|
|
||||||
|
|
||||||
ro.RelayForAddrs = append(ro.RelayForAddrs, rf)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
err := enc.Encode(co)
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshPrintTunnel(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
|
||||||
args, ok := fs.(*sshPrintTunnelFlags)
|
|
||||||
if !ok {
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
if len(a) == 0 {
|
|
||||||
return w.WriteLine("No vpn address was provided")
|
|
||||||
}
|
|
||||||
|
|
||||||
vpnAddr, err := netip.ParseAddr(a[0])
|
|
||||||
if err != nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
if !vpnAddr.IsValid() {
|
|
||||||
return w.WriteLine(fmt.Sprintf("The provided vpn addr could not be parsed: %s", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
hostInfo := ifce.hostMap.QueryVpnAddr(vpnAddr)
|
|
||||||
if hostInfo == nil {
|
|
||||||
return w.WriteLine(fmt.Sprintf("Could not find tunnel for vpn addr: %v", a[0]))
|
|
||||||
}
|
|
||||||
|
|
||||||
enc := json.NewEncoder(w.GetWriter())
|
|
||||||
if args.Pretty {
|
|
||||||
enc.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
return enc.Encode(copyHostInfo(hostInfo, ifce.hostMap.GetPreferredRanges()))
|
|
||||||
}
|
|
||||||
|
|
||||||
func sshDeviceInfo(ifce *Interface, fs any, w sshd.StringWriter) error {
|
|
||||||
|
|
||||||
data := struct {
|
|
||||||
Name string `json:"name"`
|
|
||||||
Cidr []netip.Prefix `json:"cidr"`
|
|
||||||
}{
|
|
||||||
Name: ifce.inside.Name(),
|
|
||||||
Cidr: make([]netip.Prefix, len(ifce.inside.Networks())),
|
|
||||||
}
|
|
||||||
|
|
||||||
copy(data.Cidr, ifce.inside.Networks())
|
|
||||||
|
|
||||||
flags, ok := fs.(*sshDeviceInfoFlags)
|
|
||||||
if !ok {
|
|
||||||
return fmt.Errorf("internal error: expected flags to be sshDeviceInfoFlags but was %+v", fs)
|
|
||||||
}
|
|
||||||
|
|
||||||
if flags.Json || flags.Pretty {
|
|
||||||
js := json.NewEncoder(w.GetWriter())
|
|
||||||
if flags.Pretty {
|
|
||||||
js.SetIndent("", " ")
|
|
||||||
}
|
|
||||||
|
|
||||||
return js.Encode(data)
|
|
||||||
} else {
|
|
||||||
return w.WriteLine(fmt.Sprintf("name=%v cidr=%v", data.Name, data.Cidr))
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
// sshReload reloads the config and writes the result to the client. It reloads even if the client has gone away
|
|
||||||
func sshReload(l *slog.Logger, c *config.C, w sshd.StringWriter) error {
|
|
||||||
writeErr := w.WriteLine("Reloading config")
|
|
||||||
res := c.ReloadConfig()
|
|
||||||
res.Log(l)
|
|
||||||
if err := w.WriteLine(res.String()); writeErr == nil {
|
|
||||||
writeErr = err
|
|
||||||
}
|
|
||||||
return writeErr
|
|
||||||
}
|
|
||||||
@@ -0,0 +1,57 @@
|
|||||||
|
package nebula
|
||||||
|
|
||||||
|
import (
|
||||||
|
"bytes"
|
||||||
|
"fmt"
|
||||||
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/test"
|
||||||
|
)
|
||||||
|
|
||||||
|
// TestSSHCommandSurface is a characterization test for the ssh command surface.
|
||||||
|
// It pins the registered command set, their descriptions, help text and flag definitions
|
||||||
|
// so that moving the registry to a transport neutral package is provably a relocation
|
||||||
|
// rather than a rewrite. Regenerate with `go test . -run TestSSHCommandSurface -update`.
|
||||||
|
func TestSSHCommandSurface(t *testing.T) {
|
||||||
|
got := renderCommandSurface(registeredCommands(t).All())
|
||||||
|
test.Golden(t, filepath.Join("testdata", "ssh_commands.golden"), got)
|
||||||
|
}
|
||||||
|
|
||||||
|
// registeredCommands returns a registry holding every command nebula serves. The
|
||||||
|
// Interface is never dereferenced: attachCommands only closes over it.
|
||||||
|
func registeredCommands(t *testing.T) *commands.Registry {
|
||||||
|
t.Helper()
|
||||||
|
l := test.NewLogger()
|
||||||
|
reg := commands.New()
|
||||||
|
attachCommands(l, config.NewC(l), reg, &Interface{})
|
||||||
|
return reg
|
||||||
|
}
|
||||||
|
|
||||||
|
func renderCommandSurface(cmds []*commands.Command) string {
|
||||||
|
lines := make([]string, 0, len(cmds))
|
||||||
|
for _, cmd := range cmds {
|
||||||
|
b := &bytes.Buffer{}
|
||||||
|
fmt.Fprintf(b, "%s\n", cmd.Name)
|
||||||
|
fmt.Fprintf(b, " short: %s\n", cmd.ShortDescription)
|
||||||
|
fmt.Fprintf(b, " help: %s\n", cmd.Help)
|
||||||
|
|
||||||
|
if cmd.Flags != nil {
|
||||||
|
fl, _ := cmd.Flags()
|
||||||
|
if fl != nil {
|
||||||
|
fb := &bytes.Buffer{}
|
||||||
|
fl.SetOutput(fb)
|
||||||
|
fl.PrintDefaults()
|
||||||
|
fmt.Fprintf(b, " flags:\n%s", fb.String())
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
lines = append(lines, b.String())
|
||||||
|
}
|
||||||
|
|
||||||
|
// In the order All gives, so the golden pins display order too.
|
||||||
|
return strings.Join(lines, "")
|
||||||
|
}
|
||||||
+20
-9
@@ -7,6 +7,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
@@ -26,7 +27,7 @@ func (w *lineWriter) Write(s string) error { return w.WriteLine(s) }
|
|||||||
func (w *lineWriter) WriteBytes(b []byte) error { return w.WriteLine(string(b)) }
|
func (w *lineWriter) WriteBytes(b []byte) error { return w.WriteLine(string(b)) }
|
||||||
func (w *lineWriter) GetWriter() io.Writer { return io.Discard }
|
func (w *lineWriter) GetWriter() io.Writer { return io.Discard }
|
||||||
|
|
||||||
func TestSSHReloadReportsTheResult(t *testing.T) {
|
func TestReloadCommandReportsTheResult(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
write := func(body string) {
|
write := func(body string) {
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(body), 0o600))
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte(body), 0o600))
|
||||||
@@ -43,32 +44,42 @@ func TestSSHReloadReportsTheResult(t *testing.T) {
|
|||||||
}
|
}
|
||||||
})
|
})
|
||||||
|
|
||||||
reload := func() []string {
|
// reload returns what the client was told. A reload that failed is a commands.Failure
|
||||||
|
// to the surface, exit 1 over ssh, with the result line as its message; one that
|
||||||
|
// merely needs a restart is not.
|
||||||
|
reload := func(failed bool) []string {
|
||||||
w := &lineWriter{}
|
w := &lineWriter{}
|
||||||
require.NoError(t, sshReload(l, c, w))
|
err := reloadCommand(l, c, w)
|
||||||
|
if failed {
|
||||||
|
var f *commands.Failure
|
||||||
|
require.ErrorAs(t, err, &f)
|
||||||
|
assert.Equal(t, w.lines[len(w.lines)-1], f.Msg)
|
||||||
|
} else {
|
||||||
|
require.NoError(t, err)
|
||||||
|
}
|
||||||
return w.lines
|
return w.lines
|
||||||
}
|
}
|
||||||
|
|
||||||
write("a: 2\nb: 1")
|
write("a: 2\nb: 1")
|
||||||
assert.Equal(t, []string{"Reloading config", "Config reload complete"}, reload())
|
assert.Equal(t, []string{"Reloading config", "Config reload complete"}, reload(false))
|
||||||
|
|
||||||
fail = true
|
fail = true
|
||||||
write("a: 3\nb: 1")
|
write("a: 3\nb: 1")
|
||||||
assert.Equal(t, []string{"Reloading config", "Config reload completed with errors, review the logs"}, reload())
|
assert.Equal(t, []string{"Reloading config", "Config reload completed with errors, review the logs"}, reload(true))
|
||||||
|
|
||||||
fail = false
|
fail = false
|
||||||
write("a: 3\nb: 2")
|
write("a: 3\nb: 2")
|
||||||
assert.Equal(t, []string{"Reloading config", "Config reload was completed but requires a restart to fully apply, keys [b]"}, reload())
|
assert.Equal(t, []string{"Reloading config", "Config reload was completed but requires a restart to fully apply, keys [b]"}, reload(false))
|
||||||
|
|
||||||
write("a: [")
|
write("a: [")
|
||||||
lines := reload()
|
lines := reload(true)
|
||||||
require.Len(t, lines, 2)
|
require.Len(t, lines, 2)
|
||||||
assert.Contains(t, lines[1], "Config reload failed, the new config did not load")
|
assert.Contains(t, lines[1], "Config reload failed, the new config did not load")
|
||||||
assert.Contains(t, lines[1], dir, "names the config that did not load")
|
assert.Contains(t, lines[1], dir, "names the config that did not load")
|
||||||
}
|
}
|
||||||
|
|
||||||
// A client that went away still gets the reload it asked for
|
// A client that went away still gets the reload it asked for
|
||||||
func TestSSHReloadReloadsWhenTheClientIsGone(t *testing.T) {
|
func TestReloadCommandReloadsWhenTheClientIsGone(t *testing.T) {
|
||||||
dir := t.TempDir()
|
dir := t.TempDir()
|
||||||
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte("a: 1"), 0o600))
|
require.NoError(t, os.WriteFile(filepath.Join(dir, "config.yml"), []byte("a: 1"), 0o600))
|
||||||
l := test.NewLogger()
|
l := test.NewLogger()
|
||||||
@@ -78,6 +89,6 @@ func TestSSHReloadReloadsWhenTheClientIsGone(t *testing.T) {
|
|||||||
c.RegisterReloadCallback(func(*config.C, *config.Reload) { reloaded = true })
|
c.RegisterReloadCallback(func(*config.C, *config.Reload) { reloaded = true })
|
||||||
|
|
||||||
gone := errors.New("client is gone")
|
gone := errors.New("client is gone")
|
||||||
require.ErrorIs(t, sshReload(l, c, &lineWriter{err: gone}), gone)
|
require.ErrorIs(t, reloadCommand(l, c, &lineWriter{err: gone}), gone)
|
||||||
assert.True(t, reloaded)
|
assert.True(t, reloaded)
|
||||||
}
|
}
|
||||||
-161
@@ -1,161 +0,0 @@
|
|||||||
package sshd
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"flag"
|
|
||||||
"fmt"
|
|
||||||
"sort"
|
|
||||||
"strings"
|
|
||||||
|
|
||||||
"github.com/armon/go-radix"
|
|
||||||
)
|
|
||||||
|
|
||||||
// CommandFlags is a function called before help or command execution to parse command line flags
|
|
||||||
// It should return a flag.FlagSet instance and a pointer to the struct that will contain parsed flags
|
|
||||||
type CommandFlags func() (*flag.FlagSet, any)
|
|
||||||
|
|
||||||
// CommandCallback is the function called when your command should execute.
|
|
||||||
// fs will be a a pointer to the struct provided by Command.Flags callback, if there was one. -h and -help are reserved
|
|
||||||
// and handled automatically for you.
|
|
||||||
// a will be any unconsumed arguments, if no Command.Flags was available this will be all the flags passed in.
|
|
||||||
// w is the writer to use when sending messages back to the client.
|
|
||||||
// If an error is returned by the callback it is logged locally, the callback should handle messaging errors to the user
|
|
||||||
// where appropriate
|
|
||||||
type CommandCallback func(fs any, a []string, w StringWriter) error
|
|
||||||
|
|
||||||
type Command struct {
|
|
||||||
Name string
|
|
||||||
ShortDescription string
|
|
||||||
Help string
|
|
||||||
Flags CommandFlags
|
|
||||||
Callback CommandCallback
|
|
||||||
}
|
|
||||||
|
|
||||||
func execCommand(c *Command, args []string, w StringWriter) error {
|
|
||||||
var (
|
|
||||||
fl *flag.FlagSet
|
|
||||||
fs any
|
|
||||||
)
|
|
||||||
|
|
||||||
if c.Flags != nil {
|
|
||||||
fl, fs = c.Flags()
|
|
||||||
if fl != nil {
|
|
||||||
// SetOutput() here in case fl.Parse dumps usage.
|
|
||||||
fl.SetOutput(w.GetWriter())
|
|
||||||
err := fl.Parse(args)
|
|
||||||
if err != nil {
|
|
||||||
// fl.Parse has dumped error information to the user via the w writer.
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
args = fl.Args()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return c.Callback(fs, args, w)
|
|
||||||
}
|
|
||||||
|
|
||||||
func dumpCommands(c *radix.Tree, w StringWriter) {
|
|
||||||
err := w.WriteLine("Available commands:")
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
cmds := make([]string, 0)
|
|
||||||
for _, l := range allCommands(c) {
|
|
||||||
cmds = append(cmds, fmt.Sprintf("%s - %s", l.Name, l.ShortDescription))
|
|
||||||
}
|
|
||||||
|
|
||||||
sort.Strings(cmds)
|
|
||||||
_ = w.Write(strings.Join(cmds, "\n") + "\n\n")
|
|
||||||
}
|
|
||||||
|
|
||||||
func lookupCommand(c *radix.Tree, sCmd string) (*Command, error) {
|
|
||||||
cmd, ok := c.Get(sCmd)
|
|
||||||
if !ok {
|
|
||||||
return nil, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
command, ok := cmd.(*Command)
|
|
||||||
if !ok {
|
|
||||||
return nil, errors.New("failed to cast command")
|
|
||||||
}
|
|
||||||
|
|
||||||
return command, nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func matchCommand(c *radix.Tree, cmd string) []string {
|
|
||||||
cmds := make([]string, 0)
|
|
||||||
c.WalkPrefix(cmd, func(found string, v any) bool {
|
|
||||||
cmds = append(cmds, found)
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
sort.Strings(cmds)
|
|
||||||
return cmds
|
|
||||||
}
|
|
||||||
|
|
||||||
func allCommands(c *radix.Tree) []*Command {
|
|
||||||
cmds := make([]*Command, 0)
|
|
||||||
c.WalkPrefix("", func(found string, v any) bool {
|
|
||||||
cmd, ok := v.(*Command)
|
|
||||||
if ok {
|
|
||||||
cmds = append(cmds, cmd)
|
|
||||||
}
|
|
||||||
return false
|
|
||||||
})
|
|
||||||
return cmds
|
|
||||||
}
|
|
||||||
|
|
||||||
func helpCallback(commands *radix.Tree, a []string, w StringWriter) (err error) {
|
|
||||||
// Just typed help
|
|
||||||
if len(a) == 0 {
|
|
||||||
dumpCommands(commands, w)
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
// We are printing a specific commands help text
|
|
||||||
cmd, err := lookupCommand(commands, a[0])
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd != nil {
|
|
||||||
err = w.WriteLine(fmt.Sprintf("%s - %s", cmd.Name, cmd.ShortDescription))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Help != "" {
|
|
||||||
err = w.WriteLine(fmt.Sprintf(" %s", cmd.Help))
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
if cmd.Flags != nil {
|
|
||||||
fs, _ := cmd.Flags()
|
|
||||||
if fs != nil {
|
|
||||||
fs.SetOutput(w.GetWriter())
|
|
||||||
fs.PrintDefaults()
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
err = w.WriteLine("Command not available " + a[0])
|
|
||||||
if err != nil {
|
|
||||||
return err
|
|
||||||
}
|
|
||||||
|
|
||||||
return nil
|
|
||||||
}
|
|
||||||
|
|
||||||
func checkHelpArgs(args []string) bool {
|
|
||||||
for _, a := range args {
|
|
||||||
if a == "-h" || a == "-help" {
|
|
||||||
return true
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
return false
|
|
||||||
}
|
|
||||||
+29
-30
@@ -10,7 +10,7 @@ import (
|
|||||||
"sync"
|
"sync"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
|
|
||||||
"github.com/armon/go-radix"
|
"github.com/slackhq/nebula/commands"
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -29,10 +29,9 @@ type SSHServer struct {
|
|||||||
trustedKeys map[string]map[string]bool
|
trustedKeys map[string]map[string]bool
|
||||||
trustedCAs []ssh.PublicKey
|
trustedCAs []ssh.PublicKey
|
||||||
|
|
||||||
// List of available commands
|
// Available commands.
|
||||||
helpCommand *Command
|
commands *commands.Registry
|
||||||
commands *radix.Tree
|
listener atomic.Pointer[net.Listener]
|
||||||
listener atomic.Pointer[net.Listener]
|
|
||||||
|
|
||||||
// runs counts the Runs that are listening. A reload's Run can start before the one it replaced has returned
|
// runs counts the Runs that are listening. A reload's Run can start before the one it replaced has returned
|
||||||
runs atomic.Int32
|
runs atomic.Int32
|
||||||
@@ -44,14 +43,14 @@ type SSHServer struct {
|
|||||||
ctx context.Context
|
ctx context.Context
|
||||||
}
|
}
|
||||||
|
|
||||||
// NewSSHServer creates a new ssh server rigged with default commands and prepares to listen.
|
// NewSSHServer creates a new ssh server serving the supplied command registry and
|
||||||
// The ssh server's context is parented off the supplied ctx so cancelling it
|
// prepares to listen. The ssh server's context is parented off the supplied ctx so
|
||||||
// (e.g. on Control.Stop) tears down active sessions and closes the listener.
|
// cancelling it (e.g. on Control.Stop) tears down active sessions and closes the listener.
|
||||||
func NewSSHServer(ctx context.Context, l *slog.Logger) (*SSHServer, error) {
|
func NewSSHServer(ctx context.Context, l *slog.Logger, reg *commands.Registry) (*SSHServer, error) {
|
||||||
s := &SSHServer{
|
s := &SSHServer{
|
||||||
trustedKeys: make(map[string]map[string]bool),
|
trustedKeys: make(map[string]map[string]bool),
|
||||||
l: l,
|
l: l,
|
||||||
commands: radix.New(),
|
commands: reg,
|
||||||
ctx: ctx,
|
ctx: ctx,
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -97,14 +96,6 @@ func NewSSHServer(ctx context.Context, l *slog.Logger) (*SSHServer, error) {
|
|||||||
s.authenticate = cc.Authenticate
|
s.authenticate = cc.Authenticate
|
||||||
s.config.Store(s.newConfig())
|
s.config.Store(s.newConfig())
|
||||||
|
|
||||||
s.RegisterCommand(&Command{
|
|
||||||
Name: "help",
|
|
||||||
ShortDescription: "prints available commands or help <command> for specific usage info",
|
|
||||||
Callback: func(a any, args []string, w StringWriter) error {
|
|
||||||
return helpCallback(s.commands, args, w)
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
return s, nil
|
return s, nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,28 +180,38 @@ func (s *SSHServer) AddAuthorizedKey(user, pubKey string) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// RegisterCommand adds a command that can be run by a user, by default only `help` is available
|
|
||||||
func (s *SSHServer) RegisterCommand(c *Command) {
|
|
||||||
s.commands.Insert(c.Name, c)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Run begins listening and accepting connections. Each invocation derives a fresh per-Run context
|
// Run begins listening and accepting connections. Each invocation derives a fresh per-Run context
|
||||||
// from the constructor-supplied ctx so a Stop+Run sequence (used by config reload) starts clean
|
// from the constructor-supplied ctx so a Stop+Run sequence (used by config reload) starts clean
|
||||||
// rather than carrying a permanently-cancelled context across runs.
|
// rather than carrying a permanently-cancelled context across runs.
|
||||||
func (s *SSHServer) Run(addr string) error {
|
func (s *SSHServer) Run(addr string) error {
|
||||||
if s.ctx.Err() != nil {
|
listener, err := s.Listen(addr)
|
||||||
return s.ctx.Err()
|
|
||||||
}
|
|
||||||
|
|
||||||
listener, err := net.Listen("tcp", addr)
|
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
s.Serve(listener)
|
||||||
|
return nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Listen binds addr for Serve, so a caller can tell the bind failed before serving in the background. Stop can close
|
||||||
|
// the listener from here on, Serve then returns as soon as it starts.
|
||||||
|
func (s *SSHServer) Listen(addr string) (net.Listener, error) {
|
||||||
|
if s.ctx.Err() != nil {
|
||||||
|
return nil, s.ctx.Err()
|
||||||
|
}
|
||||||
|
listener, err := net.Listen("tcp", addr)
|
||||||
|
if err != nil {
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
// s.listener is the public handle Stop uses to interrupt the active run; listener (the local) is what
|
// s.listener is the public handle Stop uses to interrupt the active run; listener (the local) is what
|
||||||
// this run owns. They start equal but a fast reload may overwrite s.listener with the next run's
|
// this run owns. They start equal but a fast reload may overwrite s.listener with the next run's
|
||||||
// listener before this run's watcher fires, so each run must close its own listener via the local
|
// listener before this run's watcher fires, so each run must close its own listener via the local
|
||||||
// reference.
|
// reference.
|
||||||
s.listener.Store(&listener)
|
s.listener.Store(&listener)
|
||||||
|
return listener, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// Serve accepts connections on a listener from Listen until Stop, or the context passed to NewSSHServer, ends it.
|
||||||
|
func (s *SSHServer) Serve(listener net.Listener) {
|
||||||
s.runs.Add(1)
|
s.runs.Add(1)
|
||||||
bound := listener.Addr()
|
bound := listener.Addr()
|
||||||
s.addr.Store(&bound)
|
s.addr.Store(&bound)
|
||||||
@@ -238,8 +239,6 @@ func (s *SSHServer) Run(addr string) error {
|
|||||||
s.run(runCtx, listener)
|
s.run(runCtx, listener)
|
||||||
|
|
||||||
s.l.Info("SSH server stopped listening")
|
s.l.Info("SSH server stopped listening")
|
||||||
// We don't return an error because run logs for us
|
|
||||||
return nil
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *SSHServer) run(ctx context.Context, listener net.Listener) {
|
func (s *SSHServer) run(ctx context.Context, listener net.Listener) {
|
||||||
|
|||||||
+37
-46
@@ -1,13 +1,11 @@
|
|||||||
package sshd
|
package sshd
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"fmt"
|
"errors"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"sort"
|
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/anmitsu/go-shlex"
|
"github.com/slackhq/nebula/commands"
|
||||||
"github.com/armon/go-radix"
|
|
||||||
"golang.org/x/crypto/ssh"
|
"golang.org/x/crypto/ssh"
|
||||||
"golang.org/x/term"
|
"golang.org/x/term"
|
||||||
)
|
)
|
||||||
@@ -16,31 +14,37 @@ type session struct {
|
|||||||
l *slog.Logger
|
l *slog.Logger
|
||||||
c *ssh.ServerConn
|
c *ssh.ServerConn
|
||||||
term *term.Terminal
|
term *term.Terminal
|
||||||
commands *radix.Tree
|
commands *commands.Registry
|
||||||
cancel func()
|
cancel func()
|
||||||
}
|
}
|
||||||
|
|
||||||
func NewSession(commands *radix.Tree, conn *ssh.ServerConn, chans <-chan ssh.NewChannel, cancel func(), l *slog.Logger) *session {
|
func NewSession(reg *commands.Registry, conn *ssh.ServerConn, chans <-chan ssh.NewChannel, cancel func(), l *slog.Logger) *session {
|
||||||
s := &session{
|
s := &session{
|
||||||
commands: radix.NewFromMap(commands.ToMap()),
|
commands: reg.Clone(),
|
||||||
l: l,
|
l: l,
|
||||||
c: conn,
|
c: conn,
|
||||||
cancel: cancel,
|
cancel: cancel,
|
||||||
}
|
}
|
||||||
|
|
||||||
s.commands.Insert("logout", &Command{
|
s.commands.Register(LogoutCommand(s.Close))
|
||||||
Name: "logout",
|
|
||||||
ShortDescription: "Ends the current session",
|
|
||||||
Callback: func(a any, args []string, w StringWriter) error {
|
|
||||||
s.Close()
|
|
||||||
return nil
|
|
||||||
},
|
|
||||||
})
|
|
||||||
|
|
||||||
go s.handleChannels(chans)
|
go s.handleChannels(chans)
|
||||||
return s
|
return s
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// LogoutCommand is the logout command a session registers on its clone of the registry,
|
||||||
|
// with end as what it does. Exported so a test can serve what a session serves.
|
||||||
|
func LogoutCommand(end func()) *commands.Command {
|
||||||
|
return &commands.Command{
|
||||||
|
Name: commands.Logout,
|
||||||
|
ShortDescription: "Ends the current session",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
end()
|
||||||
|
return nil
|
||||||
|
},
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
func (s *session) handleChannels(chans <-chan ssh.NewChannel) {
|
func (s *session) handleChannels(chans <-chan ssh.NewChannel) {
|
||||||
defer s.Close()
|
defer s.Close()
|
||||||
for newChannel := range chans {
|
for newChannel := range chans {
|
||||||
@@ -87,9 +91,13 @@ func (s *session) handleRequests(in <-chan *ssh.Request, channel ssh.Channel) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
req.Reply(true, nil)
|
req.Reply(true, nil)
|
||||||
s.dispatchCommand(payload.Value, &stringWriter{channel})
|
err := s.dispatchCommand(payload.Value, commands.NewStringWriter(channel))
|
||||||
|
|
||||||
|
// A command that failed exits 1, so `ssh host cmd` callers can tell.
|
||||||
status := struct{ Status uint32 }{uint32(0)}
|
status := struct{ Status uint32 }{uint32(0)}
|
||||||
|
if err != nil {
|
||||||
|
status.Status = 1
|
||||||
|
}
|
||||||
channel.SendRequest("exit-status", false, ssh.Marshal(status))
|
channel.SendRequest("exit-status", false, ssh.Marshal(status))
|
||||||
channel.Close()
|
channel.Close()
|
||||||
return
|
return
|
||||||
@@ -111,12 +119,11 @@ func (s *session) createTerm(channel ssh.Channel) *term.Terminal {
|
|||||||
term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
|
term.AutoCompleteCallback = func(line string, pos int, key rune) (newLine string, newPos int, ok bool) {
|
||||||
// key 9 is tab
|
// key 9 is tab
|
||||||
if key == 9 {
|
if key == 9 {
|
||||||
cmds := matchCommand(s.commands, line)
|
cmds := s.commands.Match(line)
|
||||||
if len(cmds) == 1 {
|
if len(cmds) == 1 {
|
||||||
return cmds[0] + " ", len(cmds[0]) + 1, true
|
return cmds[0] + " ", len(cmds[0]) + 1, true
|
||||||
}
|
}
|
||||||
|
|
||||||
sort.Strings(cmds)
|
|
||||||
term.Write([]byte(strings.Join(cmds, "\n") + "\n\n"))
|
term.Write([]byte(strings.Join(cmds, "\n") + "\n\n"))
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -128,7 +135,7 @@ func (s *session) createTerm(channel ssh.Channel) *term.Terminal {
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) handleInput() {
|
func (s *session) handleInput() {
|
||||||
w := &stringWriter{w: s.term}
|
w := commands.NewStringWriter(s.term)
|
||||||
for {
|
for {
|
||||||
line, err := s.term.ReadLine()
|
line, err := s.term.ReadLine()
|
||||||
if err != nil {
|
if err != nil {
|
||||||
@@ -139,36 +146,20 @@ func (s *session) handleInput() {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) dispatchCommand(line string, w StringWriter) {
|
// dispatchCommand runs line and returns the command's error, which it has logged unless
|
||||||
args, err := shlex.Split(line, true)
|
// the command already told the user everything: the log line is for the operator reading
|
||||||
if err != nil {
|
// the daemon's log, who learns nothing from a failure the user caused.
|
||||||
return
|
func (s *session) dispatchCommand(line string, w commands.StringWriter) error {
|
||||||
|
err := s.commands.Dispatch(line, w)
|
||||||
|
if err == nil {
|
||||||
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
if len(args) == 0 {
|
var f *commands.Failure
|
||||||
dumpCommands(s.commands, w)
|
if !errors.As(err, &f) {
|
||||||
return
|
s.l.Debug("Command returned an error", "line", line, "error", err)
|
||||||
}
|
}
|
||||||
|
return err
|
||||||
c, err := lookupCommand(s.commands, args[0])
|
|
||||||
if err != nil {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if c == nil {
|
|
||||||
err := w.WriteLine(fmt.Sprintf("did not understand: %s", line))
|
|
||||||
_ = err
|
|
||||||
|
|
||||||
dumpCommands(s.commands, w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
if checkHelpArgs(args) {
|
|
||||||
s.dispatchCommand(fmt.Sprintf("%s %s", "help", c.Name), w)
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
_ = execCommand(c, args[1:], w)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
func (s *session) Close() {
|
func (s *session) Close() {
|
||||||
|
|||||||
+54
-13
@@ -6,13 +6,16 @@ import (
|
|||||||
"crypto/rand"
|
"crypto/rand"
|
||||||
"crypto/rsa"
|
"crypto/rsa"
|
||||||
"encoding/pem"
|
"encoding/pem"
|
||||||
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net"
|
"net"
|
||||||
"os"
|
"os"
|
||||||
"path/filepath"
|
"path/filepath"
|
||||||
|
"strings"
|
||||||
"testing"
|
"testing"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
|
"github.com/slackhq/nebula/commands"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/sshd"
|
"github.com/slackhq/nebula/sshd"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
@@ -23,6 +26,7 @@ import (
|
|||||||
|
|
||||||
type sshdReloadHarness struct {
|
type sshdReloadHarness struct {
|
||||||
c *config.C
|
c *config.C
|
||||||
|
reg *commands.Registry
|
||||||
server *sshd.SSHServer
|
server *sshd.SSHServer
|
||||||
keyFile string
|
keyFile string
|
||||||
signer ssh.Signer
|
signer ssh.Signer
|
||||||
@@ -44,7 +48,8 @@ func newSSHDReloadHarness(t *testing.T) *sshdReloadHarness {
|
|||||||
|
|
||||||
h.c = config.NewC(l)
|
h.c = config.NewC(l)
|
||||||
require.NoError(t, h.c.LoadString(h.config()))
|
require.NoError(t, h.c.LoadString(h.config()))
|
||||||
h.server, err = sshd.NewSSHServer(t.Context(), l)
|
h.reg = commands.New()
|
||||||
|
h.server, err = sshd.NewSSHServer(t.Context(), l, h.reg)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
wireSSHReload(l, h.server, h.c)
|
wireSSHReload(l, h.server, h.c)
|
||||||
run, err := configSSH(l, h.server, h.c, nil)
|
run, err := configSSH(l, h.server, h.c, nil)
|
||||||
@@ -165,18 +170,6 @@ func TestSSHDReload_ChangedConfigRestarts(t *testing.T) {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// sandbox_dir is only read at startup, restarting sshd for it would drop every session and still not apply it
|
|
||||||
func TestSSHDReload_SandboxDirKeepsSessions(t *testing.T) {
|
|
||||||
h := newSSHDReloadHarness(t)
|
|
||||||
client, _ := h.connect(t)
|
|
||||||
|
|
||||||
h.extra = " sandbox_dir: " + t.TempDir() + "\n"
|
|
||||||
res := h.c.ReloadConfigString(h.config())
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.Equal(t, []string{"sshd.sandbox_dir"}, res.RestartKeys)
|
|
||||||
assert.False(t, dropped(client, 500*time.Millisecond), "sshd was restarted")
|
|
||||||
}
|
|
||||||
|
|
||||||
// The config can stay the same while the host key file it names changes. New connections get the new key, open
|
// The config can stay the same while the host key file it names changes. New connections get the new key, open
|
||||||
// sessions are left alone
|
// sessions are left alone
|
||||||
func TestSSHDReload_ChangedHostKeyFile(t *testing.T) {
|
func TestSSHDReload_ChangedHostKeyFile(t *testing.T) {
|
||||||
@@ -250,3 +243,51 @@ func TestSSHDReload_SkippedEntriesFail(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// TestSSHExecExitStatusReflectsTheCommand: an exec request exits 1 when the command
|
||||||
|
// returned an error and 0 otherwise, with the command's output either way.
|
||||||
|
func TestSSHExecExitStatusReflectsTheCommand(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
h.reg.Register(&commands.Command{
|
||||||
|
Name: "fails",
|
||||||
|
Callback: func(fs any, a []string, w commands.StringWriter) error {
|
||||||
|
_ = w.WriteLine("nope")
|
||||||
|
return errors.New("failed")
|
||||||
|
},
|
||||||
|
})
|
||||||
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
|
sess, err := client.NewSession()
|
||||||
|
require.NoError(t, err)
|
||||||
|
out, err := sess.CombinedOutput("fails")
|
||||||
|
var exit *ssh.ExitError
|
||||||
|
require.ErrorAs(t, err, &exit)
|
||||||
|
assert.Equal(t, 1, exit.ExitStatus())
|
||||||
|
assert.Equal(t, "nope\n", string(out))
|
||||||
|
|
||||||
|
sess, err = client.NewSession()
|
||||||
|
require.NoError(t, err)
|
||||||
|
out, err = sess.CombinedOutput("help fails")
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, "fails - \n", string(out))
|
||||||
|
|
||||||
|
// A word that is not a command is a failure too, with the command list as before.
|
||||||
|
sess, err = client.NewSession()
|
||||||
|
require.NoError(t, err)
|
||||||
|
out, err = sess.CombinedOutput("nope")
|
||||||
|
require.ErrorAs(t, err, &exit)
|
||||||
|
assert.Equal(t, 1, exit.ExitStatus())
|
||||||
|
assert.True(t, strings.HasPrefix(string(out), "did not understand: nope\nAvailable commands:\n"), string(out))
|
||||||
|
}
|
||||||
|
|
||||||
|
// A reload that can't bind the new address says so, rather than finding out in the background
|
||||||
|
func TestSSHDReload_BusyPortFails(t *testing.T) {
|
||||||
|
h := newSSHDReloadHarness(t)
|
||||||
|
busy, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
t.Cleanup(func() { _ = busy.Close() })
|
||||||
|
|
||||||
|
res := h.c.ReloadConfigString(strings.Replace(h.config(), "127.0.0.1:0", busy.Addr().String(), 1))
|
||||||
|
require.NoError(t, res.Err)
|
||||||
|
assert.True(t, res.Failed)
|
||||||
|
}
|
||||||
@@ -140,8 +140,13 @@ func (s *statsServer) reload(c *config.C, initial bool) error {
|
|||||||
|
|
||||||
// Even with nothing running, a runtime whose listener failed can still be finishing its last pass
|
// Even with nothing running, a runtime whose listener failed can still be finishing its last pass
|
||||||
s.Stop()
|
s.Stop()
|
||||||
if enabled && !s.configTest {
|
// Bind here rather than in the background, so a reload that can't listen says so
|
||||||
go s.Start()
|
run, err := s.start()
|
||||||
|
if err != nil {
|
||||||
|
return err
|
||||||
|
}
|
||||||
|
if run != nil {
|
||||||
|
go run()
|
||||||
}
|
}
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -151,36 +156,64 @@ func (s *statsServer) reload(c *config.C, initial bool) error {
|
|||||||
// the HTTP listener. For graphite it blocks on the capture loop's context.
|
// the HTTP listener. For graphite it blocks on the capture loop's context.
|
||||||
// Safe to call when stats are disabled or already running (both no-op).
|
// Safe to call when stats are disabled or already running (both no-op).
|
||||||
func (s *statsServer) Start() {
|
func (s *statsServer) Start() {
|
||||||
if !s.enabled.Load() || s.configTest {
|
run, err := s.start()
|
||||||
|
if err != nil {
|
||||||
|
s.l.Error("Failed to start the prometheus stats listener", "error", err)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
if run != nil {
|
||||||
|
run()
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// start claims the runtime and binds the prometheus listener. The returned run serves it and blocks until Stop or ctx
|
||||||
|
// ends it. A nil run and no error means stats are off or already running.
|
||||||
|
func (s *statsServer) start() (func(), error) {
|
||||||
|
if !s.enabled.Load() || s.configTest {
|
||||||
|
return nil, nil
|
||||||
|
}
|
||||||
|
|
||||||
s.runMu.Lock()
|
s.runMu.Lock()
|
||||||
|
defer s.runMu.Unlock()
|
||||||
if s.ctx.Err() != nil || s.run != nil || s.runCfg == nil {
|
if s.ctx.Err() != nil || s.run != nil || s.runCfg == nil {
|
||||||
s.runMu.Unlock()
|
return nil, nil
|
||||||
return
|
|
||||||
}
|
}
|
||||||
cfg := *s.runCfg
|
cfg := *s.runCfg
|
||||||
runCtx, cancel := context.WithCancel(s.ctx)
|
runCtx, cancel := context.WithCancel(s.ctx)
|
||||||
captureFns, listener := s.buildRuntime(runCtx, cfg)
|
captureFns, listener := s.buildRuntime(runCtx, cfg)
|
||||||
|
var ln net.Listener
|
||||||
|
if listener != nil {
|
||||||
|
var err error
|
||||||
|
ln, err = net.Listen("tcp", listener.Addr)
|
||||||
|
if err != nil {
|
||||||
|
cancel()
|
||||||
|
// Drop the config so a SIGHUP with the same one tries again once the problem is fixed
|
||||||
|
s.runCfg = nil
|
||||||
|
return nil, err
|
||||||
|
}
|
||||||
|
}
|
||||||
rt := &statsRuntime{cancel: cancel, listener: listener}
|
rt := &statsRuntime{cancel: cancel, listener: listener}
|
||||||
s.run = rt
|
s.run = rt
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
s.loopDone = done
|
s.loopDone = done
|
||||||
s.runMu.Unlock()
|
|
||||||
|
|
||||||
|
return func() { s.serve(runCtx, rt, captureFns, cfg.interval, ln, done) }, nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// serve runs the capture loop, and the prometheus listener on ln, until Stop or ctx ends them.
|
||||||
|
func (s *statsServer) serve(runCtx context.Context, rt *statsRuntime, captureFns []func(), interval time.Duration, ln net.Listener, done chan struct{}) {
|
||||||
// Prime before serving, so a scrape that lands before the first tick sees real values instead of zeros (issue #907)
|
// Prime before serving, so a scrape that lands before the first tick sees real values instead of zeros (issue #907)
|
||||||
for _, fn := range captureFns {
|
for _, fn := range captureFns {
|
||||||
fn()
|
fn()
|
||||||
}
|
}
|
||||||
go captureStatsLoop(runCtx, cfg.interval, captureFns, done)
|
go captureStatsLoop(runCtx, interval, captureFns, done)
|
||||||
|
|
||||||
cleanExit := true
|
cleanExit := true
|
||||||
if listener == nil {
|
if rt.listener == nil {
|
||||||
// Graphite: no HTTP listener to serve; block until teardown.
|
// Graphite: no HTTP listener to serve; block until teardown.
|
||||||
<-runCtx.Done()
|
<-runCtx.Done()
|
||||||
} else {
|
} else {
|
||||||
cleanExit = s.serveListener(listener)
|
cleanExit = s.serveListener(rt.listener, ln)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear our runtime only if nothing has replaced it. Stop races through
|
// Clear our runtime only if nothing has replaced it. Stop races through
|
||||||
@@ -189,10 +222,9 @@ func (s *statsServer) Start() {
|
|||||||
if s.run == rt {
|
if s.run == rt {
|
||||||
rt.cancel()
|
rt.cancel()
|
||||||
s.run = nil
|
s.run = nil
|
||||||
// A listener that exited with an error (e.g., bind conflict) leaves
|
// A listener that exited with an error leaves runCfg cached as if it
|
||||||
// runCfg cached as if it were applied. Drop it so a SIGHUP with the
|
// were applied. Drop it so a SIGHUP with the same config re-triggers
|
||||||
// same config re-triggers Start once the user fixes the underlying
|
// Start once the user fixes the underlying problem.
|
||||||
// problem.
|
|
||||||
if !cleanExit {
|
if !cleanExit {
|
||||||
s.runCfg = nil
|
s.runCfg = nil
|
||||||
}
|
}
|
||||||
@@ -200,12 +232,12 @@ func (s *statsServer) Start() {
|
|||||||
s.runMu.Unlock()
|
s.runMu.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
// serveListener runs ListenAndServe and ensures ctx cancellation unblocks it.
|
// serveListener serves listener on ln and ensures ctx cancellation unblocks it.
|
||||||
// Returns true if the listener exited cleanly (Stop, ctx cancellation, or any
|
// Returns true if the listener exited cleanly (Stop, ctx cancellation, or any
|
||||||
// other http.ErrServerClosed path), false on an unexpected error.
|
// other http.ErrServerClosed path), false on an unexpected error.
|
||||||
func (s *statsServer) serveListener(listener *http.Server) bool {
|
func (s *statsServer) serveListener(listener *http.Server, ln net.Listener) bool {
|
||||||
// Per-invocation watcher: ctx cancellation triggers a listener shutdown
|
// Per-invocation watcher: ctx cancellation triggers a listener shutdown
|
||||||
// which in turn unblocks ListenAndServe. Closing `done` on exit keeps
|
// which in turn unblocks Serve. Closing `done` on exit keeps
|
||||||
// the watcher from outliving this call.
|
// the watcher from outliving this call.
|
||||||
done := make(chan struct{})
|
done := make(chan struct{})
|
||||||
go func() {
|
go func() {
|
||||||
@@ -221,8 +253,8 @@ func (s *statsServer) serveListener(listener *http.Server) bool {
|
|||||||
}()
|
}()
|
||||||
defer close(done)
|
defer close(done)
|
||||||
|
|
||||||
s.l.Info("Starting prometheus stats listener", "addr", listener.Addr)
|
s.l.Info("Starting prometheus stats listener", "addr", ln.Addr())
|
||||||
err := listener.ListenAndServe()
|
err := listener.Serve(ln)
|
||||||
if err == nil || errors.Is(err, http.ErrServerClosed) {
|
if err == nil || errors.Is(err, http.ErrServerClosed) {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -673,3 +673,28 @@ func TestStatsServer_Start_duringStopWaitsForThePass(t *testing.T) {
|
|||||||
waitFor(t, func() bool { return len(entered) == 1 })
|
waitFor(t, func() bool { return len(entered) == 1 })
|
||||||
assert.Equal(t, int64(1), most.Load(), "a Start captured alongside the pass Stop was waiting out")
|
assert.Equal(t, int64(1), most.Load(), "a Start captured alongside the pass Stop was waiting out")
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// A reload that can't bind says so rather than finding out in the background, and the same config tries again once
|
||||||
|
// the port is free
|
||||||
|
func TestStatsServer_reload_bindFailureFails(t *testing.T) {
|
||||||
|
blocker, err := net.Listen("tcp", "127.0.0.1:0")
|
||||||
|
require.NoError(t, err)
|
||||||
|
port := strconv.Itoa(blocker.Addr().(*net.TCPAddr).Port)
|
||||||
|
|
||||||
|
s, c := newTestStatsServer(t)
|
||||||
|
stopAtCleanup(t, &s)
|
||||||
|
require.NoError(t, s.reload(c, true))
|
||||||
|
|
||||||
|
setStatsConfig(c, map[string]any{
|
||||||
|
"type": "prometheus",
|
||||||
|
"interval": "1s",
|
||||||
|
"listen": "127.0.0.1:" + port,
|
||||||
|
"path": "/metrics",
|
||||||
|
})
|
||||||
|
require.Error(t, s.reload(c, false))
|
||||||
|
assert.Nil(t, currentRuntime(s))
|
||||||
|
|
||||||
|
require.NoError(t, blocker.Close())
|
||||||
|
require.NoError(t, s.reload(c, false))
|
||||||
|
waitForListening(t, "127.0.0.1:"+port)
|
||||||
|
}
|
||||||
@@ -0,0 +1,29 @@
|
|||||||
|
package test
|
||||||
|
|
||||||
|
import (
|
||||||
|
"flag"
|
||||||
|
"os"
|
||||||
|
"path/filepath"
|
||||||
|
"testing"
|
||||||
|
|
||||||
|
"github.com/stretchr/testify/assert"
|
||||||
|
"github.com/stretchr/testify/require"
|
||||||
|
)
|
||||||
|
|
||||||
|
// Update is the -update flag: Golden rewrites its file instead of comparing against it.
|
||||||
|
var Update = flag.Bool("update", false, "rewrite golden files instead of comparing against them")
|
||||||
|
|
||||||
|
// Golden compares got with the file at path, or rewrites the file when -update is set.
|
||||||
|
func Golden(t *testing.T, path, got string) {
|
||||||
|
t.Helper()
|
||||||
|
|
||||||
|
if *Update {
|
||||||
|
require.NoError(t, os.MkdirAll(filepath.Dir(path), 0755))
|
||||||
|
require.NoError(t, os.WriteFile(path, []byte(got), 0644))
|
||||||
|
return
|
||||||
|
}
|
||||||
|
|
||||||
|
want, err := os.ReadFile(path)
|
||||||
|
require.NoError(t, err, "missing golden file, run with -update")
|
||||||
|
assert.Equal(t, string(want), got)
|
||||||
|
}
|
||||||
Vendored
+94
@@ -0,0 +1,94 @@
|
|||||||
|
change-remote
|
||||||
|
short: Changes the remote address used in the tunnel for the provided vpn addr
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-address string
|
||||||
|
The new remote address, ip:port
|
||||||
|
close-tunnel
|
||||||
|
short: Closes a tunnel for the provided vpn addr
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-local-only
|
||||||
|
Disables notifying the remote that the tunnel is shutting down
|
||||||
|
create-tunnel
|
||||||
|
short: Creates a tunnel for the provided vpn address
|
||||||
|
help: The lighthouses will be queried for real addresses but you can provide one as well.
|
||||||
|
flags:
|
||||||
|
-address string
|
||||||
|
Optionally provide a real remote address, ip:port
|
||||||
|
device-info
|
||||||
|
short: Prints information about the network device.
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-json
|
||||||
|
outputs as json with more information
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
help
|
||||||
|
short: prints available commands or help <command> for specific usage info
|
||||||
|
help:
|
||||||
|
list-hostmap
|
||||||
|
short: List all known previously connected hosts
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-by-index
|
||||||
|
gets all hosts in the hostmap from the index table
|
||||||
|
-json
|
||||||
|
outputs as json with more information
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
list-lighthouse-addrmap
|
||||||
|
short: List all lighthouse map entries
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-json
|
||||||
|
outputs as json with more information
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
list-pending-hostmap
|
||||||
|
short: List all handshaking hosts
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-by-index
|
||||||
|
gets all hosts in the hostmap from the index table
|
||||||
|
-json
|
||||||
|
outputs as json with more information
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
log-format
|
||||||
|
short: Gets or sets the current log format
|
||||||
|
help:
|
||||||
|
log-level
|
||||||
|
short: Gets or sets the current log level
|
||||||
|
help:
|
||||||
|
print-cert
|
||||||
|
short: Prints the current certificate being used or the certificate for the provided vpn addr
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-json
|
||||||
|
outputs as json
|
||||||
|
-pretty
|
||||||
|
pretty prints json, assumes -json
|
||||||
|
-raw
|
||||||
|
raw prints the PEM encoded certificate, not compatible with -json or -pretty
|
||||||
|
print-relays
|
||||||
|
short: Prints json details about all relay info
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
print-tunnel
|
||||||
|
short: Prints json details about a tunnel for the provided vpn addr
|
||||||
|
help:
|
||||||
|
flags:
|
||||||
|
-pretty
|
||||||
|
pretty prints json
|
||||||
|
query-lighthouse
|
||||||
|
short: Query the lighthouses for the provided vpn address
|
||||||
|
help: This command is asynchronous. Only currently known udp addresses will be printed.
|
||||||
|
reload
|
||||||
|
short: Reloads configuration from disk, same as sending HUP to the process
|
||||||
|
help:
|
||||||
|
version
|
||||||
|
short: Prints the currently running version of nebula
|
||||||
|
help:
|
||||||
Reference in new issue
Block a user