@@ -15,7 +15,6 @@ import (
1515"strings"
1616"time"
1717
18- "cloud.google.com/go/compute/metadata"
1918"golang.org/x/xerrors"
2019"gopkg.in/natefinch/lumberjack.v2"
2120
@@ -40,7 +39,6 @@ import (
4039
4140func (r * RootCmd )workspaceAgent ()* serpent.Command {
4241var (
43- auth string
4442logDir string
4543scriptDataDir string
4644pprofAddress string
@@ -177,11 +175,10 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
177175version := buildinfo .Version ()
178176logger .Info (ctx ,"agent is starting now" ,
179177slog .F ("url" ,r .agentURL ),
180- slog .F ("auth" ,auth ),
178+ slog .F ("auth" ,r . agentAuth ),
181179slog .F ("version" ,version ),
182180)
183-
184- client := agentsdk .New (r .agentURL )
181+ client ,err := r .createAgentClient (ctx )
185182client .SDK .SetLogger (logger )
186183// Set a reasonable timeout so requests can't hang forever!
187184// The timeout needs to be reasonably long, because requests
@@ -214,68 +211,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
214211ignorePorts [port ]= "debug"
215212}
216213
217- // exchangeToken returns a session token.
218- // This is abstracted to allow for the same looping condition
219- // regardless of instance identity auth type.
220- var exchangeToken func (context.Context ) (agentsdk.AuthenticateResponse ,error )
221- switch auth {
222- case "token" :
223- token ,_ := inv .ParsedFlags ().GetString (varAgentToken )
224- if token == "" {
225- tokenFile ,_ := inv .ParsedFlags ().GetString (varAgentTokenFile )
226- if tokenFile != "" {
227- tokenBytes ,err := os .ReadFile (tokenFile )
228- if err != nil {
229- return xerrors .Errorf ("read token file %q: %w" ,tokenFile ,err )
230- }
231- token = strings .TrimSpace (string (tokenBytes ))
232- }
233- }
234- if token == "" {
235- return xerrors .Errorf ("CODER_AGENT_TOKEN or CODER_AGENT_TOKEN_FILE must be set for token auth" )
236- }
237- client .SetSessionToken (token )
238- case "google-instance-identity" :
239- // This is *only* done for testing to mock client authentication.
240- // This will never be set in a production scenario.
241- var gcpClient * metadata.Client
242- gcpClientRaw := ctx .Value ("gcp-client" )
243- if gcpClientRaw != nil {
244- gcpClient ,_ = gcpClientRaw .(* metadata.Client )
245- }
246- exchangeToken = func (ctx context.Context ) (agentsdk.AuthenticateResponse ,error ) {
247- return client .AuthGoogleInstanceIdentity (ctx ,"" ,gcpClient )
248- }
249- case "aws-instance-identity" :
250- // This is *only* done for testing to mock client authentication.
251- // This will never be set in a production scenario.
252- var awsClient * http.Client
253- awsClientRaw := ctx .Value ("aws-client" )
254- if awsClientRaw != nil {
255- awsClient ,_ = awsClientRaw .(* http.Client )
256- if awsClient != nil {
257- client .SDK .HTTPClient = awsClient
258- }
259- }
260- exchangeToken = func (ctx context.Context ) (agentsdk.AuthenticateResponse ,error ) {
261- return client .AuthAWSInstanceIdentity (ctx )
262- }
263- case "azure-instance-identity" :
264- // This is *only* done for testing to mock client authentication.
265- // This will never be set in a production scenario.
266- var azureClient * http.Client
267- azureClientRaw := ctx .Value ("azure-client" )
268- if azureClientRaw != nil {
269- azureClient ,_ = azureClientRaw .(* http.Client )
270- if azureClient != nil {
271- client .SDK .HTTPClient = azureClient
272- }
273- }
274- exchangeToken = func (ctx context.Context ) (agentsdk.AuthenticateResponse ,error ) {
275- return client .AuthAzureInstanceIdentity (ctx )
276- }
277- }
278-
279214executablePath ,err := os .Executable ()
280215if err != nil {
281216return xerrors .Errorf ("getting os executable: %w" ,err )
@@ -343,18 +278,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
343278LogDir :logDir ,
344279ScriptDataDir :scriptDataDir ,
345280// #nosec G115 - Safe conversion as tailnet listen port is within uint16 range (0-65535)
346- TailnetListenPort :uint16 (tailnetListenPort ),
347- ExchangeToken :func (ctx context.Context ) (string ,error ) {
348- if exchangeToken == nil {
349- return client .SDK .SessionToken (),nil
350- }
351- resp ,err := exchangeToken (ctx )
352- if err != nil {
353- return "" ,err
354- }
355- client .SetSessionToken (resp .SessionToken )
356- return resp .SessionToken ,nil
357- },
281+ TailnetListenPort :uint16 (tailnetListenPort ),
358282EnvironmentVariables :environmentVariables ,
359283IgnorePorts :ignorePorts ,
360284SSHMaxTimeout :sshMaxTimeout ,
@@ -400,13 +324,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
400324}
401325
402326cmd .Options = serpent.OptionSet {
403- {
404- Flag :"auth" ,
405- Default :"token" ,
406- Description :"Specify the authentication type to use for the agent." ,
407- Env :"CODER_AGENT_AUTH" ,
408- Value :serpent .StringOf (& auth ),
409- },
410327{
411328Flag :"log-dir" ,
412329Default :os .TempDir (),