Skip to content

fix: fail server startup on invalid DERP map #10536

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 1 commit into from
Nov 6, 2023
Merged
Show file tree
Hide file tree
Changes from all 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
19 changes: 19 additions & 0 deletions cli/server_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -1755,3 +1755,22 @@ func TestConnectToPostgres(t *testing.T) {
})
require.NoError(t, sqlDB.PingContext(ctx))
}

func TestServer_InvalidDERP(t *testing.T) {
t.Parallel()

// Try to start a server with the built-in DERP server disabled and no
// external DERP map.
inv, _ := clitest.New(t,
"server",
"--in-memory",
"--http-address", ":0",
"--access-url", "http://example.com",
"--derp-server-enable=false",
"--derp-server-stun-addresses", "disable",
"--block-direct-connections",
)
err := inv.Run()
require.Error(t, err)
require.ErrorContains(t, err, "A valid DERP map is required for networking to work")
}
19 changes: 19 additions & 0 deletions tailnet/derpmap.go
Original file line number Diff line number Diff line change
Expand Up @@ -130,6 +130,25 @@ func NewDERPMap(ctx context.Context, region *tailcfg.DERPRegion, stunAddrs []str
}
}

// Fail if the DERP map has no regions or no DERP nodes.
badDerpMapMsg := "A valid DERP map is required for networking to work. You must either supply your own DERP map or use the built-in DERP server"
if len(derpMap.Regions) == 0 {
return nil, xerrors.New("DERP map has no regions. " + badDerpMapMsg)
}
foundValidNode := false
regionLoop:
for _, region := range derpMap.Regions {
for _, node := range region.Nodes {
if !node.STUNOnly {
foundValidNode = true
break regionLoop
}
}
}
if !foundValidNode {
return nil, xerrors.New("DERP map has no DERP nodes. " + badDerpMapMsg)
}

return derpMap, nil
}

Expand Down
34 changes: 32 additions & 2 deletions tailnet/derpmap_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,7 +33,10 @@ func TestNewDERPMap(t *testing.T) {
server := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
data, _ := json.Marshal(&tailcfg.DERPMap{
Regions: map[int]*tailcfg.DERPRegion{
1: {},
1: {
RegionID: 1,
Nodes: []*tailcfg.DERPNode{{}},
},
},
})
_, _ = w.Write(data)
Expand Down Expand Up @@ -66,7 +69,9 @@ func TestNewDERPMap(t *testing.T) {
localPath := filepath.Join(t.TempDir(), "derp.json")
content, err := json.Marshal(&tailcfg.DERPMap{
Regions: map[int]*tailcfg.DERPRegion{
1: {},
1: {
Nodes: []*tailcfg.DERPNode{{}},
},
},
})
require.NoError(t, err)
Expand Down Expand Up @@ -131,4 +136,29 @@ func TestNewDERPMap(t *testing.T) {
// region.
require.EqualValues(t, -1, derpMap.Regions[3].Nodes[0].STUNPort)
})
t.Run("RequireRegions", func(t *testing.T) {
t.Parallel()
_, err := tailnet.NewDERPMap(context.Background(), nil, nil, "", "", false)
require.Error(t, err)
require.ErrorContains(t, err, "DERP map has no regions")
})
t.Run("RequireDERPNodes", func(t *testing.T) {
t.Parallel()

// No nodes.
_, err := tailnet.NewDERPMap(context.Background(), &tailcfg.DERPRegion{}, nil, "", "", false)
require.Error(t, err)
require.ErrorContains(t, err, "DERP map has no DERP nodes")

// No DERP nodes.
_, err = tailnet.NewDERPMap(context.Background(), &tailcfg.DERPRegion{
Nodes: []*tailcfg.DERPNode{
{
STUNOnly: true,
},
},
}, nil, "", "", false)
require.Error(t, err)
require.ErrorContains(t, err, "DERP map has no DERP nodes")
})
}