@cryptotaxi247 / kubo / commits / a158036de

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

Marcin Rataj committed May 7, 2021 at 16:26 UTC a158036deefaa1da2b17f4596ecc067df12a4b3d
2 files changed +13 -6
core/commands/keystore.go
+6 -4
@@ -12,6 +12,7 @@ import (
12
13 cmds "github.com/ipfs/go-ipfs-cmds"
14 config "github.com/ipfs/go-ipfs-config"
15 + keystore "github.com/ipfs/go-ipfs-keystore"
16 oldcmds "github.com/ipfs/go-ipfs/commands"
17 cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
18 "github.com/ipfs/go-ipfs/core/commands/e"
@@ -150,7 +151,6 @@ path can be specified with '--output=<path>' or '-o=<path>'.
151 cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."),
152 },
153 NoRemote: true,
153 - PreRun: DaemonNotRunning,
154 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155 name := req.Arguments[0]
156
@@ -163,13 +163,15 @@ path can be specified with '--output=<path>' or '-o=<path>'.
163 return err
164 }
165
166 - r, err := fsrepo.Open(cfgRoot)
166 + // Export is read-only: safe to read it without acquiring repo lock
167 + // (this makes export work when ipfs daemon is already running)
168 + ksp := filepath.Join(cfgRoot, "keystore")
169 + ks, err := keystore.NewFSKeystore(ksp)
170 if err != nil {
171 return err
172 }
170 - defer r.Close()
173
172 - sk, err := r.Keystore().Get(name)
174 + sk, err := ks.Get(name)
175 if err != nil {
176 return fmt.Errorf("key with name '%s' doesn't exist", name)
177 }
test/sharness/t0165-keystore.sh
+7 -2
@@ -175,8 +175,13 @@ ipfs key rm key_ed25519
175 test_cmp rsa_key_id roundtrip_rsa_key_id
176 '
177
178 - test_expect_success "online export rsa key" '
179 - test_must_fail ipfs key export generated_rsa_key
178 + test_expect_success "export and import ed25519 key while daemon is running" '
179 + edhash=$(ipfs key gen exported_ed25519_key --type=ed25519)
180 + echo $edhash > ed25519_key_id
181 + ipfs key export exported_ed25519_key &&
182 + ipfs key rm exported_ed25519_key &&
183 + ipfs key import exported_ed25519_key exported_ed25519_key.key > roundtrip_ed25519_key_id &&
184 + test_cmp ed25519_key_id roundtrip_ed25519_key_id
185 '
186
187 test_expect_success "online rotate rsa key" '