@cryptotaxi247 / kubo / commits / 64cdabc88

feat: stop apply command exposing private key over HTTP API

License: MIT Signed-off-by: chenminjian <727180553@qq.com>

chenminjian committed Oct 10, 2018 at 11:16 UTC 64cdabc88fbb88cde5f5bd5056e2d0f6524d11d7
2 files changed +43 -5
core/commands/config.go
+33 -5
@@ -23,8 +23,8 @@ import (
23
24 // ConfigUpdateOutput is config profile apply command's output
25 type ConfigUpdateOutput struct {
26 - Old config.Config
27 - New config.Config
26 + OldCfg map[string]interface{}
27 + NewCfg map[string]interface{}
28 }
29
30 type ConfigField struct {
@@ -359,9 +359,22 @@ var configProfileApplyCmd = &cmds.Command{
359 res.SetError(err, cmdkit.ErrNormal)
360 return
361 }
362 +
363 + oldCfgMap, err := scrubPrivKey(oldCfg)
364 + if err != nil {
365 + res.SetError(err, cmdkit.ErrNormal)
366 + return
367 + }
368 +
369 + newCfgMap, err := scrubPrivKey(newCfg)
370 + if err != nil {
371 + res.SetError(err, cmdkit.ErrNormal)
372 + return
373 + }
374 +
375 res.SetOutput(&ConfigUpdateOutput{
363 - Old: *oldCfg,
364 - New: *newCfg,
376 + OldCfg: oldCfgMap,
377 + NewCfg: newCfgMap,
378 })
379 },
380 Marshalers: cmds.MarshalerMap{
@@ -380,7 +393,7 @@ var configProfileApplyCmd = &cmds.Command{
393 return nil, e.TypeErr(apply, v)
394 }
395
383 - diff := jsondiff.Compare(apply.Old, apply.New)
396 + diff := jsondiff.Compare(apply.OldCfg, apply.NewCfg)
397 buf := jsondiff.Format(diff)
398
399 return strings.NewReader(string(buf)), nil
@@ -404,6 +417,21 @@ func buildProfileHelp() string {
417 return out
418 }
419
420 +// scrubPrivKey scrubs private key for security reasons.
421 +func scrubPrivKey(cfg *config.Config) (map[string]interface{}, error) {
422 + cfgMap, err := config.ToMap(cfg)
423 + if err != nil {
424 + return nil, err
425 + }
426 +
427 + err = scrubValue(cfgMap, []string{config.IdentityTag, config.PrivKeyTag})
428 + if err != nil {
429 + return nil, err
430 + }
431 +
432 + return cfgMap, nil
433 +}
434 +
435 // transformConfig returns old config and new config instead of difference between they,
436 // because apply command can provide stable API through this way.
437 // If dryRun is true, repo's config should not be updated and persisted
test/sharness/t0021-config.sh
+10
@@ -262,6 +262,16 @@ test_config_cmd() {
262 test `grep "DisableNatPortMap" diff_info | wc -l` = 2
263 '
264
265 + test_expect_success "'ipfs config profile apply test --dry-run' doesn't include privkey" '
266 + ipfs config profile apply test --dry-run > show_config &&
267 + test_expect_code 1 grep PrivKey show_config
268 + '
269 +
270 + test_expect_success "'ipfs config profile apply test' doesn't include privkey" '
271 + ipfs config profile apply test > show_config &&
272 + test_expect_code 1 grep PrivKey show_config
273 + '
274 +
275 # won't work as it changes datastore definition, which makes ipfs not launch
276 # without converting first
277 # test_profile_apply_revert badgerds