mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 16:16:52 +01:00
Compare commits
1
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
ea666c1900 |
No files matched your search
@@ -7,11 +7,6 @@ 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
|
||||||
|
|
||||||
- `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
|
||||||
|
|||||||
+8
-2
@@ -8,7 +8,6 @@ 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.
|
||||||
@@ -233,6 +232,13 @@ 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:
|
||||||
@@ -255,7 +261,7 @@ tun:
|
|||||||
disabled: false
|
disabled: false
|
||||||
# Name of the device. If not set, a default will be chosen by the OS.
|
# Name of the device. If not set, a default will be chosen by the OS.
|
||||||
# For macOS: if set, must be in the form `utun[0-9]+`.
|
# For macOS: if set, must be in the form `utun[0-9]+`.
|
||||||
# For OpenBSD and NetBSD: if set, must be in the form `tun[0-9]+`, otherwise the first free one is used.
|
# For NetBSD: Required to be set, must be in the form `tun[0-9]+`
|
||||||
# Not reloadable.
|
# Not reloadable.
|
||||||
dev: nebula1
|
dev: nebula1
|
||||||
# Toggles forwarding of local broadcast packets, the address of which depends on the ip/mask encoded in pki.cert
|
# Toggles forwarding of local broadcast packets, the address of which depends on the ip/mask encoded in pki.cert
|
||||||
|
|||||||
+17
-22
@@ -65,7 +65,7 @@ type HandshakeManager struct {
|
|||||||
lightHouse *LightHouse
|
lightHouse *LightHouse
|
||||||
outside udp.Conn
|
outside udp.Conn
|
||||||
config HandshakeConfig
|
config HandshakeConfig
|
||||||
OutboundHandshakeTimer *LockingTimerWheel[*HandshakeHostInfo]
|
OutboundHandshakeTimer *LockingTimerWheel[netip.Addr]
|
||||||
messageMetrics *MessageMetrics
|
messageMetrics *MessageMetrics
|
||||||
metricInitiated metrics.Counter
|
metricInitiated metrics.Counter
|
||||||
metricTimedOut metrics.Counter
|
metricTimedOut metrics.Counter
|
||||||
@@ -81,7 +81,6 @@ type HandshakeHostInfo struct {
|
|||||||
|
|
||||||
startTime time.Time // Time that we first started trying with this handshake
|
startTime time.Time // Time that we first started trying with this handshake
|
||||||
ready bool // Is the handshake ready
|
ready bool // Is the handshake ready
|
||||||
deleted bool // Has the handshake been removed from the pending maps, deleters hold the lock
|
|
||||||
initiatingVersionOverride cert.Version // Should we use a non-default cert version for this handshake?
|
initiatingVersionOverride cert.Version // Should we use a non-default cert version for this handshake?
|
||||||
counter int64 // How many attempts have we made so far
|
counter int64 // How many attempts have we made so far
|
||||||
lastRemotes []netip.AddrPort // Remotes that we sent to during the previous attempt
|
lastRemotes []netip.AddrPort // Remotes that we sent to during the previous attempt
|
||||||
@@ -126,7 +125,7 @@ func NewHandshakeManager(l *slog.Logger, mainHostMap *HostMap, lightHouse *Light
|
|||||||
outside: outside,
|
outside: outside,
|
||||||
config: config,
|
config: config,
|
||||||
trigger: make(chan netip.Addr, config.triggerBuffer),
|
trigger: make(chan netip.Addr, config.triggerBuffer),
|
||||||
OutboundHandshakeTimer: NewLockingTimerWheel[*HandshakeHostInfo](config.tryInterval, hsTimeout(config.retries, config.tryInterval)),
|
OutboundHandshakeTimer: NewLockingTimerWheel[netip.Addr](config.tryInterval, hsTimeout(config.retries, config.tryInterval)),
|
||||||
messageMetrics: config.messageMetrics,
|
messageMetrics: config.messageMetrics,
|
||||||
metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
|
metricInitiated: metrics.GetOrRegisterCounter("handshake_manager.initiated", nil),
|
||||||
metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
|
metricTimedOut: metrics.GetOrRegisterCounter("handshake_manager.timed_out", nil),
|
||||||
@@ -143,9 +142,7 @@ func (hm *HandshakeManager) Run(ctx context.Context) {
|
|||||||
case <-ctx.Done():
|
case <-ctx.Done():
|
||||||
return
|
return
|
||||||
case vpnIP := <-hm.trigger:
|
case vpnIP := <-hm.trigger:
|
||||||
if hh := hm.queryVpnIp(vpnIP); hh != nil {
|
hm.handleOutbound(vpnIP, true)
|
||||||
hm.handleOutbound(hh, true)
|
|
||||||
}
|
|
||||||
case now := <-clockSource.C:
|
case now := <-clockSource.C:
|
||||||
hm.NextOutboundHandshakeTimerTick(now)
|
hm.NextOutboundHandshakeTimerTick(now)
|
||||||
}
|
}
|
||||||
@@ -200,25 +197,23 @@ func (hm *HandshakeManager) HandleIncoming(via ViaSender, packet []byte, h *head
|
|||||||
func (hm *HandshakeManager) NextOutboundHandshakeTimerTick(now time.Time) {
|
func (hm *HandshakeManager) NextOutboundHandshakeTimerTick(now time.Time) {
|
||||||
hm.OutboundHandshakeTimer.Advance(now)
|
hm.OutboundHandshakeTimer.Advance(now)
|
||||||
for {
|
for {
|
||||||
hh, has := hm.OutboundHandshakeTimer.Purge()
|
vpnIp, has := hm.OutboundHandshakeTimer.Purge()
|
||||||
if !has {
|
if !has {
|
||||||
break
|
break
|
||||||
}
|
}
|
||||||
hm.handleOutbound(hh, false)
|
hm.handleOutbound(vpnIp, false)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hm *HandshakeManager) handleOutbound(hh *HandshakeHostInfo, lighthouseTriggered bool) {
|
func (hm *HandshakeManager) handleOutbound(vpnIp netip.Addr, lighthouseTriggered bool) {
|
||||||
|
hh := hm.queryVpnIp(vpnIp)
|
||||||
|
if hh == nil {
|
||||||
|
return
|
||||||
|
}
|
||||||
hh.Lock()
|
hh.Lock()
|
||||||
defer hh.Unlock()
|
defer hh.Unlock()
|
||||||
|
|
||||||
// Timer entries outlive their handshake. A new handshake for the same vpn addr runs its own timer chain.
|
|
||||||
if hh.deleted {
|
|
||||||
return
|
|
||||||
}
|
|
||||||
|
|
||||||
hostinfo := hh.hostinfo
|
hostinfo := hh.hostinfo
|
||||||
vpnIp := hostinfo.vpnAddrs[0]
|
|
||||||
// If we are out of time, clean up
|
// If we are out of time, clean up
|
||||||
if hh.counter >= hm.config.retries {
|
if hh.counter >= hm.config.retries {
|
||||||
fields := []any{
|
fields := []any{
|
||||||
@@ -244,14 +239,10 @@ func (hm *HandshakeManager) handleOutbound(hh *HandshakeHostInfo, lighthouseTrig
|
|||||||
// Increment the counter to increase our delay, linear backoff
|
// Increment the counter to increase our delay, linear backoff
|
||||||
hh.counter++
|
hh.counter++
|
||||||
|
|
||||||
// A lighthouse triggered attempt is still in the timer wheel, adding it again would start a second retry chain
|
|
||||||
if !lighthouseTriggered {
|
|
||||||
defer hm.OutboundHandshakeTimer.Add(hh, hm.config.tryInterval*time.Duration(hh.counter))
|
|
||||||
}
|
|
||||||
|
|
||||||
// Check if we have a handshake packet to transmit yet
|
// Check if we have a handshake packet to transmit yet
|
||||||
if !hh.ready {
|
if !hh.ready {
|
||||||
if !hm.buildStage0Packet(hh) {
|
if !hm.buildStage0Packet(hh) {
|
||||||
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -332,6 +323,11 @@ func (hm *HandshakeManager) handleOutbound(hh *HandshakeHostInfo, lighthouseTrig
|
|||||||
}
|
}
|
||||||
|
|
||||||
hm.f.relayManager.StartRelays(hm.f, vpnIp, hh, stage0)
|
hm.f.relayManager.StartRelays(hm.f, vpnIp, hh, stage0)
|
||||||
|
|
||||||
|
// If a lighthouse triggered this attempt then we are still in the timer wheel and do not need to re-add
|
||||||
|
if !lighthouseTriggered {
|
||||||
|
hm.OutboundHandshakeTimer.Add(vpnIp, hm.config.tryInterval*time.Duration(hh.counter))
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// GetOrHandshake will try to find a hostinfo with a fully formed tunnel or start a new handshake if one is not present
|
// GetOrHandshake will try to find a hostinfo with a fully formed tunnel or start a new handshake if one is not present
|
||||||
@@ -384,7 +380,7 @@ func (hm *HandshakeManager) StartHandshake(vpnAddr netip.Addr, cacheCb func(*Han
|
|||||||
}
|
}
|
||||||
hm.vpnIps[vpnAddr] = hh
|
hm.vpnIps[vpnAddr] = hh
|
||||||
hm.metricInitiated.Inc(1)
|
hm.metricInitiated.Inc(1)
|
||||||
hm.OutboundHandshakeTimer.Add(hh, hm.config.tryInterval)
|
hm.OutboundHandshakeTimer.Add(vpnAddr, hm.config.tryInterval)
|
||||||
|
|
||||||
if cacheCb != nil {
|
if cacheCb != nil {
|
||||||
cacheCb(hh)
|
cacheCb(hh)
|
||||||
@@ -536,7 +532,6 @@ func (hm *HandshakeManager) DeleteHostInfo(hostinfo *HostInfo) {
|
|||||||
func (hm *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
|
func (hm *HandshakeManager) unlockedDeleteHostInfo(hostinfo *HostInfo) {
|
||||||
for _, addr := range hostinfo.vpnAddrs {
|
for _, addr := range hostinfo.vpnAddrs {
|
||||||
if cur, ok := hm.vpnIps[addr]; ok && cur.hostinfo == hostinfo {
|
if cur, ok := hm.vpnIps[addr]; ok && cur.hostinfo == hostinfo {
|
||||||
cur.deleted = true
|
|
||||||
delete(hm.vpnIps, addr)
|
delete(hm.vpnIps, addr)
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|||||||
@@ -65,69 +65,7 @@ func Test_NewHandshakeManagerVpnIp(t *testing.T) {
|
|||||||
assert.NotContains(t, blah.vpnIps, ip)
|
assert.NotContains(t, blah.vpnIps, ip)
|
||||||
}
|
}
|
||||||
|
|
||||||
// newTestHandshakeManager returns a manager whose handshakes never build a stage 0 packet (the cert has no key), so
|
func testCountTimerWheelEntries(tw *LockingTimerWheel[netip.Addr]) (c int) {
|
||||||
// every attempt just bumps the counter and schedules the next one. tick advances its timer wheel by one tryInterval.
|
|
||||||
func newTestHandshakeManager() (hm *HandshakeManager, tick func()) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
mainHM := newHostMap(l)
|
|
||||||
mainHM.preferredRanges.Store(&[]netip.Prefix{})
|
|
||||||
|
|
||||||
hm = NewHandshakeManager(l, mainHM, newTestLighthouse(), &udp.NoopConn{}, defaultHandshakeConfig)
|
|
||||||
hm.f = &Interface{handshakeManager: hm, pki: &PKI{}, l: l}
|
|
||||||
hm.f.pki.cs.Store(&CertState{
|
|
||||||
initiatingVersion: cert.Version1,
|
|
||||||
privateKey: []byte{},
|
|
||||||
v1Cert: &dummyCert{version: cert.Version1},
|
|
||||||
})
|
|
||||||
|
|
||||||
now := time.Now()
|
|
||||||
hm.NextOutboundHandshakeTimerTick(now)
|
|
||||||
return hm, func() {
|
|
||||||
now = now.Add(DefaultHandshakeTryInterval)
|
|
||||||
hm.NextOutboundHandshakeTimerTick(now)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_HandshakeManager_RestartDropsOldTimer(t *testing.T) {
|
|
||||||
ip := netip.MustParseAddr("172.1.1.2")
|
|
||||||
hm, tick := newTestHandshakeManager()
|
|
||||||
|
|
||||||
hm.StartHandshake(ip, nil)
|
|
||||||
old := hm.queryVpnIp(ip)
|
|
||||||
tick()
|
|
||||||
tick()
|
|
||||||
require.Equal(t, int64(1), old.counter)
|
|
||||||
|
|
||||||
// Restart the handshake while the old one still has a retry queued
|
|
||||||
hm.DeleteHostInfo(old.hostinfo)
|
|
||||||
hm.StartHandshake(ip, nil)
|
|
||||||
hh := hm.queryVpnIp(ip)
|
|
||||||
require.NotSame(t, old, hh)
|
|
||||||
|
|
||||||
// An entry for timeout d fires d/tryInterval+1 ticks after it is added, so linear backoff lands attempts on ticks
|
|
||||||
// 2, 4 and 7
|
|
||||||
var counters []int64
|
|
||||||
for range 7 {
|
|
||||||
tick()
|
|
||||||
counters = append(counters, hh.counter)
|
|
||||||
}
|
|
||||||
assert.Equal(t, []int64{0, 1, 1, 2, 2, 2, 3}, counters)
|
|
||||||
|
|
||||||
// The old handshake's queued retry ended its chain instead of retrying a deleted handshake
|
|
||||||
assert.Equal(t, int64(1), old.counter)
|
|
||||||
assert.Equal(t, 1, testCountTimerWheelEntries(hm.OutboundHandshakeTimer))
|
|
||||||
}
|
|
||||||
|
|
||||||
func Test_HandshakeManager_LighthouseTriggerKeepsOneTimer(t *testing.T) {
|
|
||||||
ip := netip.MustParseAddr("172.1.1.2")
|
|
||||||
hm, _ := newTestHandshakeManager()
|
|
||||||
|
|
||||||
hm.StartHandshake(ip, nil)
|
|
||||||
hm.handleOutbound(hm.queryVpnIp(ip), true)
|
|
||||||
assert.Equal(t, 1, testCountTimerWheelEntries(hm.OutboundHandshakeTimer))
|
|
||||||
}
|
|
||||||
|
|
||||||
func testCountTimerWheelEntries(tw *LockingTimerWheel[*HandshakeHostInfo]) (c int) {
|
|
||||||
for _, i := range tw.t.wheel {
|
for _, i := range tw.t.wheel {
|
||||||
n := i.Head
|
n := i.Head
|
||||||
for n != nil {
|
for n != nil {
|
||||||
|
|||||||
+5
-7
@@ -316,9 +316,9 @@ type cachedPacketMetrics struct {
|
|||||||
func NewHostMapFromConfig(l *slog.Logger, c *config.C) *HostMap {
|
func NewHostMapFromConfig(l *slog.Logger, c *config.C) *HostMap {
|
||||||
hm := newHostMap(l)
|
hm := newHostMap(l)
|
||||||
|
|
||||||
hm.reload(c, true, nil)
|
hm.reload(c, true)
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, _ *config.Reload) {
|
||||||
hm.reload(c, false, r)
|
hm.reload(c, false)
|
||||||
})
|
})
|
||||||
|
|
||||||
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
||||||
@@ -337,8 +337,7 @@ func newHostMap(l *slog.Logger) *HostMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// r is nil on the initial load
|
func (hm *HostMap) reload(c *config.C, initial bool) {
|
||||||
func (hm *HostMap) reload(c *config.C, initial bool, r *config.Reload) {
|
|
||||||
if initial || c.HasChanged("preferred_ranges") {
|
if initial || c.HasChanged("preferred_ranges") {
|
||||||
var preferredRanges []netip.Prefix
|
var preferredRanges []netip.Prefix
|
||||||
rawPreferredRanges := c.GetStringSlice("preferred_ranges", []string{})
|
rawPreferredRanges := c.GetStringSlice("preferred_ranges", []string{})
|
||||||
@@ -349,9 +348,8 @@ func (hm *HostMap) reload(c *config.C, initial bool, r *config.Reload) {
|
|||||||
if err != nil {
|
if err != nil {
|
||||||
hm.l.Warn("Failed to parse preferred ranges, ignoring",
|
hm.l.Warn("Failed to parse preferred ranges, ignoring",
|
||||||
"error", err,
|
"error", err,
|
||||||
"range", rawPreferredRange,
|
"range", rawPreferredRanges,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -447,16 +447,3 @@ func TestHostInfo_markOutOnly(t *testing.T) {
|
|||||||
assert.True(t, h.markOut(6), "a relayed send must not consume the epoch edge")
|
assert.True(t, h.markOut(6), "a relayed send must not consume the epoch edge")
|
||||||
assert.False(t, h.markOut(6))
|
assert.False(t, h.markOut(6))
|
||||||
}
|
}
|
||||||
|
|
||||||
// A preferred range that doesn't parse is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestHostMap_reloadBadPreferredRangeFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("preferred_ranges: [10.0.0.0/8]\n"))
|
|
||||||
hm := NewHostMapFromConfig(l, c)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("preferred_ranges: [10.0.0.0/8, nope]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix("10.0.0.0/8")}, hm.GetPreferredRanges())
|
|
||||||
}
|
|
||||||
+3
-6
@@ -138,14 +138,14 @@ func NewLightHouseFromConfig(ctx context.Context, l *slog.Logger, c *config.C, c
|
|||||||
h.metrics = newLighthouseMetrics()
|
h.metrics = newLighthouseMetrics()
|
||||||
}
|
}
|
||||||
|
|
||||||
err := h.reload(c, true, nil)
|
err := h.reload(c, true)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
r.RestartRequiredIfChanged("lighthouse.am_lighthouse", "listen.port", "handshakes.query_buffer", "stats.lighthouse_metrics")
|
r.RestartRequiredIfChanged("lighthouse.am_lighthouse", "listen.port", "handshakes.query_buffer", "stats.lighthouse_metrics")
|
||||||
err := h.reload(c, false, r)
|
err := h.reload(c, false)
|
||||||
switch v := err.(type) {
|
switch v := err.(type) {
|
||||||
case *util.ContextualError:
|
case *util.ContextualError:
|
||||||
v.Log(l)
|
v.Log(l)
|
||||||
@@ -193,8 +193,7 @@ func (lh *LightHouse) GetUpdateInterval() int64 {
|
|||||||
return lh.interval.Load()
|
return lh.interval.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
// r is nil on the initial load
|
func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
||||||
func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error {
|
|
||||||
if initial || c.HasChanged("lighthouse.advertise_addrs") {
|
if initial || c.HasChanged("lighthouse.advertise_addrs") {
|
||||||
rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
|
rawAdvAddrs := c.GetStringSlice("lighthouse.advertise_addrs", []string{})
|
||||||
advAddrs := make([]netip.AddrPort, 0)
|
advAddrs := make([]netip.AddrPort, 0)
|
||||||
@@ -229,7 +228,6 @@ func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error
|
|||||||
"addr", rawAddr,
|
"addr", rawAddr,
|
||||||
"entry", i+1,
|
"entry", i+1,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -388,7 +386,6 @@ func (lh *LightHouse) reload(c *config.C, initial bool, r *config.Reload) error
|
|||||||
"relay", v,
|
"relay", v,
|
||||||
"error", err,
|
"error", err,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
} else {
|
} else {
|
||||||
lh.l.Info("Read relay from config", "relay", v)
|
lh.l.Info("Read relay from config", "relay", v)
|
||||||
relaysForMe = append(relaysForMe, configRIP)
|
relaysForMe = append(relaysForMe, configRIP)
|
||||||
|
|||||||
+1
-29
@@ -296,7 +296,7 @@ func TestLighthouse_reload(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
c.ReloadConfigString(string(rc))
|
c.ReloadConfigString(string(rc))
|
||||||
|
|
||||||
err = lh.reload(c, false, nil)
|
err = lh.reload(c, false)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1014,31 +1014,3 @@ func TestLighthouse_reloadRestartRequired(t *testing.T) {
|
|||||||
require.NoError(t, res.Err)
|
require.NoError(t, res.Err)
|
||||||
assert.True(t, res.RestartRequired)
|
assert.True(t, res.RestartRequired)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A relay that doesn't parse is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestLighthouse_reloadBadRelayFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("listen:\n port: 4242\n"))
|
|
||||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(netip.MustParsePrefix("10.128.0.1/24")), nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("listen:\n port: 4242\nrelay:\n relays: [10.128.0.2, nope]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.Addr{netip.MustParseAddr("10.128.0.2")}, lh.GetRelaysForMe())
|
|
||||||
}
|
|
||||||
|
|
||||||
// An advertise_addrs entry inside our own network is skipped and the rest are used, the reload says one was skipped
|
|
||||||
func TestLighthouse_reloadAdvertiseAddrInNetworkFails(t *testing.T) {
|
|
||||||
l := test.NewLogger()
|
|
||||||
c := config.NewC(l)
|
|
||||||
require.NoError(t, c.LoadString("listen:\n port: 4242\n"))
|
|
||||||
lh, err := NewLightHouseFromConfig(t.Context(), l, c, testCertState(netip.MustParsePrefix("10.128.0.1/24")), nil, nil)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString("listen:\n port: 4242\nlighthouse:\n advertise_addrs: [\"1.2.3.4:4242\", \"10.128.0.5:4242\"]\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
assert.Equal(t, []netip.AddrPort{netip.MustParseAddrPort("1.2.3.4:4242")}, lh.GetAdvertiseAddrs())
|
|
||||||
}
|
|
||||||
@@ -78,7 +78,7 @@ func Main(c *config.C, configTest bool, buildVersion string, l *slog.Logger, dev
|
|||||||
wireSSHReload(l, ssh, c)
|
wireSSHReload(l, ssh, c)
|
||||||
var sshStart func()
|
var sshStart func()
|
||||||
if c.GetBool("sshd.enabled", false) {
|
if c.GetBool("sshd.enabled", false) {
|
||||||
sshStart, err = configSSH(l, ssh, c, nil)
|
sshStart, err = configSSH(l, ssh, c)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warn("Failed to configure sshd, ssh debugging will not be available", "error", err)
|
l.Warn("Failed to configure sshd, ssh debugging will not be available", "error", err)
|
||||||
sshStart = nil
|
sshStart = nil
|
||||||
|
|||||||
+1
-3
@@ -476,11 +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 {
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
hostinfo.logger(f.l).Warn("Error while validating inbound packet", "error", err, "packet", out)
|
||||||
hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
|
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
}
|
|
||||||
|
|
||||||
dropReason := f.firewall.Drop(rxc.fwPacket.Packet, true, hostinfo, f.pki.GetCAPool(), rxc.ctCache.Get())
|
dropReason := f.firewall.Drop(rxc.fwPacket.Packet, true, hostinfo, f.pki.GetCAPool(), rxc.ctCache.Get())
|
||||||
if dropReason != nil {
|
if dropReason != nil {
|
||||||
|
|||||||
@@ -1,45 +0,0 @@
|
|||||||
//go:build (openbsd || netbsd) && !e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"errors"
|
|
||||||
"fmt"
|
|
||||||
"log/slog"
|
|
||||||
"os"
|
|
||||||
"regexp"
|
|
||||||
"runtime"
|
|
||||||
|
|
||||||
"golang.org/x/sys/unix"
|
|
||||||
)
|
|
||||||
|
|
||||||
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
|
||||||
|
|
||||||
// tunDevNeedsRestart is true when tun.dev asks for a particular tun that isn't the running one. Anything else takes the
|
|
||||||
// first free one, and the one we have will do
|
|
||||||
func tunDevNeedsRestart(name, running string) bool {
|
|
||||||
return deviceNameRE.MatchString(name) && name != running
|
|
||||||
}
|
|
||||||
|
|
||||||
// openTun opens /dev/name, or the first /dev/tunN nothing else has open when name is empty or not tunN
|
|
||||||
func openTun(l *slog.Logger, name string) (int, string, error) {
|
|
||||||
if name != "" && !deviceNameRE.MatchString(name) {
|
|
||||||
// The name doesn't mean anything here, so like darwin a bad one isn't worth refusing to start over
|
|
||||||
l.Warn("interface name must be tun[0-9]+ on "+runtime.GOOS+", ignoring", "name", name)
|
|
||||||
name = ""
|
|
||||||
}
|
|
||||||
if name != "" {
|
|
||||||
fd, err := unix.Open("/dev/"+name, os.O_RDWR, 0)
|
|
||||||
return fd, name, err
|
|
||||||
}
|
|
||||||
for i := 0; ; i++ {
|
|
||||||
name = fmt.Sprintf("tun%d", i)
|
|
||||||
fd, err := unix.Open("/dev/"+name, os.O_RDWR, 0)
|
|
||||||
if err == nil {
|
|
||||||
return fd, name, nil
|
|
||||||
}
|
|
||||||
if !errors.Is(err, unix.EBUSY) {
|
|
||||||
return -1, "", fmt.Errorf("no free tun device, %s: %w", name, err)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
}
|
|
||||||
@@ -1,80 +0,0 @@
|
|||||||
//go:build (openbsd || netbsd) && !e2e_testing
|
|
||||||
|
|
||||||
package overlay
|
|
||||||
|
|
||||||
import (
|
|
||||||
"net/netip"
|
|
||||||
"testing"
|
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
|
||||||
"github.com/slackhq/nebula/test"
|
|
||||||
"github.com/stretchr/testify/assert"
|
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
|
||||||
|
|
||||||
// A tun.dev that isn't tunN is ignored like darwin does, and the first free tun is used. Only naming a different tun
|
|
||||||
// asks for a restart
|
|
||||||
func TestTunReloadDevRestart(t *testing.T) {
|
|
||||||
newWithDev := func(dev string) (*config.C, *tun, error) {
|
|
||||||
c := config.NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("tun:\n dev: "+dev+"\n"))
|
|
||||||
tn, err := newTun(c, test.NewLogger(), []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")}, false)
|
|
||||||
if err == nil {
|
|
||||||
t.Cleanup(func() { _ = tn.Close() })
|
|
||||||
}
|
|
||||||
return c, tn, err
|
|
||||||
}
|
|
||||||
if _, _, err := newWithDev(""); err != nil {
|
|
||||||
t.Skipf("can't create a tun device here: %v", err)
|
|
||||||
}
|
|
||||||
c, tn, err := newWithDev("nope")
|
|
||||||
require.NoError(t, err, "a bad name should fall back to a free tun")
|
|
||||||
require.Regexp(t, `^tun[0-9]+$`, tn.Device)
|
|
||||||
|
|
||||||
for _, dev := range []string{"nope", "", tn.Device, "nope"} {
|
|
||||||
res := c.ReloadConfigString("tun:\n dev: " + dev + "\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.False(t, res.Failed, dev)
|
|
||||||
assert.False(t, res.RestartRequired, dev)
|
|
||||||
}
|
|
||||||
|
|
||||||
other := "tun0"
|
|
||||||
if tn.Device == other {
|
|
||||||
other = "tun1"
|
|
||||||
}
|
|
||||||
res := c.ReloadConfigString("tun:\n dev: " + other + "\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.Equal(t, []string{"tun.dev"}, res.RestartKeys)
|
|
||||||
}
|
|
||||||
|
|
||||||
// An empty tun.dev takes the first tun nothing else has open. Reloading it empty, or naming the one we have, needs
|
|
||||||
// no restart
|
|
||||||
func TestTunEmptyDevPicksAFreeOne(t *testing.T) {
|
|
||||||
newEmpty := func() (*config.C, *tun, error) {
|
|
||||||
c := config.NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString("tun: {}\n"))
|
|
||||||
tn, err := newTun(c, test.NewLogger(), []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")}, false)
|
|
||||||
if err == nil {
|
|
||||||
t.Cleanup(func() { _ = tn.Close() })
|
|
||||||
}
|
|
||||||
return c, tn, err
|
|
||||||
}
|
|
||||||
c, first, err := newEmpty()
|
|
||||||
if err != nil {
|
|
||||||
t.Skipf("can't create a tun device here: %v", err)
|
|
||||||
}
|
|
||||||
_, second, err := newEmpty()
|
|
||||||
require.NoError(t, err, "the second tun should skip the one the first has open")
|
|
||||||
assert.NotEqual(t, first.Device, second.Device)
|
|
||||||
|
|
||||||
for _, cfg := range []string{
|
|
||||||
"tun: {}\npunchy:\n punch: true\n",
|
|
||||||
"tun:\n dev: " + first.Device + "\n",
|
|
||||||
"tun: {}\n",
|
|
||||||
} {
|
|
||||||
res := c.ReloadConfigString(cfg)
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.False(t, res.Failed)
|
|
||||||
assert.False(t, res.RestartRequired, cfg)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+9
-26
@@ -80,31 +80,17 @@ type ifreqAlias6 struct {
|
|||||||
Lifetime addrLifetime
|
Lifetime addrLifetime
|
||||||
}
|
}
|
||||||
|
|
||||||
// utunIndex is the utun tun.dev asks for, or -1 for whichever is free. ok is false for a name that isn't utunN, which
|
|
||||||
// also gets whichever is free
|
|
||||||
func utunIndex(name string) (index int, ok bool) {
|
|
||||||
if name == "" || name == "utun" {
|
|
||||||
return -1, true
|
|
||||||
}
|
|
||||||
if _, err := fmt.Sscanf(name, "utun%d", &index); err != nil || index < 0 {
|
|
||||||
return -1, false
|
|
||||||
}
|
|
||||||
return index, true
|
|
||||||
}
|
|
||||||
|
|
||||||
// tunDevNeedsRestart is true when tun.dev asks for a particular utun that isn't the running one. Anything else takes
|
|
||||||
// whichever is free, and the one we have will do
|
|
||||||
func tunDevNeedsRestart(name, running string) bool {
|
|
||||||
index, _ := utunIndex(name)
|
|
||||||
return index >= 0 && fmt.Sprintf("utun%d", index) != running
|
|
||||||
}
|
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
||||||
ifIndex, ok := utunIndex(c.GetString("tun.dev", ""))
|
name := c.GetString("tun.dev", "")
|
||||||
if !ok {
|
ifIndex := -1
|
||||||
|
if name != "" && name != "utun" {
|
||||||
|
_, err := fmt.Sscanf(name, "utun%d", &ifIndex)
|
||||||
|
if err != nil || ifIndex < 0 {
|
||||||
// NOTE: we don't make this error so we don't break existing
|
// NOTE: we don't make this error so we don't break existing
|
||||||
// configs that set a name before it was used.
|
// configs that set a name before it was used.
|
||||||
l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
|
l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
|
||||||
|
ifIndex = -1
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, unix.AF_SYS_CONTROL)
|
fd, err := unix.Socket(unix.AF_SYSTEM, unix.SOCK_DGRAM, unix.AF_SYS_CONTROL)
|
||||||
@@ -128,7 +114,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
return nil, fmt.Errorf("SYS_CONNECT: %v", err)
|
return nil, fmt.Errorf("SYS_CONNECT: %v", err)
|
||||||
}
|
}
|
||||||
|
|
||||||
name, err := unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
|
name, err = unix.GetsockoptString(fd, unix.AF_SYS_CONTROL, _UTUN_OPT_IFNAME)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, fmt.Errorf("failed to retrieve tun name: %w", err)
|
return nil, fmt.Errorf("failed to retrieve tun name: %w", err)
|
||||||
}
|
}
|
||||||
@@ -152,10 +138,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
r.RestartRequiredIfChanged("tun.mtu")
|
r.RestartRequiredIfChanged("tun.dev", "tun.mtu")
|
||||||
if tunDevNeedsRestart(c.GetString("tun.dev", ""), t.Device) {
|
|
||||||
r.RestartRequiredIfChanged("tun.dev")
|
|
||||||
}
|
|
||||||
err := t.reload(c, false, r)
|
err := t.reload(c, false, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
||||||
|
|||||||
@@ -103,16 +103,3 @@ func TestTunReloadLeavesRoutesItNeverInstalled(t *testing.T) {
|
|||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
assert.Equal(t, lo0.Index, routeInterfaceIn(msgs, netip.MustParsePrefix(cidr)))
|
assert.Equal(t, lo0.Index, routeInterfaceIn(msgs, netip.MustParsePrefix(cidr)))
|
||||||
}
|
}
|
||||||
|
|
||||||
// Only naming a different utun asks for a restart. Empty, plain utun and names that aren't utunN take whichever is free
|
|
||||||
func TestTunDevNeedsRestart(t *testing.T) {
|
|
||||||
for name, want := range map[string]bool{
|
|
||||||
"": false,
|
|
||||||
"utun": false,
|
|
||||||
"nope": false,
|
|
||||||
"utun7": false,
|
|
||||||
"utun3": true,
|
|
||||||
} {
|
|
||||||
assert.Equal(t, want, tunDevNeedsRestart(name, "utun7"), name)
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+85
-19
@@ -255,7 +255,7 @@ func (t *tun) reload(c *config.C, initial bool, r *config.Reload) error {
|
|||||||
return err
|
return err
|
||||||
}
|
}
|
||||||
|
|
||||||
if !initial && !routeChange && !c.HasChanged("tun.mtu") {
|
if !initial && !routeChange && !c.HasChanged("tun.mtu") && t.deviceHasConfiguredMTU() {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -280,8 +280,6 @@ func (t *tun) reload(c *config.C, initial bool, r *config.Reload) error {
|
|||||||
}
|
}
|
||||||
t.routesFromSystemLock.Unlock()
|
t.routesFromSystemLock.Unlock()
|
||||||
|
|
||||||
oldDefaultMTU := t.DefaultMTU
|
|
||||||
oldMaxMTU := t.MaxMTU
|
|
||||||
newDefaultMTU := getMTU(t.l, c)
|
newDefaultMTU := getMTU(t.l, c)
|
||||||
newMaxMTU := newDefaultMTU
|
newMaxMTU := newDefaultMTU
|
||||||
for i, r := range routes {
|
for i, r := range routes {
|
||||||
@@ -302,20 +300,27 @@ func (t *tun) reload(c *config.C, initial bool, r *config.Reload) error {
|
|||||||
t.routeTree.Store(routeTree)
|
t.routeTree.Store(routeTree)
|
||||||
|
|
||||||
if !initial {
|
if !initial {
|
||||||
if oldMaxMTU != newMaxMTU {
|
// Compare with the device rather than the last config, the mtu may have been refused or changed under us
|
||||||
t.setMTU(r)
|
if !t.deviceHasMaxMTU() {
|
||||||
t.l.Info("Set max MTU", "mtu", t.MaxMTU, "oldMTU", oldMaxMTU)
|
oldMTU, _ := t.deviceMTU()
|
||||||
|
if err := t.setMTU(t.MaxMTU); err != nil {
|
||||||
|
t.l.Error("Failed to set tun mtu", "mtu", t.MaxMTU, "error", err)
|
||||||
|
r.Failed()
|
||||||
|
} else {
|
||||||
|
t.l.Info("Set max MTU", "mtu", t.MaxMTU, "oldMTU", oldMTU)
|
||||||
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
if oldDefaultMTU != newDefaultMTU {
|
for _, network := range t.vpnNetworks {
|
||||||
for i := range t.vpnNetworks {
|
if t.defaultRouteHasMTU(network) {
|
||||||
err := t.setDefaultRoute(t.vpnNetworks[i])
|
continue
|
||||||
|
}
|
||||||
|
err := t.setDefaultRoute(network)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
t.l.Error(err.Error())
|
t.l.Error(err.Error())
|
||||||
r.Failed()
|
r.Failed()
|
||||||
} else {
|
} else {
|
||||||
t.l.Info("Set default MTU", "mtu", t.DefaultMTU, "oldMTU", oldDefaultMTU)
|
t.l.Info("Set default MTU", "mtu", t.DefaultMTU, "network", network)
|
||||||
}
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -482,7 +487,10 @@ func (t *tun) Activate() error {
|
|||||||
t.deviceIndex = link.Attrs().Index
|
t.deviceIndex = link.Attrs().Index
|
||||||
|
|
||||||
// Setup our default MTU
|
// Setup our default MTU
|
||||||
t.setMTU(nil)
|
if err := t.setMTU(t.MaxMTU); err != nil {
|
||||||
|
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
|
||||||
|
t.l.Error("Failed to set tun mtu", "error", err)
|
||||||
|
}
|
||||||
|
|
||||||
// Set the transmit queue length
|
// Set the transmit queue length
|
||||||
ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)}
|
ifrq := ifreqQLEN{Name: devName, Value: int32(t.TXQueueLen)}
|
||||||
@@ -527,14 +535,72 @@ func (t *tun) Activate() error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) setMTU(r *config.Reload) {
|
func (t *tun) setMTU(mtu int) error {
|
||||||
// Set the MTU on the device
|
ifm := ifreqMTU{Name: t.deviceBytes(), MTU: int32(mtu)}
|
||||||
ifm := ifreqMTU{Name: t.deviceBytes(), MTU: int32(t.MaxMTU)}
|
return ioctl(t.ioctlFd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm)))
|
||||||
if err := ioctl(t.ioctlFd, unix.SIOCSIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
}
|
||||||
// This is currently a non fatal condition because the route table must have the MTU set appropriately as well
|
|
||||||
t.l.Error("Failed to set tun mtu", "error", err)
|
// deviceMTU asks the kernel for the device's mtu.
|
||||||
r.Failed()
|
func (t *tun) deviceMTU() (int, error) {
|
||||||
|
ifm := ifreqMTU{Name: t.deviceBytes()}
|
||||||
|
if err := ioctl(t.ioctlFd, unix.SIOCGIFMTU, uintptr(unsafe.Pointer(&ifm))); err != nil {
|
||||||
|
return 0, err
|
||||||
}
|
}
|
||||||
|
return int(ifm.MTU), nil
|
||||||
|
}
|
||||||
|
|
||||||
|
// deviceHasConfiguredMTU is true when the device and the route for each of our networks carry the mtu the config asks
|
||||||
|
// for.
|
||||||
|
func (t *tun) deviceHasConfiguredMTU() bool {
|
||||||
|
if !t.deviceHasMaxMTU() {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, network := range t.vpnNetworks {
|
||||||
|
if !t.defaultRouteHasMTU(network) {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
|
||||||
|
// defaultRouteHasMTU is true when the kernel's route for network on our device has the default mtu and advmss. Before
|
||||||
|
// Activate there's nothing to check, Activate installs it.
|
||||||
|
func (t *tun) defaultRouteHasMTU(network netip.Prefix) bool {
|
||||||
|
if t.deviceIndex == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
family := netlink.FAMILY_V4
|
||||||
|
if network.Addr().Is6() {
|
||||||
|
family = netlink.FAMILY_V6
|
||||||
|
}
|
||||||
|
filter := &netlink.Route{
|
||||||
|
LinkIndex: t.deviceIndex,
|
||||||
|
Dst: &net.IPNet{
|
||||||
|
IP: network.Masked().Addr().AsSlice(),
|
||||||
|
Mask: net.CIDRMask(network.Bits(), network.Addr().BitLen()),
|
||||||
|
},
|
||||||
|
Table: unix.RT_TABLE_MAIN,
|
||||||
|
}
|
||||||
|
routes, err := netlink.RouteListFiltered(family, filter, netlink.RT_FILTER_OIF|netlink.RT_FILTER_DST|netlink.RT_FILTER_TABLE)
|
||||||
|
if err != nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
for _, r := range routes {
|
||||||
|
if r.MTU == t.DefaultMTU && r.AdvMSS == t.advMSS(Route{}) {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
// deviceHasMaxMTU is true when the device's mtu is the one the config asks for. Before Activate there's nothing to
|
||||||
|
// check, Activate sets it.
|
||||||
|
func (t *tun) deviceHasMaxMTU() bool {
|
||||||
|
if t.ioctlFd == 0 {
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
mtu, err := t.deviceMTU()
|
||||||
|
return err == nil && mtu == t.MaxMTU
|
||||||
}
|
}
|
||||||
|
|
||||||
func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
func (t *tun) setDefaultRoute(cidr netip.Prefix) error {
|
||||||
|
|||||||
+15
-5
@@ -8,6 +8,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -66,12 +67,24 @@ type tun struct {
|
|||||||
fd int
|
fd int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in NetBSD")
|
return nil, fmt.Errorf("newTunFromFd not supported in NetBSD")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
||||||
fd, deviceName, err := openTun(l, c.GetString("tun.dev", ""))
|
// Try to open tun device
|
||||||
|
var err error
|
||||||
|
deviceName := c.GetString("tun.dev", "")
|
||||||
|
if deviceName == "" {
|
||||||
|
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
||||||
|
}
|
||||||
|
if !deviceNameRE.MatchString(deviceName) {
|
||||||
|
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
fd, err := unix.Open("/dev/"+deviceName, os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -96,10 +109,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
r.RestartRequiredIfChanged("tun.mtu")
|
r.RestartRequiredIfChanged("tun.dev", "tun.mtu")
|
||||||
if tunDevNeedsRestart(c.GetString("tun.dev", ""), t.Device) {
|
|
||||||
r.RestartRequiredIfChanged("tun.dev")
|
|
||||||
}
|
|
||||||
err := t.reload(c, false, r)
|
err := t.reload(c, false, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
||||||
|
|||||||
+15
-5
@@ -8,6 +8,7 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
|
"regexp"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -57,12 +58,24 @@ type tun struct {
|
|||||||
fd int
|
fd int
|
||||||
}
|
}
|
||||||
|
|
||||||
|
var deviceNameRE = regexp.MustCompile(`^tun[0-9]+$`)
|
||||||
|
|
||||||
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
func newTunFromFd(_ *config.C, _ *slog.Logger, _ int, _ []netip.Prefix) (*tun, error) {
|
||||||
return nil, fmt.Errorf("newTunFromFd not supported in openbsd")
|
return nil, fmt.Errorf("newTunFromFd not supported in openbsd")
|
||||||
}
|
}
|
||||||
|
|
||||||
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*tun, error) {
|
||||||
fd, deviceName, err := openTun(l, c.GetString("tun.dev", ""))
|
// Try to open tun device
|
||||||
|
var err error
|
||||||
|
deviceName := c.GetString("tun.dev", "")
|
||||||
|
if deviceName == "" {
|
||||||
|
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
||||||
|
}
|
||||||
|
if !deviceNameRE.MatchString(deviceName) {
|
||||||
|
return nil, fmt.Errorf("a device name in the format of /dev/tunN must be specified")
|
||||||
|
}
|
||||||
|
|
||||||
|
fd, err := unix.Open("/dev/"+deviceName, os.O_RDWR, 0)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
return nil, err
|
return nil, err
|
||||||
}
|
}
|
||||||
@@ -87,10 +100,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*t
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
r.RestartRequiredIfChanged("tun.mtu")
|
r.RestartRequiredIfChanged("tun.dev", "tun.mtu")
|
||||||
if tunDevNeedsRestart(c.GetString("tun.dev", ""), t.Device) {
|
|
||||||
r.RestartRequiredIfChanged("tun.dev")
|
|
||||||
}
|
|
||||||
err := t.reload(c, false, r)
|
err := t.reload(c, false, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
util.LogWithContextIfNeeded("failed to reload tun device", err, t.l)
|
||||||
|
|||||||
@@ -7,11 +7,13 @@ import (
|
|||||||
"os"
|
"os"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/DefinedNet/netlink"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
"github.com/slackhq/nebula/routing"
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/test"
|
"github.com/slackhq/nebula/test"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
"github.com/stretchr/testify/require"
|
||||||
|
"golang.org/x/sys/unix"
|
||||||
)
|
)
|
||||||
|
|
||||||
// Only tun.mtu changes. The system calls fail without a real device, what matters is the routes nebula keeps
|
// Only tun.mtu changes. The system calls fail without a real device, what matters is the routes nebula keeps
|
||||||
@@ -76,7 +78,7 @@ func TestReloadRouteFailures(t *testing.T) {
|
|||||||
tn := &tun{
|
tn := &tun{
|
||||||
l: test.NewLogger(),
|
l: test.NewLogger(),
|
||||||
vpnNetworks: []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")},
|
vpnNetworks: []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")},
|
||||||
ioctlFd: ^uintptr(0),
|
ioctlFd: 0, // not activated, the mtu is not checked
|
||||||
routesFromSystem: map[netip.Prefix]routing.Gateways{},
|
routesFromSystem: map[netip.Prefix]routing.Gateways{},
|
||||||
}
|
}
|
||||||
require.NoError(t, tn.reload(c, true, nil))
|
require.NoError(t, tn.reload(c, true, nil))
|
||||||
@@ -105,3 +107,83 @@ func TestTunReloadRestartKeys(t *testing.T) {
|
|||||||
require.NoError(t, res.Err)
|
require.NoError(t, res.Err)
|
||||||
assert.ElementsMatch(t, []string{"tun.disabled", "tun.dev", "tun.tx_queue"}, res.RestartKeys)
|
assert.ElementsMatch(t, []string{"tun.disabled", "tun.dev", "tun.tx_queue"}, res.RestartKeys)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// There is no device behind this tun, so it refuses every mtu. Repeating the reload tries again rather than reporting
|
||||||
|
// it done
|
||||||
|
func TestReloadRefusedMaxMTURetries(t *testing.T) {
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
require.NoError(t, c.LoadString("tun:\n mtu: 1300\n"))
|
||||||
|
tn := &tun{
|
||||||
|
l: test.NewLogger(),
|
||||||
|
vpnNetworks: []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")},
|
||||||
|
ioctlFd: ^uintptr(0),
|
||||||
|
routesFromSystem: map[netip.Prefix]routing.Gateways{},
|
||||||
|
}
|
||||||
|
require.NoError(t, tn.reload(c, true, nil))
|
||||||
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
|
require.NoError(t, tn.reload(c, false, r))
|
||||||
|
})
|
||||||
|
|
||||||
|
for range 2 {
|
||||||
|
res := c.ReloadConfigString("tun:\n mtu: 1400\n")
|
||||||
|
require.NoError(t, res.Err)
|
||||||
|
assert.True(t, res.Failed)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// A reload puts the configured mtu back on the device and our network's route when they were changed outside nebula
|
||||||
|
func TestTunReloadRestoresMTU(t *testing.T) {
|
||||||
|
c := config.NewC(test.NewLogger())
|
||||||
|
require.NoError(t, c.LoadString("tun:\n dev: nebmtu0\n mtu: 1300\n"))
|
||||||
|
tn, err := newTun(c, test.NewLogger(), []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")}, false)
|
||||||
|
if err != nil {
|
||||||
|
t.Skipf("can't create a tun device here: %v", err)
|
||||||
|
}
|
||||||
|
t.Cleanup(func() { _ = tn.Close() })
|
||||||
|
require.NoError(t, tn.Activate())
|
||||||
|
|
||||||
|
link, err := netlink.LinkByName(tn.Device)
|
||||||
|
require.NoError(t, err)
|
||||||
|
require.NoError(t, netlink.LinkSetMTU(link, 1280))
|
||||||
|
network := &netlink.Route{LinkIndex: tn.deviceIndex, Table: unix.RT_TABLE_MAIN}
|
||||||
|
routeMTU := func() int {
|
||||||
|
routes, err := netlink.RouteListFiltered(netlink.FAMILY_V4, network, netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE)
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, r := range routes {
|
||||||
|
if r.Dst != nil && r.Dst.String() == "10.1.0.0/24" {
|
||||||
|
return r.MTU
|
||||||
|
}
|
||||||
|
}
|
||||||
|
t.Fatal("no route for our network")
|
||||||
|
return 0
|
||||||
|
}
|
||||||
|
require.Equal(t, 1300, routeMTU())
|
||||||
|
setRouteMTU := func(mtu int) {
|
||||||
|
routes, err := netlink.RouteListFiltered(netlink.FAMILY_V4, network, netlink.RT_FILTER_OIF|netlink.RT_FILTER_TABLE)
|
||||||
|
require.NoError(t, err)
|
||||||
|
for _, r := range routes {
|
||||||
|
if r.Dst != nil && r.Dst.String() == "10.1.0.0/24" {
|
||||||
|
r.MTU = mtu
|
||||||
|
require.NoError(t, netlink.RouteReplace(&r))
|
||||||
|
}
|
||||||
|
}
|
||||||
|
require.Equal(t, mtu, routeMTU())
|
||||||
|
}
|
||||||
|
reload := func() {
|
||||||
|
res := c.ReloadConfigString("tun:\n dev: nebmtu0\n mtu: 1300\n")
|
||||||
|
require.NoError(t, res.Err)
|
||||||
|
assert.False(t, res.Failed)
|
||||||
|
}
|
||||||
|
|
||||||
|
setRouteMTU(1280)
|
||||||
|
reload()
|
||||||
|
mtu, err := tn.deviceMTU()
|
||||||
|
require.NoError(t, err)
|
||||||
|
assert.Equal(t, 1300, mtu)
|
||||||
|
assert.Equal(t, 1300, routeMTU())
|
||||||
|
|
||||||
|
// Only the route this time
|
||||||
|
setRouteMTU(1280)
|
||||||
|
reload()
|
||||||
|
assert.Equal(t, 1300, routeMTU())
|
||||||
|
}
|
||||||
@@ -109,15 +109,7 @@ func newTun(c *config.C, l *slog.Logger, vpnNetworks []netip.Prefix, _ bool) (*w
|
|||||||
disableDNSRegistration(l, *guid)
|
disableDNSRegistration(l, *guid)
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
r.RestartRequiredIfChanged("tun.dev", "tun.mtu")
|
r.RestartRequiredIfChanged("tun.dev", "tun.mtu", "tun.network_category")
|
||||||
// Compare with the running category, not the last config. A restart with one we can't parse would fail, so say
|
|
||||||
// that now rather than ask for one, and going back to the running one after a bad reload needs no restart
|
|
||||||
if cat, setCat, err := parseNetworkCategory(c.GetString("tun.network_category", "private")); err != nil {
|
|
||||||
t.l.Error("Invalid tun.network_category, a restart with it would fail", "error", err)
|
|
||||||
r.Failed()
|
|
||||||
} else if cat != t.networkCategory || setCat != t.setCategory {
|
|
||||||
r.RestartRequiredIfChanged("tun.network_category")
|
|
||||||
}
|
|
||||||
r.RestartRequiredIfChanged(interfaceBypassKeys...)
|
r.RestartRequiredIfChanged(interfaceBypassKeys...)
|
||||||
err := t.reload(c, false, r)
|
err := t.reload(c, false, r)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
|
|||||||
@@ -181,27 +181,6 @@ func TestWinTun_ReloadRouteAlreadyGone(t *testing.T) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// A network category we can't parse would fail the restart, so it fails the reload instead of asking for one
|
|
||||||
func TestWinTun_ReloadInvalidNetworkCategoryFails(t *testing.T) {
|
|
||||||
rt := newRouteTest(t, unsafeRoutes())
|
|
||||||
for range 2 {
|
|
||||||
res := rt.c.ReloadConfigString(unsafeRoutes() + " network_category: nope\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed, "a bad category that didn't change since the last reload is still bad")
|
|
||||||
assert.False(t, res.RestartRequired)
|
|
||||||
}
|
|
||||||
|
|
||||||
// Fixing it back to the running category needs nothing
|
|
||||||
res := rt.c.ReloadConfigString(unsafeRoutes() + " network_category: private\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.False(t, res.Failed)
|
|
||||||
assert.False(t, res.RestartRequired)
|
|
||||||
|
|
||||||
res = rt.c.ReloadConfigString(unsafeRoutes() + " network_category: public\n")
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.Equal(t, []string{"tun.network_category"}, res.RestartKeys)
|
|
||||||
}
|
|
||||||
|
|
||||||
// forFamilies runs f with two unsafe_routes CIDRs of each address family, the tun's own network stays IPv4
|
// forFamilies runs f with two unsafe_routes CIDRs of each address family, the tun's own network stays IPv4
|
||||||
func forFamilies(t *testing.T, f func(t *testing.T, a, b string)) {
|
func forFamilies(t *testing.T, f func(t *testing.T, a, b string)) {
|
||||||
t.Run("ipv4", func(t *testing.T) { f(t, "10.251.1.0/24", "10.251.2.0/24") })
|
t.Run("ipv4", func(t *testing.T) { f(t, "10.251.1.0/24", "10.251.2.0/24") })
|
||||||
|
|||||||
@@ -2,9 +2,7 @@ package nebula
|
|||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/fips140"
|
"crypto/fips140"
|
||||||
"crypto/sha256"
|
|
||||||
"encoding/binary"
|
"encoding/binary"
|
||||||
"encoding/hex"
|
|
||||||
"encoding/json"
|
"encoding/json"
|
||||||
"errors"
|
"errors"
|
||||||
"fmt"
|
"fmt"
|
||||||
@@ -63,18 +61,7 @@ func NewPKIFromConfig(l *slog.Logger, c *config.C) (*PKI, error) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
// Compare with the running cipher, not the last config. A restart with one we can't run would fail, so say that
|
|
||||||
// now rather than ask for one, and going back to the running one after a bad reload needs no restart
|
|
||||||
if cipher := c.GetString("cipher", "aes"); !validCipher(cipher) {
|
|
||||||
l.Error("Unknown cipher, a restart with it would fail", "cipher", cipher)
|
|
||||||
r.Failed()
|
|
||||||
} else if cipher == "chachapoly" && fipsOnly() {
|
|
||||||
// newCipherSuite refuses it at startup
|
|
||||||
l.Error("ChaChaPoly is not allowed in FIPS 140-only mode, a restart with it would fail", "cipher", cipher)
|
|
||||||
r.Failed()
|
|
||||||
} else if cipher != pki.getCertState().cipher {
|
|
||||||
r.RestartRequiredIfChanged("cipher")
|
r.RestartRequiredIfChanged("cipher")
|
||||||
}
|
|
||||||
rErr := pki.reload(c, false, r)
|
rErr := pki.reload(c, false, r)
|
||||||
if rErr != nil {
|
if rErr != nil {
|
||||||
util.LogWithContextIfNeeded("Failed to reload PKI from config", rErr, l)
|
util.LogWithContextIfNeeded("Failed to reload PKI from config", rErr, l)
|
||||||
@@ -129,21 +116,16 @@ func (p *PKI) reload(c *config.C, initial bool, r *config.Reload) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// fipsOnly is fips140.Enforced, a variable so a test can run the FIPS 140-only reload check without GODEBUG
|
|
||||||
var fipsOnly = fips140.Enforced
|
|
||||||
|
|
||||||
// validCipher is true for the ciphers nebula can run. Each post-handshake CipherState in noiseutil hardcodes its own
|
|
||||||
// nonce endianness, so there's nothing to set up for either.
|
|
||||||
func validCipher(cipher string) bool {
|
|
||||||
return cipher == "aes" || cipher == "chachapoly"
|
|
||||||
}
|
|
||||||
|
|
||||||
func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
func (p *PKI) reloadCerts(c *config.C, initial bool) *util.ContextualError {
|
||||||
var cipher string
|
var cipher string
|
||||||
var currentState *CertState
|
var currentState *CertState
|
||||||
if initial {
|
if initial {
|
||||||
cipher = c.GetString("cipher", "aes")
|
cipher = c.GetString("cipher", "aes")
|
||||||
if !validCipher(cipher) {
|
switch cipher {
|
||||||
|
case "aes", "chachapoly":
|
||||||
|
// Each post-handshake CipherState in noiseutil hardcodes its own
|
||||||
|
// nonce endianness now, so there's nothing to set up here.
|
||||||
|
default:
|
||||||
return util.NewContextualError(
|
return util.NewContextualError(
|
||||||
"unknown cipher",
|
"unknown cipher",
|
||||||
m{"cipher": cipher},
|
m{"cipher": cipher},
|
||||||
@@ -235,14 +217,12 @@ 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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -617,22 +597,11 @@ 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 {
|
||||||
var bad []string
|
for _, fp := range bl {
|
||||||
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)-len(bad))
|
l.Info("Blocklisted certificates", "fingerprintCount", len(bl))
|
||||||
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
|
||||||
|
|||||||
-82
@@ -1,7 +1,6 @@
|
|||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
import (
|
import (
|
||||||
"crypto/fips140"
|
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -129,23 +128,6 @@ func TestPKIReloadRestartRequired(t *testing.T) {
|
|||||||
assert.True(t, res.RestartRequired)
|
assert.True(t, res.RestartRequired)
|
||||||
assert.Equal(t, "aes", p.getCertState().cipher)
|
assert.Equal(t, "aes", p.getCertState().cipher)
|
||||||
|
|
||||||
// A cipher we can't run would fail the restart, so it fails the reload instead of asking for one
|
|
||||||
c4 := config.NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c4.LoadString(pkiConfig("aes", "10.1.0.2/24")))
|
|
||||||
_, err = NewPKIFromConfig(test.NewLogger(), c4)
|
|
||||||
require.NoError(t, err)
|
|
||||||
for range 2 {
|
|
||||||
res = c4.ReloadConfigString(pkiConfig("nope", "10.1.0.2/24"))
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed, "a bad cipher that didn't change since the last reload is still bad")
|
|
||||||
assert.False(t, res.RestartRequired)
|
|
||||||
}
|
|
||||||
// Fixing it back to the running cipher needs nothing
|
|
||||||
res = c4.ReloadConfigString(pkiConfig("aes", "10.1.0.2/24"))
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.False(t, res.Failed)
|
|
||||||
assert.False(t, res.RestartRequired)
|
|
||||||
|
|
||||||
// A cert that keeps our networks applies without a restart
|
// A cert that keeps our networks applies without a restart
|
||||||
c3 := config.NewC(test.NewLogger())
|
c3 := config.NewC(test.NewLogger())
|
||||||
require.NoError(t, c3.LoadString(pkiConfig("aes", "10.1.0.2/24")))
|
require.NoError(t, c3.LoadString(pkiConfig("aes", "10.1.0.2/24")))
|
||||||
@@ -196,67 +178,3 @@ func TestPKIReloadNewVersionWithOtherNetworks(t *testing.T) {
|
|||||||
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix(tc.network)}, p.getCertState().myVpnNetworks, "accepted either way, as before")
|
assert.Equal(t, []netip.Prefix{netip.MustParsePrefix(tc.network)}, p.getCertState().myVpnNetworks, "accepted either way, as before")
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
// FIPS 140-only mode refuses chachapoly at startup, so reloading to it fails rather than asks for a restart
|
|
||||||
func TestPKIReloadChachapolyInFIPSOnlyFails(t *testing.T) {
|
|
||||||
fipsOnly = func() bool { return true }
|
|
||||||
t.Cleanup(func() { fipsOnly = fips140.Enforced })
|
|
||||||
ca, _, caKey, caPEM := cert_test.NewTestCaCert(cert.Version2, cert.Curve_P256, time.Time{}, time.Time{}, nil, nil, nil)
|
|
||||||
_, _, keyPEM, certPEM := cert_test.NewTestCert(cert.Version2, cert.Curve_P256, ca, caKey, "host", time.Time{}, time.Time{}, []netip.Prefix{netip.MustParsePrefix("10.1.0.2/24")}, nil, nil)
|
|
||||||
pkiConfig := func(cipher string) string {
|
|
||||||
return fmt.Sprintf("cipher: %s\npki:\n ca: %q\n cert: %q\n key: %q\n", cipher, caPEM, certPEM, keyPEM)
|
|
||||||
}
|
|
||||||
|
|
||||||
c := config.NewC(test.NewLogger())
|
|
||||||
require.NoError(t, c.LoadString(pkiConfig("aes")))
|
|
||||||
_, err := NewPKIFromConfig(test.NewLogger(), c)
|
|
||||||
require.NoError(t, err)
|
|
||||||
|
|
||||||
res := c.ReloadConfigString(pkiConfig("chachapoly"))
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
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))
|
|
||||||
}
|
|
||||||
@@ -11,8 +11,11 @@ import (
|
|||||||
"net"
|
"net"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"slices"
|
"path/filepath"
|
||||||
|
"runtime"
|
||||||
|
"runtime/pprof"
|
||||||
"sort"
|
"sort"
|
||||||
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
@@ -56,11 +59,13 @@ type sshDeviceInfoFlags struct {
|
|||||||
|
|
||||||
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
|
||||||
// handshakes use the new key without a restart
|
// handshakes use the new key without a restart
|
||||||
if ssh.Running() && !slices.ContainsFunc(sshdServerKeys, c.HasChanged) {
|
if ssh.Running() && !c.HasChanged("sshd") {
|
||||||
key, err := loadSSHHostKey(c)
|
key, err := loadSSHHostKey(c)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = ssh.SetHostKey(key)
|
err = ssh.SetHostKey(key)
|
||||||
@@ -72,7 +77,7 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sshRun, err := configSSH(l, ssh, c, r)
|
sshRun, err := configSSH(l, ssh, c)
|
||||||
if 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()
|
||||||
@@ -87,9 +92,6 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
// sshdServerKeys are what configSSH reads
|
|
||||||
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.
|
||||||
func loadSSHHostKey(c *config.C) ([]byte, error) {
|
func loadSSHHostKey(c *config.C) ([]byte, error) {
|
||||||
hostKeyPathOrKey := c.GetString("sshd.host_key", "")
|
hostKeyPathOrKey := c.GetString("sshd.host_key", "")
|
||||||
@@ -110,8 +112,7 @@ func loadSSHHostKey(c *config.C) ([]byte, error) {
|
|||||||
// updates the passed-in SSHServer. On success, it returns a function
|
// updates the passed-in SSHServer. On success, it returns a function
|
||||||
// that callers may invoke to run the configured ssh server. On
|
// that callers may invoke to run the configured ssh server. On
|
||||||
// failure, it returns nil, error.
|
// failure, it returns nil, error.
|
||||||
// r is nil at startup, entries that are skipped with a warning fail it on a reload
|
func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error) {
|
||||||
func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reload) (func(), error) {
|
|
||||||
listen := c.GetString("sshd.listen", "")
|
listen := c.GetString("sshd.listen", "")
|
||||||
if listen == "" {
|
if listen == "" {
|
||||||
return nil, fmt.Errorf("sshd.listen must be provided")
|
return nil, fmt.Errorf("sshd.listen must be provided")
|
||||||
@@ -144,7 +145,6 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
err := ssh.AddTrustedCA(caAuthorizedKey)
|
err := ssh.AddTrustedCA(caAuthorizedKey)
|
||||||
if err != nil {
|
if err != nil {
|
||||||
l.Warn("SSH CA had an error, ignoring", "error", err, "sshCA", caAuthorizedKey)
|
l.Warn("SSH CA had an error, ignoring", "error", err, "sshCA", caAuthorizedKey)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -156,14 +156,12 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
kDef, ok := rk.(map[string]any)
|
kDef, ok := rk.(map[string]any)
|
||||||
if !ok {
|
if !ok {
|
||||||
l.Warn("Authorized user had an error, ignoring", "sshKeyConfig", rk)
|
l.Warn("Authorized user had an error, ignoring", "sshKeyConfig", rk)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
user, ok := kDef["user"].(string)
|
user, ok := kDef["user"].(string)
|
||||||
if !ok {
|
if !ok {
|
||||||
l.Warn("Authorized user is missing the user field", "sshKeyConfig", rk)
|
l.Warn("Authorized user is missing the user field", "sshKeyConfig", rk)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -177,7 +175,6 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
"sshKeyConfig", rk,
|
"sshKeyConfig", rk,
|
||||||
"sshKey", v,
|
"sshKey", v,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -189,7 +186,6 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
"sshKeyConfig", rk,
|
"sshKeyConfig", rk,
|
||||||
"sshKey", subK,
|
"sshKey", subK,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -199,14 +195,12 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
"error", err,
|
"error", err,
|
||||||
"sshKeyConfig", sk,
|
"sshKeyConfig", sk,
|
||||||
)
|
)
|
||||||
r.Failed()
|
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
default:
|
default:
|
||||||
l.Warn("Authorized user is missing the keys field or was not understood", "sshKeyConfig", rk)
|
l.Warn("Authorized user is missing the keys field or was not understood", "sshKeyConfig", rk)
|
||||||
r.Failed()
|
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
} else {
|
} else {
|
||||||
@@ -229,6 +223,12 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C, r *config.Reloa
|
|||||||
}
|
}
|
||||||
|
|
||||||
func attachCommands(l *slog.Logger, c *config.C, ssh *sshd.SSHServer, f *Interface) {
|
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{
|
ssh.RegisterCommand(&sshd.Command{
|
||||||
Name: "list-hostmap",
|
Name: "list-hostmap",
|
||||||
ShortDescription: "List all known previously connected hosts",
|
ShortDescription: "List all known previously connected hosts",
|
||||||
@@ -284,6 +284,45 @@ func attachCommands(l *slog.Logger, c *config.C, ssh *sshd.SSHServer, f *Interfa
|
|||||||
},
|
},
|
||||||
})
|
})
|
||||||
|
|
||||||
|
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{
|
ssh.RegisterCommand(&sshd.Command{
|
||||||
Name: "log-level",
|
Name: "log-level",
|
||||||
ShortDescription: "Gets or sets the current log level",
|
ShortDescription: "Gets or sets the current log level",
|
||||||
@@ -514,6 +553,58 @@ func sshListLighthouseMap(lightHouse *LightHouse, a any, w sshd.StringWriter) er
|
|||||||
return nil
|
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 {
|
func sshVersion(ifce *Interface, fs any, a []string, w sshd.StringWriter) error {
|
||||||
return w.WriteLine(fmt.Sprintf("%s", ifce.version))
|
return w.WriteLine(fmt.Sprintf("%s", ifce.version))
|
||||||
}
|
}
|
||||||
@@ -666,6 +757,76 @@ func sshChangeRemote(ifce *Interface, fs any, a []string, w sshd.StringWriter) e
|
|||||||
return w.WriteLine("Changed")
|
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 {
|
func sshLogLevel(l *slog.Logger, fs any, a []string, w sshd.StringWriter) error {
|
||||||
ctrl, ok := l.Handler().(interface {
|
ctrl, ok := l.Handler().(interface {
|
||||||
GetLevel() slog.Level
|
GetLevel() slog.Level
|
||||||
|
|||||||
+5
-49
@@ -26,15 +26,13 @@ type sshdReloadHarness struct {
|
|||||||
server *sshd.SSHServer
|
server *sshd.SSHServer
|
||||||
keyFile string
|
keyFile string
|
||||||
signer ssh.Signer
|
signer ssh.Signer
|
||||||
listen string
|
|
||||||
users []string
|
|
||||||
extra string
|
extra string
|
||||||
}
|
}
|
||||||
|
|
||||||
func newSSHDReloadHarness(t *testing.T) *sshdReloadHarness {
|
func newSSHDReloadHarness(t *testing.T) *sshdReloadHarness {
|
||||||
t.Helper()
|
t.Helper()
|
||||||
l := test.NewLogger()
|
l := test.NewLogger()
|
||||||
h := &sshdReloadHarness{keyFile: filepath.Join(t.TempDir(), "host_key"), listen: "127.0.0.1:0", users: []string{"test"}}
|
h := &sshdReloadHarness{keyFile: filepath.Join(t.TempDir(), "host_key")}
|
||||||
h.writeHostKey(t, newEd25519Key(t))
|
h.writeHostKey(t, newEd25519Key(t))
|
||||||
|
|
||||||
_, userKey, err := ed25519.GenerateKey(rand.Reader)
|
_, userKey, err := ed25519.GenerateKey(rand.Reader)
|
||||||
@@ -47,7 +45,7 @@ func newSSHDReloadHarness(t *testing.T) *sshdReloadHarness {
|
|||||||
h.server, err = sshd.NewSSHServer(t.Context(), l)
|
h.server, err = sshd.NewSSHServer(t.Context(), l)
|
||||||
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)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
go run()
|
go run()
|
||||||
t.Cleanup(h.server.Stop)
|
t.Cleanup(h.server.Stop)
|
||||||
@@ -73,12 +71,8 @@ func (h *sshdReloadHarness) writeHostKey(t *testing.T, key crypto.Signer) ssh.Pu
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *sshdReloadHarness) config() string {
|
func (h *sshdReloadHarness) config() string {
|
||||||
users := ""
|
return fmt.Sprintf("sshd:\n enabled: true\n listen: 127.0.0.1:0\n host_key: %s\n authorized_users:\n - user: test\n keys: [%q]\n%s",
|
||||||
for _, u := range h.users {
|
h.keyFile, string(ssh.MarshalAuthorizedKey(h.signer.PublicKey())), h.extra)
|
||||||
users += fmt.Sprintf(" - user: %s\n keys: [%q]\n", u, string(ssh.MarshalAuthorizedKey(h.signer.PublicKey())))
|
|
||||||
}
|
|
||||||
return fmt.Sprintf("sshd:\n enabled: true\n listen: %s\n host_key: %s\n authorized_users:\n%s%s",
|
|
||||||
h.listen, h.keyFile, users, h.extra)
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// dial connects to wherever the server listens now, a restart on :0 moves it. hostKeyAlgorithms limits which host keys
|
// dial connects to wherever the server listens now, a restart on :0 moves it. hostKeyAlgorithms limits which host keys
|
||||||
@@ -140,29 +134,15 @@ func TestSSHDReload_UnchangedKeepsSessions(t *testing.T) {
|
|||||||
assert.False(t, dropped(client, 500*time.Millisecond), "an unchanged sshd was restarted")
|
assert.False(t, dropped(client, 500*time.Millisecond), "an unchanged sshd was restarted")
|
||||||
}
|
}
|
||||||
|
|
||||||
// Every key the server reads restarts it when it changes
|
|
||||||
func TestSSHDReload_ChangedConfigRestarts(t *testing.T) {
|
func TestSSHDReload_ChangedConfigRestarts(t *testing.T) {
|
||||||
cases := map[string]func(t *testing.T, h *sshdReloadHarness){
|
|
||||||
"listen": func(t *testing.T, h *sshdReloadHarness) { h.listen = "localhost:0" },
|
|
||||||
"authorized_users": func(t *testing.T, h *sshdReloadHarness) { h.users = append(h.users, "other") },
|
|
||||||
"trusted_cas": func(t *testing.T, h *sshdReloadHarness) { h.extra = " trusted_cas: []\n" },
|
|
||||||
"host_key": func(t *testing.T, h *sshdReloadHarness) {
|
|
||||||
h.keyFile = filepath.Join(t.TempDir(), "other_host_key")
|
|
||||||
h.writeHostKey(t, newEd25519Key(t))
|
|
||||||
},
|
|
||||||
}
|
|
||||||
for name, change := range cases {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
h := newSSHDReloadHarness(t)
|
h := newSSHDReloadHarness(t)
|
||||||
client, _ := h.connect(t)
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
change(t, h)
|
h.extra = " trusted_cas: []\n"
|
||||||
require.NoError(t, h.c.ReloadConfigString(h.config()).Err)
|
require.NoError(t, h.c.ReloadConfigString(h.config()).Err)
|
||||||
assert.True(t, dropped(client, 5*time.Second), "a changed sshd config was not applied")
|
assert.True(t, dropped(client, 5*time.Second), "a changed sshd config was not applied")
|
||||||
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
||||||
h.connect(t)
|
h.connect(t)
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
}
|
||||||
|
|
||||||
// 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
|
||||||
@@ -214,27 +194,3 @@ func TestSSHDReload_BadHostKeyFileStops(t *testing.T) {
|
|||||||
assert.True(t, h.c.ReloadConfigString(h.config()).Failed)
|
assert.True(t, h.c.ReloadConfigString(h.config()).Failed)
|
||||||
require.Eventually(t, func() bool { return !h.server.Running() }, 5*time.Second, 10*time.Millisecond)
|
require.Eventually(t, func() bool { return !h.server.Running() }, 5*time.Second, 10*time.Millisecond)
|
||||||
}
|
}
|
||||||
|
|
||||||
// A trusted CA or authorized user that is skipped with a warning fails the reload, the valid ones are still used
|
|
||||||
func TestSSHDReload_SkippedEntriesFail(t *testing.T) {
|
|
||||||
cases := map[string]string{
|
|
||||||
"bad trusted ca": " trusted_cas: [nope]\n",
|
|
||||||
"user entry not a map": " - nope\n",
|
|
||||||
"user missing": " - keys: [nope]\n",
|
|
||||||
"bad key": " - user: other\n keys: nope\n",
|
|
||||||
"key in a list not a string": " - user: other\n keys: [5]\n",
|
|
||||||
"bad key in a list": " - user: other\n keys: [nope]\n",
|
|
||||||
"keys missing": " - user: other\n",
|
|
||||||
}
|
|
||||||
for name, extra := range cases {
|
|
||||||
t.Run(name, func(t *testing.T) {
|
|
||||||
h := newSSHDReloadHarness(t)
|
|
||||||
h.extra = extra
|
|
||||||
res := h.c.ReloadConfigString(h.config())
|
|
||||||
require.NoError(t, res.Err)
|
|
||||||
assert.True(t, res.Failed)
|
|
||||||
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
|
||||||
h.connect(t)
|
|
||||||
})
|
|
||||||
}
|
|
||||||
}
|
|
||||||
+2
-5
@@ -147,10 +147,7 @@ func (tw *TimerWheel[T]) Purge() (T, bool) {
|
|||||||
tw.expired.Tail = nil
|
tw.expired.Tail = nil
|
||||||
}
|
}
|
||||||
|
|
||||||
// Clear out the items references, a cached item must not keep its value alive
|
// Clear out the items references
|
||||||
v := ti.Item
|
|
||||||
var zero T
|
|
||||||
ti.Item = zero
|
|
||||||
ti.Next = nil
|
ti.Next = nil
|
||||||
|
|
||||||
// Maybe cache it for later
|
// Maybe cache it for later
|
||||||
@@ -160,7 +157,7 @@ func (tw *TimerWheel[T]) Purge() (T, bool) {
|
|||||||
tw.itemsCached++
|
tw.itemsCached++
|
||||||
}
|
}
|
||||||
|
|
||||||
return v, true
|
return ti.Item, true
|
||||||
}
|
}
|
||||||
|
|
||||||
// findWheel find the next position in the wheel for the provided timeout given the current tick
|
// findWheel find the next position in the wheel for the provided timeout given the current tick
|
||||||
|
|||||||
+1
-3
@@ -7,7 +7,6 @@ import (
|
|||||||
|
|
||||||
"github.com/slackhq/nebula/firewall"
|
"github.com/slackhq/nebula/firewall"
|
||||||
"github.com/stretchr/testify/assert"
|
"github.com/stretchr/testify/assert"
|
||||||
"github.com/stretchr/testify/require"
|
|
||||||
)
|
)
|
||||||
|
|
||||||
func TestNewTimerWheel(t *testing.T) {
|
func TestNewTimerWheel(t *testing.T) {
|
||||||
@@ -151,8 +150,7 @@ func TestTimerWheel_Purge(t *testing.T) {
|
|||||||
assert.Equal(t, 4, tw.itemsCached)
|
assert.Equal(t, 4, tw.itemsCached)
|
||||||
ci := tw.itemCache
|
ci := tw.itemCache
|
||||||
for range 4 {
|
for range 4 {
|
||||||
require.NotNil(t, ci)
|
assert.NotNil(t, ci)
|
||||||
assert.Zero(t, ci.Item)
|
|
||||||
ci = ci.Next
|
ci = ci.Next
|
||||||
}
|
}
|
||||||
assert.Nil(t, ci)
|
assert.Nil(t, ci)
|
||||||
|
|||||||
Reference in new issue
Block a user