Skip to content

Commit

Permalink
Timeout login command (#308)
Browse files Browse the repository at this point in the history
  • Loading branch information
BruceMacD authored Oct 14, 2021
1 parent 5e1bb06 commit c1e146e
Show file tree
Hide file tree
Showing 5 changed files with 42 additions and 8 deletions.
6 changes: 5 additions & 1 deletion internal/cmd/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -182,6 +182,8 @@ func updateKubeconfig(destinations []api.Destination) error {
}

func newLoginCmd() (*cobra.Command, error) {
var options LoginOptions

cmd := &cobra.Command{
Use: "login REGISTRY",
Short: "Login to an Infra Registry",
Expand All @@ -193,10 +195,12 @@ func newLoginCmd() (*cobra.Command, error) {
registry = args[0]
}

return login(registry, true)
return login(registry, true, options)
},
}

cmd.Flags().DurationVarP(&options.Timeout, "timeout", "t", defaultTimeout, "login timeout")

return cmd, nil
}

Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/list.go
Original file line number Diff line number Diff line change
Expand Up @@ -37,7 +37,7 @@ func list() error {
case http.StatusForbidden:
fmt.Fprintln(os.Stderr, "Session has expired.")

if err = login("", false); err != nil {
if err = login("", false, LoginOptions{}); err != nil {
return err
}

Expand Down
30 changes: 28 additions & 2 deletions internal/cmd/local_server.go
Original file line number Diff line number Diff line change
Expand Up @@ -7,8 +7,15 @@ import (
"html/template"
"net/http"
"strings"
"time"
)

type ErrResultTimedOut struct{}

func (e *ErrResultTimedOut) Error() string {
return "local server timed out waiting for result"
}

type CodeResponse struct {
Code string
State string
Expand All @@ -20,6 +27,8 @@ type LocalServer struct {
srv *http.Server
}

const defaultTimeout = 300000 * time.Millisecond

//go:embed pages
var pages embed.FS

Expand Down Expand Up @@ -63,13 +72,30 @@ func newLocalServer() (*LocalServer, error) {
return ls, nil
}

func (l *LocalServer) wait() (string, string, error) {
result := <-l.ResultChan
func (l *LocalServer) wait(timeout time.Duration) (string, string, error) {
var result CodeResponse

timedOut := false

if timeout <= 0 {
timeout = defaultTimeout
}

select {
case result = <-l.ResultChan:
// do nothing
case <-time.After(timeout):
timedOut = true
}

if err := l.srv.Shutdown(context.Background()); err != nil {
return "", "", err
}

if timedOut {
return "", "", &ErrResultTimedOut{}
}

return result.Code, result.State, result.Error
}

Expand Down
10 changes: 7 additions & 3 deletions internal/cmd/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -29,7 +29,11 @@ func (e *ErrUnauthenticated) Error() string {
return "Could not read local credentials. Are you logged in? Use \"infra login\" to login."
}

func login(registry string, useCurrentConfig bool) error {
type LoginOptions struct {
Timeout time.Duration
}

func login(registry string, useCurrentConfig bool, options LoginOptions) error {
homeDir, err := os.UserHomeDir()
if err != nil {
return err
Expand Down Expand Up @@ -123,7 +127,7 @@ func login(registry string, useCurrentConfig bool) error {
}

if !proceed {
return fmt.Errorf("Could not continue with login.")
return fmt.Errorf("could not continue with login")
}

client, err := NewApiClient(selectedRegistry.Host, skipTLSVerify)
Expand Down Expand Up @@ -181,7 +185,7 @@ func login(registry string, useCurrentConfig bool) error {
return err
}

code, recvstate, err := ls.wait()
code, recvstate, err := ls.wait(options.Timeout)
if err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion internal/cmd/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -62,7 +62,7 @@ func tokenCreate(destination string) error {

fmt.Fprintln(os.Stderr, "Session has expired.")

if err = login("", false); err != nil {
if err = login("", false, LoginOptions{}); err != nil {
return err
}

Expand Down

0 comments on commit c1e146e

Please sign in to comment.