Skip to content

Commit 730a390

Browse files
committed
Merge branch 'main' into add_expiration_warning
2 parents 827e8e0 + 218d6a9 commit 730a390

File tree

105 files changed

+4474
-1072
lines changed

Some content is hidden

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

105 files changed

+4474
-1072
lines changed

.github/workflows/ci.yaml

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -152,6 +152,7 @@ jobs:
152152

153153
- uses: actions/setup-go@v4
154154
with:
155+
cache: false
155156
go-version: "~1.20"
156157

157158
- name: Echo Go Cache Paths
@@ -252,6 +253,7 @@ jobs:
252253

253254
- uses: actions/setup-go@v4
254255
with:
256+
cache: false
255257
go-version: "~1.20"
256258

257259
- name: Echo Go Cache Paths
@@ -339,6 +341,7 @@ jobs:
339341

340342
- uses: actions/setup-go@v4
341343
with:
344+
cache: false
342345
go-version: "~1.20"
343346

344347
- name: Echo Go Cache Paths
@@ -429,6 +432,7 @@ jobs:
429432

430433
- uses: actions/setup-go@v4
431434
with:
435+
cache: false
432436
go-version: "~1.20"
433437

434438
- name: Echo Go Cache Paths
@@ -558,6 +562,7 @@ jobs:
558562

559563
- uses: actions/setup-go@v4
560564
with:
565+
cache: false
561566
go-version: "~1.20"
562567

563568
- uses: hashicorp/setup-terraform@v2

agent/agent.go

Lines changed: 7 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -161,7 +161,7 @@ type agent struct {
161161
}
162162

163163
func (a *agent) init(ctx context.Context) {
164-
sshSrv, err := agentssh.NewServer(ctx, a.logger.Named("ssh-server"), a.sshMaxTimeout)
164+
sshSrv, err := agentssh.NewServer(ctx, a.logger.Named("ssh-server"), a.filesystem, a.sshMaxTimeout, "")
165165
if err != nil {
166166
panic(err)
167167
}
@@ -1036,16 +1036,13 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, logger slog.Logger, m
10361036
<-ctx.Done()
10371037
_ = process.Kill()
10381038
}()
1039-
go func() {
1040-
// If the process dies randomly, we should
1041-
// close the pty.
1042-
_ = process.Wait()
1043-
rpty.Close()
1044-
}()
1039+
// We don't need to separately monitor for the process exiting.
1040+
// When it exits, our ptty.OutputReader() will return EOF after
1041+
// reading all process output.
10451042
if err = a.trackConnGoroutine(func() {
10461043
buffer := make([]byte, 1024)
10471044
for {
1048-
read, err := rpty.ptty.Output().Read(buffer)
1045+
read, err := rpty.ptty.OutputReader().Read(buffer)
10491046
if err != nil {
10501047
// When the PTY is closed, this is triggered.
10511048
break
@@ -1138,7 +1135,7 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, logger slog.Logger, m
11381135
logger.Warn(ctx, "read conn", slog.Error(err))
11391136
return nil
11401137
}
1141-
_, err = rpty.ptty.Input().Write([]byte(req.Data))
1138+
_, err = rpty.ptty.InputWriter().Write([]byte(req.Data))
11421139
if err != nil {
11431140
logger.Warn(ctx, "write to pty", slog.Error(err))
11441141
return nil
@@ -1358,7 +1355,7 @@ type reconnectingPTY struct {
13581355
circularBuffer *circbuf.Buffer
13591356
circularBufferMutex sync.RWMutex
13601357
timeout *time.Timer
1361-
ptty pty.PTY
1358+
ptty pty.PTYCmd
13621359
}
13631360

13641361
// Close ends all connections to the reconnecting

agent/agent_test.go

Lines changed: 23 additions & 44 deletions
Original file line numberDiff line numberDiff line change
@@ -45,6 +45,7 @@ import (
4545
"github.com/coder/coder/coderd/httpapi"
4646
"github.com/coder/coder/codersdk"
4747
"github.com/coder/coder/codersdk/agentsdk"
48+
"github.com/coder/coder/pty"
4849
"github.com/coder/coder/pty/ptytest"
4950
"github.com/coder/coder/tailnet"
5051
"github.com/coder/coder/tailnet/tailnettest"
@@ -481,17 +482,10 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
481482
}
482483
}()
483484

484-
pty := ptytest.New(t)
485-
486-
cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%d:127.0.0.1:%d", randomPort, remotePort)}, []string{"sleep", "5"})
487-
cmd.Stdin = pty.Input()
488-
cmd.Stdout = pty.Output()
489-
cmd.Stderr = pty.Output()
490-
err = cmd.Start()
491-
require.NoError(t, err)
485+
_, proc := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%d:127.0.0.1:%d", randomPort, remotePort)}, []string{"sleep", "5"})
492486

493487
go func() {
494-
err := cmd.Wait()
488+
err := proc.Wait()
495489
select {
496490
case <-done:
497491
default:
@@ -523,7 +517,7 @@ func TestAgent_TCPLocalForwarding(t *testing.T) {
523517

524518
<-done
525519

526-
_ = cmd.Process.Kill()
520+
_ = proc.Kill()
527521
}
528522

529523
//nolint:paralleltest // This test reserves a port.
@@ -562,17 +556,10 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
562556
}
563557
}()
564558

565-
pty := ptytest.New(t)
566-
567-
cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("127.0.0.1:%d:127.0.0.1:%d", randomPort, localPort)}, []string{"sleep", "5"})
568-
cmd.Stdin = pty.Input()
569-
cmd.Stdout = pty.Output()
570-
cmd.Stderr = pty.Output()
571-
err = cmd.Start()
572-
require.NoError(t, err)
559+
_, proc := setupSSHCommand(t, []string{"-R", fmt.Sprintf("127.0.0.1:%d:127.0.0.1:%d", randomPort, localPort)}, []string{"sleep", "5"})
573560

574561
go func() {
575-
err := cmd.Wait()
562+
err := proc.Wait()
576563
select {
577564
case <-done:
578565
default:
@@ -604,7 +591,7 @@ func TestAgent_TCPRemoteForwarding(t *testing.T) {
604591

605592
<-done
606593

607-
_ = cmd.Process.Kill()
594+
_ = proc.Kill()
608595
}
609596

610597
func TestAgent_UnixLocalForwarding(t *testing.T) {
@@ -641,17 +628,10 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
641628
}
642629
}()
643630

644-
pty := ptytest.New(t)
645-
646-
cmd := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%s:%s", localSocketPath, remoteSocketPath)}, []string{"sleep", "5"})
647-
cmd.Stdin = pty.Input()
648-
cmd.Stdout = pty.Output()
649-
cmd.Stderr = pty.Output()
650-
err = cmd.Start()
651-
require.NoError(t, err)
631+
_, proc := setupSSHCommand(t, []string{"-L", fmt.Sprintf("%s:%s", localSocketPath, remoteSocketPath)}, []string{"sleep", "5"})
652632

653633
go func() {
654-
err := cmd.Wait()
634+
err := proc.Wait()
655635
select {
656636
case <-done:
657637
default:
@@ -676,7 +656,7 @@ func TestAgent_UnixLocalForwarding(t *testing.T) {
676656
_ = conn.Close()
677657
<-done
678658

679-
_ = cmd.Process.Kill()
659+
_ = proc.Kill()
680660
}
681661

682662
func TestAgent_UnixRemoteForwarding(t *testing.T) {
@@ -713,17 +693,10 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
713693
}
714694
}()
715695

716-
pty := ptytest.New(t)
717-
718-
cmd := setupSSHCommand(t, []string{"-R", fmt.Sprintf("%s:%s", remoteSocketPath, localSocketPath)}, []string{"sleep", "5"})
719-
cmd.Stdin = pty.Input()
720-
cmd.Stdout = pty.Output()
721-
cmd.Stderr = pty.Output()
722-
err = cmd.Start()
723-
require.NoError(t, err)
696+
_, proc := setupSSHCommand(t, []string{"-R", fmt.Sprintf("%s:%s", remoteSocketPath, localSocketPath)}, []string{"sleep", "5"})
724697

725698
go func() {
726-
err := cmd.Wait()
699+
err := proc.Wait()
727700
select {
728701
case <-done:
729702
default:
@@ -733,12 +706,15 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
733706

734707
// It's possible that the socket is created but the server is not ready to
735708
// accept connections yet. We need to retry until we can connect.
709+
//
710+
// Note that we wait long here because if the tailnet connection has trouble
711+
// connecting, it could take 5 seconds or more to reconnect.
736712
var conn net.Conn
737713
require.Eventually(t, func() bool {
738714
var err error
739715
conn, err = net.Dial("unix", remoteSocketPath)
740716
return err == nil
741-
}, testutil.WaitShort, testutil.IntervalFast)
717+
}, testutil.WaitLong, testutil.IntervalFast)
742718
defer conn.Close()
743719
_, err = conn.Write([]byte("test"))
744720
require.NoError(t, err)
@@ -750,7 +726,7 @@ func TestAgent_UnixRemoteForwarding(t *testing.T) {
750726

751727
<-done
752728

753-
_ = cmd.Process.Kill()
729+
_ = proc.Kill()
754730
}
755731

756732
func TestAgent_SFTP(t *testing.T) {
@@ -1645,7 +1621,7 @@ func TestAgent_WriteVSCodeConfigs(t *testing.T) {
16451621
}, testutil.WaitShort, testutil.IntervalFast)
16461622
}
16471623

1648-
func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exec.Cmd {
1624+
func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) (*ptytest.PTYCmd, pty.Process) {
16491625
//nolint:dogsled
16501626
agentConn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{}, 0)
16511627
listener, err := net.Listen("tcp", "127.0.0.1:0")
@@ -1687,7 +1663,8 @@ func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exe
16871663
"host",
16881664
)
16891665
args = append(args, afterArgs...)
1690-
return exec.Command("ssh", args...)
1666+
cmd := exec.Command("ssh", args...)
1667+
return ptytest.Start(t, cmd)
16911668
}
16921669

16931670
func setupSSHSession(t *testing.T, options agentsdk.Manifest) *ssh.Session {
@@ -1775,7 +1752,9 @@ func setupAgent(t *testing.T, metadata agentsdk.Manifest, ptyTimeout time.Durati
17751752
t.Cleanup(func() {
17761753
_ = agentConn.Close()
17771754
})
1778-
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitMedium)
1755+
// Ideally we wouldn't wait too long here, but sometimes the the
1756+
// networking needs more time to resolve itself.
1757+
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
17791758
defer cancel()
17801759
if !agentConn.AwaitReachable(ctx) {
17811760
t.Fatal("agent not reachable")

0 commit comments

Comments
 (0)