Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Fix regression with OKTETO_TOKEN and improve DevX for invalid token #4297

Merged
merged 2 commits into from
May 20, 2024
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
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 {
Copy link
Contributor Author

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

removing this fixes the regression

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() {
Copy link
Member

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

We should probably create a specific error and being able to use error.Is() instead of comparing string values of the error. For example, not sure if we could have some problem with a trailing slash (/) at the end of the context name at some point.

Not needed to do as part of this PR, but it should be the way to go

Copy link
Contributor Author

@andreafalzetti andreafalzetti May 20, 2024

Choose a reason for hiding this comment

The reason will be displayed to describe this comment to others. Learn more.

@ifbyol I agree, that is what I've attempted to begin with, then realized that ErrNotLogged is being used like this all over, so it's tech debt that we need to adress. I didn't want to extend the scope of this change too much, but we should look into improve it!

return analytics.TestMetadata{}, err
}
if err := contextCMD.NewContextCommand().Run(ctx, &contextCMD.Options{Namespace: options.Namespace}); err != nil {
return analytics.TestMetadata{}, err
}
Expand Down
12 changes: 11 additions & 1 deletion pkg/okteto/client.go
Original file line number Diff line number Diff line change
Expand Up @@ -22,6 +22,7 @@ import (
"net/http"
"net/url"
"os"
"regexp"
"strings"
"sync"

Expand Down Expand Up @@ -55,6 +56,10 @@ var serverName string
var strictTLSOnce sync.Once
var errURLNotSet = errors.New("the okteto URL is not set")

const unauthorizedTokenPattern = `^non-200 OK status code: 401 Unauthorized body: "fail to find user with token [A-Za-z0-9]+: not-authorized\\n"$`

var unauthorizedTokenRegex = regexp.MustCompile(unauthorizedTokenPattern)

// graphqlClientInterface contains the functions that a graphqlClient must have
type graphqlClientInterface interface {
Query(ctx context.Context, q interface{}, variables map[string]interface{}) error
Expand Down Expand Up @@ -344,12 +349,18 @@ 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\"":
andreafalzetti marked this conversation as resolved.
Show resolved Hide resolved
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":
return oktetoErrors.ErrNotFound

default:
if unauthorizedTokenRegex.MatchString(err.Error()) {
return fmt.Errorf(oktetoErrors.ErrNotLogged, GetContext().Name)
}

switch {
case oktetoErrors.IsX509(err):
return oktetoErrors.UserError{
Expand All @@ -366,7 +377,6 @@ func translateAPIErr(err error) error {
oktetoLog.Infof("Unrecognized API error: %s", err)
return err
}

}

func isAPILicenseError(err error) bool {
Expand Down
Loading