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
This commit is contained in:
Marcin Rataj 2021-05-07 16:26:23 +02:00
parent b8de0e8ad2
commit a158036dee
No known key found for this signature in database
GPG Key ID: 222B6784D5A79E42
2 changed files with 13 additions and 6 deletions

View File

@ -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=<path>' or '-o=<path>'.
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=<path>' or '-o=<path>'.
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)
}

View File

@ -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" '