Skip to content

Commit cf5bc72

Browse files
authored
Merge branch 'main' into restructure-new
2 parents 4059f9f + 13e5c51 commit cf5bc72

File tree

88 files changed

+1911
-920
lines changed

Some content is hidden

Large Commits have some content hidden by default. Use the searchbox below for content that may be hidden.

88 files changed

+1911
-920
lines changed

cli/agent.go

Lines changed: 22 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -50,6 +50,8 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
5050
slogJSONPath string
5151
slogStackdriverPath string
5252
blockFileTransfer bool
53+
agentHeaderCommand string
54+
agentHeader []string
5355
)
5456
cmd := &serpent.Command{
5557
Use: "agent",
@@ -176,6 +178,14 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
176178
// with large payloads can take a bit. e.g. startup scripts
177179
// may take a while to insert.
178180
client.SDK.HTTPClient.Timeout = 30 * time.Second
181+
// Attach header transport so we process --agent-header and
182+
// --agent-header-command flags
183+
headerTransport, err := headerTransport(ctx, r.agentURL, agentHeader, agentHeaderCommand)
184+
if err != nil {
185+
return xerrors.Errorf("configure header transport: %w", err)
186+
}
187+
headerTransport.Transport = client.SDK.HTTPClient.Transport
188+
client.SDK.HTTPClient.Transport = headerTransport
179189

180190
// Enable pprof handler
181191
// This prevents the pprof import from being accidentally deleted.
@@ -361,6 +371,18 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
361371
Value: serpent.StringOf(&pprofAddress),
362372
Description: "The address to serve pprof.",
363373
},
374+
{
375+
Flag: "agent-header-command",
376+
Env: "CODER_AGENT_HEADER_COMMAND",
377+
Value: serpent.StringOf(&agentHeaderCommand),
378+
Description: "An external command that outputs additional HTTP headers added to all requests. The command must output each header as `key=value` on its own line.",
379+
},
380+
{
381+
Flag: "agent-header",
382+
Env: "CODER_AGENT_HEADER",
383+
Value: serpent.StringArrayOf(&agentHeader),
384+
Description: "Additional HTTP headers added to all requests. Provide as " + `key=value` + ". Can be specified multiple times.",
385+
},
364386
{
365387
Flag: "no-reap",
366388

cli/agent_test.go

Lines changed: 40 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -3,10 +3,13 @@ package cli_test
33
import (
44
"context"
55
"fmt"
6+
"net/http"
7+
"net/http/httptest"
68
"os"
79
"path/filepath"
810
"runtime"
911
"strings"
12+
"sync/atomic"
1013
"testing"
1114

1215
"github.com/google/uuid"
@@ -229,6 +232,43 @@ func TestWorkspaceAgent(t *testing.T) {
229232
require.Equal(t, codersdk.AgentSubsystemEnvbox, resources[0].Agents[0].Subsystems[0])
230233
require.Equal(t, codersdk.AgentSubsystemExectrace, resources[0].Agents[0].Subsystems[1])
231234
})
235+
t.Run("Header", func(t *testing.T) {
236+
t.Parallel()
237+
238+
var url string
239+
var called int64
240+
srv := httptest.NewServer(http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
241+
assert.Equal(t, "wow", r.Header.Get("X-Testing"))
242+
assert.Equal(t, "Ethan was Here!", r.Header.Get("Cool-Header"))
243+
assert.Equal(t, "very-wow-"+url, r.Header.Get("X-Process-Testing"))
244+
assert.Equal(t, "more-wow", r.Header.Get("X-Process-Testing2"))
245+
atomic.AddInt64(&called, 1)
246+
w.WriteHeader(http.StatusGone)
247+
}))
248+
defer srv.Close()
249+
url = srv.URL
250+
coderURLEnv := "$CODER_URL"
251+
if runtime.GOOS == "windows" {
252+
coderURLEnv = "%CODER_URL%"
253+
}
254+
255+
logDir := t.TempDir()
256+
inv, _ := clitest.New(t,
257+
"agent",
258+
"--auth", "token",
259+
"--agent-token", "fake-token",
260+
"--agent-url", srv.URL,
261+
"--log-dir", logDir,
262+
"--agent-header", "X-Testing=wow",
263+
"--agent-header", "Cool-Header=Ethan was Here!",
264+
"--agent-header-command", "printf X-Process-Testing=very-wow-"+coderURLEnv+"'\\r\\n'X-Process-Testing2=more-wow",
265+
)
266+
267+
clitest.Start(t, inv)
268+
require.Eventually(t, func() bool {
269+
return atomic.LoadInt64(&called) > 0
270+
}, testutil.WaitShort, testutil.IntervalFast)
271+
})
232272
}
233273

234274
func matchAgentWithVersion(rs []codersdk.WorkspaceResource) bool {

cli/cliui/agent.go

Lines changed: 65 additions & 18 deletions
Original file line numberDiff line numberDiff line change
@@ -351,53 +351,100 @@ func PeerDiagnostics(w io.Writer, d tailnet.PeerDiagnostics) {
351351
}
352352

353353
type ConnDiags struct {
354-
ConnInfo *workspacesdk.AgentConnectionInfo
354+
ConnInfo workspacesdk.AgentConnectionInfo
355355
PingP2P bool
356356
DisableDirect bool
357357
LocalNetInfo *tailcfg.NetInfo
358358
LocalInterfaces *healthsdk.InterfacesReport
359359
AgentNetcheck *healthsdk.AgentNetcheckReport
360+
ClientIPIsAWS bool
361+
AgentIPIsAWS bool
362+
Verbose bool
360363
// TODO: More diagnostics
361364
}
362365

363-
func ConnDiagnostics(w io.Writer, d ConnDiags) {
366+
func (d ConnDiags) Write(w io.Writer) {
367+
_, _ = fmt.Fprintln(w, "")
368+
general, client, agent := d.splitDiagnostics()
369+
for _, msg := range general {
370+
_, _ = fmt.Fprintln(w, msg)
371+
}
372+
if len(client) > 0 {
373+
_, _ = fmt.Fprint(w, "Possible client-side issues with direct connection:\n\n")
374+
for _, msg := range client {
375+
_, _ = fmt.Fprintf(w, " - %s\n\n", msg)
376+
}
377+
}
378+
if len(agent) > 0 {
379+
_, _ = fmt.Fprint(w, "Possible agent-side issues with direct connections:\n\n")
380+
for _, msg := range agent {
381+
_, _ = fmt.Fprintf(w, " - %s\n\n", msg)
382+
}
383+
}
384+
}
385+
386+
func (d ConnDiags) splitDiagnostics() (general, client, agent []string) {
387+
if d.PingP2P {
388+
general = append(general, "✔ You are connected directly (p2p)")
389+
} else {
390+
general = append(general, "❗ You are connected via a DERP relay, not directly (p2p)")
391+
}
392+
364393
if d.AgentNetcheck != nil {
365394
for _, msg := range d.AgentNetcheck.Interfaces.Warnings {
366-
_, _ = fmt.Fprintf(w, "❗ Agent: %s\n", msg.Message)
395+
agent = append(agent, msg.Message)
367396
}
368397
}
369398

370399
if d.LocalInterfaces != nil {
371400
for _, msg := range d.LocalInterfaces.Warnings {
372-
_, _ = fmt.Fprintf(w, "❗ Client: %s\n", msg.Message)
401+
client = append(client, msg.Message)
373402
}
374403
}
375404

376-
if d.PingP2P {
377-
_, _ = fmt.Fprint(w, "✔ You are connected directly (p2p)\n")
378-
return
405+
if d.PingP2P && !d.Verbose {
406+
return general, client, agent
379407
}
380-
_, _ = fmt.Fprint(w, "❗ You are connected via a DERP relay, not directly (p2p)\n")
381408

382409
if d.DisableDirect {
383-
_, _ = fmt.Fprint(w, "❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`\n")
384-
return
410+
general = append(general, "❗ Direct connections are disabled locally, by `--disable-direct` or `CODER_DISABLE_DIRECT`")
411+
if !d.Verbose {
412+
return general, client, agent
413+
}
385414
}
386415

387-
if d.ConnInfo != nil && d.ConnInfo.DisableDirectConnections {
388-
_, _ = fmt.Fprint(w, "❗ Your Coder administrator has blocked direct connections\n")
389-
return
416+
if d.ConnInfo.DisableDirectConnections {
417+
general = append(general, "❗ Your Coder administrator has blocked direct connections")
418+
if !d.Verbose {
419+
return general, client, agent
420+
}
390421
}
391422

392-
if d.ConnInfo != nil && d.ConnInfo.DERPMap != nil && !d.ConnInfo.DERPMap.HasSTUN() {
393-
_, _ = fmt.Fprint(w, "✘ The DERP map is not configured to use STUN, which will prevent direct connections from starting outside of local networks\n")
423+
if !d.ConnInfo.DERPMap.HasSTUN() {
424+
general = append(general, "The DERP map is not configured to use STUN")
425+
} else if d.LocalNetInfo != nil && !d.LocalNetInfo.UDP {
426+
client = append(client, "Client could not connect to STUN over UDP")
394427
}
395428

396429
if d.LocalNetInfo != nil && d.LocalNetInfo.MappingVariesByDestIP.EqualBool(true) {
397-
_, _ = fmt.Fprint(w, "❗ Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers\n")
430+
client = append(client, "Client is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers")
431+
}
432+
433+
if d.AgentNetcheck != nil && d.AgentNetcheck.NetInfo != nil {
434+
if d.AgentNetcheck.NetInfo.MappingVariesByDestIP.EqualBool(true) {
435+
agent = append(agent, "Agent is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers")
436+
}
437+
if !d.AgentNetcheck.NetInfo.UDP {
438+
agent = append(agent, "Agent could not connect to STUN over UDP")
439+
}
440+
}
441+
442+
if d.ClientIPIsAWS {
443+
client = append(client, "Client IP address is within an AWS range (AWS uses hard NAT)")
398444
}
399445

400-
if d.AgentNetcheck != nil && d.AgentNetcheck.NetInfo != nil && d.AgentNetcheck.NetInfo.MappingVariesByDestIP.EqualBool(true) {
401-
_, _ = fmt.Fprint(w, "Agent is potentially behind a hard NAT, as multiple endpoints were retrieved from different STUN servers\n")
446+
if d.AgentIPIsAWS {
447+
agent = append(agent, "Agent IP address is within an AWS range (AWS uses hard NAT)")
402448
}
449+
return general, client, agent
403450
}

0 commit comments

Comments
 (0)
pFad - Phonifier reborn

Pfad - The Proxy pFad of © 2024 Garber Painting. All rights reserved.

Note: This service is not intended for secure transactions such as banking, social media, email, or purchasing. Use at your own risk. We assume no liability whatsoever for broken pages.


Alternative Proxies:

Alternative Proxy

pFad Proxy

pFad v3 Proxy

pFad v4 Proxy