Skip to content

Commit de5b13b

Browse files
committed
Add the ability to block endpoints
1 parent 186a5e2 commit de5b13b

File tree

3 files changed

+67
-35
lines changed

3 files changed

+67
-35
lines changed

enterprise/coderd/coderdenttest/coderdenttest.go

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -68,7 +68,7 @@ func NewWithAPI(t *testing.T, options *Options) (*codersdk.Client, io.Closer, *c
6868
BrowserOnly: options.BrowserOnly,
6969
SCIMAPIKey: options.SCIMAPIKey,
7070
DERPServerRelayAddress: oop.AccessURL.String(),
71-
DERPServerRegionID: 1,
71+
DERPServerRegionID: oop.DERPMap.RegionIDs()[0],
7272
ReplicaID: uuid.New(),
7373
UserWorkspaceQuota: options.UserWorkspaceQuota,
7474
Options: oop,

enterprise/coderd/replicas_test.go

Lines changed: 52 additions & 28 deletions
Original file line numberDiff line numberDiff line change
@@ -3,7 +3,6 @@ package coderd_test
33
import (
44
"context"
55
"testing"
6-
"time"
76

87
"github.com/stretchr/testify/require"
98

@@ -13,38 +12,63 @@ import (
1312
"github.com/coder/coder/coderd/coderdtest"
1413
"github.com/coder/coder/coderd/database/dbtestutil"
1514
"github.com/coder/coder/enterprise/coderd/coderdenttest"
15+
"github.com/coder/coder/testutil"
1616
)
1717

1818
func TestReplicas(t *testing.T) {
1919
t.Parallel()
20-
db, pubsub := dbtestutil.NewDB(t)
21-
firstClient := coderdenttest.New(t, &coderdenttest.Options{
22-
Options: &coderdtest.Options{
23-
IncludeProvisionerDaemon: true,
24-
Database: db,
25-
Pubsub: pubsub,
26-
},
20+
t.Run("WarningsWithoutLicense", func(t *testing.T) {
21+
t.Parallel()
22+
db, pubsub := dbtestutil.NewDB(t)
23+
firstClient := coderdenttest.New(t, &coderdenttest.Options{
24+
Options: &coderdtest.Options{
25+
IncludeProvisionerDaemon: true,
26+
Database: db,
27+
Pubsub: pubsub,
28+
},
29+
})
30+
_ = coderdtest.CreateFirstUser(t, firstClient)
31+
secondClient := coderdenttest.New(t, &coderdenttest.Options{
32+
Options: &coderdtest.Options{
33+
Database: db,
34+
Pubsub: pubsub,
35+
},
36+
})
37+
secondClient.SessionToken = firstClient.SessionToken
38+
ents, err := secondClient.Entitlements(context.Background())
39+
require.NoError(t, err)
40+
require.Len(t, ents.Warnings, 1)
2741
})
28-
firstUser := coderdtest.CreateFirstUser(t, firstClient)
29-
coderdenttest.AddLicense(t, firstClient, coderdenttest.LicenseOptions{
30-
HighAvailability: true,
31-
})
32-
33-
secondClient := coderdenttest.New(t, &coderdenttest.Options{
34-
Options: &coderdtest.Options{
35-
Database: db,
36-
Pubsub: pubsub,
37-
},
38-
})
39-
secondClient.SessionToken = firstClient.SessionToken
42+
t.Run("ConnectAcrossMultiple", func(t *testing.T) {
43+
t.Parallel()
44+
db, pubsub := dbtestutil.NewDB(t)
45+
firstClient := coderdenttest.New(t, &coderdenttest.Options{
46+
Options: &coderdtest.Options{
47+
IncludeProvisionerDaemon: true,
48+
Database: db,
49+
Pubsub: pubsub,
50+
},
51+
})
52+
firstUser := coderdtest.CreateFirstUser(t, firstClient)
53+
coderdenttest.AddLicense(t, firstClient, coderdenttest.LicenseOptions{
54+
HighAvailability: true,
55+
})
4056

41-
agentID := setupWorkspaceAgent(t, firstClient, firstUser)
42-
conn, err := secondClient.DialWorkspaceAgentTailnet(context.Background(), slogtest.Make(t, nil).Leveled(slog.LevelDebug), agentID)
43-
require.NoError(t, err)
44-
require.Eventually(t, func() bool {
45-
_, err = conn.Ping()
46-
return err == nil
47-
}, 10*time.Second, 250*time.Millisecond)
57+
secondClient := coderdenttest.New(t, &coderdenttest.Options{
58+
Options: &coderdtest.Options{
59+
Database: db,
60+
Pubsub: pubsub,
61+
},
62+
})
63+
secondClient.SessionToken = firstClient.SessionToken
4864

49-
_ = conn.Close()
65+
agentID := setupWorkspaceAgent(t, firstClient, firstUser)
66+
conn, err := secondClient.DialWorkspaceAgentTailnet(context.Background(), slogtest.Make(t, nil).Leveled(slog.LevelDebug), agentID)
67+
require.NoError(t, err)
68+
require.Eventually(t, func() bool {
69+
_, err = conn.Ping()
70+
return err == nil
71+
}, testutil.WaitShort, testutil.IntervalFast)
72+
_ = conn.Close()
73+
})
5074
}

tailnet/conn.go

Lines changed: 14 additions & 6 deletions
Original file line numberDiff line numberDiff line change
@@ -48,7 +48,10 @@ type Options struct {
4848
Addresses []netip.Prefix
4949
DERPMap *tailcfg.DERPMap
5050

51-
Logger slog.Logger
51+
// BlockEndpoints specifies whether P2P endpoints are blocked.
52+
// If so, only DERPs can establish connections.
53+
BlockEndpoints bool
54+
Logger slog.Logger
5255
}
5356

5457
// NewConn constructs a new Wireguard server that will accept connections from the addresses provided.
@@ -175,6 +178,7 @@ func NewConn(options *Options) (*Conn, error) {
175178
wireguardEngine.SetFilter(filter.New(netMap.PacketFilter, localIPs, logIPs, nil, Logger(options.Logger.Named("packet-filter"))))
176179
dialContext, dialCancel := context.WithCancel(context.Background())
177180
server := &Conn{
181+
blockEndpoints: options.BlockEndpoints,
178182
dialContext: dialContext,
179183
dialCancel: dialCancel,
180184
closed: make(chan struct{}),
@@ -240,11 +244,12 @@ func IP() netip.Addr {
240244

241245
// Conn is an actively listening Wireguard connection.
242246
type Conn struct {
243-
dialContext context.Context
244-
dialCancel context.CancelFunc
245-
mutex sync.Mutex
246-
closed chan struct{}
247-
logger slog.Logger
247+
dialContext context.Context
248+
dialCancel context.CancelFunc
249+
mutex sync.Mutex
250+
closed chan struct{}
251+
logger slog.Logger
252+
blockEndpoints bool
248253

249254
dialer *tsdial.Dialer
250255
tunDevice *tstun.Wrapper
@@ -429,6 +434,9 @@ func (c *Conn) sendNode() {
429434
PreferredDERP: c.lastPreferredDERP,
430435
DERPLatency: c.lastDERPLatency,
431436
}
437+
if c.blockEndpoints {
438+
node.Endpoints = nil
439+
}
432440
nodeCallback := c.nodeCallback
433441
if nodeCallback == nil {
434442
return

0 commit comments

Comments
 (0)