From e51ee55f07fb0fd1a3d83752dacee350f0089947 Mon Sep 17 00:00:00 2001 From: Mitchell Date: Wed, 12 Jun 2019 16:51:29 -0700 Subject: [PATCH] Refactors to spc timeouts and password error handling --- cli/repositories/config.go | 3 ++- credentials/commands/create.go | 2 +- credentials/commands/delete.go | 2 +- credentials/commands/get.go | 7 +++++-- credentials/commands/list.go | 4 ++-- 5 files changed, 11 insertions(+), 7 deletions(-) diff --git a/cli/repositories/config.go b/cli/repositories/config.go index 7fc8455..6916cb8 100644 --- a/cli/repositories/config.go +++ b/cli/repositories/config.go @@ -5,6 +5,7 @@ import ( "fmt" "io/ioutil" "os" + "strings" "github.com/mitchellh/go-homedir" "github.com/spf13/viper" @@ -69,7 +70,7 @@ func (mgr *ConfigManager) OpenConfig() (output string, v *viper.Viper, err error return output, nil, err } - if err = mgr.v.ReadConfig(bytes.NewBuffer(contents)); err != nil && err.Error() == "While parsing config: (1, 1): unexpected token" { + if err = mgr.v.ReadConfig(bytes.NewBuffer(contents)); err != nil && strings.HasPrefix(err.Error(), "While parsing config") { return output, nil, fmt.Errorf("incorrect master password") } else if err != nil { return output, nil, err diff --git a/credentials/commands/create.go b/credentials/commands/create.go index c18241d..4c663db 100644 --- a/credentials/commands/create.go +++ b/credentials/commands/create.go @@ -130,7 +130,7 @@ password.`, } } - ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*25) defer cancel() c, err := initClient(ctx).Create(ctx, ci) diff --git a/credentials/commands/delete.go b/credentials/commands/delete.go index 7883c41..cd731d0 100644 --- a/credentials/commands/delete.go +++ b/credentials/commands/delete.go @@ -21,7 +21,7 @@ func MakeDelete(initConfig CredentialClientInit) *cobra.Command { check(survey.AskOne(prompt, &confirmed, nil)) if confirmed { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*10) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*25) defer cancel() check(initConfig(ctx).Delete(ctx, args[0])) diff --git a/credentials/commands/get.go b/credentials/commands/get.go index 2b14450..c017850 100644 --- a/credentials/commands/get.go +++ b/credentials/commands/get.go @@ -26,13 +26,16 @@ func MakeGet(repo clitypes.ConfigRepo, initClient CredentialClientInit) *cobra.C decrypting password.`, Run: func(cmd *cobra.Command, args []string) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*15) defer cancel() client := initClient(ctx) masterpass, cfg, err := repo.OpenConfig() check(err) + ctx, cancel = context.WithTimeout(context.Background(), time.Second*5) + defer cancel() + mdch, errch := client.GetAllMetadata(ctx, "") mds := map[string][]types.Metadata{} @@ -42,7 +45,7 @@ decrypting password.`, for count := 0; ; count++ { select { case <-ctx.Done(): - check(fmt.Errorf("context timeout")) + check(ctx.Err()) case err := <-errch: check(err) diff --git a/credentials/commands/list.go b/credentials/commands/list.go index c53ba3d..23d8d50 100644 --- a/credentials/commands/list.go +++ b/credentials/commands/list.go @@ -22,7 +22,7 @@ func MakeList(initClient CredentialClientInit) *cobra.Command { includes almost all the information but the most sensitive.`, Run: func(cmd *cobra.Command, args []string) { - ctx, cancel := context.WithTimeout(context.Background(), time.Second*5) + ctx, cancel := context.WithTimeout(context.Background(), time.Second*20) defer cancel() mdch, errch := initClient(ctx).GetAllMetadata(ctx, sourceHost) @@ -34,7 +34,7 @@ includes almost all the information but the most sensitive.`, for count := 0; ; count++ { select { case <-ctx.Done(): - check(fmt.Errorf("context timeout")) + check(ctx.Err()) case err := <-errch: check(err)