Skip to content

Commit

Permalink
improve devx for misconfigured credentials
Browse files Browse the repository at this point in the history
Signed-off-by: Andrea Falzetti <andrea@okteto.com>
  • Loading branch information
andreafalzetti committed May 17, 2024
1 parent 38026db commit c01ab95
Show file tree
Hide file tree
Showing 9 changed files with 25 additions and 16 deletions.
15 changes: 8 additions & 7 deletions cmd/context/create.go
Original file line number Diff line number Diff line change
Expand Up @@ -146,10 +146,6 @@ func (c *Command) UseContext(ctx context.Context, ctxOptions *Options) error {
ctxOptions.IsOkteto = true
}

if ctxOptions.Context == okteto.CloudURL {
ctxOptions.IsOkteto = true
}

if !ctxOptions.IsOkteto {

if isUrl(ctxOptions.Context) {
Expand Down Expand Up @@ -354,14 +350,15 @@ func getLoggedUserContext(ctx context.Context, c *Command, ctxOptions *Options)

ctxOptions.Token = user.Token

okteto.GetContext().Token = user.Token
okteto.SetInsecureSkipTLSVerifyPolicy(okteto.GetContext().IsStoredAsInsecure)
okCtx := okteto.GetContext()
okCtx.Token = user.Token
okteto.SetInsecureSkipTLSVerifyPolicy(okCtx.IsStoredAsInsecure)

if ctxOptions.Namespace == "" {
ctxOptions.Namespace = user.Namespace
}

userContext, err := c.getUserContext(ctx, okteto.GetContext().Name, okteto.GetContext().Namespace, okteto.GetContext().Token)
userContext, err := c.getUserContext(ctx, okCtx.Name, okCtx.Namespace, okCtx.Token)
if err != nil {
return nil, err
}
Expand Down Expand Up @@ -416,6 +413,10 @@ func (c Command) getUserContext(ctx context.Context, ctxName, ns, token string)
return nil, err
}

if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return nil, err
}

if oktetoErrors.IsForbidden(err) {
if err := c.OktetoContextWriter.Write(); err != nil {
oktetoLog.Infof("error updating okteto contexts: %v", err)
Expand Down
3 changes: 0 additions & 3 deletions cmd/context/options.go
Original file line number Diff line number Diff line change
Expand Up @@ -91,9 +91,6 @@ func (o *Options) InitFromEnvVars() {
envToken := os.Getenv(model.OktetoTokenEnvVar)
if o.Token != "" || envToken != "" {
o.IsOkteto = true
if o.Context == "" {
o.Context = okteto.CloudURL
}
}

if o.Token == "" && envToken != "" {
Expand Down
6 changes: 3 additions & 3 deletions cmd/context/options_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -142,7 +142,7 @@ func Test_initFromEnvVars(t *testing.T) {
},
want: &Options{
Token: "token",
Context: okteto.CloudURL,
Context: "",
IsOkteto: true,
},
},
Expand All @@ -159,7 +159,7 @@ func Test_initFromEnvVars(t *testing.T) {
},
want: &Options{
Token: "token",
Context: okteto.CloudURL,
Context: "",
IsOkteto: true,
},
},
Expand All @@ -174,7 +174,7 @@ func Test_initFromEnvVars(t *testing.T) {
},
want: &Options{
Token: "token",
Context: okteto.CloudURL,
Context: "",
IsOkteto: true,
InferredToken: true,
},
Expand Down
2 changes: 1 addition & 1 deletion cmd/deploy/deploy.go
Original file line number Diff line number Diff line change
Expand Up @@ -181,7 +181,7 @@ func Deploy(ctx context.Context, at AnalyticsTrackerInterface, insightsTracker b

// Loads, updates and uses the context from path. If not found, it creates and uses a new context
if err := contextCMD.LoadContextFromPath(ctx, options.Namespace, options.K8sContext, options.ManifestPath, contextCMD.Options{Show: true}); err != nil {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.CloudURL).Error() {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: options.Namespace}); err != nil {
Expand Down
5 changes: 4 additions & 1 deletion cmd/deploy/endpoints.go
Original file line number Diff line number Diff line change
Expand Up @@ -80,7 +80,7 @@ func NewEndpointGetter(k8sLogger *io.K8sLogger) (EndpointGetter, error) {

}

// Endpoints deploys the okteto manifest
// Endpoints lists the endpoints for a development environment
func Endpoints(ctx context.Context, k8sLogger *io.K8sLogger) *cobra.Command {
options := &EndpointsOptions{}
fs := afero.NewOsFs()
Expand All @@ -107,6 +107,9 @@ func Endpoints(ctx context.Context, k8sLogger *io.K8sLogger) *cobra.Command {
showCtxHeader := options.Output == ""
// Loads, updates and uses the context from path. If not found, it creates and uses a new context
if err := contextCMD.LoadContextFromPath(ctx, options.Namespace, options.K8sContext, options.ManifestPath, contextCMD.Options{Show: showCtxHeader}); err != nil {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: options.Namespace, Show: showCtxHeader}); err != nil {
return err
}
Expand Down
2 changes: 1 addition & 1 deletion cmd/destroy/destroy.go
Original file line number Diff line number Diff line change
Expand Up @@ -161,7 +161,7 @@ func Destroy(ctx context.Context, at analyticsTrackerInterface, insights buildTr
options.ManifestPath = uptManifestPath
}
if err := contextCMD.LoadContextFromPath(ctx, options.Namespace, options.K8sContext, options.ManifestPath, contextCMD.Options{Show: true}); err != nil {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.CloudURL).Error() {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: options.Namespace}); err != nil {
Expand Down
3 changes: 3 additions & 0 deletions cmd/push.go
Original file line number Diff line number Diff line change
Expand Up @@ -69,6 +69,9 @@ func Push(ctx context.Context, at analyticsTrackerInterface) *cobra.Command {
}
// Loads, updates and uses the context from path. If not found, it creates and uses a new context
if err := contextCMD.LoadContextFromPath(ctx, pushOpts.Namespace, pushOpts.K8sContext, pushOpts.DevPath, contextCMD.Options{Show: true}); err != nil {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: pushOpts.Namespace, Show: false}); err != nil {
return err
}
Expand Down
3 changes: 3 additions & 0 deletions cmd/test/cmd.go
Original file line number Diff line number Diff line change
Expand Up @@ -111,6 +111,9 @@ func doRun(ctx context.Context, servicesToTest []string, options *Options, ioCtr

// Loads, updates and uses the context from path. If not found, it creates and uses a new context
if err := contextCMD.LoadContextFromPath(ctx, options.Namespace, options.K8sContext, options.ManifestPath, contextCMD.Options{Show: true}); err != nil {
if err.Error() == fmt.Errorf(oktetoErrors.ErrNotLogged, okteto.GetContext().Name).Error() {
return analytics.TestMetadata{}, err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: options.Namespace}); err != nil {
return analytics.TestMetadata{}, err
}
Expand Down
2 changes: 2 additions & 0 deletions pkg/okteto/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -344,6 +344,8 @@ func translateAPIErr(err error) error {
return fmt.Errorf("server temporarily unavailable, please try again")
case "non-200 OK status code: 401 Unauthorized body: \"\"":
return fmt.Errorf("unauthorized. Please run 'okteto context url' and try again")
case "non-200 OK status code: 401 Unauthorized body: \"not-authorized\\n\"":
return fmt.Errorf(oktetoErrors.ErrNotLogged, GetContext().Name)
case "non-200 OK status code: 401 Unauthorized body: \"not-authorized: token is expired\\n\"":
return oktetoErrors.ErrTokenExpired
case "not-found":
Expand Down

0 comments on commit c01ab95

Please sign in to comment.