- Notifications
You must be signed in to change notification settings - Fork1k
fix: rewrite url to agent ip in single tailnet#11810
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 ourterms of service andprivacy statement. We’ll occasionally send you account related emails.
Already on GitHub?Sign in to your account
Merged
Uh oh!
There was an error while loading.Please reload this page.
Merged
Changes fromall commits
Commits
File filter
Filter by extension
Conversations
Failed to load comments.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Jump to
Jump to file
Failed to load files.
Loading
Uh oh!
There was an error while loading.Please reload this page.
Diff view
Diff view
There are no files selected for viewing
2 changes: 1 addition & 1 deletioncoderd/files.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
25 changes: 16 additions & 9 deletionscoderd/tailnet.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
229 changes: 199 additions & 30 deletionscoderd/tailnet_test.go
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.Learn more about bidirectional Unicode characters
Original file line number | Diff line number | Diff line change |
---|---|---|
@@ -3,10 +3,13 @@ package coderd_test | ||
import ( | ||
"context" | ||
"fmt" | ||
"io" | ||
"net" | ||
"net/http" | ||
"net/http/httptest" | ||
"net/url" | ||
"strconv" | ||
"sync/atomic" | ||
"testing" | ||
"github.com/google/uuid" | ||
@@ -35,9 +38,10 @@ func TestServerTailnet_AgentConn_OK(t *testing.T) { | ||
defer cancel() | ||
// Connect through the ServerTailnet | ||
agents, serverTailnet := setupServerTailnetAgent(t, 1) | ||
a := agents[0] | ||
conn, release, err := serverTailnet.AgentConn(ctx,a.id) | ||
require.NoError(t, err) | ||
defer release() | ||
@@ -53,12 +57,13 @@ func TestServerTailnet_ReverseProxy(t *testing.T) { | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
agents, serverTailnet := setupServerTailnetAgent(t, 1) | ||
a := agents[0] | ||
u, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", codersdk.WorkspaceAgentHTTPAPIServerPort)) | ||
require.NoError(t, err) | ||
rp := serverTailnet.ReverseProxy(u, u,a.id) | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest( | ||
@@ -74,13 +79,147 @@ func TestServerTailnet_ReverseProxy(t *testing.T) { | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
}) | ||
t.Run("HostRewrite", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
agents, serverTailnet := setupServerTailnetAgent(t, 1) | ||
a := agents[0] | ||
u, err := url.Parse(fmt.Sprintf("http://127.0.0.1:%d", codersdk.WorkspaceAgentHTTPAPIServerPort)) | ||
require.NoError(t, err) | ||
rp := serverTailnet.ReverseProxy(u, u, a.id) | ||
req, err := http.NewRequestWithContext(ctx, http.MethodGet, u.String(), nil) | ||
require.NoError(t, err) | ||
// Ensure the reverse proxy director rewrites the url host to the agent's IP. | ||
rp.Director(req) | ||
assert.Equal(t, | ||
fmt.Sprintf("[%s]:%d", tailnet.IPFromUUID(a.id).String(), codersdk.WorkspaceAgentHTTPAPIServerPort), | ||
req.URL.Host, | ||
) | ||
}) | ||
coadler marked this conversation as resolved. Show resolvedHide resolvedUh oh!There was an error while loading.Please reload this page. | ||
t.Run("CachesConnection", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
agents, serverTailnet := setupServerTailnetAgent(t, 1) | ||
a := agents[0] | ||
port := ":4444" | ||
ln, err := a.TailnetConn().Listen("tcp", port) | ||
require.NoError(t, err) | ||
wln := &wrappedListener{Listener: ln} | ||
serverClosed := make(chan struct{}) | ||
go func() { | ||
defer close(serverClosed) | ||
//nolint:gosec | ||
_ = http.Serve(wln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte("hello from agent")) | ||
})) | ||
}() | ||
defer func() { | ||
// wait for server to close | ||
<-serverClosed | ||
}() | ||
defer ln.Close() | ||
u, err := url.Parse("http://127.0.0.1" + port) | ||
require.NoError(t, err) | ||
rp := serverTailnet.ReverseProxy(u, u, a.id) | ||
for i := 0; i < 5; i++ { | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest( | ||
http.MethodGet, | ||
u.String(), | ||
nil, | ||
).WithContext(ctx) | ||
rp.ServeHTTP(rw, req) | ||
res := rw.Result() | ||
_, _ = io.Copy(io.Discard, res.Body) | ||
res.Body.Close() | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
} | ||
assert.Equal(t, 1, wln.getDials()) | ||
}) | ||
t.Run("NotReusedBetweenAgents", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
agents, serverTailnet := setupServerTailnetAgent(t, 2) | ||
port := ":4444" | ||
for i, ag := range agents { | ||
i := i | ||
ln, err := ag.TailnetConn().Listen("tcp", port) | ||
require.NoError(t, err) | ||
wln := &wrappedListener{Listener: ln} | ||
serverClosed := make(chan struct{}) | ||
go func() { | ||
defer close(serverClosed) | ||
//nolint:gosec | ||
_ = http.Serve(wln, http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) { | ||
w.WriteHeader(http.StatusOK) | ||
w.Write([]byte(strconv.Itoa(i))) | ||
})) | ||
}() | ||
defer func() { //nolint:revive | ||
// wait for server to close | ||
<-serverClosed | ||
}() | ||
defer ln.Close() //nolint:revive | ||
} | ||
u, err := url.Parse("http://127.0.0.1" + port) | ||
require.NoError(t, err) | ||
for i, ag := range agents { | ||
rp := serverTailnet.ReverseProxy(u, u, ag.id) | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest( | ||
http.MethodGet, | ||
u.String(), | ||
nil, | ||
).WithContext(ctx) | ||
rp.ServeHTTP(rw, req) | ||
res := rw.Result() | ||
body, _ := io.ReadAll(res.Body) | ||
res.Body.Close() | ||
assert.Equal(t, http.StatusOK, res.StatusCode) | ||
assert.Equal(t, strconv.Itoa(i), string(body)) | ||
} | ||
}) | ||
t.Run("HTTPSProxy", func(t *testing.T) { | ||
t.Parallel() | ||
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong) | ||
defer cancel() | ||
agents, serverTailnet := setupServerTailnetAgent(t, 1) | ||
a := agents[0] | ||
const expectedResponseCode = 209 | ||
// Test that we can proxy HTTPS traffic. | ||
@@ -92,7 +231,7 @@ func TestServerTailnet_ReverseProxy(t *testing.T) { | ||
uri, err := url.Parse(s.URL) | ||
require.NoError(t, err) | ||
rp := serverTailnet.ReverseProxy(uri, uri,a.id) | ||
rw := httptest.NewRecorder() | ||
req := httptest.NewRequest( | ||
@@ -109,44 +248,74 @@ func TestServerTailnet_ReverseProxy(t *testing.T) { | ||
}) | ||
} | ||
type wrappedListener struct { | ||
net.Listener | ||
dials int32 | ||
} | ||
func (w *wrappedListener) Accept() (net.Conn, error) { | ||
conn, err := w.Listener.Accept() | ||
if err != nil { | ||
return nil, err | ||
} | ||
atomic.AddInt32(&w.dials, 1) | ||
return conn, nil | ||
} | ||
func (w *wrappedListener) getDials() int { | ||
return int(atomic.LoadInt32(&w.dials)) | ||
} | ||
type agentWithID struct { | ||
id uuid.UUID | ||
agent.Agent | ||
} | ||
func setupServerTailnetAgent(t *testing.T, agentNum int) ([]agentWithID, *coderd.ServerTailnet) { | ||
logger := slogtest.Make(t, nil).Leveled(slog.LevelDebug) | ||
derpMap, derpServer := tailnettest.RunDERPAndSTUN(t) | ||
coord := tailnet.NewCoordinator(logger) | ||
t.Cleanup(func() { | ||
_ = coord.Close() | ||
}) | ||
agents := []agentWithID{} | ||
for i := 0; i < agentNum; i++ { | ||
manifest := agentsdk.Manifest{ | ||
AgentID: uuid.New(), | ||
DERPMap: derpMap, | ||
} | ||
c := agenttest.NewClient(t, logger, manifest.AgentID, manifest, make(chan *agentsdk.Stats, 50), coord) | ||
t.Cleanup(c.Close) | ||
options := agent.Options{ | ||
Client: c, | ||
Filesystem: afero.NewMemMapFs(), | ||
Logger: logger.Named("agent"), | ||
} | ||
ag := agent.New(options) | ||
t.Cleanup(func() { | ||
_ = ag.Close() | ||
}) | ||
// Wait for the agent to connect. | ||
require.Eventually(t, func() bool { | ||
return coord.Node(manifest.AgentID) != nil | ||
}, testutil.WaitShort, testutil.IntervalFast) | ||
agents = append(agents, agentWithID{id: manifest.AgentID, Agent: ag}) | ||
} | ||
serverTailnet, err := coderd.NewServerTailnet( | ||
context.Background(), | ||
logger, | ||
derpServer, | ||
func() *tailcfg.DERPMap { returnderpMap }, | ||
false, | ||
func(context.Context) (tailnet.MultiAgentConn, error) { return coord.ServeMultiAgent(uuid.New()), nil }, | ||
trace.NewNoopTracerProvider(), | ||
@@ -157,5 +326,5 @@ func setupAgent(t *testing.T, agentAddresses []netip.Prefix) (uuid.UUID, agent.A | ||
_ = serverTailnet.Close() | ||
}) | ||
returnagents, serverTailnet | ||
} |
Add this suggestion to a batch that can be applied as a single commit.This suggestion is invalid because no changes were made to the code.Suggestions cannot be applied while the pull request is closed.Suggestions cannot be applied while viewing a subset of changes.Only one suggestion per line can be applied in a batch.Add this suggestion to a batch that can be applied as a single commit.Applying suggestions on deleted lines is not supported.You must change the existing code in this line in order to create a valid suggestion.Outdated suggestions cannot be applied.This suggestion has been applied or marked resolved.Suggestions cannot be applied from pending reviews.Suggestions cannot be applied on multi-line comments.Suggestions cannot be applied while the pull request is queued to merge.Suggestion cannot be applied right now. Please check back later.