@cryptotaxi247 / kubo / commits / 6f6f04543

Improve error message when running key command that locks repo (#7821)

* Improve error message when running key commands that must be run when the daemon is not already running Fixes Issue #7814 `ipfs key export` now does a PreRun check like `ipfs key rotate` was to give a better error to the user then "someone else has the lock" in the event that the daemon is running while trying to execute these offline-only commands. While unlikely the "someone else has the lock" error can still be shown if two processes try and grab the repo lock at the same time. This PreRun function is also exported so it can be used by `ipfs init` where it was originally copied from. * Added more `ipfs key` command tests When daemon is running: - Test that import works - Test that export fails - Test that rotate fails

Andrew Gillis committed Jan 28, 2021 at 12:15 UTC 6f6f04543b07fcae08fd8b516082672d730ba8cd
3 files changed +40 -32
cmd/ipfs/init.go
+1 -16
@@ -69,22 +69,7 @@ environment variable:
69 },
70 NoRemote: true,
71 Extra: commands.CreateCmdExtras(commands.SetDoesNotUseRepo(true), commands.SetDoesNotUseConfigAsInput(true)),
72 - PreRun: func(req *cmds.Request, env cmds.Environment) error {
73 - cctx := env.(*oldcmds.Context)
74 - daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot)
75 - if err != nil {
76 - return err
77 - }
78 -
79 - log.Info("checking if daemon is running...")
80 - if daemonLocked {
81 - log.Debug("ipfs daemon is running")
82 - e := "ipfs daemon is running. please stop it to run this command"
83 - return cmds.ClientError(e)
84 - }
85 -
86 - return nil
87 - },
72 + PreRun: commands.DaemonNotRunning,
73 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
74 cctx := env.(*oldcmds.Context)
75 empty, _ := req.Options[emptyRepoOptionName].(bool)
core/commands/keystore.go
+21 -16
@@ -150,6 +150,7 @@ path can be specified with '--output=<path>' or '-o=<path>'.
150 cmds.StringOption(outputOptionName, "o", "The path where the output should be stored."),
151 },
152 NoRemote: true,
153 + PreRun: DaemonNotRunning,
154 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
155 name := req.Arguments[0]
156
@@ -459,22 +460,7 @@ environment variable:
460 cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"),
461 },
462 NoRemote: true,
462 - PreRun: func(req *cmds.Request, env cmds.Environment) error {
463 - cctx := env.(*oldcmds.Context)
464 - daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot)
465 - if err != nil {
466 - return err
467 - }
468 -
469 - log.Info("checking if daemon is running...")
470 - if daemonLocked {
471 - log.Debug("ipfs daemon is running")
472 - e := "ipfs daemon is running. please stop it to run this command"
473 - return cmds.ClientError(e)
474 - }
475 -
476 - return nil
477 - },
463 + PreRun: DaemonNotRunning,
464 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
465 cctx := env.(*oldcmds.Context)
466 nBitsForKeypair, nBitsGiven := req.Options[keyStoreSizeOptionName].(int)
@@ -556,3 +542,22 @@ func keyOutputListEncoders() cmds.EncoderFunc {
542 return nil
543 })
544 }
545 +
546 +// DaemonNotRunning checks to see if the ipfs repo is locked, indicating that
547 +// the daemon is running, and returns and error if the daemon is running.
548 +func DaemonNotRunning(req *cmds.Request, env cmds.Environment) error {
549 + cctx := env.(*oldcmds.Context)
550 + daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot)
551 + if err != nil {
552 + return err
553 + }
554 +
555 + log.Info("checking if daemon is running...")
556 + if daemonLocked {
557 + log.Debug("ipfs daemon is running")
558 + e := "ipfs daemon is running. please stop it to run this command"
559 + return cmds.ClientError(e)
560 + }
561 +
562 + return nil
563 +}
test/sharness/t0165-keystore.sh
+18
@@ -167,6 +167,24 @@ ipfs key rm key_ed25519
167 test_must_fail ipfs key rename -f fooed self 2>&1 | tee key_rename_out &&
168 grep -q "Error: cannot overwrite key with name" key_rename_out
169 '
170 +
171 + test_launch_ipfs_daemon
172 +
173 + test_expect_success "online import rsa key" '
174 + ipfs key import generated_rsa_key generated_rsa_key.key > roundtrip_rsa_key_id &&
175 + test_cmp rsa_key_id roundtrip_rsa_key_id
176 + '
177 +
178 + test_must_fail "online export rsa key" '
179 + ipfs key export generated_rsa_key
180 + '
181 +
182 + test_must_fail "online rotate rsa key" '
183 + ipfs key rotate
184 + '
185 +
186 + test_kill_ipfs_daemon
187 +
188 }
189
190 test_check_rsa2048_sk() {