Skip to content

Commit c824855

Browse files
authored
Merge pull request #8113 from ipfs/feat/key-export-online
feat: allow key export in online mode
2 parents c54cdaa + 1d77f9d commit c824855

File tree

2 files changed

+30
-7
lines changed

2 files changed

+30
-7
lines changed

core/commands/keystore.go

Lines changed: 16 additions & 4 deletions
Original file line numberDiff line numberDiff line change
@@ -12,11 +12,13 @@ import (
1212

1313
cmds "github.com/ipfs/go-ipfs-cmds"
1414
config "github.com/ipfs/go-ipfs-config"
15+
keystore "github.com/ipfs/go-ipfs-keystore"
1516
oldcmds "github.com/ipfs/go-ipfs/commands"
1617
cmdenv "github.com/ipfs/go-ipfs/core/commands/cmdenv"
1718
"github.com/ipfs/go-ipfs/core/commands/e"
1819
ke "github.com/ipfs/go-ipfs/core/commands/keyencode"
1920
fsrepo "github.com/ipfs/go-ipfs/repo/fsrepo"
21+
migrations "github.com/ipfs/go-ipfs/repo/fsrepo/migrations"
2022
options "github.com/ipfs/interface-go-ipfs-core/options"
2123
"github.com/libp2p/go-libp2p-core/crypto"
2224
peer "github.com/libp2p/go-libp2p-core/peer"
@@ -150,7 +152,6 @@ path can be specified with '--output=<path>' or '-o=<path>'.
150152
cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."),
151153
},
152154
NoRemote: true,
153-
PreRun: DaemonNotRunning,
154155
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155156
name := req.Arguments[0]
156157

@@ -163,13 +164,24 @@ path can be specified with '--output=<path>' or '-o=<path>'.
163164
return err
164165
}
165166

166-
r, err := fsrepo.Open(cfgRoot)
167+
// Check repo version, and error out if not matching
168+
ver, err := migrations.RepoVersion(cfgRoot)
169+
if err != nil {
170+
return err
171+
}
172+
if ver != fsrepo.RepoVersion {
173+
return fmt.Errorf("key export expects repo version (%d) but found (%d)", fsrepo.RepoVersion, ver)
174+
}
175+
176+
// Export is read-only: safe to read it without acquiring repo lock
177+
// (this makes export work when ipfs daemon is already running)
178+
ksp := filepath.Join(cfgRoot, "keystore")
179+
ks, err := keystore.NewFSKeystore(ksp)
167180
if err != nil {
168181
return err
169182
}
170-
defer r.Close()
171183

172-
sk, err := r.Keystore().Get(name)
184+
sk, err := ks.Get(name)
173185
if err != nil {
174186
return fmt.Errorf("key with name '%s' doesn't exist", name)
175187
}

test/sharness/t0165-keystore.sh

Lines changed: 14 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -175,14 +175,25 @@ ipfs key rm key_ed25519
175175
test_cmp rsa_key_id roundtrip_rsa_key_id
176176
'
177177

178-
test_expect_success "online export rsa key" '
179-
test_must_fail ipfs key export generated_rsa_key
178+
# export works directly on the keystore present in IPFS_PATH
179+
test_expect_success "export and import ed25519 key while daemon is running" '
180+
edhash=$(ipfs key gen exported_ed25519_key --type=ed25519)
181+
echo $edhash > ed25519_key_id
182+
ipfs key export exported_ed25519_key &&
183+
ipfs key rm exported_ed25519_key &&
184+
ipfs key import exported_ed25519_key exported_ed25519_key.key > roundtrip_ed25519_key_id &&
185+
test_cmp ed25519_key_id roundtrip_ed25519_key_id
186+
'
187+
188+
test_expect_success "key export over HTTP /api/v0/key/export is not possible" '
189+
ipfs key gen nohttpexporttest_key --type=ed25519 &&
190+
curl -X POST -sI "http://$API_ADDR/api/v0/key/export&arg=nohttpexporttest_key" | grep -q "^HTTP/1.1 404 Not Found"
180191
'
181192

182193
test_expect_success "online rotate rsa key" '
183194
test_must_fail ipfs key rotate
184195
'
185-
196+
186197
test_kill_ipfs_daemon
187198

188199
}

0 commit comments

Comments
 (0)