Skip to content

Commit

Permalink
Merge pull request #5844 from ipfs/feat/always-load-key
Browse files Browse the repository at this point in the history
startup: always load the private key
  • Loading branch information
Stebalien authored Dec 13, 2018
2 parents c72f6c9 + 58292f7 commit d0c2727
Show file tree
Hide file tree
Showing 10 changed files with 25 additions and 118 deletions.
5 changes: 0 additions & 5 deletions cmd/ipfs/init.go
Original file line number Diff line number Diff line change
Expand Up @@ -237,10 +237,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
}
defer nd.Close()

err = nd.SetupOfflineRouting()
if err != nil {
return err
}

return namesys.InitializeKeyspace(ctx, nd.Namesys, nd.Pinning, nd.PrivateKey)
}
19 changes: 14 additions & 5 deletions core/builder.go
Original file line number Diff line number Diff line change
Expand Up @@ -10,16 +10,14 @@ import (
"time"

filestore "github.com/ipfs/go-ipfs/filestore"
namesys "github.com/ipfs/go-ipfs/namesys"
pin "github.com/ipfs/go-ipfs/pin"
repo "github.com/ipfs/go-ipfs/repo"
cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
"github.com/ipfs/go-ipfs/thirdparty/verifbs"
bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
resolver "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path/resolver"
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
uio "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/io"

ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
ipns "gx/ipfs/QmPrt2JqvtFcgMBmYBjtZ5jFzq6HoFXy8PTwLb2Dpm2cGf/go-ipns"
libp2p "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p"
bstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
Expand All @@ -29,6 +27,10 @@ import (
cfg "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
pstoremem "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore/pstoremem"
resolver "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path/resolver"
dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
uio "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/io"
offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
metrics "gx/ipfs/QmekzFM3hPZjTjUFGTABdQkEnQ3PTiMstY198PwSFr5w1Q/go-metrics-interface"
ds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
retry "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/retrystore"
Expand Down Expand Up @@ -176,11 +178,16 @@ func isTooManyFDError(err error) bool {
}

func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
// setup local peer ID (private key is loaded in online setup)
// setup local identity
if err := n.loadID(); err != nil {
return err
}

// load the private key (if present)
if err := n.loadPrivateKey(); err != nil {
return err
}

rds := &retry.Datastore{
Batching: n.Repo.Datastore(),
Delay: time.Millisecond * 200,
Expand Down Expand Up @@ -254,6 +261,8 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
}
} else {
n.Exchange = offline.Exchange(n.Blockstore)
n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
}

n.Blocks = bserv.New(n.Blockstore, n.Exchange)
Expand Down
11 changes: 0 additions & 11 deletions core/commands/cat.go
Original file line number Diff line number Diff line change
Expand Up @@ -33,22 +33,11 @@ var CatCmd = &cmds.Command{
cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
},
Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
node, err := cmdenv.GetNode(env)
if err != nil {
return err
}

api, err := cmdenv.GetApi(env)
if err != nil {
return err
}

if !node.OnlineMode() {
if err := node.SetupOfflineRouting(); err != nil {
return err
}
}

offset, _ := req.Options[offsetOptionName].(int64)
if offset < 0 {
return fmt.Errorf("cannot specify negative offset")
Expand Down
6 changes: 0 additions & 6 deletions core/commands/id.go
Original file line number Diff line number Diff line change
Expand Up @@ -176,12 +176,6 @@ func printSelf(node *core.IpfsNode) (interface{}, error) {
info := new(IdOutput)
info.ID = node.Identity.Pretty()

if node.PrivateKey == nil {
if err := node.LoadPrivateKey(); err != nil {
return nil, err
}
}

pk := node.PrivateKey.GetPublic()
pkb, err := ic.MarshalPublicKey(pk)
if err != nil {
Expand Down
12 changes: 0 additions & 12 deletions core/commands/resolve.go
Original file line number Diff line number Diff line change
Expand Up @@ -79,18 +79,6 @@ Resolve the value of an IPFS DAG path:
return err
}

n, err := cmdenv.GetNode(env)
if err != nil {
return err
}

if !n.OnlineMode() {
err := n.SetupOfflineRouting()
if err != nil {
return err
}
}

name := req.Arguments[0]
recursive, _ := req.Options[resolveRecursiveOptionName].(bool)

Expand Down
42 changes: 11 additions & 31 deletions core/core.go
Original file line number Diff line number Diff line change
Expand Up @@ -71,7 +71,6 @@ import (
merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
ft "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs"
nilrouting "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/none"
offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
yamux "gx/ipfs/Qmdps3CYh5htGQSrPvzg5PHouVexLmtpbuLCqc4vuej8PC/go-smux-yamux"
ds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
record "gx/ipfs/QmfARXVCzpwFXQdepAJZuqyNDgV9doEsMnVCo1ssmuSe1U/go-libp2p-record"
Expand Down Expand Up @@ -160,9 +159,8 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
return errors.New("node already online")
}

// load private key
if err := n.LoadPrivateKey(); err != nil {
return err
if n.PrivateKey == nil {
return fmt.Errorf("private key not available")
}

// get undialable addrs from config
Expand Down Expand Up @@ -773,13 +771,17 @@ func (n *IpfsNode) loadID() error {
// GetKey will return a key from the Keystore with name `name`.
func (n *IpfsNode) GetKey(name string) (ic.PrivKey, error) {
if name == "self" {
if n.PrivateKey == nil {
return nil, fmt.Errorf("private key not available")
}
return n.PrivateKey, nil
} else {
return n.Repo.Keystore().Get(name)
}
}

func (n *IpfsNode) LoadPrivateKey() error {
// loadPrivateKey loads the private key *if* available
func (n *IpfsNode) loadPrivateKey() error {
if n.Identity == "" || n.Peerstore == nil {
return errors.New("loaded private key out of order")
}
Expand All @@ -794,6 +796,10 @@ func (n *IpfsNode) LoadPrivateKey() error {
return err
}

if cfg.Identity.PrivKey == "" {
return nil
}

sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
if err != nil {
return err
Expand Down Expand Up @@ -864,32 +870,6 @@ func (n *IpfsNode) loadFilesRoot() error {
return nil
}

// SetupOfflineRouting instantiates a routing system in offline mode. This is
// primarily used for offline ipns modifications.
func (n *IpfsNode) SetupOfflineRouting() error {
if n.Routing != nil {
// Routing was already set up
return nil
}

// TODO: move this somewhere else.
err := n.LoadPrivateKey()
if err != nil {
return err
}

n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)

size, err := n.getCacheSize()
if err != nil {
return err
}

n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)

return nil
}

func loadPrivateKey(cfg *config.Identity, id peer.ID) (ic.PrivKey, error) {
sk, err := cfg.DecodePrivateKey("passphrase todo!")
if err != nil {
Expand Down
11 changes: 0 additions & 11 deletions core/coreapi/name.go
Original file line number Diff line number Diff line change
Expand Up @@ -48,10 +48,6 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
if !options.AllowOffline {
return nil, coreiface.ErrOffline
}
err := n.SetupOfflineRouting()
if err != nil {
return nil, err
}
}

if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
Expand Down Expand Up @@ -97,13 +93,6 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name

n := api.node

if !n.OnlineMode() {
err := n.SetupOfflineRouting()
if err != nil {
return nil, err
}
}

var resolver namesys.Resolver = n.Namesys

if options.Local && !options.Cache {
Expand Down
17 changes: 0 additions & 17 deletions core/coreapi/unixfs_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -707,23 +707,6 @@ func TestGetNonUnixfs(t *testing.T) {
}
}

func TestCatOffline(t *testing.T) {
ctx := context.Background()
_, api, err := makeAPI(ctx)
if err != nil {
t.Error(err)
}

p, err := coreiface.ParsePath("/ipns/Qmfoobar")
if err != nil {
t.Error(err)
}
_, err = api.Unixfs().Get(ctx, p)
if err != coreiface.ErrOffline {
t.Fatalf("expected ErrOffline, got: %s", err)
}
}

func TestLs(t *testing.T) {
ctx := context.Background()
node, api, err := makeAPI(ctx)
Expand Down
10 changes: 0 additions & 10 deletions fuse/ipns/ipns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,12 +13,10 @@ import (
"testing"

core "github.com/ipfs/go-ipfs/core"
namesys "github.com/ipfs/go-ipfs/namesys"

u "gx/ipfs/QmNohiVssaPw3KVLZik59DBVGTSm2dGvYT9eoXt5DQ36Yz/go-ipfs-util"
ci "gx/ipfs/QmPuhRE325DR8ChNcFtgd6F1eANCHy1oohXZPpYop4xsK6/go-testutil/ci"
fstest "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse/fs/fstestutil"
offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
racedet "gx/ipfs/Qmf7HqcW7LtCi1W8y2bdx2eJpze74jkbKqpByxgXikdbLF/go-detect-race"
)

Expand Down Expand Up @@ -117,14 +115,6 @@ func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *mountWra
t.Fatal(err)
}

err = node.LoadPrivateKey()
if err != nil {
t.Fatal(err)
}

node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.RecordValidator)
node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0)

err = InitializeKeyspace(node, node.PrivateKey)
if err != nil {
t.Fatal(err)
Expand Down
10 changes: 0 additions & 10 deletions fuse/node/mount_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -13,10 +13,8 @@ import (
core "github.com/ipfs/go-ipfs/core"
ipns "github.com/ipfs/go-ipfs/fuse/ipns"
mount "github.com/ipfs/go-ipfs/fuse/mount"
namesys "github.com/ipfs/go-ipfs/namesys"

ci "gx/ipfs/QmPuhRE325DR8ChNcFtgd6F1eANCHy1oohXZPpYop4xsK6/go-testutil/ci"
offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
)

func maybeSkipFuseTests(t *testing.T) {
Expand Down Expand Up @@ -46,14 +44,6 @@ func TestExternalUnmount(t *testing.T) {
t.Fatal(err)
}

err = node.LoadPrivateKey()
if err != nil {
t.Fatal(err)
}

node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.RecordValidator)
node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0)

err = ipns.InitializeKeyspace(node, node.PrivateKey)
if err != nil {
t.Fatal(err)
Expand Down

0 comments on commit d0c2727

Please sign in to comment.