Skip to content

Commit

Permalink
disable retry login when envvar TSURU_TOKEN is set
Browse files Browse the repository at this point in the history
  • Loading branch information
wpjunior committed Mar 18, 2024
1 parent 4da163e commit 11af4ef
Show file tree
Hide file tree
Showing 4 changed files with 45 additions and 27 deletions.
2 changes: 1 addition & 1 deletion tsuru/client/shell_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -150,5 +150,5 @@ func (s *S) TestShellToContainerSessionExpired(c *check.C) {
s.setupFakeTransport(&transport)
err = command.Run(&context)
c.Assert(err, check.NotNil)
c.Assert(err, check.ErrorMatches, ".*unauthorized")
c.Assert(err, check.ErrorMatches, ".*Unauthorized")
}
2 changes: 1 addition & 1 deletion tsuru/http/client_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -159,7 +159,7 @@ func (s *S) TestShouldHandleUnauthorizedErrorSpecially(c *check.C) {
c.Assert(err, check.IsNil)
var buf bytes.Buffer
client := NewTerminalClient(TerminalClientOptions{
RoundTripper: &cmdtest.Transport{Message: "You can't do this", Status: http.StatusUnauthorized},
RoundTripper: &cmdtest.Transport{Message: "unauthorized", Status: http.StatusUnauthorized},

Stdout: &buf,
ClientName: "test",
Expand Down
34 changes: 22 additions & 12 deletions tsuru/http/transport.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,7 +10,6 @@ import (
"io"
"net/http"
"net/http/httputil"
"net/url"
"os"
"strconv"

Expand Down Expand Up @@ -107,10 +106,7 @@ func (v *TerminalRoundTripper) RoundTrip(req *http.Request) (*http.Response, err
if !validateVersion(supported, v.CurrentVersion) {
fmt.Fprintf(v.Stderr, invalidVersionFormat, v.Progname, supported, v.CurrentVersion)
}
if response.StatusCode == http.StatusUnauthorized {
fmt.Fprintln(v.Stderr, "Session expired")
return nil, errUnauthorized
}

if response.StatusCode > 399 {
err := &tsuruerr.HTTP{
Code: response.StatusCode,
Expand All @@ -136,18 +132,16 @@ func detectClientError(err error) error {
detectErr := func(e error) error {
target, _ := config.ReadTarget()

switch e.(type) {
switch e := e.(type) {
case *tsuruerr.HTTP:
return errors.Wrapf(e, "Error received from tsuru server (%s), %d", target, e.Code)
case x509.UnknownAuthorityError:
return errors.Wrapf(e, "Failed to connect to tsuru server (%s)", target)
}
return errors.Wrapf(e, "Failed to connect to tsuru server (%s), it's probably down", target)
}

if urlErr, ok := err.(*url.Error); ok {
return detectErr(urlErr.Err)
}

return detectErr(err)
return detectErr(UnwrapErr(err))
}

// validateVersion checks whether current version is greater or equal to
Expand Down Expand Up @@ -184,7 +178,23 @@ func (v *TokenV1RoundTripper) RoundTrip(req *http.Request) (*http.Response, erro
req.Header.Set("Authorization", "bearer "+token)
}

return roundTripper.RoundTrip(req)
response, err := roundTripper.RoundTrip(req)

if err != nil {
return nil, err
}

if response.StatusCode == http.StatusUnauthorized {
if teamToken := config.ReadTeamToken(); teamToken != "" {
fmt.Fprintln(os.Stderr, "Invalid session - maybe invalid defined token on TSURU_TOKEN envvar")
} else {
fmt.Fprintln(os.Stderr, "Invalid session")
}

return nil, errUnauthorized
}

return response, nil
}

func NewTokenV1RoundTripper() http.RoundTripper {
Expand Down
34 changes: 21 additions & 13 deletions tsuru/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -229,6 +229,10 @@ Services aren’t managed by tsuru, but by their creators.`)
m.Register(&client.ServiceInstanceInfo{})
registerExtraCommands(m)
m.RetryHook = func(err error) (retry bool) {
if teamToken := config.ReadTeamToken(); teamToken != "" {
return false
}

mustLogin := false

err = tsuruHTTP.UnwrapErr(err)
Expand All @@ -241,21 +245,25 @@ Services aren’t managed by tsuru, but by their creators.`)
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
}
if !mustLogin {
return false
}

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

if loginErr != nil {
fmt.Fprintf(os.Stderr, "Could not login: %s\n", loginErr.Error())
return false
}

return false
initAuthorization() // re-init updated token provider
return true

}
return m
}
Expand Down

0 comments on commit 11af4ef

Please sign in to comment.