Skip to content

Commit 9a50ac4

Browse files
committed
Add flag to disable P2P connections
1 parent de5b13b commit 9a50ac4

15 files changed

+56
-35
lines changed

cli/agent_test.go

Lines changed: 3 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -7,8 +7,6 @@ import (
77
"github.com/stretchr/testify/assert"
88
"github.com/stretchr/testify/require"
99

10-
"cdr.dev/slog"
11-
1210
"github.com/coder/coder/cli/clitest"
1311
"github.com/coder/coder/coderd/coderdtest"
1412
"github.com/coder/coder/provisioner/echo"
@@ -67,7 +65,7 @@ func TestWorkspaceAgent(t *testing.T) {
6765
if assert.NotEmpty(t, workspace.LatestBuild.Resources) && assert.NotEmpty(t, resources[0].Agents) {
6866
assert.NotEmpty(t, resources[0].Agents[0].Version)
6967
}
70-
dialer, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, resources[0].Agents[0].ID)
68+
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
7169
require.NoError(t, err)
7270
defer dialer.Close()
7371
require.Eventually(t, func() bool {
@@ -128,7 +126,7 @@ func TestWorkspaceAgent(t *testing.T) {
128126
if assert.NotEmpty(t, resources) && assert.NotEmpty(t, resources[0].Agents) {
129127
assert.NotEmpty(t, resources[0].Agents[0].Version)
130128
}
131-
dialer, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, resources[0].Agents[0].ID)
129+
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
132130
require.NoError(t, err)
133131
defer dialer.Close()
134132
require.Eventually(t, func() bool {
@@ -189,7 +187,7 @@ func TestWorkspaceAgent(t *testing.T) {
189187
if assert.NotEmpty(t, resources) && assert.NotEmpty(t, resources[0].Agents) {
190188
assert.NotEmpty(t, resources[0].Agents[0].Version)
191189
}
192-
dialer, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, resources[0].Agents[0].ID)
190+
dialer, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
193191
require.NoError(t, err)
194192
defer dialer.Close()
195193
require.Eventually(t, func() bool {

cli/configssh_test.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -19,7 +19,6 @@ import (
1919
"github.com/stretchr/testify/assert"
2020
"github.com/stretchr/testify/require"
2121

22-
"cdr.dev/slog"
2322
"cdr.dev/slog/sloggers/slogtest"
2423

2524
"github.com/coder/coder/agent"
@@ -115,7 +114,7 @@ func TestConfigSSH(t *testing.T) {
115114
_ = agentCloser.Close()
116115
}()
117116
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
118-
agentConn, err := client.DialWorkspaceAgentTailnet(context.Background(), slog.Logger{}, resources[0].Agents[0].ID)
117+
agentConn, err := client.DialWorkspaceAgent(context.Background(), resources[0].Agents[0].ID, nil)
119118
require.NoError(t, err)
120119
defer agentConn.Close()
121120

cli/portforward.go

Lines changed: 1 addition & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -16,7 +16,6 @@ import (
1616
"github.com/spf13/cobra"
1717
"golang.org/x/xerrors"
1818

19-
"cdr.dev/slog"
2019
"github.com/coder/coder/agent"
2120
"github.com/coder/coder/cli/cliflag"
2221
"github.com/coder/coder/cli/cliui"
@@ -96,7 +95,7 @@ func portForward() *cobra.Command {
9695
return xerrors.Errorf("await agent: %w", err)
9796
}
9897

99-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, workspaceAgent.ID)
98+
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, nil)
10099
if err != nil {
101100
return err
102101
}

cli/speedtest.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,7 +55,9 @@ func speedtest() *cobra.Command {
5555
if cliflag.IsSetBool(cmd, varVerbose) {
5656
logger = logger.Leveled(slog.LevelDebug)
5757
}
58-
conn, err := client.DialWorkspaceAgentTailnet(ctx, logger, workspaceAgent.ID)
58+
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, &codersdk.DialWorkspaceAgentOptions{
59+
Logger: logger,
60+
})
5961
if err != nil {
6062
return err
6163
}

cli/ssh.go

Lines changed: 1 addition & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -20,8 +20,6 @@ import (
2020
"golang.org/x/term"
2121
"golang.org/x/xerrors"
2222

23-
"cdr.dev/slog"
24-
2523
"github.com/coder/coder/cli/cliflag"
2624
"github.com/coder/coder/cli/cliui"
2725
"github.com/coder/coder/coderd/autobuild/notify"
@@ -86,7 +84,7 @@ func ssh() *cobra.Command {
8684
return xerrors.Errorf("await agent: %w", err)
8785
}
8886

89-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, workspaceAgent.ID)
87+
conn, err := client.DialWorkspaceAgent(ctx, workspaceAgent.ID, nil)
9088
if err != nil {
9189
return err
9290
}

coderd/activitybump_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -74,7 +74,9 @@ func TestWorkspaceActivityBump(t *testing.T) {
7474
client, workspace, assertBumped := setupActivityTest(t)
7575

7676
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
77-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slogtest.Make(t, nil), resources[0].Agents[0].ID)
77+
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
78+
Logger: slogtest.Make(t, nil),
79+
})
7880
require.NoError(t, err)
7981
defer conn.Close()
8082

coderd/coderd.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func New(options *Options) *API {
123123
options.TailnetCoordinator = tailnet.NewCoordinator()
124124
}
125125
if options.DERPServer == nil {
126-
options.DERPServer = derp.NewServer(key.NewNode(), tailnet.Logger(options.Logger))
126+
options.DERPServer = derp.NewServer(key.NewNode(), tailnet.Logger(options.Logger.Named("derp").Leveled(slog.LevelDebug)))
127127
options.DERPServer.SetMeshKey("todo-kyle-change-this")
128128
}
129129
if options.Auditor == nil {

coderd/templates_test.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -626,7 +626,9 @@ func TestTemplateDAUs(t *testing.T) {
626626
require.NoError(t, err)
627627
assert.Zero(t, workspaces[0].LastUsedAt)
628628

629-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slogtest.Make(t, nil).Named("tailnet"), resources[0].Agents[0].ID)
629+
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
630+
Logger: slogtest.Make(t, nil).Named("tailnet"),
631+
})
630632
require.NoError(t, err)
631633
defer func() {
632634
_ = conn.Close()

coderd/workspaceagents_test.go

Lines changed: 4 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -123,7 +123,7 @@ func TestWorkspaceAgentListen(t *testing.T) {
123123
defer cancel()
124124

125125
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
126-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slog.Logger{}, resources[0].Agents[0].ID)
126+
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, nil)
127127
require.NoError(t, err)
128128
defer func() {
129129
_ = conn.Close()
@@ -253,7 +253,9 @@ func TestWorkspaceAgentTailnet(t *testing.T) {
253253

254254
ctx, cancelFunc := context.WithCancel(context.Background())
255255
defer cancelFunc()
256-
conn, err := client.DialWorkspaceAgentTailnet(ctx, slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug), resources[0].Agents[0].ID)
256+
conn, err := client.DialWorkspaceAgent(ctx, resources[0].Agents[0].ID, &codersdk.DialWorkspaceAgentOptions{
257+
Logger: slogtest.Make(t, nil).Named("client").Leveled(slog.LevelDebug),
258+
})
257259
require.NoError(t, err)
258260
defer conn.Close()
259261
sshClient, err := conn.SSHClient()

codersdk/workspaceagents.go

Lines changed: 15 additions & 8 deletions
Original file line numberDiff line numberDiff line change
@@ -331,7 +331,13 @@ func (c *Client) ListenWorkspaceAgentTailnet(ctx context.Context) (net.Conn, err
331331
return websocket.NetConn(ctx, conn, websocket.MessageBinary), nil
332332
}
333333

334-
func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logger, agentID uuid.UUID) (*AgentConn, error) {
334+
type DialWorkspaceAgentOptions struct {
335+
Logger slog.Logger
336+
// BlockEndpoints forced a direct connection through DERP.
337+
BlockEndpoints bool
338+
}
339+
340+
func (c *Client) DialWorkspaceAgent(ctx context.Context, agentID uuid.UUID, options *DialWorkspaceAgentOptions) (*AgentConn, error) {
335341
res, err := c.Request(ctx, http.MethodGet, fmt.Sprintf("/api/v2/workspaceagents/%s/connection", agentID), nil)
336342
if err != nil {
337343
return nil, err
@@ -348,9 +354,10 @@ func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logg
348354

349355
ip := tailnet.IP()
350356
conn, err := tailnet.NewConn(&tailnet.Options{
351-
Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)},
352-
DERPMap: connInfo.DERPMap,
353-
Logger: logger,
357+
Addresses: []netip.Prefix{netip.PrefixFrom(ip, 128)},
358+
DERPMap: connInfo.DERPMap,
359+
Logger: options.Logger,
360+
BlockEndpoints: options.BlockEndpoints,
354361
})
355362
if err != nil {
356363
return nil, xerrors.Errorf("create tailnet: %w", err)
@@ -378,7 +385,7 @@ func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logg
378385
defer close(closed)
379386
isFirst := true
380387
for retrier := retry.New(50*time.Millisecond, 10*time.Second); retrier.Wait(ctx); {
381-
logger.Debug(ctx, "connecting")
388+
options.Logger.Debug(ctx, "connecting")
382389
// nolint:bodyclose
383390
ws, res, err := websocket.Dial(ctx, coordinateURL.String(), &websocket.DialOptions{
384391
HTTPClient: httpClient,
@@ -397,21 +404,21 @@ func (c *Client) DialWorkspaceAgentTailnet(ctx context.Context, logger slog.Logg
397404
if errors.Is(err, context.Canceled) {
398405
return
399406
}
400-
logger.Debug(ctx, "failed to dial", slog.Error(err))
407+
options.Logger.Debug(ctx, "failed to dial", slog.Error(err))
401408
continue
402409
}
403410
sendNode, errChan := tailnet.ServeCoordinator(websocket.NetConn(ctx, ws, websocket.MessageBinary), func(node []*tailnet.Node) error {
404411
return conn.UpdateNodes(node)
405412
})
406413
conn.SetNodeCallback(sendNode)
407-
logger.Debug(ctx, "serving coordinator")
414+
options.Logger.Debug(ctx, "serving coordinator")
408415
err = <-errChan
409416
if errors.Is(err, context.Canceled) {
410417
_ = ws.Close(websocket.StatusGoingAway, "")
411418
return
412419
}
413420
if err != nil {
414-
logger.Debug(ctx, "error serving coordinator", slog.Error(err))
421+
options.Logger.Debug(ctx, "error serving coordinator", slog.Error(err))
415422
_ = ws.Close(websocket.StatusGoingAway, "")
416423
continue
417424
}

enterprise/coderd/coderd.go

Lines changed: 3 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -3,6 +3,7 @@ package coderd
33
import (
44
"context"
55
"crypto/ed25519"
6+
"fmt"
67
"net/http"
78
"sync"
89
"time"
@@ -126,7 +127,7 @@ func New(ctx context.Context, options *Options) (*API, error) {
126127
if err != nil {
127128
return nil, xerrors.Errorf("initialize replica: %w", err)
128129
}
129-
api.derpMesh = derpmesh.New(options.Logger, api.DERPServer)
130+
api.derpMesh = derpmesh.New(options.Logger.Named("derpmesh"), api.DERPServer)
130131

131132
err = api.updateEntitlements(ctx)
132133
if err != nil {
@@ -246,6 +247,7 @@ func (api *API) updateEntitlements(ctx context.Context) error {
246247
coordinator = haCoordinator
247248
}
248249

250+
fmt.Printf("HA enabled\n")
249251
api.replicaManager.SetCallback(func() {
250252
addresses := make([]string, 0)
251253
for _, replica := range api.replicaManager.Regional() {

enterprise/coderd/replicas_test.go

Lines changed: 6 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -11,6 +11,7 @@ import (
1111

1212
"github.com/coder/coder/coderd/coderdtest"
1313
"github.com/coder/coder/coderd/database/dbtestutil"
14+
"github.com/coder/coder/codersdk"
1415
"github.com/coder/coder/enterprise/coderd/coderdenttest"
1516
"github.com/coder/coder/testutil"
1617
)
@@ -61,14 +62,16 @@ func TestReplicas(t *testing.T) {
6162
},
6263
})
6364
secondClient.SessionToken = firstClient.SessionToken
64-
6565
agentID := setupWorkspaceAgent(t, firstClient, firstUser)
66-
conn, err := secondClient.DialWorkspaceAgentTailnet(context.Background(), slogtest.Make(t, nil).Leveled(slog.LevelDebug), agentID)
66+
conn, err := secondClient.DialWorkspaceAgent(context.Background(), agentID, &codersdk.DialWorkspaceAgentOptions{
67+
BlockEndpoints: true,
68+
Logger: slogtest.Make(t, nil).Leveled(slog.LevelDebug),
69+
})
6770
require.NoError(t, err)
6871
require.Eventually(t, func() bool {
6972
_, err = conn.Ping()
7073
return err == nil
71-
}, testutil.WaitShort, testutil.IntervalFast)
74+
}, testutil.WaitLong, testutil.IntervalFast)
7275
_ = conn.Close()
7376
})
7477
}

enterprise/coderd/workspaceagents_test.go

Lines changed: 2 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -8,7 +8,6 @@ import (
88
"github.com/google/uuid"
99
"github.com/stretchr/testify/require"
1010

11-
"cdr.dev/slog"
1211
"cdr.dev/slog/sloggers/slogtest"
1312
"github.com/coder/coder/agent"
1413
"github.com/coder/coder/coderd/coderdtest"
@@ -33,7 +32,7 @@ func TestBlockNonBrowser(t *testing.T) {
3332
BrowserOnly: true,
3433
})
3534
id := setupWorkspaceAgent(t, client, user)
36-
_, err := client.DialWorkspaceAgentTailnet(context.Background(), slog.Logger{}, id)
35+
_, err := client.DialWorkspaceAgent(context.Background(), id, nil)
3736
var apiErr *codersdk.Error
3837
require.ErrorAs(t, err, &apiErr)
3938
require.Equal(t, http.StatusConflict, apiErr.StatusCode())
@@ -50,7 +49,7 @@ func TestBlockNonBrowser(t *testing.T) {
5049
BrowserOnly: false,
5150
})
5251
id := setupWorkspaceAgent(t, client, user)
53-
conn, err := client.DialWorkspaceAgentTailnet(context.Background(), slog.Logger{}, id)
52+
conn, err := client.DialWorkspaceAgent(context.Background(), id, nil)
5453
require.NoError(t, err)
5554
_ = conn.Close()
5655
})

enterprise/derpmesh/derpmesh.go

Lines changed: 7 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -2,6 +2,7 @@ package derpmesh
22

33
import (
44
"context"
5+
"net"
56
"net/url"
67
"sync"
78

@@ -88,11 +89,15 @@ func (m *Mesh) addAddress(address string) (bool, error) {
8889
if isActive {
8990
return false, nil
9091
}
91-
client, err := derphttp.NewClient(m.server.PrivateKey(), address, tailnet.Logger(m.logger))
92+
client, err := derphttp.NewClient(m.server.PrivateKey(), address, tailnet.Logger(m.logger.Named("client")))
9293
if err != nil {
9394
return false, xerrors.Errorf("create derp client: %w", err)
9495
}
9596
client.MeshKey = m.server.MeshKey()
97+
client.SetURLDialer(func(ctx context.Context, network, addr string) (net.Conn, error) {
98+
var dialer net.Dialer
99+
return dialer.DialContext(ctx, network, addr)
100+
})
96101
ctx, cancelFunc := context.WithCancel(m.ctx)
97102
closed := make(chan struct{})
98103
closeFunc := func() {
@@ -103,7 +108,7 @@ func (m *Mesh) addAddress(address string) (bool, error) {
103108
m.active[address] = closeFunc
104109
go func() {
105110
defer close(closed)
106-
client.RunWatchConnectionLoop(ctx, m.server.PublicKey(), tailnet.Logger(m.logger), func(np key.NodePublic) {
111+
client.RunWatchConnectionLoop(ctx, m.server.PublicKey(), tailnet.Logger(m.logger.Named("loop")), func(np key.NodePublic) {
107112
m.server.AddPacketForwarder(np, client)
108113
}, func(np key.NodePublic) {
109114
m.server.RemovePacketForwarder(np, client)

tailnet/conn.go

Lines changed: 3 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -344,6 +344,9 @@ func (c *Conn) UpdateNodes(nodes []*Node) error {
344344
// reason. TODO: @kylecarbs debug this!
345345
KeepAlive: ok && peerStatus.Active,
346346
}
347+
if c.blockEndpoints {
348+
peerNode.Endpoints = nil
349+
}
347350
c.peerMap[node.ID] = peerNode
348351
}
349352
c.netMap.Peers = make([]*tailcfg.Node, 0, len(c.peerMap))

0 commit comments

Comments
 (0)