Skip to content

fix: never send local endpoints if disabled #12138

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 4 commits into from
Feb 20, 2024
Merged
Show file tree
Hide file tree
Changes from 2 commits
Commits
File filter

Filter by extension

Filter by extension


Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion go.mod
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,7 @@ replace github.com/dlclark/regexp2 => github.com/dlclark/regexp2 v1.7.0

// There are a few minor changes we make to Tailscale that we're slowly upstreaming. Compare here:
// https://github.com/tailscale/tailscale/compare/main...coder:tailscale:main
replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20231205095743-61c97bad8c8b
replace tailscale.com => github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1

// Fixes a race-condition in coder/wgtunnel.
// Upstream PR: https://github.com/WireGuard/wireguard-go/pull/85
Expand Down
4 changes: 2 additions & 2 deletions go.sum
Original file line number Diff line number Diff line change
Expand Up @@ -202,8 +202,8 @@ github.com/coder/retry v1.5.1 h1:iWu8YnD8YqHs3XwqrqsjoBTAVqT9ml6z9ViJ2wlMiqc=
github.com/coder/retry v1.5.1/go.mod h1:blHMk9vs6LkoRT9ZHyuZo360cufXEhrxqvEzeMtRGoY=
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788 h1:YoUSJ19E8AtuUFVYBpXuOD6a/zVP3rcxezNsoDseTUw=
github.com/coder/ssh v0.0.0-20231128192721-70855dedb788/go.mod h1:aGQbuCLyhRLMzZF067xc84Lh7JDs1FKwCmF1Crl9dxQ=
github.com/coder/tailscale v1.1.1-0.20231205095743-61c97bad8c8b h1:ut/aL6oI8TjGdg4JI8+bKB9w5j73intbe0dJAmcmYyQ=
github.com/coder/tailscale v1.1.1-0.20231205095743-61c97bad8c8b/go.mod h1:L8tPrwSi31RAMEMV8rjb0vYTGs7rXt8rAHbqY/p41j4=
github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1 h1:A7dZHNidAVH6Kxn5D3hTEH+iRO8slnM0aRer6/cxlyE=
github.com/coder/tailscale v1.1.1-0.20240214140224-3788ab894ba1/go.mod h1:L8tPrwSi31RAMEMV8rjb0vYTGs7rXt8rAHbqY/p41j4=
github.com/coder/terraform-provider-coder v0.14.2 h1:CfQyQH9bOTI3cauxKJyPCUDkIcHOaMyFifxehkkkz/8=
github.com/coder/terraform-provider-coder v0.14.2/go.mod h1:pACHRoXSHBGyY696mLeQ1hR/Ag1G2wFk5bw0mT5Zp2g=
github.com/coder/wgtunnel v0.1.13-0.20231127054351-578bfff9b92a h1:KhR9LUVllMZ+e9lhubZ1HNrtJDgH5YLoTvpKwmrGag4=
Expand Down
2 changes: 2 additions & 0 deletions tailnet/conn.go
Original file line number Diff line number Diff line change
Expand Up @@ -171,6 +171,7 @@ func NewConn(options *Options) (conn *Conn, err error) {

magicConn := sys.MagicSock.Get()
magicConn.SetDERPForceWebsockets(options.DERPForceWebSockets)
magicConn.SetBlockEndpoints(options.BlockEndpoints)
if options.DERPHeader != nil {
magicConn.SetDERPHeader(options.DERPHeader.Clone())
}
Expand Down Expand Up @@ -346,6 +347,7 @@ func (c *Conn) SetDERPForceWebSockets(v bool) {
func (c *Conn) SetBlockEndpoints(blockEndpoints bool) {
c.configMaps.setBlockEndpoints(blockEndpoints)
c.nodeUpdater.setBlockEndpoints(blockEndpoints)
c.magicConn.SetBlockEndpoints(blockEndpoints)
}

// SetDERPRegionDialer updates the dialer to use for connecting to DERP regions.
Expand Down
78 changes: 77 additions & 1 deletion tailnet/conn_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -4,6 +4,7 @@ import (
"context"
"net/netip"
"testing"
"time"

"github.com/google/uuid"
"github.com/stretchr/testify/assert"
Expand Down Expand Up @@ -412,9 +413,78 @@ parentLoop:
require.True(t, client2.AwaitReachable(awaitReachableCtx4, ip))
}

func TestConn_BlockEndpoints(t *testing.T) {
t.Parallel()
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug)

derpMap, _ := tailnettest.RunDERPAndSTUN(t)

// Setup conn 1.
ip1 := tailnet.IP()
conn1, err := tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(ip1, 128)},
Logger: logger.Named("w1"),
DERPMap: derpMap,
BlockEndpoints: true,
})
require.NoError(t, err)
defer func() {
err := conn1.Close()
assert.NoError(t, err)
}()

// Setup conn 2.
ip2 := tailnet.IP()
conn2, err := tailnet.NewConn(&tailnet.Options{
Addresses: []netip.Prefix{netip.PrefixFrom(ip2, 128)},
Logger: logger.Named("w2"),
DERPMap: derpMap,
BlockEndpoints: true,
})
require.NoError(t, err)
defer func() {
err := conn2.Close()
assert.NoError(t, err)
}()

// Connect them together and wait for them to be reachable.
nodes1 := stitch(t, conn2, conn1)
nodes2 := stitch(t, conn1, conn2)
awaitReachableCtx, awaitReachableCancel := context.WithTimeout(context.Background(), testutil.WaitShort)
defer awaitReachableCancel()
require.True(t, conn1.AwaitReachable(awaitReachableCtx, ip2))

// Watch for 10 seconds to ensure that neither peer has any endpoints.
// There's no good way to force endpoint updates, so we just wait.
parentLoop:
for {
select {
case node := <-nodes1:
require.Empty(t, node.Endpoints, "conn1 got endpoints")
case node := <-nodes2:
require.Empty(t, node.Endpoints, "conn2 got endpoints")
case <-time.After(testutil.WaitShort):
Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

timer gets reset each time we go around the loop, so we could wait for much longer than 10 sec.

But, I don't think we add much value by waiting for 10s. There are unit tests for tailnet.Conn blocking endpoints on node updates.

What wasn't covered prior to this is endpoints discovered via disco. The status tests you have below cover that.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

Oops, yeah I guess I can just remove this section then. I do still think we should wait because we can't force it to gather local endpoints, unless you know a way to test this reliably?

Copy link
Contributor

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

we have tests that demonstrate that tailnet.Conn won't send endpoint in a Node update callback when blockendpoints is set.

E.g. TestNodeUpdater_setBlockEndpoints_different

Where we got the endpoints from (local, STUN, etc) is immaterial.

Copy link
Member Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

I removed the node endpoint check but I still think the delay is required unless you know of another way to force disco endpoints to be gathered and sent immediately

break parentLoop
}
}

// Double check that both peers don't have endpoints for the other peer
// according to magicsock.
conn1Status, ok := conn1.Status().Peer[conn2.Node().Key]
require.True(t, ok)
require.Empty(t, conn1Status.Addrs)
require.Empty(t, conn1Status.CurAddr)
conn2Status, ok := conn2.Status().Peer[conn1.Node().Key]
require.True(t, ok)
require.Empty(t, conn2Status.Addrs)
require.Empty(t, conn2Status.CurAddr)
}

// stitch sends node updates from src Conn as peer updates to dst Conn. Sort of
// like the Coordinator would, but without actually needing a Coordinator.
func stitch(t *testing.T, dst, src *tailnet.Conn) {
func stitch(t *testing.T, dst, src *tailnet.Conn) <-chan *tailnet.Node {
// Buffered to avoid blocking the callback.
out := make(chan *tailnet.Node, 50)
srcID := uuid.New()
src.SetNodeCallback(func(node *tailnet.Node) {
pn, err := tailnet.NodeToProto(node)
Expand All @@ -427,5 +497,11 @@ func stitch(t *testing.T, dst, src *tailnet.Conn) {
Kind: proto.CoordinateResponse_PeerUpdate_NODE,
}})
assert.NoError(t, err)

select {
case out <- node:
default:
}
})
return out
}