Skip to content

Commit 7eb42ab

Browse files
Expose access token environment variable without leaking its value in… (#335)
* Expose access token environment variable without leaking its value in help output Signed-off-by: bpatra3 <bpatra3@opentext.com> * feat: add zip package installation to Dockerfile Signed-off-by: bpatra3 <bpatra3@opentext.com> --------- Signed-off-by: bpatra3 <bpatra3@opentext.com>
1 parent b33edd6 commit 7eb42ab

3 files changed

Lines changed: 44 additions & 1 deletion

File tree

‎build/docker/debian.Dockerfile‎

Lines changed: 1 addition & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -134,6 +134,7 @@ RUN apt -y update && apt -y install --no-install-recommends \
134134
tar \
135135
uuid-dev \
136136
xz-utils \
137+
zip \
137138
zlib1g && \
138139
curl -O https://download.swift.org/swiftly/linux/swiftly-$(uname -m).tar.gz && \
139140
tar zxf swiftly-$(uname -m).tar.gz && \

‎internal/cmd/root/root.go‎

Lines changed: 4 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -40,10 +40,13 @@ Complete documentation is available at https://docs.debricked.com/tools-and-inte
4040
&accessToken,
4141
OldAccessTokenFlag,
4242
"t",
43-
viper.GetString(AccessTokenFlag),
43+
"",
4444
`Debricked access token.
4545
Read more: https://docs.debricked.com/product/administration/generate-access-token`,
4646
)
47+
// Set the env var value directly on the variable (not as the flag's registered default) so it
48+
// isn't echoed back in --help/usage output, while still allowing -t/--access-token to override it.
49+
accessToken = viper.GetString(AccessTokenFlag)
4750

4851
var debClient = container.DebClient()
4952
debClient.SetAccessToken(&accessToken)

‎internal/cmd/root/root_test.go‎

Lines changed: 39 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -1,6 +1,7 @@
11
package root
22

33
import (
4+
"bytes"
45
"testing"
56

67
"github.com/debricked/cli/internal/wire"
@@ -52,3 +53,41 @@ func TestPreRun(t *testing.T) {
5253
cmd := NewRootCmd("", wire.GetCliContainer())
5354
cmd.PreRun(cmd, nil)
5455
}
56+
57+
func TestAccessTokenFlagDoesNotLeakEnvValueAsDefault(t *testing.T) {
58+
t.Setenv("DEBRICKED_TOKEN", "supersecrettoken")
59+
60+
cmd := NewRootCmd("v0.0.0", wire.GetCliContainer())
61+
62+
flag := cmd.PersistentFlags().Lookup(OldAccessTokenFlag)
63+
assert.NotNil(t, flag)
64+
assert.Empty(t, flag.DefValue, "flag default must not expose the env var value")
65+
assert.NotContains(t, cmd.UsageString(), "supersecrettoken")
66+
}
67+
68+
func TestAccessTokenFallsBackToEnvValue(t *testing.T) {
69+
t.Setenv("DEBRICKED_TOKEN", "env-token")
70+
71+
NewRootCmd("v0.0.0", wire.GetCliContainer())
72+
73+
assert.Equal(t, "env-token", accessToken, "env var must still be used when the flag is omitted")
74+
}
75+
76+
func TestAccessTokenFlagOverridesEnvValue(t *testing.T) {
77+
cases := []string{"--" + OldAccessTokenFlag, "-t"}
78+
for _, flagName := range cases {
79+
t.Run(flagName, func(t *testing.T) {
80+
t.Setenv("DEBRICKED_TOKEN", "env-token")
81+
82+
cmd := NewRootCmd("v0.0.0", wire.GetCliContainer())
83+
output := &bytes.Buffer{}
84+
cmd.SetOut(output)
85+
cmd.SetErr(output)
86+
cmd.SetArgs([]string{flagName, "flag-token"})
87+
88+
assert.NoError(t, cmd.Execute())
89+
assert.Equal(t, "flag-token", accessToken, "flag must take precedence over the env var")
90+
assert.NotContains(t, output.String(), "env-token", "help output must not contain the env var value")
91+
})
92+
}
93+
}

0 commit comments

Comments
 (0)