Skip to content

Commit 03e6e62

Browse files
committed
Write VS Code special configuration if providers exist
1 parent 40b874d commit 03e6e62

File tree

4 files changed

+53
-0
lines changed

4 files changed

+53
-0
lines changed

agent/agent.go

Lines changed: 15 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -26,6 +26,7 @@ import (
2626
"github.com/gliderlabs/ssh"
2727
"github.com/google/uuid"
2828
"github.com/pkg/sftp"
29+
"github.com/spf13/afero"
2930
"go.uber.org/atomic"
3031
gossh "golang.org/x/crypto/ssh"
3132
"golang.org/x/xerrors"
@@ -35,6 +36,7 @@ import (
3536
"cdr.dev/slog"
3637
"github.com/coder/coder/agent/usershell"
3738
"github.com/coder/coder/buildinfo"
39+
"github.com/coder/coder/coderd/gitauth"
3840
"github.com/coder/coder/codersdk"
3941
"github.com/coder/coder/pty"
4042
"github.com/coder/coder/tailnet"
@@ -53,6 +55,7 @@ const (
5355
)
5456

5557
type Options struct {
58+
Filesystem afero.Fs
5659
ExchangeToken func(ctx context.Context) error
5760
Client Client
5861
ReconnectingPTYTimeout time.Duration
@@ -72,6 +75,9 @@ func New(options Options) io.Closer {
7275
if options.ReconnectingPTYTimeout == 0 {
7376
options.ReconnectingPTYTimeout = 5 * time.Minute
7477
}
78+
if options.Filesystem == nil {
79+
options.Filesystem = afero.NewOsFs()
80+
}
7581
ctx, cancelFunc := context.WithCancel(context.Background())
7682
server := &agent{
7783
reconnectingPTYTimeout: options.ReconnectingPTYTimeout,
@@ -81,6 +87,7 @@ func New(options Options) io.Closer {
8187
envVars: options.EnvironmentVariables,
8288
client: options.Client,
8389
exchangeToken: options.ExchangeToken,
90+
filesystem: options.Filesystem,
8491
stats: &Stats{},
8592
}
8693
server.init(ctx)
@@ -91,6 +98,7 @@ type agent struct {
9198
logger slog.Logger
9299
client Client
93100
exchangeToken func(ctx context.Context) error
101+
filesystem afero.Fs
94102

95103
reconnectingPTYs sync.Map
96104
reconnectingPTYTimeout time.Duration
@@ -171,6 +179,13 @@ func (a *agent) run(ctx context.Context) error {
171179
}()
172180
}
173181

182+
if metadata.GitAuthConfigs > 0 {
183+
err = gitauth.OverrideVSCodeConfigs(a.filesystem)
184+
if err != nil {
185+
return xerrors.Errorf("override vscode configuration for git auth: %w", err)
186+
}
187+
}
188+
174189
// This automatically closes when the context ends!
175190
appReporterCtx, appReporterCtxCancel := context.WithCancel(ctx)
176191
defer appReporterCtxCancel()

agent/agent_test.go

Lines changed: 33 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -27,6 +27,7 @@ import (
2727
"github.com/google/uuid"
2828
"github.com/pion/udp"
2929
"github.com/pkg/sftp"
30+
"github.com/spf13/afero"
3031
"github.com/stretchr/testify/assert"
3132
"github.com/stretchr/testify/require"
3233
"go.uber.org/goleak"
@@ -543,6 +544,38 @@ func TestAgent(t *testing.T) {
543544
return initialized.Load() == 2
544545
}, testutil.WaitShort, testutil.IntervalFast)
545546
})
547+
548+
t.Run("WriteVSCodeConfigs", func(t *testing.T) {
549+
t.Parallel()
550+
client := &client{
551+
t: t,
552+
agentID: uuid.New(),
553+
metadata: codersdk.WorkspaceAgentMetadata{
554+
GitAuthConfigs: 1,
555+
},
556+
statsChan: make(chan *codersdk.AgentStats),
557+
coordinator: tailnet.NewCoordinator(),
558+
}
559+
filesystem := afero.NewMemMapFs()
560+
closer := agent.New(agent.Options{
561+
ExchangeToken: func(ctx context.Context) error {
562+
return nil
563+
},
564+
Client: client,
565+
Logger: slogtest.Make(t, nil).Leveled(slog.LevelInfo),
566+
Filesystem: filesystem,
567+
})
568+
t.Cleanup(func() {
569+
_ = closer.Close()
570+
})
571+
home, err := os.UserHomeDir()
572+
require.NoError(t, err)
573+
path := filepath.Join(home, ".vscode-server", "data", "Machine", "settings.json")
574+
require.Eventually(t, func() bool {
575+
_, err := filesystem.Stat(path)
576+
return err == nil
577+
}, testutil.WaitShort, testutil.IntervalFast)
578+
})
546579
}
547580

548581
func setupSSHCommand(t *testing.T, beforeArgs []string, afterArgs []string) *exec.Cmd {

coderd/workspaceagents.go

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -87,6 +87,7 @@ func (api *API) workspaceAgentMetadata(rw http.ResponseWriter, r *http.Request)
8787
httpapi.Write(ctx, rw, http.StatusOK, codersdk.WorkspaceAgentMetadata{
8888
Apps: convertApps(dbApps),
8989
DERPMap: api.DERPMap,
90+
GitAuthConfigs: len(api.GitAuthConfigs),
9091
EnvironmentVariables: apiAgent.EnvironmentVariables,
9192
StartupScript: apiAgent.StartupScript,
9293
Directory: apiAgent.Directory,

codersdk/workspaceagents.go

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -119,6 +119,10 @@ type PostWorkspaceAgentVersionRequest struct {
119119

120120
// @typescript-ignore WorkspaceAgentMetadata
121121
type WorkspaceAgentMetadata struct {
122+
// GitAuthConfigs stores the number of Git configurations
123+
// the Coder deployment has. If this number is >0, we
124+
// set up special configuration in the workspace.
125+
GitAuthConfigs int `json:"git_auth_configs"`
122126
Apps []WorkspaceApp `json:"apps"`
123127
DERPMap *tailcfg.DERPMap `json:"derpmap"`
124128
EnvironmentVariables map[string]string `json:"environment_variables"`

0 commit comments

Comments
 (0)