From a158036deefaa1da2b17f4596ecc067df12a4b3d Mon Sep 17 00:00:00 2001 From: Marcin Rataj Date: Fri, 7 May 2021 16:26:23 +0200 Subject: [PATCH] feat: allow key export in online mode Export does not require repo lock and it is safe to do even when ipfs daemon is running. This enables apps like Brave browser to do import/export without stopping/starting daemon. Ref. https://github.com/brave/brave-browser/issues/15422 --- core/commands/keystore.go | 10 ++++++---- test/sharness/t0165-keystore.sh | 9 +++++++-- 2 files changed, 13 insertions(+), 6 deletions(-) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 85c6ae1a3..8a806113c 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -12,6 +12,7 @@ import ( cmds "github.com/ipfs/go-ipfs-cmds" config "github.com/ipfs/go-ipfs-config" + keystore "github.com/ipfs/go-ipfs-keystore" oldcmds "github.com/ipfs/go-ipfs/commands" cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv" "github.com/ipfs/go-ipfs/core/commands/e" @@ -150,7 +151,6 @@ path can be specified with '--output=' or '-o='. cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."), }, NoRemote: true, - PreRun: DaemonNotRunning, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { name := req.Arguments[0] @@ -163,13 +163,15 @@ path can be specified with '--output=' or '-o='. return err } - r, err := fsrepo.Open(cfgRoot) + // Export is read-only: safe to read it without acquiring repo lock + // (this makes export work when ipfs daemon is already running) + ksp := filepath.Join(cfgRoot, "keystore") + ks, err := keystore.NewFSKeystore(ksp) if err != nil { return err } - defer r.Close() - sk, err := r.Keystore().Get(name) + sk, err := ks.Get(name) if err != nil { return fmt.Errorf("key with name '%s' doesn't exist", name) } diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index b3ae12fef..0bd8f74e6 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -175,8 +175,13 @@ ipfs key rm key_ed25519 test_cmp rsa_key_id roundtrip_rsa_key_id ' - test_expect_success "online export rsa key" ' - test_must_fail ipfs key export generated_rsa_key + test_expect_success "export and import ed25519 key while daemon is running" ' + edhash=$(ipfs key gen exported_ed25519_key --type=ed25519) + echo $edhash > ed25519_key_id + ipfs key export exported_ed25519_key && + ipfs key rm exported_ed25519_key && + ipfs key import exported_ed25519_key exported_ed25519_key.key > roundtrip_ed25519_key_id && + test_cmp ed25519_key_id roundtrip_ed25519_key_id ' test_expect_success "online rotate rsa key" '