Skip to content

Commit

Permalink
Merge pull request openconfig#470 from nokia/fix458
Browse files Browse the repository at this point in the history
Do not env expand "password" and "token" fields unless they start with '$'
  • Loading branch information
karimra authored Jun 25, 2024
2 parents d6866bb + aa35aed commit fcc4b8a
Show file tree
Hide file tree
Showing 5 changed files with 45 additions and 6 deletions.
2 changes: 1 addition & 1 deletion pkg/app/app.go
Original file line number Diff line number Diff line change
Expand Up @@ -222,7 +222,7 @@ func (a *App) PreRunE(cmd *cobra.Command, args []string) error {
}
a.Logger.SetOutput(logOutput)
a.Logger.SetFlags(flags)
a.Config.Address = config.SanitizeArrayFlagValue(a.Config.Address)
a.Config.Address = config.ParseAddressField(a.Config.Address)
a.Logger.Printf("version=%s, commit=%s, date=%s, gitURL=%s, docs=https://gnmic.openconfig.net", version, commit, date, gitURL)

if a.Config.Debug {
Expand Down
16 changes: 15 additions & 1 deletion pkg/config/config.go
Original file line number Diff line number Diff line change
Expand Up @@ -863,7 +863,21 @@ func SanitizeArrayFlagValue(ls []string) []string {
for strings.HasPrefix(ls[i], "[") && strings.HasSuffix(ls[i], "]") {
ls[i] = ls[i][1 : len(ls[i])-1]
}
res = append(res, strings.Split(ls[i], ",")...)
res = append(res, ls[i])
}
return res
}

func ParseAddressField(addr []string) []string {
res := make([]string, 0, len(addr))
for i := range addr {
if addr[i] == "[]" {
continue
}
for strings.HasPrefix(addr[i], "[") && strings.HasSuffix(addr[i], "]") {
addr[i] = addr[i][1 : len(addr[i])-1]
}
res = append(res, strings.Split(addr[i], ",")...)
}
return res
}
Expand Down
15 changes: 14 additions & 1 deletion pkg/config/environment.go
Original file line number Diff line number Diff line change
Expand Up @@ -59,8 +59,21 @@ func (c *Config) mergeEnvVars() {

func (c *Config) SetGlobalsFromEnv(cmd *cobra.Command) {
cmd.PersistentFlags().VisitAll(func(f *pflag.Flag) {
// expand password and token global attr only if they start with '$'
if f.Name == "password" || f.Name == "token" {
if !f.Changed && c.FileConfig.IsSet(f.Name) {
val := c.FileConfig.GetString(f.Name)
if strings.HasPrefix(val, "$") {
c.setFlagValue(cmd, f.Name, val)
}
}
return
}
// other global flags
if !f.Changed && c.FileConfig.IsSet(f.Name) {
c.setFlagValue(cmd, f.Name, os.ExpandEnv(c.FileConfig.GetString(f.Name)))
if val := os.ExpandEnv(c.FileConfig.GetString(f.Name)); val != "" {
c.setFlagValue(cmd, f.Name, val)
}
}
})
}
Expand Down
2 changes: 2 additions & 0 deletions tests/configs/gnmic_env.yaml
Original file line number Diff line number Diff line change
@@ -0,0 +1,2 @@
address: $CUSTOM_ADDR
skip-verify: $SKIPVER
16 changes: 13 additions & 3 deletions tests/env_vars.sh
Original file line number Diff line number Diff line change
Expand Up @@ -13,13 +13,15 @@ targets=clab-test1-srl1,clab-test1-srl2,clab-test1-srl3
./gnmic-rc1 -u admin -p NokiaSrl1! --skip-verify --debug -a $targets -e json_ietf \
set \
--update-path /system/configuration/role[name=readonly]/rule[path-reference="/"]/action \
--update-value "read"
--update-value "read" \
--update-path /system/aaa/authorization/role[rolename=readonly] \
--update-value '{"services": ["gnmi"]}'

# create a new user
./gnmic-rc1 -u admin -p NokiaSrl1! --skip-verify --debug -a $targets -e json_ietf \
set \
--update-path /system/aaa/authentication/user[username=user1]/password \
--update-value '|Bo|Z%TYe*&$P33~'
--update-value "|Bo|Z%TYe*&\$P33~"

# assign readonly role to the new user
./gnmic-rc1 -u admin -p NokiaSrl1! --skip-verify --debug -a $targets -e json_ietf \
Expand All @@ -33,7 +35,7 @@ targets=clab-test1-srl1,clab-test1-srl2,clab-test1-srl3
--path /system/name

# password from ENV
GNMIC_PASSWORD='|Bo|Z%TYe*&$P33~' ./gnmic-rc1 -u user1 --skip-verify --debug -a $targets -e json_ietf \
GNMIC_PASSWORD="|Bo|Z%TYe*&\$P33~" ./gnmic-rc1 -u user1 --skip-verify --debug -a $targets -e json_ietf \
get \
--path /system/name

Expand All @@ -56,3 +58,11 @@ GNMIC_USERNAME=user1 GNMIC_PASSWORD='|Bo|Z%TYe*&$P33~' GNMIC_DEBUG=true ./gnmic-
GNMIC_USERNAME=user1 GNMIC_PASSWORD='|Bo|Z%TYe*&$P33~' GNMIC_DEBUG=true GNMIC_SKIP_VERIFY=true GNMIC_ENCODING=json_ietf GNMIC_ADDRESS=$targets ./gnmic-rc1 \
get \
--path /system/name

## config file expansion
CUSTOM_ADDR=$targets GNMIC_USERNAME=user1 GNMIC_PASSWORD='|Bo|Z%TYe*&$P33~' GNMIC_SKIP_VERIFY=true GNMIC_ENCODING=json_ietf ./gnmic-rc1 --config configs/gnmic_env.yaml --debug \
get \
--path /system/name
CUSTOM_ADDR=$targets GNMIC_USERNAME=user1 GNMIC_PASSWORD='|Bo|Z%TYe*&$P33~' GNMIC_SKIP_VERIFY=true SKIPVER=false GNMIC_ENCODING=json_ietf ./gnmic-rc1 --config configs/gnmic_env.yaml --debug \
get \
--path /system/dns

0 comments on commit fcc4b8a

Please sign in to comment.