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

fix(cli): improve display of error responses for token exchange #4005

Merged
merged 3 commits into from
Sep 2, 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
5 changes: 2 additions & 3 deletions cli/config/configurator.go
Original file line number Diff line number Diff line change
Expand Up @@ -117,7 +117,7 @@ func (c Configurator) Start(ctx context.Context, prev *Config, flags agentConfig

_, err = c.handleOAuth(ctx, cfg, prev)
if err != nil {
c.logger.Error("Could not handle OAuth", zap.Error(err))
c.logger.Debug("Could not handle OAuth", zap.Error(err))
return err
}

Expand Down Expand Up @@ -265,7 +265,7 @@ func (c Configurator) handleOAuth(ctx context.Context, cfg Config, prev *Config)
var err error
cfg, err = c.exchangeToken(cfg, c.flags.Token)
if err != nil {
c.logger.Error("Could not exchange token", zap.Error(err))
c.logger.Debug("could not exchange token", zap.Error(err))
return Config{}, err
}
}
Expand Down Expand Up @@ -304,7 +304,6 @@ func (c Configurator) exchangeToken(cfg Config, token string) (Config, error) {
c.logger.Debug("Exchanging token", zap.String("token", token))
jwt, err := oauth.ExchangeToken(cfg.OAuthEndpoint(), token)
if err != nil {
c.logger.Error("Could not exchange token", zap.Error(err))
return Config{}, err
}

Expand Down
33 changes: 27 additions & 6 deletions cli/pkg/oauth/oauth.go
Original file line number Diff line number Diff line change
Expand Up @@ -85,6 +85,19 @@ func SetLogger(l *zap.Logger) {
logger = l
}

type oauthError struct {
err error
msg string
}

func (e oauthError) Error() string {
return e.err.Error()
}

func (e oauthError) Message() string {
return e.msg
}

func ExchangeToken(endpoint string, token string) (string, error) {
logger.Debug("Exchanging token", zap.String("endpoint", endpoint), zap.String("token", token))
req, err := http.NewRequest("GET", fmt.Sprintf("%s/tokens/%s/exchange", endpoint, token), nil)
Expand All @@ -95,16 +108,24 @@ func ExchangeToken(endpoint string, token string) (string, error) {

res, err := http.DefaultClient.Do(req)
if err != nil {
logger.Debug("Failed to exchange token", zap.Error(err))
return "", fmt.Errorf("failed to exchange token: %w", err)
logger.Debug("Cannot create exchange token request", zap.Error(err))
return "", fmt.Errorf("cannot create exchange token request: %w", err)
}
defer res.Body.Close()

if res.StatusCode != http.StatusCreated {
logger.Debug("Failed to exchange token", zap.String("status", res.Status))
return "", fmt.Errorf("failed to exchange token: %s", res.Status)
switch res.StatusCode {
case http.StatusNotFound:
return "", oauthError{err: fmt.Errorf("token not found"), msg: "Token not found"}
case http.StatusUnauthorized:
return "", oauthError{err: fmt.Errorf("token expired"), msg: "Token has expired"}
case http.StatusCreated:
logger.Debug("Token exchanged")
default:
b, _ := io.ReadAll(res.Body)
logger.Debug("Failed to exchange token", zap.String("status", res.Status), zap.String("response", string(b)))
return "", oauthError{err: fmt.Errorf("failed to exchange token: %s", res.Status), msg: "Unexpected error exchanging token"}
}

defer res.Body.Close()
body, err := io.ReadAll(res.Body)
if err != nil {
logger.Debug("Failed to read response body", zap.Error(err))
Expand Down
Loading