@@ -15,7 +15,6 @@ import (
15
15
"strings"
16
16
"time"
17
17
18
- "cloud.google.com/go/compute/metadata"
19
18
"golang.org/x/xerrors"
20
19
"gopkg.in/natefinch/lumberjack.v2"
21
20
@@ -40,7 +39,6 @@ import (
40
39
41
40
func (r * RootCmd ) workspaceAgent () * serpent.Command {
42
41
var (
43
- auth string
44
42
logDir string
45
43
scriptDataDir string
46
44
pprofAddress string
@@ -177,11 +175,10 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
177
175
version := buildinfo .Version ()
178
176
logger .Info (ctx , "agent is starting now" ,
179
177
slog .F ("url" , r .agentURL ),
180
- slog .F ("auth" , auth ),
178
+ slog .F ("auth" , r . agentAuth ),
181
179
slog .F ("version" , version ),
182
180
)
183
-
184
- client := agentsdk .New (r .agentURL )
181
+ client , err := r .createAgentClient (ctx )
185
182
client .SDK .SetLogger (logger )
186
183
// Set a reasonable timeout so requests can't hang forever!
187
184
// The timeout needs to be reasonably long, because requests
@@ -214,68 +211,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
214
211
ignorePorts [port ] = "debug"
215
212
}
216
213
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
-
279
214
executablePath , err := os .Executable ()
280
215
if err != nil {
281
216
return xerrors .Errorf ("getting os executable: %w" , err )
@@ -343,18 +278,7 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
343
278
LogDir : logDir ,
344
279
ScriptDataDir : scriptDataDir ,
345
280
// #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 ),
358
282
EnvironmentVariables : environmentVariables ,
359
283
IgnorePorts : ignorePorts ,
360
284
SSHMaxTimeout : sshMaxTimeout ,
@@ -400,13 +324,6 @@ func (r *RootCmd) workspaceAgent() *serpent.Command {
400
324
}
401
325
402
326
cmd .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
- },
410
327
{
411
328
Flag : "log-dir" ,
412
329
Default : os .TempDir (),
0 commit comments