Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

feat(cli): save default agent token to local config #4050

Merged
merged 1 commit into from
Oct 15, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
2 changes: 1 addition & 1 deletion agent/runner/runner.go
Original file line number Diff line number Diff line change
Expand Up @@ -72,7 +72,7 @@ func (s *Runner) onStartAgent(ctx context.Context, cfg config.Config) {
return
}

agentToken, err := s.getAgentToken(ctx, cfg.FullURL(), cfg.OrganizationID, cfg.EnvironmentID, cfg.Jwt)
agentToken, err := config.GetAgentToken(ctx, s.logger, cfg.FullURL(), cfg.OrganizationID, cfg.EnvironmentID, cfg.Jwt)
if err != nil {
s.ui.Error(err.Error())
}
Expand Down
2 changes: 2 additions & 0 deletions cli/cmd/configure_cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -51,6 +51,8 @@ var configureCmd = &cobra.Command{
flags.OrganizationID = configParams.OrganizationID
}

configurator = configurator.WithLogger(cliLogger)

// early exit if the versions are not compatible
err = configurator.Start(ctx, &cfg, flags)
if errors.Is(err, config.ErrVersionMismatch) {
Expand Down
21 changes: 11 additions & 10 deletions agent/runner/agent.go → cli/config/agent_token.go
Original file line number Diff line number Diff line change
@@ -1,4 +1,4 @@
package runner
package config

import (
"context"
Expand All @@ -14,11 +14,12 @@ type agentResponse struct {
Key string `json:"key"`
}

func (s *Runner) getAgentToken(ctx context.Context, endpoint, orgID, envID, token string) (string, error) {
s.logger.Debug("Getting agent token", zap.String("endpoint", endpoint), zap.String("token", token))
req, err := http.NewRequestWithContext(ctx, "GET", fmt.Sprintf("%s/organizations/%s/environments/%s/agent", endpoint, orgID, envID), nil)
func GetAgentToken(ctx context.Context, logger *zap.Logger, endpoint, orgID, envID, token string) (string, error) {
reqUrl := fmt.Sprintf("%s/organizations/%s/environments/%s/agent", endpoint, orgID, envID)
logger.Debug("Getting agent token", zap.String("endpoint", reqUrl))
req, err := http.NewRequestWithContext(ctx, "GET", reqUrl, nil)
if err != nil {
s.logger.Debug("Failed to create request", zap.Error(err))
logger.Debug("Failed to create request", zap.Error(err))
return "", fmt.Errorf("failed to create request: %w", err)
}

Expand All @@ -27,30 +28,30 @@ func (s *Runner) getAgentToken(ctx context.Context, endpoint, orgID, envID, toke

res, err := http.DefaultClient.Do(req)
if err != nil {
s.logger.Debug("Failed to get agent token", zap.Error(err))
logger.Debug("Failed to get agent token", zap.Error(err))
return "", fmt.Errorf("failed to get agent token: %w", err)
}

if res.StatusCode != http.StatusOK {
s.logger.Debug("Failed to get agent token", zap.String("status", res.Status))
logger.Debug("Failed to get agent token", zap.String("status", res.Status))
return "", fmt.Errorf("failed to get agent token: %s", res.Status)
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
s.logger.Debug("Failed to read response body", zap.Error(err))
logger.Debug("Failed to read response body", zap.Error(err))
return "", fmt.Errorf("failed to read response body: %w", err)
}

var agentResponse agentResponse
err = json.Unmarshal(body, &agentResponse)
if err != nil {
s.logger.Debug("Failed to unmarshal response body", zap.Error(err))
logger.Debug("Failed to unmarshal response body", zap.Error(err))
return "", fmt.Errorf("failed to unmarshal response body: %w", err)
}

s.logger.Debug("Agent token received")
logger.Debug("Agent token received")

return agentResponse.Key, nil
}
1 change: 1 addition & 0 deletions cli/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,6 +33,7 @@ type Config struct {
Token string `yaml:"token,omitempty"`
IsOrgToken bool `yaml:"isOrgToken,omitempty"`
Jwt string `yaml:"jwt,omitempty"`
DefaultAgenApiKey string `yaml:"defaultAgentApiKey,omitempty"`
AgentApiKey string `yaml:"-"`
EndpointOverriden bool `yaml:"-"`

Expand Down
17 changes: 17 additions & 0 deletions cli/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -398,6 +398,12 @@ func (c Configurator) showOrganizationSelector(ctx context.Context, prev *Config
}

cfg.EnvironmentID = envID

cfg.DefaultAgenApiKey, err = c.getEnvironmentDefaultAgentApiKey(ctx, cfg)
if err != nil {
c.errorHandlerFn(ctx, err)
return
}
}

ctx, err := Save(ctx, cfg)
Expand All @@ -410,6 +416,17 @@ func (c Configurator) showOrganizationSelector(ctx context.Context, prev *Config
c.onFinish(ctx, cfg)
}

func (c Configurator) getEnvironmentDefaultAgentApiKey(ctx context.Context, cfg Config) (string, error) {
c.logger.Debug("Getting environment default agent API key", zap.String("environmentID", cfg.EnvironmentID))
agentApiKey, err := GetAgentToken(ctx, c.logger, cfg.FullURL(), cfg.OrganizationID, cfg.EnvironmentID, cfg.Jwt)
if err != nil {
c.logger.Debug("Could not get environment", zap.Error(err))
return "", err
}

return agentApiKey, nil
}

func SetupHttpClient(cfg Config) *resourcemanager.HTTPClient {
extraHeaders := http.Header{}
extraHeaders.Set("x-client-id", analytics.ClientID())
Expand Down
Loading