Skip to content

Commit 6ee3ff0

Browse files
authored
fix: add env var config (#1640)
1 parent 39141a1 commit 6ee3ff0

File tree

6 files changed

+25
-67
lines changed

6 files changed

+25
-67
lines changed

CONTRIBUTING.md

+8
Original file line numberDiff line numberDiff line change
@@ -159,6 +159,14 @@ Once pushed, a [GitHub Action](https://github.com/redhat-developer/app-services-
159159

160160
> NOTE: To create a pre-release, the tag should have appropriate suffix, e.g v0.20.1-alpha1
161161
162+
### Environment variables
163+
164+
RHOASCONFIG="./config.json" - custom configuration location (useful for testing)
165+
RHOAS_CONTEXT="./context.json" - custom context location
166+
RHOAS_TELEMETRY=false - Enables/Disables telemetry (should happen automatically in non tty sessions)
167+
EDITOR=Code -w - controls CLI editor
168+
KUBECONFIG=./config.json - custom kubernetes config used for other commands
169+
162170
### Changelog generation
163171

164172
[git-chglog](https://github.com/git-chglog/git-chglog) is used to generate a changelog for the current release.

cmd/rhoas/main.go

-3
Original file line numberDiff line numberDiff line change
@@ -50,9 +50,6 @@ func main() {
5050
err = executeCommandWithTelemetry(rootCmd, cmdFactory)
5151

5252
if err == nil {
53-
if flagutil.DebugEnabled() {
54-
build.CheckForUpdate(cmdFactory.Context, build.Version, cmdFactory.Logger, localizer)
55-
}
5653
return
5754
}
5855
cmdFactory.Logger.Errorf("%v\n", rootError(err, localizer))

pkg/cmd/login/login.go

+7-41
Original file line numberDiff line numberDiff line change
@@ -8,8 +8,6 @@ import (
88
"net/url"
99

1010
"github.com/redhat-developer/app-services-cli/pkg/core/auth/login"
11-
"github.com/redhat-developer/app-services-cli/pkg/core/auth/token"
12-
"github.com/redhat-developer/app-services-cli/pkg/core/cmdutil/flagutil"
1311

1412
"github.com/redhat-developer/app-services-cli/pkg/core/config"
1513
"github.com/redhat-developer/app-services-cli/pkg/core/ioutil/icon"
@@ -156,67 +154,35 @@ func runLogin(opts *options) (err error) {
156154
return err
157155
}
158156
}
159-
160-
if opts.offlineToken != "" {
161-
if err = loginWithOfflineToken(opts); err != nil {
162-
spinner.Stop()
163-
opts.Logger.Info()
164-
return err
165-
}
166-
}
167157
spinner.Stop()
168158

169159
cfg, err := opts.Config.Load()
170160
if err != nil {
171161
return err
172162
}
173163

164+
if opts.offlineToken != "" {
165+
cfg.RefreshToken = opts.offlineToken
166+
}
167+
174168
cfg.APIUrl = gatewayURL.String()
175169
cfg.Insecure = opts.insecureSkipTLSVerify
176170
cfg.ClientID = opts.clientID
177171
cfg.AuthURL = opts.authURL
178172
cfg.Scopes = opts.scopes
173+
// Reset access token on login to avoid reusing previous users valid token
174+
cfg.AccessToken = ""
179175

180176
if err = opts.Config.Save(cfg); err != nil {
181177
return err
182178
}
183179

184-
username, ok := token.GetUsername(cfg.AccessToken)
185-
186180
opts.Logger.Info()
187-
if !ok {
188-
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccessNoUsername"))
189-
} else {
190-
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess", localize.NewEntry("Username", username)))
191-
}
192-
193-
// debug mode checks this for a version update also.
194-
// so we check if is enabled first so as not to print it twice
195-
if !flagutil.DebugEnabled() {
196-
build.CheckForUpdate(opts.Context, build.Version, opts.Logger, opts.localizer)
197-
}
181+
opts.Logger.Info(icon.SuccessPrefix(), opts.localizer.MustLocalize("login.log.info.loginSuccess"))
198182

199183
return nil
200184
}
201185

202-
func loginWithOfflineToken(opts *options) (err error) {
203-
cfg, err := opts.Config.Load()
204-
if err != nil {
205-
return err
206-
}
207-
cfg.Insecure = opts.insecureSkipTLSVerify
208-
cfg.ClientID = opts.clientID
209-
cfg.AuthURL = opts.authURL
210-
cfg.Scopes = opts.scopes
211-
cfg.RefreshToken = opts.offlineToken
212-
213-
if err = opts.Config.Save(cfg); err != nil {
214-
return err
215-
}
216-
217-
return err
218-
}
219-
220186
func createTransport(insecure bool) *http.Transport {
221187
// #nosec 402
222188
return &http.Transport{

pkg/core/auth/token/token.go

+10-2
Original file line numberDiff line numberDiff line change
@@ -88,7 +88,9 @@ func MapClaims(token *jwt.Token) (claims jwt.MapClaims, err error) {
8888

8989
func GetExpiry(tokenStr string, now time.Time) (expires bool,
9090
left time.Duration, err error) {
91-
91+
if tokenStr == "" {
92+
return true, 0, nil
93+
}
9294
token, err := Parse(tokenStr)
9395
if err != nil {
9496
return false, 0, err
@@ -120,7 +122,13 @@ func GetExpiry(tokenStr string, now time.Time) (expires bool,
120122

121123
// GetUsername extracts the username claim value from the JWT
122124
func GetUsername(tokenStr string) (username string, ok bool) {
123-
accessTkn, _ := Parse(tokenStr)
125+
if tokenStr == "" {
126+
return "", false
127+
}
128+
accessTkn, err := Parse(tokenStr)
129+
if err != nil {
130+
return "", false
131+
}
124132
tknClaims, _ := MapClaims(accessTkn)
125133
u, ok := tknClaims["preferred_username"]
126134
if ok {

pkg/core/cmdutil/profile/profile.go

-17
Original file line numberDiff line numberDiff line change
@@ -2,14 +2,9 @@
22
package profile
33

44
import (
5-
"os"
6-
"strconv"
7-
85
"github.com/spf13/cobra"
96
)
107

11-
const DevPreviewEnv = "RHOAS_DEV"
12-
138
// ApplyDevPreviewLabel adds visual element displayed in help
149
func ApplyDevPreviewLabel(cmd *cobra.Command) {
1510
cmd.Short = "[beta] " + cmd.Short
@@ -24,15 +19,3 @@ func ApplyDevPreviewLabel(cmd *cobra.Command) {
2419
func DevPreviewAnnotation() map[string]string {
2520
return map[string]string{"channel": "preview"}
2621
}
27-
28-
// DevModeEnabled Check if development mode is enabled
29-
func DevModeEnabled() bool {
30-
rawEnvVal := os.Getenv(DevPreviewEnv)
31-
32-
boolVal, err := strconv.ParseBool(rawEnvVal)
33-
if err != nil {
34-
return false
35-
}
36-
37-
return boolVal
38-
}

pkg/core/localize/locales/en/cmd/login.en.toml

-4
Original file line numberDiff line numberDiff line change
@@ -83,10 +83,6 @@ one = 'Redirected to callback URL: {{.URL}}'
8383

8484
[login.log.info.loginSuccess]
8585
description = 'Log in success message'
86-
one = 'You are now logged in as "{{.Username}}"'
87-
88-
[login.log.info.loginSuccessNoUsername]
89-
description = 'Log in success message'
9086
one = 'You are now logged in'
9187

9288
[login.log.info.openSSOUrl]

0 commit comments

Comments
 (0)