cmd: harden config show with key
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Aug 29, 2016 at 19:06 UTC
671425ea3321df62ce6719f03f1ce3f633494e16
2 files changed
+29
-4
core/commands/config.go
+17
-2
@@ -15,6 +15,7 @@ import (
15
repo "github.com/ipfs/go-ipfs/repo"
16
config "github.com/ipfs/go-ipfs/repo/config"
17
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
18
+
19
u "gx/ipfs/QmZNVWh8LLjAavuQ2JXuFmuYH3C11xo988vSgp7UQrTRj1/go-ipfs-util"
20
)
21
@@ -164,11 +165,25 @@ included in the output of this command.
165
166
idmap, ok := cfg["Identity"].(map[string]interface{})
167
if !ok {
167
- res.SetError(fmt.Errorf("config has no identity"), cmds.ErrNormal)
168
+ res.SetError(errors.New("config has no identity"), cmds.ErrNormal)
169
return
170
}
171
171
- delete(idmap, "PrivKey")
172
+ privKeyKey := "" // make sure we both find the name of privkey and we delete it
173
+ for key, _ := range idmap {
174
+ if strings.ToLower(key) == "privkey" {
175
+ if privKeyKey != "" {
176
+ res.SetError(errors.New("found multiple PrivKey keys"), cmds.ErrNormal)
177
+ return
178
+ }
179
+ privKeyKey = key
180
+ }
181
+ }
182
+ if privKeyKey == "" {
183
+ res.SetError(errors.New("haven't found PriveKey key"), cmds.ErrNormal)
184
+ }
185
+
186
+ delete(idmap, privKeyKey)
187
188
output, err := config.HumanOutput(cfg)
189
if err != nil {
test/sharness/t0021-config.sh
+12
-2
@@ -111,10 +111,10 @@ test_config_cmd() {
111
112
test_expect_success "'ipfs config replace' injects privkey back" '
113
ipfs config replace show_config &&
114
- grep "\"PrivKey\":" "$IPFS_PATH/config" | grep -e ": \".\+\"" >/dev/null
114
+ grep "\"PrivKey\":" "$IPFS_PATH/config" | grep -e ": \".\+\"" >/dev/null
115
'
116
117
- test_expect_success "'ipfs config replace' with privkey erors out" '
117
+ test_expect_success "'ipfs config replace' with privkey errors out" '
118
cp "$IPFS_PATH/config" real_config &&
119
test_expect_code 1 ipfs config replace - < real_config 2> replace_out
120
'
@@ -124,6 +124,16 @@ test_config_cmd() {
124
test_cmp replace_out replace_expected
125
'
126
127
+ test_expect_success "'ipfs config replace' with lower case privkey errors out" '
128
+ cp "$IPFS_PATH/config" real_config &&
129
+ sed -i -e '\''s/PrivKey/privkey/'\'' real_config &&
130
+ test_expect_code 1 ipfs config replace - < real_config 2> replace_out
131
+ '
132
+
133
+ test_expect_success "output looks good" '
134
+ echo "Error: setting private key with API is not supported" > replace_expected
135
+ test_cmp replace_out replace_expected
136
+ '
137
}
138
139
test_init_ipfs