Skip to content

Commit

Permalink
fix: add env var config (#1640)
Browse files Browse the repository at this point in the history
  • Loading branch information
wtrocki authored Jul 13, 2022
1 parent 39141a1 commit 6ee3ff0
Show file tree
Hide file tree
Showing 6 changed files with 25 additions and 67 deletions.
8 changes: 8 additions & 0 deletions CONTRIBUTING.md
Original file line number Diff line number Diff line change
Expand Up @@ -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.
Expand Down
3 changes: 0 additions & 3 deletions cmd/rhoas/main.go
Original file line number Diff line number Diff line change
Expand Up @@ -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))
Expand Down
48 changes: 7 additions & 41 deletions pkg/cmd/login/login.go
Original file line number Diff line number Diff line change
Expand Up @@ -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"
Expand Down Expand Up @@ -156,67 +154,35 @@ 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()
if err != nil {
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{
Expand Down
12 changes: 10 additions & 2 deletions pkg/core/auth/token/token.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand Down Expand Up @@ -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 {
Expand Down
17 changes: 0 additions & 17 deletions pkg/core/cmdutil/profile/profile.go
Original file line number Diff line number Diff line change
Expand Up @@ -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
Expand All @@ -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
}
4 changes: 0 additions & 4 deletions pkg/core/localize/locales/en/cmd/login.en.toml
Original file line number Diff line number Diff line change
Expand Up @@ -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]
Expand Down

0 comments on commit 6ee3ff0

Please sign in to comment.