Skip to content

Commit 6ada21c

Browse files
committed
cmd/cue: reject login --token=""
Don't go straight into the OAuth device flow as if the flag had not been given at all, because that can be rather confusing in the case where a developer used --token=${UNSET_VAR}. An alternative would have been a custom pflag.Value implementaion that recorded whether the string value was set via a boolean field. However, that feels unnecessary given that looping over the set flags is pretty easy and works with any flag of any type already. The linear looping is fine given that we don't expect CLI users to set more than a dozen or so flags at any time. Fixes #3665. Signed-off-by: Daniel Martí <[email protected]> Change-Id: I7de34e3b3503d6fd495e10ec9f64851f2a6ddd11 Reviewed-on: https://review.gerrithub.io/c/cue-lang/cue/+/1207558 Unity-Result: CUE porcuepine <[email protected]> Reviewed-by: Paul Jolly <[email protected]> TryBot-Result: CUEcueckoo <[email protected]>
1 parent 18f0ae8 commit 6ada21c

File tree

3 files changed

+22
-0
lines changed

3 files changed

+22
-0
lines changed

cmd/cue/cmd/flags.go

Lines changed: 13 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -130,6 +130,19 @@ func (f flagName) ensureAdded(cmd *Command) {
130130
}
131131
}
132132

133+
// IsSet reports whether f was provided by the user, which is useful to tell
134+
// `cue` apart from `cue --flag=` when the flag's default value is the zero value.
135+
func (f flagName) IsSet(cmd *Command) bool {
136+
f.ensureAdded(cmd)
137+
found := false
138+
cmd.Flags().Visit(func(pf *pflag.Flag) {
139+
if pf.Name == string(f) {
140+
found = true
141+
}
142+
})
143+
return found
144+
}
145+
133146
func (f flagName) Bool(cmd *Command) bool {
134147
f.ensureAdded(cmd)
135148
v, _ := cmd.Flags().GetBool(string(f))

cmd/cue/cmd/login.go

Lines changed: 5 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -91,6 +91,11 @@ inside $CUE_CONFIG_DIR; see 'cue help environment'.
9191
switch tokenStr := flagToken.String(cmd); {
9292
case tokenStr == "":
9393
// By default, we perform the OAuth 2.0 device flow to obtain a new token.
94+
// Note that we refuse to continue if the user set --token="",
95+
// because that can be an easy mistake to make via --token=${UNSET_VAR}.
96+
if flagToken.IsSet(cmd) {
97+
return fmt.Errorf("the --token flag needs a non-empty string")
98+
}
9499

95100
ctx := backgroundContext()
96101
// Cause the oauth2 logic to log HTTP requests when logging is enabled.

cmd/cue/cmd/testdata/script/login_token.txtar

Lines changed: 4 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -7,6 +7,10 @@ oauthregistry device-code-expired
77
! exec cue login --token=unrecognized_token_format_1234
88
stderr -count=1 'unknown token format, expected an appv1_ prefix'
99

10+
# An empty token is rejected, to prevent issues like --token=${UNSET_VAR}.
11+
! exec cue login --token=
12+
stderr -count=1 'the --token flag needs a non-empty string'
13+
1014
# Ensure that only one token is stored when starting from an empty logins.json file.
1115
exec cue login --token=appv1_validtoken1234
1216
grep -count=1 '"registries": {' cueconfig/logins.json

0 commit comments

Comments
 (0)