diff --git a/cmd/ipfs/init.go b/cmd/ipfs/init.go index 0842b71976df7ac3fdc303de735e45e892a5e10c..f419604f2e38d7d550340d46ecf474915d49b2d6 100644 --- a/cmd/ipfs/init.go +++ b/cmd/ipfs/init.go @@ -69,22 +69,7 @@ environment variable: }, NoRemote: true, Extra: commands.CreateCmdExtras(commands.SetDoesNotUseRepo(true), commands.SetDoesNotUseConfigAsInput(true)), - PreRun: func(req *cmds.Request, env cmds.Environment) error { - cctx := env.(*oldcmds.Context) - daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot) - if err != nil { - return err - } - - log.Info("checking if daemon is running...") - if daemonLocked { - log.Debug("ipfs daemon is running") - e := "ipfs daemon is running. please stop it to run this command" - return cmds.ClientError(e) - } - - return nil - }, + PreRun: commands.DaemonNotRunning, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { cctx := env.(*oldcmds.Context) empty, _ := req.Options[emptyRepoOptionName].(bool) diff --git a/core/commands/keystore.go b/core/commands/keystore.go index 5f2388ddeae9806534afab3d450e2c53b61ddc91..9b86ba36b56ec32b7faa68ef0b3ed15d7f3fde74 100644 --- a/core/commands/keystore.go +++ b/core/commands/keystore.go @@ -150,6 +150,7 @@ 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] @@ -459,22 +460,7 @@ environment variable: cmds.IntOption(keyStoreSizeOptionName, "s", "size of the key to generate"), }, NoRemote: true, - PreRun: func(req *cmds.Request, env cmds.Environment) error { - cctx := env.(*oldcmds.Context) - daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot) - if err != nil { - return err - } - - log.Info("checking if daemon is running...") - if daemonLocked { - log.Debug("ipfs daemon is running") - e := "ipfs daemon is running. please stop it to run this command" - return cmds.ClientError(e) - } - - return nil - }, + PreRun: DaemonNotRunning, Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error { cctx := env.(*oldcmds.Context) nBitsForKeypair, nBitsGiven := req.Options[keyStoreSizeOptionName].(int) @@ -556,3 +542,22 @@ func keyOutputListEncoders() cmds.EncoderFunc { return nil }) } + +// DaemonNotRunning checks to see if the ipfs repo is locked, indicating that +// the daemon is running, and returns and error if the daemon is running. +func DaemonNotRunning(req *cmds.Request, env cmds.Environment) error { + cctx := env.(*oldcmds.Context) + daemonLocked, err := fsrepo.LockedByOtherProcess(cctx.ConfigRoot) + if err != nil { + return err + } + + log.Info("checking if daemon is running...") + if daemonLocked { + log.Debug("ipfs daemon is running") + e := "ipfs daemon is running. please stop it to run this command" + return cmds.ClientError(e) + } + + return nil +} diff --git a/test/sharness/t0165-keystore.sh b/test/sharness/t0165-keystore.sh index 07a9ad9fe8e173c65349b58267e96ab3c2689049..0bbbe91aca54e32c2bd6ce642eac837cdbbfa03d 100755 --- a/test/sharness/t0165-keystore.sh +++ b/test/sharness/t0165-keystore.sh @@ -167,6 +167,24 @@ ipfs key rm key_ed25519 test_must_fail ipfs key rename -f fooed self 2>&1 | tee key_rename_out && grep -q "Error: cannot overwrite key with name" key_rename_out ' + + test_launch_ipfs_daemon + + test_expect_success "online import rsa key" ' + ipfs key import generated_rsa_key generated_rsa_key.key > roundtrip_rsa_key_id && + test_cmp rsa_key_id roundtrip_rsa_key_id + ' + + test_must_fail "online export rsa key" ' + ipfs key export generated_rsa_key + ' + + test_must_fail "online rotate rsa key" ' + ipfs key rotate + ' + + test_kill_ipfs_daemon + } test_check_rsa2048_sk() {