From 6ee3ff0b5fd7d76b81b87018c0a48daaaf5ad294 Mon Sep 17 00:00:00 2001 From: Wojciech Trocki Date: Wed, 13 Jul 2022 15:07:00 +0200 Subject: [PATCH] fix: add env var config (#1640) --- CONTRIBUTING.md | 8 ++++ cmd/rhoas/main.go | 3 -- pkg/cmd/login/login.go | 48 +++---------------- pkg/core/auth/token/token.go | 12 ++++- pkg/core/cmdutil/profile/profile.go | 17 ------- .../localize/locales/en/cmd/login.en.toml | 4 -- 6 files changed, 25 insertions(+), 67 deletions(-) diff --git a/CONTRIBUTING.md b/CONTRIBUTING.md index 7c5ba6572..aa56e461d 100644 --- a/CONTRIBUTING.md +++ b/CONTRIBUTING.md @@ -159,6 +159,14 @@ Once pushed, a [GitHub Action](https://github.com/redhat-developer/app-services- > NOTE: To create a pre-release, the tag should have appropriate suffix, e.g v0.20.1-alpha1 +### Environment variables + +RHOASCONFIG="./config.json" - custom configuration location (useful for testing) +RHOAS_CONTEXT="./context.json" - custom context location +RHOAS_TELEMETRY=false - Enables/Disables telemetry (should happen automatically in non tty sessions) +EDITOR=Code -w - controls CLI editor +KUBECONFIG=./config.json - custom kubernetes config used for other commands + ### Changelog generation [git-chglog](https://github.com/git-chglog/git-chglog) is used to generate a changelog for the current release. diff --git a/cmd/rhoas/main.go b/cmd/rhoas/main.go index 5813f1740..304333dbb 100644 --- a/cmd/rhoas/main.go +++ b/cmd/rhoas/main.go @@ -50,9 +50,6 @@ func main() { err = executeCommandWithTelemetry(rootCmd, cmdFactory) if err == nil { - if flagutil.DebugEnabled() { - build.CheckForUpdate(cmdFactory.Context, build.Version, cmdFactory.Logger, localizer) - } return } cmdFactory.Logger.Errorf("%v\n", rootError(err, localizer)) diff --git a/pkg/cmd/login/login.go b/pkg/cmd/login/login.go index d79d1e53f..becb648aa 100644 --- a/pkg/cmd/login/login.go +++ b/pkg/cmd/login/login.go @@ -8,8 +8,6 @@ import ( "net/url" "github.com/redhat-developer/app-services-cli/pkg/core/auth/login" - "github.com/redhat-developer/app-services-cli/pkg/core/auth/token" - "github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil" "github.com/redhat-developer/app-services-cli/pkg/core/config" "github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon" @@ -156,14 +154,6 @@ func runLogin(opts *options) (err error) { return err } } - - if opts.offlineToken != "" { - if err = loginWithOfflineToken(opts); err != nil { - spinner.Stop() - opts.Logger.Info() - return err - } - } spinner.Stop() cfg, err := opts.Config.Load() @@ -171,52 +161,28 @@ func runLogin(opts *options) (err error) { return err } + if opts.offlineToken != "" { + cfg.RefreshToken = opts.offlineToken + } + cfg.APIUrl = gatewayURL.String() cfg.Insecure = opts.insecureSkipTLSVerify cfg.ClientID = opts.clientID cfg.AuthURL = opts.authURL cfg.Scopes = opts.scopes + // Reset access token on login to avoid reusing previous users valid token + cfg.AccessToken = "" if err = opts.Config.Save(cfg); err != nil { return err } - username, ok := token.GetUsername(cfg.AccessToken) - opts.Logger.Info() - if !ok { - opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccessNoUsername")) - } else { - opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess", localize.NewEntry("Username", username))) - } - - // debug mode checks this for a version update also. - // so we check if is enabled first so as not to print it twice - if !flagutil.DebugEnabled() { - build.CheckForUpdate(opts.Context, build.Version, opts.Logger, opts.localizer) - } + opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess")) return nil } -func loginWithOfflineToken(opts *options) (err error) { - cfg, err := opts.Config.Load() - if err != nil { - return err - } - cfg.Insecure = opts.insecureSkipTLSVerify - cfg.ClientID = opts.clientID - cfg.AuthURL = opts.authURL - cfg.Scopes = opts.scopes - cfg.RefreshToken = opts.offlineToken - - if err = opts.Config.Save(cfg); err != nil { - return err - } - - return err -} - func createTransport(insecure bool) *http.Transport { // #nosec 402 return &http.Transport{ diff --git a/pkg/core/auth/token/token.go b/pkg/core/auth/token/token.go index 4aee5e6ca..4d9125b82 100644 --- a/pkg/core/auth/token/token.go +++ b/pkg/core/auth/token/token.go @@ -88,7 +88,9 @@ func MapClaims(token *jwt.Token) (claims jwt.MapClaims, err error) { func GetExpiry(tokenStr string, now time.Time) (expires bool, left time.Duration, err error) { - + if tokenStr == "" { + return true, 0, nil + } token, err := Parse(tokenStr) if err != nil { return false, 0, err @@ -120,7 +122,13 @@ func GetExpiry(tokenStr string, now time.Time) (expires bool, // GetUsername extracts the username claim value from the JWT func GetUsername(tokenStr string) (username string, ok bool) { - accessTkn, _ := Parse(tokenStr) + if tokenStr == "" { + return "", false + } + accessTkn, err := Parse(tokenStr) + if err != nil { + return "", false + } tknClaims, _ := MapClaims(accessTkn) u, ok := tknClaims["preferred_username"] if ok { diff --git a/pkg/core/cmdutil/profile/profile.go b/pkg/core/cmdutil/profile/profile.go index 9196fb492..04026e920 100644 --- a/pkg/core/cmdutil/profile/profile.go +++ b/pkg/core/cmdutil/profile/profile.go @@ -2,14 +2,9 @@ package profile import ( - "os" - "strconv" - "github.com/spf13/cobra" ) -const DevPreviewEnv = "RHOAS_DEV" - // ApplyDevPreviewLabel adds visual element displayed in help func ApplyDevPreviewLabel(cmd *cobra.Command) { cmd.Short = "[beta] " + cmd.Short @@ -24,15 +19,3 @@ func ApplyDevPreviewLabel(cmd *cobra.Command) { func DevPreviewAnnotation() map[string]string { return map[string]string{"channel": "preview"} } - -// DevModeEnabled Check if development mode is enabled -func DevModeEnabled() bool { - rawEnvVal := os.Getenv(DevPreviewEnv) - - boolVal, err := strconv.ParseBool(rawEnvVal) - if err != nil { - return false - } - - return boolVal -} diff --git a/pkg/core/localize/locales/en/cmd/login.en.toml b/pkg/core/localize/locales/en/cmd/login.en.toml index abeefd4c9..0562f1633 100644 --- a/pkg/core/localize/locales/en/cmd/login.en.toml +++ b/pkg/core/localize/locales/en/cmd/login.en.toml @@ -83,10 +83,6 @@ one = 'Redirected to callback URL: {{.URL}}' [login.log.info.loginSuccess] description = 'Log in success message' -one = 'You are now logged in as "{{.Username}}"' - -[login.log.info.loginSuccessNoUsername] -description = 'Log in success message' one = 'You are now logged in' [login.log.info.openSSOUrl]