@cryptotaxi247 / kubo / commits / 2eafa3f3c

startup: always load the private key

Loading this at the last minute means we need a bunch of special cases in *every* command that needs routing, namesys, or even the public key. If we ever have a case where we don't want to do this, we can add an option to the (eventual) IPFS constructor. Handling this up-front is going to be significantly less error prone. Motivation: https://github.com/ipfs/go-ipfs/pull/5825/files#diff-fe35ea64d478c4f3fb767a3f618e5d01R863 License: MIT Signed-off-by: Steven Allen <steven@stebalien.com>

Steven Allen committed Dec 12, 2018 at 18:17 UTC 2eafa3f3ca827bffe97ca17cd6f04537b86bb05e
10 files changed +20 -120
cmd/ipfs/init.go
-5
@@ -237,10 +237,5 @@ func initializeIpnsKeyspace(repoRoot string) error {
237 }
238 defer nd.Close()
239
240 - err = nd.SetupOfflineRouting()
241 - if err != nil {
242 - return err
243 - }
244 -
240 return namesys.InitializeKeyspace(ctx, nd.Namesys, nd.Pinning, nd.PrivateKey)
241 }
core/builder.go
+14 -5
@@ -10,16 +10,14 @@ import (
10 "time"
11
12 filestore "github.com/ipfs/go-ipfs/filestore"
13 + namesys "github.com/ipfs/go-ipfs/namesys"
14 pin "github.com/ipfs/go-ipfs/pin"
15 repo "github.com/ipfs/go-ipfs/repo"
16 cidv0v1 "github.com/ipfs/go-ipfs/thirdparty/cidv0v1"
17 "github.com/ipfs/go-ipfs/thirdparty/verifbs"
17 - bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
18 - resolver "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path/resolver"
19 - dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
20 - uio "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/io"
18
19 ci "gx/ipfs/QmNiJiXwWE3kRhZrC5ej3kSjWHm337pYfhjLGSCDNKJP2s/go-libp2p-crypto"
20 + bserv "gx/ipfs/QmPoh3SrQzFBWtdGK6qmHDV4EanKR6kYPj4DD3J2NLoEmZ/go-blockservice"
21 ipns "gx/ipfs/QmPrt2JqvtFcgMBmYBjtZ5jFzq6HoFXy8PTwLb2Dpm2cGf/go-ipns"
22 libp2p "gx/ipfs/QmRBaUEQEeFWywfrZJ64QgsmvcqgLSK3VbvGMR2NM2Edpf/go-libp2p"
23 bstore "gx/ipfs/QmS2aqUZLJp8kF1ihE5rvDGE5LvmKDPnx32w9Z1BW9xLV5/go-ipfs-blockstore"
@@ -29,6 +27,10 @@ import (
27 cfg "gx/ipfs/QmYyzmMnhNTtoXx5ttgUaRdHHckYnQWjPL98hgLAR2QLDD/go-ipfs-config"
28 pstore "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore"
29 pstoremem "gx/ipfs/QmZ9zH2FnLcxv1xyzFeUpDUeo55xEhZQHgveZijcxr7TLj/go-libp2p-peerstore/pstoremem"
30 + resolver "gx/ipfs/QmZErC2Ay6WuGi96CPg316PwitdwgLo6RxZRqVjJjRj2MR/go-path/resolver"
31 + dag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
32 + uio "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs/io"
33 + offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
34 metrics "gx/ipfs/QmekzFM3hPZjTjUFGTABdQkEnQ3PTiMstY198PwSFr5w1Q/go-metrics-interface"
35 ds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
36 retry "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore/retrystore"
@@ -176,11 +178,16 @@ func isTooManyFDError(err error) bool {
178 }
179
180 func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
179 - // setup local peer ID (private key is loaded in online setup)
181 + // setup local identity
182 if err := n.loadID(); err != nil {
183 return err
184 }
185
186 + // load the private key (if present)
187 + if err := n.loadPrivateKey(); err != nil {
188 + return err
189 + }
190 +
191 rds := &retry.Datastore{
192 Batching: n.Repo.Datastore(),
193 Delay: time.Millisecond * 200,
@@ -254,6 +261,8 @@ func setupNode(ctx context.Context, n *IpfsNode, cfg *BuildCfg) error {
261 }
262 } else {
263 n.Exchange = offline.Exchange(n.Blockstore)
264 + n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
265 + n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), 0)
266 }
267
268 n.Blocks = bserv.New(n.Blockstore, n.Exchange)
core/commands/cat.go
-11
@@ -33,22 +33,11 @@ var CatCmd = &cmds.Command{
33 cmdkit.Int64Option(lengthOptionName, "l", "Maximum number of bytes to read."),
34 },
35 Run: func(req *cmds.Request, res cmds.ResponseEmitter, env cmds.Environment) error {
36 - node, err := cmdenv.GetNode(env)
37 - if err != nil {
38 - return err
39 - }
40 -
36 api, err := cmdenv.GetApi(env)
37 if err != nil {
38 return err
39 }
40
46 - if !node.OnlineMode() {
47 - if err := node.SetupOfflineRouting(); err != nil {
48 - return err
49 - }
50 - }
51 -
41 offset, _ := req.Options[offsetOptionName].(int64)
42 if offset < 0 {
43 return fmt.Errorf("cannot specify negative offset")
core/commands/id.go
-6
@@ -176,12 +176,6 @@ func printSelf(node *core.IpfsNode) (interface{}, error) {
176 info := new(IdOutput)
177 info.ID = node.Identity.Pretty()
178
179 - if node.PrivateKey == nil {
180 - if err := node.LoadPrivateKey(); err != nil {
181 - return nil, err
182 - }
183 - }
184 -
179 pk := node.PrivateKey.GetPublic()
180 pkb, err := ic.MarshalPublicKey(pk)
181 if err != nil {
core/commands/resolve.go
-12
@@ -79,18 +79,6 @@ Resolve the value of an IPFS DAG path:
79 return err
80 }
81
82 - n, err := cmdenv.GetNode(env)
83 - if err != nil {
84 - return err
85 - }
86 -
87 - if !n.OnlineMode() {
88 - err := n.SetupOfflineRouting()
89 - if err != nil {
90 - return err
91 - }
92 - }
93 -
82 name := req.Arguments[0]
83 recursive, _ := req.Options[resolveRecursiveOptionName].(bool)
84
core/core.go
+6 -33
@@ -71,7 +71,6 @@ import (
71 merkledag "gx/ipfs/QmdV35UHnL1FM52baPkeUo6u7Fxm2CRUkPTLRPxeF8a4Ap/go-merkledag"
72 ft "gx/ipfs/QmdYvDbHp7qAhZ7GsCj6e1cMo55ND6y2mjWVzwdvcv4f12/go-unixfs"
73 nilrouting "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/none"
74 - offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
74 yamux "gx/ipfs/Qmdps3CYh5htGQSrPvzg5PHouVexLmtpbuLCqc4vuej8PC/go-smux-yamux"
75 ds "gx/ipfs/Qmf4xQhNomPNhrtZc67qSnfJSjxjXs9LWvknJtSXwimPrM/go-datastore"
76 record "gx/ipfs/QmfARXVCzpwFXQdepAJZuqyNDgV9doEsMnVCo1ssmuSe1U/go-libp2p-record"
@@ -160,11 +159,6 @@ func (n *IpfsNode) startOnlineServices(ctx context.Context, routingOption Routin
159 return errors.New("node already online")
160 }
161
163 - // load private key
164 - if err := n.LoadPrivateKey(); err != nil {
165 - return err
166 - }
167 -
162 // get undialable addrs from config
163 cfg, err := n.Repo.Config()
164 if err != nil {
@@ -779,7 +773,8 @@ func (n *IpfsNode) GetKey(name string) (ic.PrivKey, error) {
773 }
774 }
775
782 -func (n *IpfsNode) LoadPrivateKey() error {
776 +// loadPrivateKey loads the private key *if* available
777 +func (n *IpfsNode) loadPrivateKey() error {
778 if n.Identity == "" || n.Peerstore == nil {
779 return errors.New("loaded private key out of order")
780 }
@@ -794,6 +789,10 @@ func (n *IpfsNode) LoadPrivateKey() error {
789 return err
790 }
791
792 + if cfg.Identity.PrivKey == "" {
793 + return nil
794 + }
795 +
796 sk, err := loadPrivateKey(&cfg.Identity, n.Identity)
797 if err != nil {
798 return err
@@ -864,32 +863,6 @@ func (n *IpfsNode) loadFilesRoot() error {
863 return nil
864 }
865
867 -// SetupOfflineRouting instantiates a routing system in offline mode. This is
868 -// primarily used for offline ipns modifications.
869 -func (n *IpfsNode) SetupOfflineRouting() error {
870 - if n.Routing != nil {
871 - // Routing was already set up
872 - return nil
873 - }
874 -
875 - // TODO: move this somewhere else.
876 - err := n.LoadPrivateKey()
877 - if err != nil {
878 - return err
879 - }
880 -
881 - n.Routing = offroute.NewOfflineRouter(n.Repo.Datastore(), n.RecordValidator)
882 -
883 - size, err := n.getCacheSize()
884 - if err != nil {
885 - return err
886 - }
887 -
888 - n.Namesys = namesys.NewNameSystem(n.Routing, n.Repo.Datastore(), size)
889 -
890 - return nil
891 -}
892 -
866 func loadPrivateKey(cfg *config.Identity, id peer.ID) (ic.PrivKey, error) {
867 sk, err := cfg.DecodePrivateKey("passphrase todo!")
868 if err != nil {
core/coreapi/name.go
-11
@@ -48,10 +48,6 @@ func (api *NameAPI) Publish(ctx context.Context, p coreiface.Path, opts ...caopt
48 if !options.AllowOffline {
49 return nil, coreiface.ErrOffline
50 }
51 - err := n.SetupOfflineRouting()
52 - if err != nil {
53 - return nil, err
54 - }
51 }
52
53 if n.Mounts.Ipns != nil && n.Mounts.Ipns.IsActive() {
@@ -97,13 +93,6 @@ func (api *NameAPI) Search(ctx context.Context, name string, opts ...caopts.Name
93
94 n := api.node
95
100 - if !n.OnlineMode() {
101 - err := n.SetupOfflineRouting()
102 - if err != nil {
103 - return nil, err
104 - }
105 - }
106 -
96 var resolver namesys.Resolver = n.Namesys
97
98 if options.Local && !options.Cache {
core/coreapi/unixfs_test.go
-17
@@ -707,23 +707,6 @@ func TestGetNonUnixfs(t *testing.T) {
707 }
708 }
709
710 -func TestCatOffline(t *testing.T) {
711 - ctx := context.Background()
712 - _, api, err := makeAPI(ctx)
713 - if err != nil {
714 - t.Error(err)
715 - }
716 -
717 - p, err := coreiface.ParsePath("/ipns/Qmfoobar")
718 - if err != nil {
719 - t.Error(err)
720 - }
721 - _, err = api.Unixfs().Get(ctx, p)
722 - if err != coreiface.ErrOffline {
723 - t.Fatalf("expected ErrOffline, got: %s", err)
724 - }
725 -}
726 -
710 func TestLs(t *testing.T) {
711 ctx := context.Background()
712 node, api, err := makeAPI(ctx)
fuse/ipns/ipns_test.go
-10
@@ -13,12 +13,10 @@ import (
13 "testing"
14
15 core "github.com/ipfs/go-ipfs/core"
16 - namesys "github.com/ipfs/go-ipfs/namesys"
16
17 u "gx/ipfs/QmNohiVssaPw3KVLZik59DBVGTSm2dGvYT9eoXt5DQ36Yz/go-ipfs-util"
18 ci "gx/ipfs/QmPuhRE325DR8ChNcFtgd6F1eANCHy1oohXZPpYop4xsK6/go-testutil/ci"
19 fstest "gx/ipfs/QmSJBsmLP1XMjv8hxYg2rUMdPDB7YUpyBo9idjrJ6Cmq6F/fuse/fs/fstestutil"
21 - offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
20 racedet "gx/ipfs/Qmf7HqcW7LtCi1W8y2bdx2eJpze74jkbKqpByxgXikdbLF/go-detect-race"
21 )
22
@@ -117,14 +115,6 @@ func setupIpnsTest(t *testing.T, node *core.IpfsNode) (*core.IpfsNode, *mountWra
115 t.Fatal(err)
116 }
117
120 - err = node.LoadPrivateKey()
121 - if err != nil {
122 - t.Fatal(err)
123 - }
124 -
125 - node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.RecordValidator)
126 - node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0)
127 -
118 err = InitializeKeyspace(node, node.PrivateKey)
119 if err != nil {
120 t.Fatal(err)
fuse/node/mount_test.go
-10
@@ -13,10 +13,8 @@ import (
13 core "github.com/ipfs/go-ipfs/core"
14 ipns "github.com/ipfs/go-ipfs/fuse/ipns"
15 mount "github.com/ipfs/go-ipfs/fuse/mount"
16 - namesys "github.com/ipfs/go-ipfs/namesys"
16
17 ci "gx/ipfs/QmPuhRE325DR8ChNcFtgd6F1eANCHy1oohXZPpYop4xsK6/go-testutil/ci"
19 - offroute "gx/ipfs/QmdmWkx54g7VfVyxeG8ic84uf4G6Eq1GohuyKA3XDuJ8oC/go-ipfs-routing/offline"
18 )
19
20 func maybeSkipFuseTests(t *testing.T) {
@@ -46,14 +44,6 @@ func TestExternalUnmount(t *testing.T) {
44 t.Fatal(err)
45 }
46
49 - err = node.LoadPrivateKey()
50 - if err != nil {
51 - t.Fatal(err)
52 - }
53 -
54 - node.Routing = offroute.NewOfflineRouter(node.Repo.Datastore(), node.RecordValidator)
55 - node.Namesys = namesys.NewNameSystem(node.Routing, node.Repo.Datastore(), 0)
56 -
47 err = ipns.InitializeKeyspace(node, node.PrivateKey)
48 if err != nil {
49 t.Fatal(err)