core/cmds/config: do not show privkey on the network
License: MIT Signed-off-by: Jakub Sztandera <kubuxu@protonmail.ch>
Jakub Sztandera committed
Jul 8, 2016 at 16:20 UTC
011a546b9cdaf2a43f7855711558221a6374123d
2 files changed
+59
-14
core/commands/config.go
+42
-14
@@ -58,6 +58,14 @@ Set the value of the 'datastore.path' key:
58
args := req.Arguments()
59
key := args[0]
60
61
+ // This is a temporary fix until we move the private key out of the config file
62
+ switch key {
63
+ case "Identity", "Identity.PrivKey":
64
+ res.SetError(fmt.Errorf("cannot show or change private key through API"), cmds.ErrNormal)
65
+ return
66
+ default:
67
+ }
68
+
69
r, err := fsrepo.Open(req.InvocContext().ConfigRoot)
70
if err != nil {
71
res.SetError(err, cmds.ErrNormal)
@@ -134,18 +142,34 @@ included in the output of this command.
142
},
143
144
Run: func(req cmds.Request, res cmds.Response) {
137
- filename, err := config.Filename(req.InvocContext().ConfigRoot)
145
+ fname, err := config.Filename(req.InvocContext().ConfigRoot)
146
if err != nil {
147
res.SetError(err, cmds.ErrNormal)
148
return
149
}
150
143
- output, err := showConfig(filename)
151
+ data, err := ioutil.ReadFile(fname)
152
if err != nil {
153
res.SetError(err, cmds.ErrNormal)
154
return
155
}
148
- res.SetOutput(output)
156
+
157
+ var cfg map[string]interface{}
158
+ err = json.Unmarshal(data, &cfg)
159
+ if err != nil {
160
+ res.SetError(err, cmds.ErrNormal)
161
+ return
162
+ }
163
+
164
+ cfg["Identity"].(map[string]interface{})["PrivKey"] = nil
165
+
166
+ output, err := config.HumanOutput(cfg)
167
+ if err != nil {
168
+ res.SetError(err, cmds.ErrNormal)
169
+ return
170
+ }
171
+
172
+ res.SetOutput(bytes.NewReader(output))
173
},
174
}
175
@@ -219,22 +243,20 @@ func getConfig(r repo.Repo, key string) (*ConfigField, error) {
243
}
244
245
func setConfig(r repo.Repo, key string, value interface{}) (*ConfigField, error) {
222
- err := r.SetConfigKey(key, value)
246
+ keyF, err := getConfig(r, "Identity.PrivKey")
247
+ if err != nil {
248
+ return nil, fmt.Errorf("Failed to get PrivKey")
249
+ }
250
+ privkey := keyF.Value
251
+ err = r.SetConfigKey(key, value)
252
if err != nil {
253
return nil, fmt.Errorf("Failed to set config value: %s (maybe use --json?)", err)
254
}
226
- return getConfig(r, key)
227
-}
228
-
229
-func showConfig(filename string) (io.Reader, error) {
230
- // TODO maybe we should omit privkey so we don't accidentally leak it?
231
-
232
- data, err := ioutil.ReadFile(filename)
255
+ err = r.SetConfigKey("Identity.PrivKey", privkey)
256
if err != nil {
234
- return nil, err
257
+ return nil, fmt.Errorf("Failed to set PrivKey")
258
}
236
-
237
- return bytes.NewReader(data), nil
259
+ return getConfig(r, key)
260
}
261
262
func editConfig(filename string) error {
@@ -254,5 +276,11 @@ func replaceConfig(r repo.Repo, file io.Reader) error {
276
return errors.New("Failed to decode file as config")
277
}
278
279
+ keyF, err := getConfig(r, "Identity.PrivKey")
280
+ if err != nil {
281
+ return fmt.Errorf("Failed to get PrivKey")
282
+ }
283
+ cfg.Identity.PrivKey = keyF.Value.(string)
284
+
285
return r.SetConfig(&cfg)
286
}
test/sharness/t0021-config.sh
+17
@@ -71,6 +71,23 @@ test_config_cmd() {
71
grep "\"beep2\": false," actual &&
72
grep "\"beep3\": false," actual
73
'
74
+
75
+ test_expect_success "'ipfs config Identity' fails" '
76
+ test_expect_code 1 ipfs config Identity 2> ident_out
77
+ '
78
+
79
+ test_expect_success "output looks good" '
80
+ echo "Error: cannot show private key through API" > ident_exp &&
81
+ test_cmp ident_exp ident_out
82
+ '
83
+
84
+ test_expect_success "'ipfs config Identity.PrivKey' fails" '
85
+ test_expect_code 1 ipfs config Identity.PrivKey 2> ident_out
86
+ '
87
+
88
+ test_expect_success "output looks good" '
89
+ test_cmp ident_exp ident_out
90
+ '
91
}
92
93
test_init_ipfs