Skip to content

Commit 77bd93d

Browse files
committed
Merge branch 'main' into 9983-license-prometheus
2 parents 9ee99d5 + 19400d6 commit 77bd93d

File tree

261 files changed

+3657
-1947
lines changed

Some content is hidden

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

261 files changed

+3657
-1947
lines changed

.github/workflows/ci.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -136,7 +136,7 @@ jobs:
136136
137137
# Check for any typos
138138
- name: Check for typos
139-
uses: crate-ci/typos@v1.16.15
139+
uses: crate-ci/typos@v1.16.17
140140
with:
141141
config: .github/workflows/typos.toml
142142

.github/workflows/dogfood.yaml

Lines changed: 1 addition & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -38,7 +38,7 @@ jobs:
3838
echo "tag=${tag}" >> $GITHUB_OUTPUT
3939
4040
- name: Install Nix
41-
uses: DeterminateSystems/nix-installer-action@v4
41+
uses: DeterminateSystems/nix-installer-action@v5
4242

4343
- name: Run the Magic Nix Cache
4444
uses: DeterminateSystems/magic-nix-cache-action@v2

agent/agent.go

Lines changed: 17 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -833,7 +833,10 @@ func (a *agent) createTailnet(ctx context.Context, agentID uuid.UUID, derpMap *t
833833
}
834834
break
835835
}
836-
logger.Debug(ctx, "accepted conn", slog.F("remote", conn.RemoteAddr().String()))
836+
clog := logger.With(
837+
slog.F("remote", conn.RemoteAddr().String()),
838+
slog.F("local", conn.LocalAddr().String()))
839+
clog.Info(ctx, "accepted conn")
837840
wg.Add(1)
838841
closed := make(chan struct{})
839842
go func() {
@@ -865,7 +868,7 @@ func (a *agent) createTailnet(ctx context.Context, agentID uuid.UUID, derpMap *t
865868
logger.Warn(ctx, "failed to unmarshal init", slog.F("raw", data))
866869
return
867870
}
868-
_ = a.handleReconnectingPTY(ctx, logger, msg, conn)
871+
_ = a.handleReconnectingPTY(ctx, clog, msg, conn)
869872
}()
870873
}
871874
wg.Wait()
@@ -892,6 +895,10 @@ func (a *agent) createTailnet(ctx context.Context, agentID uuid.UUID, derpMap *t
892895
}
893896
break
894897
}
898+
clog := a.logger.Named("speedtest").With(
899+
slog.F("remote", conn.RemoteAddr().String()),
900+
slog.F("local", conn.LocalAddr().String()))
901+
clog.Info(ctx, "accepted conn")
895902
wg.Add(1)
896903
closed := make(chan struct{})
897904
go func() {
@@ -904,7 +911,12 @@ func (a *agent) createTailnet(ctx context.Context, agentID uuid.UUID, derpMap *t
904911
}()
905912
go func() {
906913
defer close(closed)
907-
_ = speedtest.ServeConn(conn)
914+
sErr := speedtest.ServeConn(conn)
915+
if sErr != nil {
916+
clog.Error(ctx, "test ended with error", slog.Error(sErr))
917+
return
918+
}
919+
clog.Info(ctx, "test ended")
908920
}()
909921
}
910922
wg.Wait()
@@ -1021,12 +1033,12 @@ func (a *agent) handleReconnectingPTY(ctx context.Context, logger slog.Logger, m
10211033
// If the agent is closed, we don't want to
10221034
// log this as an error since it's expected.
10231035
if closed {
1024-
connLogger.Debug(ctx, "reconnecting pty failed with attach error (agent closed)", slog.Error(err))
1036+
connLogger.Info(ctx, "reconnecting pty failed with attach error (agent closed)", slog.Error(err))
10251037
} else {
10261038
connLogger.Error(ctx, "reconnecting pty failed with attach error", slog.Error(err))
10271039
}
10281040
}
1029-
connLogger.Debug(ctx, "reconnecting pty connection closed")
1041+
connLogger.Info(ctx, "reconnecting pty connection closed")
10301042
}()
10311043

10321044
var rpty reconnectingpty.ReconnectingPTY

agent/agent_test.go

Lines changed: 5 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -356,7 +356,7 @@ func TestAgent_Session_TTY_MOTD(t *testing.T) {
356356
Enabled: true,
357357
Message: "\n\n\n\n\n\nbanner\n\n\n\n\n\n",
358358
},
359-
expectedRe: regexp.MustCompile("([^\n\r]|^)banner\r\n\r\n[^\r\n]"),
359+
expectedRe: regexp.MustCompile(`([^\n\r]|^)banner\r\n\r\n[^\r\n]`),
360360
},
361361
}
362362

@@ -1850,13 +1850,16 @@ func TestAgent_UpdatedDERP(t *testing.T) {
18501850
func TestAgent_Speedtest(t *testing.T) {
18511851
t.Parallel()
18521852
t.Skip("This test is relatively flakey because of Tailscale's speedtest code...")
1853+
logger := slogtest.Make(t, &slogtest.Options{IgnoreErrors: true}).Leveled(slog.LevelDebug)
18531854
ctx, cancel := context.WithTimeout(context.Background(), testutil.WaitLong)
18541855
defer cancel()
18551856
derpMap, _ := tailnettest.RunDERPAndSTUN(t)
18561857
//nolint:dogsled
18571858
conn, _, _, _, _ := setupAgent(t, agentsdk.Manifest{
18581859
DERPMap: derpMap,
1859-
}, 0)
1860+
}, 0, func(client *agenttest.Client, options *agent.Options) {
1861+
options.Logger = logger.Named("agent")
1862+
})
18601863
defer conn.Close()
18611864
res, err := conn.Speedtest(ctx, speedtest.Upload, 250*time.Millisecond)
18621865
require.NoError(t, err)

agent/agentssh/agentssh.go

Lines changed: 26 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -19,6 +19,7 @@ import (
1919
"time"
2020

2121
"github.com/gliderlabs/ssh"
22+
"github.com/kballard/go-shellquote"
2223
"github.com/pkg/sftp"
2324
"github.com/prometheus/client_golang/prometheus"
2425
"github.com/spf13/afero"
@@ -515,8 +516,32 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
515516
if runtime.GOOS == "windows" {
516517
caller = "/c"
517518
}
519+
name := shell
518520
args := []string{caller, script}
519521

522+
// A preceding space is generally not idiomatic for a shebang,
523+
// but in Terraform it's quite standard to use <<EOF for a multi-line
524+
// string which would indent with spaces, so we accept it for user-ease.
525+
if strings.HasPrefix(strings.TrimSpace(script), "#!") {
526+
// If the script starts with a shebang, we should
527+
// execute it directly. This is useful for running
528+
// scripts that aren't executable.
529+
shebang := strings.SplitN(strings.TrimSpace(script), "\n", 2)[0]
530+
shebang = strings.TrimSpace(shebang)
531+
shebang = strings.TrimPrefix(shebang, "#!")
532+
words, err := shellquote.Split(shebang)
533+
if err != nil {
534+
return nil, xerrors.Errorf("split shebang: %w", err)
535+
}
536+
name = words[0]
537+
if len(words) > 1 {
538+
args = words[1:]
539+
} else {
540+
args = []string{}
541+
}
542+
args = append(args, caller, script)
543+
}
544+
520545
// gliderlabs/ssh returns a command slice of zero
521546
// when a shell is requested.
522547
if len(script) == 0 {
@@ -528,7 +553,7 @@ func (s *Server) CreateCommand(ctx context.Context, script string, env []string)
528553
}
529554
}
530555

531-
cmd := pty.CommandContext(ctx, shell, args...)
556+
cmd := pty.CommandContext(ctx, name, args...)
532557
cmd.Dir = manifest.Directory
533558

534559
// If the metadata directory doesn't exist, we run the command

agent/agentssh/agentssh_test.go

Lines changed: 37 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,7 @@ import (
66
"bytes"
77
"context"
88
"net"
9+
"runtime"
910
"strings"
1011
"sync"
1112
"testing"
@@ -71,6 +72,42 @@ func TestNewServer_ServeClient(t *testing.T) {
7172
<-done
7273
}
7374

75+
func TestNewServer_ExecuteShebang(t *testing.T) {
76+
t.Parallel()
77+
if runtime.GOOS == "windows" {
78+
t.Skip("bash doesn't exist on Windows")
79+
}
80+
81+
ctx := context.Background()
82+
logger := slogtest.Make(t, nil)
83+
s, err := agentssh.NewServer(ctx, logger, prometheus.NewRegistry(), afero.NewMemMapFs(), 0, "")
84+
require.NoError(t, err)
85+
t.Cleanup(func() {
86+
_ = s.Close()
87+
})
88+
s.AgentToken = func() string { return "" }
89+
s.Manifest = atomic.NewPointer(&agentsdk.Manifest{})
90+
91+
t.Run("Basic", func(t *testing.T) {
92+
t.Parallel()
93+
cmd, err := s.CreateCommand(ctx, `#!/bin/bash
94+
echo test`, nil)
95+
require.NoError(t, err)
96+
output, err := cmd.AsExec().CombinedOutput()
97+
require.NoError(t, err)
98+
require.Equal(t, "test\n", string(output))
99+
})
100+
t.Run("Args", func(t *testing.T) {
101+
t.Parallel()
102+
cmd, err := s.CreateCommand(ctx, `#!/usr/bin/env bash
103+
echo test`, nil)
104+
require.NoError(t, err)
105+
output, err := cmd.AsExec().CombinedOutput()
106+
require.NoError(t, err)
107+
require.Equal(t, "test\n", string(output))
108+
})
109+
}
110+
74111
func TestNewServer_CloseActiveConnections(t *testing.T) {
75112
t.Parallel()
76113

cli/agent_test.go

Lines changed: 6 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -176,8 +176,9 @@ func TestWorkspaceAgent(t *testing.T) {
176176
GoogleTokenValidator: validator,
177177
IncludeProvisionerDaemon: true,
178178
})
179-
user := coderdtest.CreateFirstUser(t, client)
180-
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
179+
owner := coderdtest.CreateFirstUser(t, client)
180+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
181+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, &echo.Responses{
181182
Parse: echo.ParseComplete,
182183
ProvisionApply: []*proto.Response{{
183184
Type: &proto.Response_Apply{
@@ -195,14 +196,14 @@ func TestWorkspaceAgent(t *testing.T) {
195196
},
196197
}},
197198
})
198-
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
199+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
199200
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
200-
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
201+
workspace := coderdtest.CreateWorkspace(t, member, owner.OrganizationID, template.ID)
201202
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
202203

203204
inv, cfg := clitest.New(t, "agent", "--auth", "google-instance-identity", "--agent-url", client.URL.String())
204205
ptytest.New(t).Attach(inv)
205-
clitest.SetupConfig(t, client, cfg)
206+
clitest.SetupConfig(t, member, cfg)
206207
clitest.Start(t,
207208
inv.WithContext(
208209
//nolint:revive,staticcheck

cli/configssh_test.go

Lines changed: 12 additions & 10 deletions
Original file line numberDiff line numberDiff line change
@@ -75,9 +75,10 @@ func TestConfigSSH(t *testing.T) {
7575
},
7676
},
7777
})
78-
user := coderdtest.CreateFirstUser(t, client)
78+
owner := coderdtest.CreateFirstUser(t, client)
79+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
7980
authToken := uuid.NewString()
80-
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID, &echo.Responses{
81+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID, &echo.Responses{
8182
Parse: echo.ParseComplete,
8283
ProvisionPlan: []*proto.Response{{
8384
Type: &proto.Response_Plan{
@@ -96,8 +97,8 @@ func TestConfigSSH(t *testing.T) {
9697
ProvisionApply: echo.ProvisionApplyWithAgent(authToken),
9798
})
9899
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
99-
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
100-
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
100+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
101+
workspace := coderdtest.CreateWorkspace(t, member, owner.OrganizationID, template.ID)
101102
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
102103
_ = agenttest.New(t, client.URL, authToken)
103104
resources := coderdtest.AwaitWorkspaceAgents(t, client, workspace.ID)
@@ -145,7 +146,7 @@ func TestConfigSSH(t *testing.T) {
145146
"--ssh-option", "Port "+strconv.Itoa(tcpAddr.Port),
146147
"--ssh-config-file", sshConfigFile,
147148
"--skip-proxy-command")
148-
clitest.SetupConfig(t, client, root)
149+
clitest.SetupConfig(t, member, root)
149150
pty := ptytest.New(t)
150151
inv.Stdin = pty.Input()
151152
inv.Stdout = pty.Output()
@@ -710,19 +711,20 @@ func TestConfigSSH_Hostnames(t *testing.T) {
710711
}
711712

712713
client := coderdtest.New(t, &coderdtest.Options{IncludeProvisionerDaemon: true})
713-
user := coderdtest.CreateFirstUser(t, client)
714+
owner := coderdtest.CreateFirstUser(t, client)
715+
member, _ := coderdtest.CreateAnotherUser(t, client, owner.OrganizationID)
714716
// authToken := uuid.NewString()
715-
version := coderdtest.CreateTemplateVersion(t, client, user.OrganizationID,
717+
version := coderdtest.CreateTemplateVersion(t, client, owner.OrganizationID,
716718
echo.WithResources(resources))
717719
coderdtest.AwaitTemplateVersionJobCompleted(t, client, version.ID)
718-
template := coderdtest.CreateTemplate(t, client, user.OrganizationID, version.ID)
719-
workspace := coderdtest.CreateWorkspace(t, client, user.OrganizationID, template.ID)
720+
template := coderdtest.CreateTemplate(t, client, owner.OrganizationID, version.ID)
721+
workspace := coderdtest.CreateWorkspace(t, member, owner.OrganizationID, template.ID)
720722
coderdtest.AwaitWorkspaceBuildJobCompleted(t, client, workspace.LatestBuild.ID)
721723

722724
sshConfigFile := sshConfigFileName(t)
723725

724726
inv, root := clitest.New(t, "config-ssh", "--ssh-config-file", sshConfigFile)
725-
clitest.SetupConfig(t, client, root)
727+
clitest.SetupConfig(t, member, root)
726728

727729
pty := ptytest.New(t)
728730
inv.Stdin = pty.Input()

0 commit comments

Comments
 (0)