2022-03-07 17:40:54 +00:00
|
|
|
package cli
|
|
|
|
|
|
|
|
import (
|
2022-03-25 16:34:45 +00:00
|
|
|
"context"
|
2022-03-28 19:31:03 +00:00
|
|
|
"net/http"
|
2022-03-07 17:40:54 +00:00
|
|
|
"net/url"
|
2022-03-25 16:34:45 +00:00
|
|
|
"time"
|
2022-03-07 17:40:54 +00:00
|
|
|
|
2022-03-25 19:48:08 +00:00
|
|
|
"cloud.google.com/go/compute/metadata"
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/spf13/cobra"
|
|
|
|
"golang.org/x/xerrors"
|
|
|
|
|
2022-03-22 19:17:50 +00:00
|
|
|
"cdr.dev/slog"
|
|
|
|
"cdr.dev/slog/sloggers/sloghuman"
|
|
|
|
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/coder/coder/agent"
|
2022-03-28 19:26:41 +00:00
|
|
|
"github.com/coder/coder/cli/cliflag"
|
2022-03-07 17:40:54 +00:00
|
|
|
"github.com/coder/coder/codersdk"
|
2022-03-25 16:34:45 +00:00
|
|
|
"github.com/coder/retry"
|
2022-03-07 17:40:54 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
func workspaceAgent() *cobra.Command {
|
2022-03-25 19:48:08 +00:00
|
|
|
var (
|
|
|
|
rawURL string
|
|
|
|
auth string
|
2022-03-28 19:26:41 +00:00
|
|
|
token string
|
2022-03-25 19:48:08 +00:00
|
|
|
)
|
|
|
|
cmd := &cobra.Command{
|
2022-03-07 17:40:54 +00:00
|
|
|
Use: "agent",
|
2022-03-25 16:34:45 +00:00
|
|
|
// This command isn't useful to manually execute.
|
2022-03-07 17:40:54 +00:00
|
|
|
Hidden: true,
|
|
|
|
RunE: func(cmd *cobra.Command, args []string) error {
|
2022-03-25 19:48:08 +00:00
|
|
|
if rawURL == "" {
|
2022-03-07 17:40:54 +00:00
|
|
|
return xerrors.New("CODER_URL must be set")
|
|
|
|
}
|
2022-03-25 19:48:08 +00:00
|
|
|
coderURL, err := url.Parse(rawURL)
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-03-25 19:48:08 +00:00
|
|
|
return xerrors.Errorf("parse %q: %w", rawURL, err)
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
2022-03-25 19:48:08 +00:00
|
|
|
logger := slog.Make(sloghuman.Sink(cmd.OutOrStdout())).Leveled(slog.LevelDebug)
|
2022-03-07 17:40:54 +00:00
|
|
|
client := codersdk.New(coderURL)
|
2022-03-28 19:31:03 +00:00
|
|
|
|
|
|
|
// exchangeToken returns a session token.
|
|
|
|
// This is abstracted to allow for the same looping condition
|
|
|
|
// regardless of instance identity auth type.
|
|
|
|
var exchangeToken func(context.Context) (codersdk.WorkspaceAgentAuthenticateResponse, error)
|
2022-03-25 16:34:45 +00:00
|
|
|
switch auth {
|
|
|
|
case "token":
|
2022-03-28 19:26:41 +00:00
|
|
|
if token == "" {
|
2022-03-25 16:34:45 +00:00
|
|
|
return xerrors.Errorf("CODER_TOKEN must be set for token auth")
|
|
|
|
}
|
2022-03-28 19:26:41 +00:00
|
|
|
client.SessionToken = token
|
2022-03-25 16:34:45 +00:00
|
|
|
case "google-instance-identity":
|
2022-03-25 19:48:08 +00:00
|
|
|
// This is *only* done for testing to mock client authentication.
|
|
|
|
// This will never be set in a production scenario.
|
|
|
|
var gcpClient *metadata.Client
|
|
|
|
gcpClientRaw := cmd.Context().Value("gcp-client")
|
|
|
|
if gcpClientRaw != nil {
|
|
|
|
gcpClient, _ = gcpClientRaw.(*metadata.Client)
|
|
|
|
}
|
2022-03-28 19:31:03 +00:00
|
|
|
exchangeToken = func(ctx context.Context) (codersdk.WorkspaceAgentAuthenticateResponse, error) {
|
|
|
|
return client.AuthWorkspaceGoogleInstanceIdentity(ctx, "", gcpClient)
|
|
|
|
}
|
|
|
|
case "aws-instance-identity":
|
|
|
|
// This is *only* done for testing to mock client authentication.
|
|
|
|
// This will never be set in a production scenario.
|
|
|
|
var awsClient *http.Client
|
|
|
|
awsClientRaw := cmd.Context().Value("aws-client")
|
|
|
|
if awsClientRaw != nil {
|
|
|
|
awsClient, _ = awsClientRaw.(*http.Client)
|
|
|
|
if awsClient != nil {
|
|
|
|
client.HTTPClient = awsClient
|
|
|
|
}
|
|
|
|
}
|
|
|
|
exchangeToken = func(ctx context.Context) (codersdk.WorkspaceAgentAuthenticateResponse, error) {
|
|
|
|
return client.AuthWorkspaceAWSInstanceIdentity(ctx)
|
|
|
|
}
|
|
|
|
case "azure-instance-identity":
|
|
|
|
return xerrors.Errorf("not implemented")
|
|
|
|
}
|
2022-03-25 19:48:08 +00:00
|
|
|
|
2022-03-28 19:31:03 +00:00
|
|
|
if exchangeToken != nil {
|
|
|
|
// Agent's can start before resources are returned from the provisioner
|
|
|
|
// daemon. If there are many resources being provisioned, this time
|
|
|
|
// could be significant. This is arbitrarily set at an hour to prevent
|
|
|
|
// tons of idle agents from pinging coderd.
|
|
|
|
ctx, cancelFunc := context.WithTimeout(cmd.Context(), time.Hour)
|
2022-03-25 16:34:45 +00:00
|
|
|
defer cancelFunc()
|
|
|
|
for retry.New(100*time.Millisecond, 5*time.Second).Wait(ctx) {
|
|
|
|
var response codersdk.WorkspaceAgentAuthenticateResponse
|
2022-03-25 19:48:08 +00:00
|
|
|
|
2022-03-28 19:31:03 +00:00
|
|
|
response, err = exchangeToken(ctx)
|
2022-03-25 16:34:45 +00:00
|
|
|
if err != nil {
|
2022-03-28 19:31:03 +00:00
|
|
|
logger.Warn(ctx, "authenticate workspace", slog.F("method", auth), slog.Error(err))
|
2022-03-25 16:34:45 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
client.SessionToken = response.SessionToken
|
2022-03-28 19:31:03 +00:00
|
|
|
logger.Info(ctx, "authenticated", slog.F("method", auth))
|
2022-03-25 16:34:45 +00:00
|
|
|
break
|
|
|
|
}
|
2022-03-07 17:40:54 +00:00
|
|
|
if err != nil {
|
2022-03-25 16:34:45 +00:00
|
|
|
return xerrors.Errorf("agent failed to authenticate in time: %w", err)
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|
|
|
|
}
|
2022-03-28 19:31:03 +00:00
|
|
|
|
2022-04-07 22:40:27 +00:00
|
|
|
cfg := createConfig(cmd)
|
|
|
|
err = cfg.AgentSession().Write(client.SessionToken)
|
|
|
|
if err != nil {
|
|
|
|
return xerrors.Errorf("writing agent session token to config: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-04-18 22:40:25 +00:00
|
|
|
closer := agent.New(client.ListenWorkspaceAgent, logger)
|
2022-03-07 17:40:54 +00:00
|
|
|
<-cmd.Context().Done()
|
|
|
|
return closer.Close()
|
|
|
|
},
|
|
|
|
}
|
2022-03-28 19:26:41 +00:00
|
|
|
|
|
|
|
cliflag.StringVarP(cmd.Flags(), &auth, "auth", "", "CODER_AUTH", "token", "Specify the authentication type to use for the agent")
|
|
|
|
cliflag.StringVarP(cmd.Flags(), &rawURL, "url", "", "CODER_URL", "", "Specify the URL to access Coder")
|
2022-03-29 00:19:28 +00:00
|
|
|
cliflag.StringVarP(cmd.Flags(), &token, "token", "", "CODER_TOKEN", "", "Specifies the authentication token to access Coder")
|
2022-03-25 19:48:08 +00:00
|
|
|
|
|
|
|
return cmd
|
2022-03-07 17:40:54 +00:00
|
|
|
}
|