mirror of
https://github.com/slackhq/nebula.git
synced 2026-11-16 21:26:52 +01:00
Compare commits
7
Commits
| Author | SHA1 | Date | |
|---|---|---|---|
|
|
7aa6f6c7da | ||
|
|
56a1a94a54 | ||
|
|
a0045aef09 | ||
|
|
bbc49669b7 | ||
|
|
915495d5f4 | ||
|
|
a203d044bb | ||
|
|
316bf87780 |
No files matched your search
+1
-1
@@ -261,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 NetBSD: Required to be set, must be in the form `tun[0-9]+`
|
# For OpenBSD and NetBSD: if set, must be in the form `tun[0-9]+`, otherwise the first free one is used.
|
||||||
# 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
|
||||||
|
|||||||
+101
-34
@@ -32,16 +32,21 @@ type conn struct {
|
|||||||
Expires time.Time // Time when this conntrack entry will expire
|
Expires time.Time // Time when this conntrack entry will expire
|
||||||
|
|
||||||
// record why the original connection passed the firewall, so we can re-validate
|
// record why the original connection passed the firewall, so we can re-validate
|
||||||
// after ruleset changes. Note, rulesVersion is a uint16 so that these two
|
// after ruleset changes or when the peer handshakes again, which may present a
|
||||||
// fields pack for free after the uint32 above
|
// cert with different groups. These pack into the word after Expires.
|
||||||
incoming bool
|
incoming bool
|
||||||
rulesVersion uint16
|
rulesVersion uint16
|
||||||
|
localIndex uint32
|
||||||
}
|
}
|
||||||
|
|
||||||
// TODO: need conntrack max tracked connections handling
|
// TODO: need conntrack max tracked connections handling
|
||||||
type Firewall struct {
|
type Firewall struct {
|
||||||
Conntrack *FirewallConntrack
|
Conntrack *FirewallConntrack
|
||||||
|
|
||||||
|
// siblings finds the other gateways we route an unsafe network through, so a reply that comes back through a
|
||||||
|
// different gateway than the flow went out through can be matched to it. Nil turns that off.
|
||||||
|
siblings gatewaySiblings
|
||||||
|
|
||||||
InRules *FirewallTable
|
InRules *FirewallTable
|
||||||
OutRules *FirewallTable
|
OutRules *FirewallTable
|
||||||
|
|
||||||
@@ -82,8 +87,8 @@ type firewallMetrics struct {
|
|||||||
type FirewallConntrack struct {
|
type FirewallConntrack struct {
|
||||||
sync.Mutex
|
sync.Mutex
|
||||||
|
|
||||||
Conns map[firewall.Packet]*conn
|
Conns map[firewall.ConntrackKey]*conn
|
||||||
TimerWheel *TimerWheel[firewall.Packet]
|
TimerWheel *TimerWheel[firewall.ConntrackKey]
|
||||||
}
|
}
|
||||||
|
|
||||||
// FirewallTable is the entry point for a rule, the evaluation order is:
|
// FirewallTable is the entry point for a rule, the evaluation order is:
|
||||||
@@ -167,8 +172,8 @@ func NewFirewall(l *slog.Logger, tcpTimeout, UDPTimeout, defaultTimeout time.Dur
|
|||||||
|
|
||||||
return &Firewall{
|
return &Firewall{
|
||||||
Conntrack: &FirewallConntrack{
|
Conntrack: &FirewallConntrack{
|
||||||
Conns: make(map[firewall.Packet]*conn),
|
Conns: make(map[firewall.ConntrackKey]*conn),
|
||||||
TimerWheel: NewTimerWheel[firewall.Packet](tmin, tmax),
|
TimerWheel: NewTimerWheel[firewall.ConntrackKey](tmin, tmax),
|
||||||
},
|
},
|
||||||
InRules: newFirewallTable(),
|
InRules: newFirewallTable(),
|
||||||
OutRules: newFirewallTable(),
|
OutRules: newFirewallTable(),
|
||||||
@@ -425,6 +430,7 @@ var ErrNoMatchingRule = errors.New("no matching rule in firewall table")
|
|||||||
// returns nil if the packet should not be dropped.
|
// returns nil if the packet should not be dropped.
|
||||||
func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) error {
|
func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) error {
|
||||||
// Make sure remote address matches nebula certificate, and determine how to treat it
|
// Make sure remote address matches nebula certificate, and determine how to treat it
|
||||||
|
unsafe := false
|
||||||
if h.networks == nil {
|
if h.networks == nil {
|
||||||
// Simple case: Certificate has one address and no unsafe networks
|
// Simple case: Certificate has one address and no unsafe networks
|
||||||
if h.vpnAddrs[0] != fp.RemoteAddr {
|
if h.vpnAddrs[0] != fp.RemoteAddr {
|
||||||
@@ -444,7 +450,7 @@ func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *
|
|||||||
f.metrics(incoming).droppedRemoteAddr.Inc(1)
|
f.metrics(incoming).droppedRemoteAddr.Inc(1)
|
||||||
return ErrPeerRejected // reject for now, one day this may have different FW rules
|
return ErrPeerRejected // reject for now, one day this may have different FW rules
|
||||||
case NetworkTypeUnsafe:
|
case NetworkTypeUnsafe:
|
||||||
break // nothing special, one day this may have different FW rules
|
unsafe = true // nothing else special, one day this may have different FW rules
|
||||||
default:
|
default:
|
||||||
f.metrics(incoming).droppedRemoteAddr.Inc(1)
|
f.metrics(incoming).droppedRemoteAddr.Inc(1)
|
||||||
return ErrUnknownNetworkType //should never happen
|
return ErrUnknownNetworkType //should never happen
|
||||||
@@ -457,8 +463,11 @@ func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *
|
|||||||
return ErrInvalidLocalIP
|
return ErrInvalidLocalIP
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// The first vpn address identifies the peer, as it does elsewhere. A host's v1 and v2 certs must agree on it
|
||||||
|
key := firewall.ConntrackKey{Packet: fp, Peer: h.vpnAddrs[0]}
|
||||||
|
|
||||||
// Check if we spoke to this tuple, if we did then allow this packet
|
// Check if we spoke to this tuple, if we did then allow this packet
|
||||||
if f.inConns(fp, h, caPool, localCache) {
|
if f.inConns(key, h, caPool, localCache) {
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -469,12 +478,15 @@ func (f *Firewall) Drop(fp firewall.Packet, incoming bool, h *HostInfo, caPool *
|
|||||||
|
|
||||||
// We now know which firewall table to check against
|
// We now know which firewall table to check against
|
||||||
if !table.match(fp, incoming, h.ConnectionState.peerCert, caPool) {
|
if !table.match(fp, incoming, h.ConnectionState.peerCert, caPool) {
|
||||||
|
if unsafe && f.inheritConn(key, incoming, h, caPool) {
|
||||||
|
return nil
|
||||||
|
}
|
||||||
f.metrics(incoming).droppedNoRule.Inc(1)
|
f.metrics(incoming).droppedNoRule.Inc(1)
|
||||||
return ErrNoMatchingRule
|
return ErrNoMatchingRule
|
||||||
}
|
}
|
||||||
|
|
||||||
// We always want to conntrack since it is a faster operation
|
// We always want to conntrack since it is a faster operation
|
||||||
f.addConn(fp, incoming)
|
f.addConn(key, incoming, h)
|
||||||
|
|
||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
@@ -503,9 +515,9 @@ func (f *Firewall) EmitStats() {
|
|||||||
metrics.GetOrRegisterGauge("firewall.rules.hash", nil).Update(int64(f.GetRuleHashFNV()))
|
metrics.GetOrRegisterGauge("firewall.rules.hash", nil).Update(int64(f.GetRuleHashFNV()))
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Firewall) inConns(fp firewall.Packet, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) bool {
|
func (f *Firewall) inConns(key firewall.ConntrackKey, h *HostInfo, caPool *cert.CAPool, localCache firewall.ConntrackCache) bool {
|
||||||
if localCache != nil {
|
if localCache != nil {
|
||||||
if _, ok := localCache[fp]; ok {
|
if _, ok := localCache[key]; ok {
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
}
|
}
|
||||||
@@ -518,49 +530,52 @@ func (f *Firewall) inConns(fp firewall.Packet, h *HostInfo, caPool *cert.CAPool,
|
|||||||
f.evict(ep)
|
f.evict(ep)
|
||||||
}
|
}
|
||||||
|
|
||||||
c, ok := conntrack.Conns[fp]
|
c, ok := conntrack.Conns[key]
|
||||||
|
|
||||||
if !ok {
|
if !ok {
|
||||||
conntrack.Unlock()
|
conntrack.Unlock()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if c.rulesVersion != f.rulesVersion {
|
if c.rulesVersion != f.rulesVersion || c.localIndex != h.localIndexId {
|
||||||
// This conntrack entry was for an older rule set, validate
|
// This conntrack entry was allowed by an older rule set or for an earlier tunnel to the peer, validate
|
||||||
// it still passes with the current rule set
|
// it still passes with the current rule set and the peer's current certificate
|
||||||
table := f.OutRules
|
table := f.OutRules
|
||||||
if c.incoming {
|
if c.incoming {
|
||||||
table = f.InRules
|
table = f.InRules
|
||||||
}
|
}
|
||||||
|
|
||||||
// We now know which firewall table to check against
|
// We now know which firewall table to check against
|
||||||
if !table.match(fp, c.incoming, h.ConnectionState.peerCert, caPool) {
|
if !table.match(key.Packet, c.incoming, h.ConnectionState.peerCert, caPool) {
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
h.logger(f.l).Debug("dropping old conntrack entry, does not match new ruleset",
|
h.logger(f.l).Debug("dropping old conntrack entry, does not match current ruleset and certificate",
|
||||||
"fwPacket", fp,
|
"fwPacket", key.Packet,
|
||||||
"incoming", c.incoming,
|
"incoming", c.incoming,
|
||||||
"rulesVersion", f.rulesVersion,
|
"rulesVersion", f.rulesVersion,
|
||||||
"oldRulesVersion", c.rulesVersion,
|
"oldRulesVersion", c.rulesVersion,
|
||||||
|
"oldLocalIndex", c.localIndex,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
delete(conntrack.Conns, fp)
|
delete(conntrack.Conns, key)
|
||||||
conntrack.Unlock()
|
conntrack.Unlock()
|
||||||
return false
|
return false
|
||||||
}
|
}
|
||||||
|
|
||||||
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
if f.l.Enabled(context.Background(), slog.LevelDebug) {
|
||||||
h.logger(f.l).Debug("keeping old conntrack entry, does match new ruleset",
|
h.logger(f.l).Debug("keeping old conntrack entry, does match current ruleset and certificate",
|
||||||
"fwPacket", fp,
|
"fwPacket", key.Packet,
|
||||||
"incoming", c.incoming,
|
"incoming", c.incoming,
|
||||||
"rulesVersion", f.rulesVersion,
|
"rulesVersion", f.rulesVersion,
|
||||||
"oldRulesVersion", c.rulesVersion,
|
"oldRulesVersion", c.rulesVersion,
|
||||||
|
"oldLocalIndex", c.localIndex,
|
||||||
)
|
)
|
||||||
}
|
}
|
||||||
|
|
||||||
c.rulesVersion = f.rulesVersion
|
c.rulesVersion = f.rulesVersion
|
||||||
|
c.localIndex = h.localIndexId
|
||||||
}
|
}
|
||||||
|
|
||||||
switch fp.Protocol {
|
switch key.Protocol {
|
||||||
case iputil.IPProtocolTCP:
|
case iputil.IPProtocolTCP:
|
||||||
c.Expires = time.Now().Add(f.TCPTimeout)
|
c.Expires = time.Now().Add(f.TCPTimeout)
|
||||||
case iputil.IPProtocolUDP:
|
case iputil.IPProtocolUDP:
|
||||||
@@ -572,17 +587,17 @@ func (f *Firewall) inConns(fp firewall.Packet, h *HostInfo, caPool *cert.CAPool,
|
|||||||
conntrack.Unlock()
|
conntrack.Unlock()
|
||||||
|
|
||||||
if localCache != nil {
|
if localCache != nil {
|
||||||
localCache[fp] = struct{}{}
|
localCache[key] = struct{}{}
|
||||||
}
|
}
|
||||||
|
|
||||||
return true
|
return true
|
||||||
}
|
}
|
||||||
|
|
||||||
func (f *Firewall) addConn(fp firewall.Packet, incoming bool) {
|
func (f *Firewall) addConn(key firewall.ConntrackKey, incoming bool, h *HostInfo) {
|
||||||
var timeout time.Duration
|
var timeout time.Duration
|
||||||
c := &conn{}
|
c := &conn{}
|
||||||
|
|
||||||
switch fp.Protocol {
|
switch key.Protocol {
|
||||||
case iputil.IPProtocolTCP:
|
case iputil.IPProtocolTCP:
|
||||||
timeout = f.TCPTimeout
|
timeout = f.TCPTimeout
|
||||||
case iputil.IPProtocolUDP:
|
case iputil.IPProtocolUDP:
|
||||||
@@ -593,26 +608,78 @@ func (f *Firewall) addConn(fp firewall.Packet, incoming bool) {
|
|||||||
|
|
||||||
conntrack := f.Conntrack
|
conntrack := f.Conntrack
|
||||||
conntrack.Lock()
|
conntrack.Lock()
|
||||||
if _, ok := conntrack.Conns[fp]; !ok {
|
if _, ok := conntrack.Conns[key]; !ok {
|
||||||
conntrack.TimerWheel.Advance(time.Now())
|
conntrack.TimerWheel.Advance(time.Now())
|
||||||
conntrack.TimerWheel.Add(fp, timeout)
|
conntrack.TimerWheel.Add(key, timeout)
|
||||||
}
|
}
|
||||||
|
|
||||||
// Record which rulesVersion allowed this connection, so we can retest after
|
// Record which rulesVersion and tunnel allowed this connection, so we can retest after
|
||||||
// firewall reload
|
// firewall reload or when the peer handshakes again
|
||||||
c.incoming = incoming
|
c.incoming = incoming
|
||||||
c.rulesVersion = f.rulesVersion
|
c.rulesVersion = f.rulesVersion
|
||||||
|
c.localIndex = h.localIndexId
|
||||||
c.Expires = time.Now().Add(timeout)
|
c.Expires = time.Now().Add(timeout)
|
||||||
conntrack.Conns[fp] = c
|
conntrack.Conns[key] = c
|
||||||
conntrack.Unlock()
|
conntrack.Unlock()
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// gatewaySiblings finds the other gateways we route an unsafe network through
|
||||||
|
type gatewaySiblings interface {
|
||||||
|
// siblingPeers appends to buf the conntrack peer of every other gateway we route remote through, if h is one of
|
||||||
|
// those gateways
|
||||||
|
siblingPeers(remote netip.Addr, h *HostInfo, buf []netip.Addr) []netip.Addr
|
||||||
|
}
|
||||||
|
|
||||||
|
// inheritConn lets h carry a flow that another of our gateways for the same unsafe network already carries. We
|
||||||
|
// spread the flows we open across the gateways, but the hosts behind them answer through whichever gateway their own
|
||||||
|
// routing picks, so a reply can come back through a different gateway than the flow went out through. The flow is
|
||||||
|
// checked against the rules that allowed it in the first place with h's certificate, the same check an entry gets
|
||||||
|
// when its peer reconnects. Peers that aren't one of our gateways for the network never inherit a flow.
|
||||||
|
func (f *Firewall) inheritConn(key firewall.ConntrackKey, incoming bool, h *HostInfo, caPool *cert.CAPool) bool {
|
||||||
|
if f.siblings == nil {
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
|
var buf [4]netip.Addr
|
||||||
|
now := time.Now()
|
||||||
|
for _, peer := range f.siblings.siblingPeers(key.RemoteAddr, h, buf[:0]) {
|
||||||
|
sk := key
|
||||||
|
sk.Peer = peer
|
||||||
|
conntrack := f.Conntrack
|
||||||
|
conntrack.Lock()
|
||||||
|
c, ok := conntrack.Conns[sk]
|
||||||
|
live := ok && c.Expires.After(now)
|
||||||
|
var allowedIncoming bool
|
||||||
|
if live {
|
||||||
|
allowedIncoming = c.incoming
|
||||||
|
}
|
||||||
|
conntrack.Unlock()
|
||||||
|
|
||||||
|
// The rules for this packet's own direction already said no, only a flow allowed the other way can help
|
||||||
|
if !live || allowedIncoming == incoming {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
table := f.OutRules
|
||||||
|
if allowedIncoming {
|
||||||
|
table = f.InRules
|
||||||
|
}
|
||||||
|
if !table.match(key.Packet, allowedIncoming, h.ConnectionState.peerCert, caPool) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
|
||||||
|
f.addConn(key, allowedIncoming, h)
|
||||||
|
return true
|
||||||
|
}
|
||||||
|
return false
|
||||||
|
}
|
||||||
|
|
||||||
// Evict checks if a conntrack entry has expired, if so it is removed, if not it is re-added to the wheel
|
// Evict checks if a conntrack entry has expired, if so it is removed, if not it is re-added to the wheel
|
||||||
// Caller must own the connMutex lock!
|
// Caller must own the connMutex lock!
|
||||||
func (f *Firewall) evict(p firewall.Packet) {
|
func (f *Firewall) evict(key firewall.ConntrackKey) {
|
||||||
// Are we still tracking this conn?
|
// Are we still tracking this conn?
|
||||||
conntrack := f.Conntrack
|
conntrack := f.Conntrack
|
||||||
t, ok := conntrack.Conns[p]
|
t, ok := conntrack.Conns[key]
|
||||||
if !ok {
|
if !ok {
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
@@ -622,12 +689,12 @@ func (f *Firewall) evict(p firewall.Packet) {
|
|||||||
// Timeout is in the future, re-add the timer
|
// Timeout is in the future, re-add the timer
|
||||||
if newT > 0 {
|
if newT > 0 {
|
||||||
conntrack.TimerWheel.Advance(time.Now())
|
conntrack.TimerWheel.Advance(time.Now())
|
||||||
conntrack.TimerWheel.Add(p, newT)
|
conntrack.TimerWheel.Add(key, newT)
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
|
|
||||||
// This conn is done
|
// This conn is done
|
||||||
delete(conntrack.Conns, p)
|
delete(conntrack.Conns, key)
|
||||||
}
|
}
|
||||||
|
|
||||||
func (ft *FirewallTable) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
|
func (ft *FirewallTable) match(p firewall.Packet, incoming bool, c *cert.CachedCertificate, caPool *cert.CAPool) bool {
|
||||||
|
|||||||
+9
-1
@@ -3,15 +3,23 @@ package firewall
|
|||||||
import (
|
import (
|
||||||
"context"
|
"context"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
|
"net/netip"
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"time"
|
"time"
|
||||||
|
|
||||||
"github.com/slackhq/nebula/logging"
|
"github.com/slackhq/nebula/logging"
|
||||||
)
|
)
|
||||||
|
|
||||||
|
// ConntrackKey identifies a tracked flow by its packet and the peer it was allowed for, so peers that route
|
||||||
|
// the same unsafe network never share a conntrack entry.
|
||||||
|
type ConntrackKey struct {
|
||||||
|
Peer netip.Addr
|
||||||
|
Packet
|
||||||
|
}
|
||||||
|
|
||||||
// ConntrackCache is used as a local routine cache to know if a given flow
|
// ConntrackCache is used as a local routine cache to know if a given flow
|
||||||
// has been seen in the conntrack table.
|
// has been seen in the conntrack table.
|
||||||
type ConntrackCache map[Packet]struct{}
|
type ConntrackCache map[ConntrackKey]struct{}
|
||||||
|
|
||||||
type ConntrackCacheTicker struct {
|
type ConntrackCacheTicker struct {
|
||||||
cacheV uint64
|
cacheV uint64
|
||||||
|
|||||||
@@ -23,7 +23,7 @@ func newFixedTicker(t *testing.T, l *slog.Logger, cacheLen int) *ConntrackCacheT
|
|||||||
cache: make(ConntrackCache, cacheLen),
|
cache: make(ConntrackCache, cacheLen),
|
||||||
}
|
}
|
||||||
for i := range cacheLen {
|
for i := range cacheLen {
|
||||||
c.cache[Packet{LocalPort: uint16(i) + 1}] = struct{}{}
|
c.cache[ConntrackKey{Packet: Packet{LocalPort: uint16(i) + 1}}] = struct{}{}
|
||||||
}
|
}
|
||||||
c.cacheTick.Store(1) // cacheV starts at 0, so Get() takes the reset path
|
c.cacheTick.Store(1) // cacheV starts at 0, so Get() takes the reset path
|
||||||
return c
|
return c
|
||||||
|
|||||||
@@ -26,6 +26,10 @@ type Packet struct {
|
|||||||
RemotePort uint16
|
RemotePort uint16
|
||||||
Protocol uint8
|
Protocol uint8
|
||||||
Fragment bool
|
Fragment bool
|
||||||
|
|
||||||
|
// pad fills what would otherwise be trailing padding so maps hash and compare a ConntrackKey as one
|
||||||
|
// block of memory instead of field by field. It must always be zero.
|
||||||
|
pad [2]byte
|
||||||
}
|
}
|
||||||
|
|
||||||
func (fp *Packet) Copy() *Packet {
|
func (fp *Packet) Copy() *Packet {
|
||||||
|
|||||||
+248
-1
@@ -3,6 +3,7 @@ package nebula
|
|||||||
import (
|
import (
|
||||||
"bytes"
|
"bytes"
|
||||||
"errors"
|
"errors"
|
||||||
|
"fmt"
|
||||||
"log/slog"
|
"log/slog"
|
||||||
"math"
|
"math"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
@@ -979,6 +980,252 @@ func TestFirewall_ConntrackSourceSpoofingAcrossPeers(t *testing.T) {
|
|||||||
"SECURITY: attacker spoofed victim's overlay source IP (192.0.2.2) by reusing an existing conntrack 4-tuple; Drop returned %v instead of rejecting", got)
|
"SECURITY: attacker spoofed victim's overlay source IP (192.0.2.2) by reusing an existing conntrack 4-tuple; Drop returned %v instead of rejecting", got)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
func TestFirewall_ConntrackUnsafeNetworkPeers(t *testing.T) {
|
||||||
|
l := test.NewLoggerWithOutput(&bytes.Buffer{})
|
||||||
|
|
||||||
|
myVpnNetworksTable := new(bart.Lite)
|
||||||
|
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
|
||||||
|
|
||||||
|
owner := &dummyCert{
|
||||||
|
name: "owner",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Two routers for the same unsafe network, only one of them is trusted inbound
|
||||||
|
router := func(name string, addr string, groups ...string) *HostInfo {
|
||||||
|
inverted := map[string]struct{}{}
|
||||||
|
for _, g := range groups {
|
||||||
|
inverted[g] = struct{}{}
|
||||||
|
}
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: name,
|
||||||
|
groups: groups,
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix(addr + "/24")},
|
||||||
|
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
|
||||||
|
},
|
||||||
|
InvertedGroups: inverted,
|
||||||
|
}
|
||||||
|
h := &HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{peerCert: c},
|
||||||
|
vpnAddrs: []netip.Addr{netip.MustParseAddr(addr)},
|
||||||
|
}
|
||||||
|
h.buildNetworks(myVpnNetworksTable, c.Certificate)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
trusted := router("trusted", "192.0.2.2", "trusted")
|
||||||
|
other := router("other", "192.0.2.3")
|
||||||
|
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
flow := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("192.0.2.1"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("198.51.100.5"),
|
||||||
|
LocalPort: 443,
|
||||||
|
RemotePort: 55000,
|
||||||
|
Protocol: iputil.IPProtocolTCP,
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, useCache := range []bool{false, true} {
|
||||||
|
var cache firewall.ConntrackCache
|
||||||
|
if useCache {
|
||||||
|
cache = firewall.ConntrackCache{}
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run(fmt.Sprintf("inbound flow allowed for one peer/cache=%v", useCache), func(t *testing.T) {
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"trusted"}, "", "", "", "", ""))
|
||||||
|
clear(cache)
|
||||||
|
|
||||||
|
require.NoError(t, fw.Drop(flow, true, trusted, cp, cache))
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, other, cp, cache))
|
||||||
|
require.NoError(t, fw.Drop(flow, true, trusted, cp, cache))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run(fmt.Sprintf("reply to a flow opened via one peer/cache=%v", useCache), func(t *testing.T) {
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
|
||||||
|
clear(cache)
|
||||||
|
|
||||||
|
require.NoError(t, fw.Drop(flow, false, trusted, cp, cache))
|
||||||
|
require.NoError(t, fw.Drop(flow, true, trusted, cp, cache))
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, other, cp, cache))
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
|
|
||||||
|
// fakeSiblings maps each gateway to the conntrack peers of the gateways it shares a route with
|
||||||
|
type fakeSiblings map[*HostInfo][]netip.Addr
|
||||||
|
|
||||||
|
func (s fakeSiblings) siblingPeers(_ netip.Addr, h *HostInfo, buf []netip.Addr) []netip.Addr {
|
||||||
|
return append(buf, s[h]...)
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFirewall_ConntrackGatewaySiblings(t *testing.T) {
|
||||||
|
l := test.NewLoggerWithOutput(&bytes.Buffer{})
|
||||||
|
|
||||||
|
myVpnNetworksTable := new(bart.Lite)
|
||||||
|
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
|
||||||
|
|
||||||
|
owner := &dummyCert{
|
||||||
|
name: "owner",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Routers for the same unsafe network. a and b are both our gateways for it, c only claims it in its cert
|
||||||
|
router := func(name string, addr string, groups ...string) *HostInfo {
|
||||||
|
inverted := map[string]struct{}{}
|
||||||
|
for _, g := range groups {
|
||||||
|
inverted[g] = struct{}{}
|
||||||
|
}
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: name,
|
||||||
|
groups: groups,
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix(addr + "/24")},
|
||||||
|
unsafeNetworks: []netip.Prefix{netip.MustParsePrefix("198.51.100.0/24")},
|
||||||
|
},
|
||||||
|
InvertedGroups: inverted,
|
||||||
|
}
|
||||||
|
h := &HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{peerCert: c},
|
||||||
|
vpnAddrs: []netip.Addr{netip.MustParseAddr(addr)},
|
||||||
|
}
|
||||||
|
h.buildNetworks(myVpnNetworksTable, c.Certificate)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
a := router("a", "192.0.2.2", "trusted")
|
||||||
|
b := router("b", "192.0.2.3", "trusted")
|
||||||
|
c := router("c", "192.0.2.4", "trusted")
|
||||||
|
siblings := fakeSiblings{a: {b.vpnAddrs[0]}, b: {a.vpnAddrs[0]}}
|
||||||
|
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
flow := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("192.0.2.1"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("198.51.100.5"),
|
||||||
|
LocalPort: 55000,
|
||||||
|
RemotePort: 443,
|
||||||
|
Protocol: iputil.IPProtocolTCP,
|
||||||
|
}
|
||||||
|
|
||||||
|
// We open flows through our gateways, nothing is allowed in on its own
|
||||||
|
newFirewall := func(outGroup string) *Firewall {
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{outGroup}, "", "", "", "", ""))
|
||||||
|
fw.siblings = siblings
|
||||||
|
return fw
|
||||||
|
}
|
||||||
|
|
||||||
|
t.Run("reply through the other gateway", func(t *testing.T) {
|
||||||
|
fw := newFirewall("trusted")
|
||||||
|
require.NoError(t, fw.Drop(flow, false, b, cp, nil))
|
||||||
|
require.NoError(t, fw.Drop(flow, true, a, cp, nil))
|
||||||
|
|
||||||
|
// a has its own entry now, and keeps the flow when b's is gone
|
||||||
|
delete(fw.Conntrack.Conns, firewall.ConntrackKey{Packet: flow, Peer: b.vpnAddrs[0]})
|
||||||
|
require.NoError(t, fw.Drop(flow, true, a, cp, nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("not our gateway", func(t *testing.T) {
|
||||||
|
fw := newFirewall("trusted")
|
||||||
|
require.NoError(t, fw.Drop(flow, false, b, cp, nil))
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, c, cp, nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("off without siblings", func(t *testing.T) {
|
||||||
|
fw := newFirewall("trusted")
|
||||||
|
fw.siblings = nil
|
||||||
|
require.NoError(t, fw.Drop(flow, false, b, cp, nil))
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, a, cp, nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("expired sibling entry", func(t *testing.T) {
|
||||||
|
fw := newFirewall("trusted")
|
||||||
|
require.NoError(t, fw.Drop(flow, false, b, cp, nil))
|
||||||
|
fw.Conntrack.Conns[firewall.ConntrackKey{Packet: flow, Peer: b.vpnAddrs[0]}].Expires = time.Now().Add(-time.Second)
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, a, cp, nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("the original rules must allow the new gateway", func(t *testing.T) {
|
||||||
|
fw := newFirewall("b-only")
|
||||||
|
b.ConnectionState.peerCert.InvertedGroups["b-only"] = struct{}{}
|
||||||
|
defer delete(b.ConnectionState.peerCert.InvertedGroups, "b-only")
|
||||||
|
|
||||||
|
require.NoError(t, fw.Drop(flow, false, b, cp, nil))
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, a, cp, nil))
|
||||||
|
})
|
||||||
|
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestFirewall_ConntrackPeerRehandshake(t *testing.T) {
|
||||||
|
l := test.NewLoggerWithOutput(&bytes.Buffer{})
|
||||||
|
|
||||||
|
myVpnNetworksTable := new(bart.Lite)
|
||||||
|
myVpnNetworksTable.Insert(netip.MustParsePrefix("192.0.2.1/24"))
|
||||||
|
|
||||||
|
owner := &dummyCert{
|
||||||
|
name: "owner",
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.1/24")},
|
||||||
|
}
|
||||||
|
|
||||||
|
// Each call is a new tunnel to the same peer, presenting a cert with the given groups
|
||||||
|
tunnel := func(localIndex uint32, groups ...string) *HostInfo {
|
||||||
|
inverted := map[string]struct{}{}
|
||||||
|
for _, g := range groups {
|
||||||
|
inverted[g] = struct{}{}
|
||||||
|
}
|
||||||
|
c := &cert.CachedCertificate{
|
||||||
|
Certificate: &dummyCert{
|
||||||
|
name: "peer",
|
||||||
|
groups: groups,
|
||||||
|
networks: []netip.Prefix{netip.MustParsePrefix("192.0.2.2/24"), netip.MustParsePrefix("2001:db8::2/64")},
|
||||||
|
},
|
||||||
|
InvertedGroups: inverted,
|
||||||
|
}
|
||||||
|
h := &HostInfo{
|
||||||
|
ConnectionState: &ConnectionState{peerCert: c},
|
||||||
|
vpnAddrs: []netip.Addr{netip.MustParseAddr("192.0.2.2"), netip.MustParseAddr("2001:db8::2")},
|
||||||
|
localIndexId: localIndex,
|
||||||
|
}
|
||||||
|
h.buildNetworks(myVpnNetworksTable, c.Certificate)
|
||||||
|
return h
|
||||||
|
}
|
||||||
|
|
||||||
|
cp := cert.NewCAPool()
|
||||||
|
flow := firewall.Packet{
|
||||||
|
LocalAddr: netip.MustParseAddr("192.0.2.1"),
|
||||||
|
RemoteAddr: netip.MustParseAddr("192.0.2.2"),
|
||||||
|
LocalPort: 443,
|
||||||
|
RemotePort: 55000,
|
||||||
|
Protocol: iputil.IPProtocolTCP,
|
||||||
|
}
|
||||||
|
key := firewall.ConntrackKey{Packet: flow, Peer: netip.MustParseAddr("192.0.2.2")}
|
||||||
|
|
||||||
|
t.Run("inbound flow follows the peer's groups", func(t *testing.T) {
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
|
||||||
|
require.NoError(t, fw.AddRule(true, firewall.ProtoAny, 0, 0, []string{"trusted"}, "", "", "", "", ""))
|
||||||
|
|
||||||
|
require.NoError(t, fw.Drop(flow, true, tunnel(1, "trusted"), cp, nil))
|
||||||
|
|
||||||
|
// Same groups on a new tunnel keeps the entry and moves it to the new tunnel
|
||||||
|
require.NoError(t, fw.Drop(flow, true, tunnel(2, "trusted"), cp, nil))
|
||||||
|
require.Contains(t, fw.Conntrack.Conns, key)
|
||||||
|
assert.Equal(t, uint32(2), fw.Conntrack.Conns[key].localIndex)
|
||||||
|
|
||||||
|
// Losing the group on a new tunnel removes the entry
|
||||||
|
assert.Equal(t, ErrNoMatchingRule, fw.Drop(flow, true, tunnel(3), cp, nil))
|
||||||
|
assert.NotContains(t, fw.Conntrack.Conns, key)
|
||||||
|
})
|
||||||
|
|
||||||
|
t.Run("reply to a flow we opened survives a new tunnel", func(t *testing.T) {
|
||||||
|
fw := NewFirewall(l, time.Second, time.Minute, time.Hour, owner)
|
||||||
|
require.NoError(t, fw.AddRule(false, firewall.ProtoAny, 0, 0, []string{"any"}, "", "", "", "", ""))
|
||||||
|
|
||||||
|
require.NoError(t, fw.Drop(flow, false, tunnel(1, "trusted"), cp, nil))
|
||||||
|
require.NoError(t, fw.Drop(flow, true, tunnel(2), cp, nil))
|
||||||
|
assert.Equal(t, uint32(2), fw.Conntrack.Conns[key].localIndex)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
|
||||||
// BenchmarkFirewallDropConntrackHit measures Drop on an already-established flow
|
// BenchmarkFirewallDropConntrackHit measures Drop on an already-established flow
|
||||||
// (a conntrack hit). This is the fast path that the source-IP<->cert binding
|
// (a conntrack hit). This is the fast path that the source-IP<->cert binding
|
||||||
// reordering adds work to, so it quantifies the cost of moving the address checks
|
// reordering adds work to, so it quantifies the cost of moving the address checks
|
||||||
@@ -1742,6 +1989,6 @@ func (mf *mockFirewall) AddRule(incoming bool, proto uint8, startPort int32, end
|
|||||||
|
|
||||||
func resetConntrack(fw *Firewall) {
|
func resetConntrack(fw *Firewall) {
|
||||||
fw.Conntrack.Lock()
|
fw.Conntrack.Lock()
|
||||||
fw.Conntrack.Conns = map[firewall.Packet]*conn{}
|
fw.Conntrack.Conns = map[firewall.ConntrackKey]*conn{}
|
||||||
fw.Conntrack.Unlock()
|
fw.Conntrack.Unlock()
|
||||||
}
|
}
|
||||||
+7
-5
@@ -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)
|
hm.reload(c, true, nil)
|
||||||
c.RegisterReloadCallback(func(c *config.C, _ *config.Reload) {
|
c.RegisterReloadCallback(func(c *config.C, r *config.Reload) {
|
||||||
hm.reload(c, false)
|
hm.reload(c, false, r)
|
||||||
})
|
})
|
||||||
|
|
||||||
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
l.Info("Main HostMap created", "preferredRanges", hm.GetPreferredRanges())
|
||||||
@@ -337,7 +337,8 @@ func newHostMap(l *slog.Logger) *HostMap {
|
|||||||
}
|
}
|
||||||
}
|
}
|
||||||
|
|
||||||
func (hm *HostMap) reload(c *config.C, initial bool) {
|
// r is nil on the initial load
|
||||||
|
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{})
|
||||||
@@ -348,8 +349,9 @@ func (hm *HostMap) reload(c *config.C, initial bool) {
|
|||||||
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", rawPreferredRanges,
|
"range", rawPreferredRange,
|
||||||
)
|
)
|
||||||
|
r.Failed()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
|
|||||||
@@ -447,3 +447,16 @@ 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())
|
||||||
|
}
|
||||||
@@ -25,6 +25,7 @@ import (
|
|||||||
"github.com/slackhq/nebula/overlay/batch"
|
"github.com/slackhq/nebula/overlay/batch"
|
||||||
"github.com/slackhq/nebula/overlay/tio"
|
"github.com/slackhq/nebula/overlay/tio"
|
||||||
"github.com/slackhq/nebula/ratelimit"
|
"github.com/slackhq/nebula/ratelimit"
|
||||||
|
"github.com/slackhq/nebula/routing"
|
||||||
"github.com/slackhq/nebula/udp"
|
"github.com/slackhq/nebula/udp"
|
||||||
)
|
)
|
||||||
|
|
||||||
@@ -268,6 +269,9 @@ func NewInterface(ctx context.Context, c *InterfaceConfig) (*Interface, error) {
|
|||||||
ifce.reQueryWait.Store(int64(c.reQueryWait))
|
ifce.reQueryWait.Store(int64(c.reQueryWait))
|
||||||
|
|
||||||
ifce.connectionManager.intf = ifce
|
ifce.connectionManager.intf = ifce
|
||||||
|
if ifce.firewall != nil {
|
||||||
|
ifce.firewall.siblings = ifce
|
||||||
|
}
|
||||||
|
|
||||||
// Held until Close so waiting on the interface blocks until the resources are actually released
|
// Held until Close so waiting on the interface blocks until the resources are actually released
|
||||||
ifce.wg.Add(1)
|
ifce.wg.Add(1)
|
||||||
@@ -554,6 +558,7 @@ func (f *Interface) reloadFirewall(c *config.C, r *config.Reload) {
|
|||||||
}
|
}
|
||||||
|
|
||||||
oldFw := f.firewall
|
oldFw := f.firewall
|
||||||
|
fw.siblings = f
|
||||||
conntrack := oldFw.Conntrack
|
conntrack := oldFw.Conntrack
|
||||||
conntrack.Lock()
|
conntrack.Lock()
|
||||||
defer conntrack.Unlock()
|
defer conntrack.Unlock()
|
||||||
@@ -711,3 +716,24 @@ func (f *Interface) Close() error {
|
|||||||
f.wg.Done()
|
f.wg.Done()
|
||||||
return errors.Join(errs...)
|
return errors.Join(errs...)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// siblingPeers implements gatewaySiblings from the routes and tunnels we have now. Routes name a gateway by any of its
|
||||||
|
// vpn addresses, conntrack keys a peer by its first one, so each gateway is resolved through its tunnel. A gateway
|
||||||
|
// we have no tunnel to can't be carrying a live flow.
|
||||||
|
func (f *Interface) siblingPeers(remote netip.Addr, h *HostInfo, buf []netip.Addr) []netip.Addr {
|
||||||
|
gateways := f.inside.RoutesFor(remote)
|
||||||
|
isGateway := func(g routing.Gateway) bool { return slices.Contains(h.vpnAddrs, g.Addr()) }
|
||||||
|
if len(gateways) < 2 || !slices.ContainsFunc(gateways, isGateway) {
|
||||||
|
return buf
|
||||||
|
}
|
||||||
|
|
||||||
|
for _, g := range gateways {
|
||||||
|
if isGateway(g) {
|
||||||
|
continue
|
||||||
|
}
|
||||||
|
if s := f.hostMap.QueryVpnAddr(g.Addr()); s != nil && s != h {
|
||||||
|
buf = append(buf, s.vpnAddrs[0])
|
||||||
|
}
|
||||||
|
}
|
||||||
|
return buf
|
||||||
|
}
|
||||||
@@ -39,7 +39,7 @@ func Test_statsEmitter_setsGauges(t *testing.T) {
|
|||||||
hostMap: hostMap,
|
hostMap: hostMap,
|
||||||
inside: &overlaytest.NoopTun{},
|
inside: &overlaytest.NoopTun{},
|
||||||
outside: &udp.NoopConn{},
|
outside: &udp.NoopConn{},
|
||||||
firewall: &Firewall{Conntrack: &FirewallConntrack{Conns: map[firewall.Packet]*conn{}}},
|
firewall: &Firewall{Conntrack: &FirewallConntrack{Conns: map[firewall.ConntrackKey]*conn{}}},
|
||||||
lightHouse: lh,
|
lightHouse: lh,
|
||||||
pki: &PKI{},
|
pki: &PKI{},
|
||||||
handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
|
handshakeManager: NewHandshakeManager(l, hostMap, lh, &udp.NoopConn{}, defaultHandshakeConfig),
|
||||||
|
|||||||
@@ -4,8 +4,11 @@ import (
|
|||||||
"net/netip"
|
"net/netip"
|
||||||
"testing"
|
"testing"
|
||||||
|
|
||||||
|
"github.com/gaissmai/bart"
|
||||||
"github.com/slackhq/nebula/cert"
|
"github.com/slackhq/nebula/cert"
|
||||||
"github.com/slackhq/nebula/config"
|
"github.com/slackhq/nebula/config"
|
||||||
|
"github.com/slackhq/nebula/overlay/overlaytest"
|
||||||
|
"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"
|
||||||
@@ -140,3 +143,42 @@ func TestReloadFirewall_BadRulesFailTheReload(t *testing.T) {
|
|||||||
assert.True(t, res.Failed)
|
assert.True(t, res.Failed)
|
||||||
assert.Same(t, fw, f.firewall)
|
assert.Same(t, fw, f.firewall)
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// routesTun is a NoopTun with a route table
|
||||||
|
type routesTun struct {
|
||||||
|
overlaytest.NoopTun
|
||||||
|
routes *bart.Table[routing.Gateways]
|
||||||
|
}
|
||||||
|
|
||||||
|
func (t routesTun) RoutesFor(ip netip.Addr) routing.Gateways {
|
||||||
|
r, _ := t.routes.Lookup(ip)
|
||||||
|
return r
|
||||||
|
}
|
||||||
|
|
||||||
|
func TestInterface_SiblingPeers(t *testing.T) {
|
||||||
|
addr := netip.MustParseAddr
|
||||||
|
routes := new(bart.Table[routing.Gateways])
|
||||||
|
// a and b carry 198.51.100.0/24, the route names b by its v6 address
|
||||||
|
routes.Insert(netip.MustParsePrefix("198.51.100.0/24"), routing.Gateways{
|
||||||
|
routing.NewGateway(addr("192.0.2.2"), 1),
|
||||||
|
routing.NewGateway(addr("fd00::3"), 1),
|
||||||
|
routing.NewGateway(addr("192.0.2.5"), 1), // no tunnel to this one
|
||||||
|
})
|
||||||
|
routes.Insert(netip.MustParsePrefix("203.0.113.0/24"), routing.Gateways{routing.NewGateway(addr("192.0.2.2"), 1)})
|
||||||
|
|
||||||
|
hm := newHostMap(test.NewLogger())
|
||||||
|
a := &HostInfo{vpnAddrs: []netip.Addr{addr("192.0.2.2"), addr("fd00::2")}, localIndexId: 1}
|
||||||
|
b := &HostInfo{vpnAddrs: []netip.Addr{addr("192.0.2.3"), addr("fd00::3")}, localIndexId: 2}
|
||||||
|
c := &HostInfo{vpnAddrs: []netip.Addr{addr("192.0.2.4")}, localIndexId: 3}
|
||||||
|
for _, h := range []*HostInfo{a, b, c} {
|
||||||
|
hm.unlockedAddHostInfo(h, &Interface{})
|
||||||
|
}
|
||||||
|
f := &Interface{inside: routesTun{routes: routes}, hostMap: hm}
|
||||||
|
|
||||||
|
// Siblings come back as their first vpn address, the one conntrack keys on, whichever address the route names
|
||||||
|
assert.Equal(t, []netip.Addr{addr("192.0.2.3")}, f.siblingPeers(addr("198.51.100.5"), a, nil))
|
||||||
|
assert.Equal(t, []netip.Addr{addr("192.0.2.2")}, f.siblingPeers(addr("198.51.100.5"), b, nil))
|
||||||
|
assert.Empty(t, f.siblingPeers(addr("198.51.100.5"), c, nil), "not one of our gateways for the network")
|
||||||
|
assert.Empty(t, f.siblingPeers(addr("203.0.113.5"), a, nil), "a single gateway route has no siblings")
|
||||||
|
assert.Empty(t, f.siblingPeers(addr("192.0.2.3"), a, nil), "no route")
|
||||||
|
}
|
||||||
+6
-3
@@ -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)
|
err := h.reload(c, true, nil)
|
||||||
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)
|
err := h.reload(c, false, r)
|
||||||
switch v := err.(type) {
|
switch v := err.(type) {
|
||||||
case *util.ContextualError:
|
case *util.ContextualError:
|
||||||
v.Log(l)
|
v.Log(l)
|
||||||
@@ -193,7 +193,8 @@ func (lh *LightHouse) GetUpdateInterval() int64 {
|
|||||||
return lh.interval.Load()
|
return lh.interval.Load()
|
||||||
}
|
}
|
||||||
|
|
||||||
func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
// r is nil on the initial load
|
||||||
|
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)
|
||||||
@@ -228,6 +229,7 @@ func (lh *LightHouse) reload(c *config.C, initial bool) error {
|
|||||||
"addr", rawAddr,
|
"addr", rawAddr,
|
||||||
"entry", i+1,
|
"entry", i+1,
|
||||||
)
|
)
|
||||||
|
r.Failed()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -386,6 +388,7 @@ func (lh *LightHouse) reload(c *config.C, initial bool) 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)
|
||||||
|
|||||||
+29
-1
@@ -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)
|
err = lh.reload(c, false, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -1014,3 +1014,31 @@ 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)
|
sshStart, err = configSSH(l, ssh, c, nil)
|
||||||
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
|
||||||
|
|||||||
+4
-2
@@ -476,8 +476,10 @@ 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) {
|
||||||
return
|
hostinfo.logger(f.l).Debug("Error while validating inbound packet", "error", err, "packet", out)
|
||||||
|
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())
|
||||||
|
|||||||
@@ -0,0 +1,45 @@
|
|||||||
|
//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)
|
||||||
|
}
|
||||||
|
}
|
||||||
|
}
|
||||||
@@ -0,0 +1,80 @@
|
|||||||
|
//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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+29
-12
@@ -80,17 +80,31 @@ 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) {
|
||||||
name := c.GetString("tun.dev", "")
|
ifIndex, ok := utunIndex(c.GetString("tun.dev", ""))
|
||||||
ifIndex := -1
|
if !ok {
|
||||||
if name != "" && name != "utun" {
|
// NOTE: we don't make this error so we don't break existing
|
||||||
_, err := fmt.Sscanf(name, "utun%d", &ifIndex)
|
// configs that set a name before it was used.
|
||||||
if err != nil || ifIndex < 0 {
|
l.Warn("interface name must be utun[0-9]+ on Darwin, ignoring")
|
||||||
// NOTE: we don't make this error so we don't break existing
|
|
||||||
// configs that set a name before it was used.
|
|
||||||
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)
|
||||||
@@ -114,7 +128,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)
|
||||||
}
|
}
|
||||||
@@ -138,7 +152,10 @@ 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.dev", "tun.mtu")
|
r.RestartRequiredIfChanged("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,3 +103,16 @@ 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)
|
||||||
|
}
|
||||||
|
}
|
||||||
+5
-15
@@ -8,7 +8,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -67,24 +66,12 @@ 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) {
|
||||||
// Try to open tun device
|
fd, deviceName, err := openTun(l, c.GetString("tun.dev", ""))
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -109,7 +96,10 @@ 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.dev", "tun.mtu")
|
r.RestartRequiredIfChanged("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)
|
||||||
|
|||||||
+5
-15
@@ -8,7 +8,6 @@ import (
|
|||||||
"log/slog"
|
"log/slog"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"os"
|
"os"
|
||||||
"regexp"
|
|
||||||
"sync/atomic"
|
"sync/atomic"
|
||||||
"syscall"
|
"syscall"
|
||||||
"unsafe"
|
"unsafe"
|
||||||
@@ -58,24 +57,12 @@ 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) {
|
||||||
// Try to open tun device
|
fd, deviceName, err := openTun(l, c.GetString("tun.dev", ""))
|
||||||
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
|
||||||
}
|
}
|
||||||
@@ -100,7 +87,10 @@ 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.dev", "tun.mtu")
|
r.RestartRequiredIfChanged("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)
|
||||||
|
|||||||
@@ -109,7 +109,15 @@ 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", "tun.network_category")
|
r.RestartRequiredIfChanged("tun.dev", "tun.mtu")
|
||||||
|
// 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,6 +181,27 @@ 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") })
|
||||||
|
|||||||
@@ -61,7 +61,18 @@ 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) {
|
||||||
r.RestartRequiredIfChanged("cipher")
|
// 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")
|
||||||
|
}
|
||||||
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)
|
||||||
@@ -116,16 +127,21 @@ 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")
|
||||||
switch cipher {
|
if !validCipher(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},
|
||||||
|
|||||||
+39
@@ -1,6 +1,7 @@
|
|||||||
package nebula
|
package nebula
|
||||||
|
|
||||||
import (
|
import (
|
||||||
|
"crypto/fips140"
|
||||||
"fmt"
|
"fmt"
|
||||||
"net/netip"
|
"net/netip"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -128,6 +129,23 @@ 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")))
|
||||||
@@ -178,3 +196,24 @@ 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)
|
||||||
|
}
|
||||||
@@ -14,6 +14,7 @@ import (
|
|||||||
"path/filepath"
|
"path/filepath"
|
||||||
"runtime"
|
"runtime"
|
||||||
"runtime/pprof"
|
"runtime/pprof"
|
||||||
|
"slices"
|
||||||
"sort"
|
"sort"
|
||||||
"strconv"
|
"strconv"
|
||||||
"strings"
|
"strings"
|
||||||
@@ -65,7 +66,7 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
// 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() && !c.HasChanged("sshd") {
|
if ssh.Running() && !slices.ContainsFunc(sshdServerKeys, c.HasChanged) {
|
||||||
key, err := loadSSHHostKey(c)
|
key, err := loadSSHHostKey(c)
|
||||||
if err == nil {
|
if err == nil {
|
||||||
err = ssh.SetHostKey(key)
|
err = ssh.SetHostKey(key)
|
||||||
@@ -77,7 +78,7 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
}
|
}
|
||||||
return
|
return
|
||||||
}
|
}
|
||||||
sshRun, err := configSSH(l, ssh, c)
|
sshRun, err := configSSH(l, ssh, c, r)
|
||||||
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()
|
||||||
@@ -92,6 +93,10 @@ func wireSSHReload(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) {
|
|||||||
})
|
})
|
||||||
}
|
}
|
||||||
|
|
||||||
|
// sshdServerKeys are what configSSH reads. sshd.sandbox_dir isn't one, it's read once at startup and restarting the
|
||||||
|
// server doesn't apply it
|
||||||
|
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", "")
|
||||||
@@ -112,7 +117,8 @@ 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.
|
||||||
func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), 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, 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")
|
||||||
@@ -145,6 +151,7 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
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,12 +163,14 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
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
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -175,6 +184,7 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
"sshKeyConfig", rk,
|
"sshKeyConfig", rk,
|
||||||
"sshKey", v,
|
"sshKey", v,
|
||||||
)
|
)
|
||||||
|
r.Failed()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -186,6 +196,7 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
"sshKeyConfig", rk,
|
"sshKeyConfig", rk,
|
||||||
"sshKey", subK,
|
"sshKey", subK,
|
||||||
)
|
)
|
||||||
|
r.Failed()
|
||||||
continue
|
continue
|
||||||
}
|
}
|
||||||
|
|
||||||
@@ -195,12 +206,14 @@ func configSSH(l *slog.Logger, ssh *sshd.SSHServer, c *config.C) (func(), error)
|
|||||||
"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 {
|
||||||
|
|||||||
+65
-9
@@ -26,13 +26,15 @@ 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")}
|
h := &sshdReloadHarness{keyFile: filepath.Join(t.TempDir(), "host_key"), listen: "127.0.0.1:0", users: []string{"test"}}
|
||||||
h.writeHostKey(t, newEd25519Key(t))
|
h.writeHostKey(t, newEd25519Key(t))
|
||||||
|
|
||||||
_, userKey, err := ed25519.GenerateKey(rand.Reader)
|
_, userKey, err := ed25519.GenerateKey(rand.Reader)
|
||||||
@@ -45,7 +47,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)
|
run, err := configSSH(l, h.server, h.c, nil)
|
||||||
require.NoError(t, err)
|
require.NoError(t, err)
|
||||||
go run()
|
go run()
|
||||||
t.Cleanup(h.server.Stop)
|
t.Cleanup(h.server.Stop)
|
||||||
@@ -71,8 +73,12 @@ func (h *sshdReloadHarness) writeHostKey(t *testing.T, key crypto.Signer) ssh.Pu
|
|||||||
}
|
}
|
||||||
|
|
||||||
func (h *sshdReloadHarness) config() string {
|
func (h *sshdReloadHarness) config() string {
|
||||||
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",
|
users := ""
|
||||||
h.keyFile, string(ssh.MarshalAuthorizedKey(h.signer.PublicKey())), h.extra)
|
for _, u := range h.users {
|
||||||
|
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
|
||||||
@@ -134,15 +140,41 @@ 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)
|
||||||
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
|
change(t, h)
|
||||||
|
require.NoError(t, h.c.ReloadConfigString(h.config()).Err)
|
||||||
|
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)
|
||||||
|
h.connect(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)
|
h := newSSHDReloadHarness(t)
|
||||||
client, _ := h.connect(t)
|
client, _ := h.connect(t)
|
||||||
|
|
||||||
h.extra = " trusted_cas: []\n"
|
h.extra = " sandbox_dir: " + t.TempDir() + "\n"
|
||||||
require.NoError(t, h.c.ReloadConfigString(h.config()).Err)
|
res := h.c.ReloadConfigString(h.config())
|
||||||
assert.True(t, dropped(client, 5*time.Second), "a changed sshd config was not applied")
|
require.NoError(t, res.Err)
|
||||||
require.Eventually(t, h.server.Running, 5*time.Second, 10*time.Millisecond)
|
assert.Equal(t, []string{"sshd.sandbox_dir"}, res.RestartKeys)
|
||||||
h.connect(t)
|
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
|
||||||
@@ -194,3 +226,27 @@ 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)
|
||||||
|
})
|
||||||
|
}
|
||||||
|
}
|
||||||
Reference in new issue
Block a user