Skip to content

Commit

Permalink
Retry when receive an oauth2 error
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Mar 18, 2024
1 parent 3d30ed2 commit 4da163e
Show file tree
Hide file tree
Showing 3 changed files with 42 additions and 10 deletions.
24 changes: 19 additions & 5 deletions tsuru/http/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,12 +69,26 @@ func TsuruClientFromEnvironment() (*tsuru.APIClient, error) {
return cli, nil
}

type errWrapped interface {
Unwrap() error
}

type errCauser interface {
Cause() error
}

func UnwrapErr(err error) error {
if err == nil {
return nil
}
if urlErr, ok := err.(*url.Error); ok {
return urlErr.Err
for err != nil {
if cause, ok := err.(errCauser); ok {
err = cause.Cause()
} else if u, ok := err.(errWrapped); ok {
err = u.Unwrap()
} else if urlErr, ok := err.(*url.Error); ok {
err = urlErr.Err
} else {
break
}
}

return err
}
2 changes: 1 addition & 1 deletion tsuru/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -140,7 +140,7 @@ func detectClientError(err error) error {
case x509.UnknownAuthorityError:
return errors.Wrapf(e, "Failed to connect to tsuru server (%s)", target)
}
return errors.Errorf("Failed to connect to tsuru server (%s), it's probably down, internal err: %q", target, e.Error())
return errors.Wrapf(e, "Failed to connect to tsuru server (%s), it's probably down", target)
}

if urlErr, ok := err.(*url.Error); ok {
Expand Down
26 changes: 22 additions & 4 deletions tsuru/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,15 +229,28 @@ Services aren’t managed by tsuru, but by their creators.`)
m.Register(&client.ServiceInstanceInfo{})
registerExtraCommands(m)
m.RetryHook = func(err error) (retry bool) {
mustLogin := false

err = tsuruHTTP.UnwrapErr(err)
if httpErr, ok := err.(*tsuruErrors.HTTP); ok && httpErr.StatusCode() == http.StatusUnauthorized {

if oauth2Err, ok := err.(*oauth2.RetrieveError); ok {
fmt.Fprintf(os.Stderr, "oauth2 error: %s, %s\n", oauth2Err.ErrorCode, oauth2Err.ErrorDescription)
mustLogin = true
} else if httpErr, ok := err.(*tsuruErrors.HTTP); ok && httpErr.StatusCode() == http.StatusUnauthorized {
fmt.Fprintf(os.Stderr, "http error: %d\n", httpErr.StatusCode())
mustLogin = true
}

if mustLogin {
fmt.Fprintln(os.Stderr, "trying to login again")
c := &auth.Login{}
loginErr := c.Run(&cmd.Context{
Stderr: stderr,
Stdout: stdout,
})

if loginErr == nil {
initAuthorization() // re-init updated token provider
return true
}
}
Expand Down Expand Up @@ -271,6 +284,14 @@ func main() {

name := cmd.ExtractProgramName(os.Args[0])

initAuthorization()

m := buildManager(name)
m.Run(os.Args[1:])
}

func initAuthorization() {
name := cmd.ExtractProgramName(os.Args[0])
roundTripper, tokenProvider := roundTripperAndTokenProvider()

tsuruHTTP.AuthenticatedClient = tsuruHTTP.NewTerminalClient(tsuruHTTP.TerminalClientOptions{
Expand All @@ -281,9 +302,6 @@ func main() {
Stderr: os.Stderr,
})
config.DefaultTokenProvider = tokenProvider

m := buildManager(name)
m.Run(os.Args[1:])
}

func roundTripperAndTokenProvider() (http.RoundTripper, config.TokenProvider) {
Expand Down

0 comments on commit 4da163e

Please sign in to comment.