Skip to content

Commit 535b467

Browse files
author
postables
authored
Merge pull request #3 from ipfs/feat/pluggable-keystore
pluggable keystores
2 parents ab5e543 + b72a340 commit 535b467

File tree

6 files changed

+120
-4
lines changed

6 files changed

+120
-4
lines changed

go.mod

+5-2
Original file line numberDiff line numberDiff line change
@@ -9,6 +9,7 @@ require (
99
github.com/cenkalti/backoff v2.1.1+incompatible
1010
github.com/dustin/go-humanize v1.0.0
1111
github.com/elgris/jsondiff v0.0.0-20160530203242-765b5c24c302
12+
github.com/facebookgo/atomicfile v0.0.0-20151019160806-2de1f203e7d5 // indirect
1213
github.com/fatih/color v1.7.0 // indirect
1314
github.com/fsnotify/fsnotify v1.4.7
1415
github.com/gogo/protobuf v1.2.1
@@ -33,7 +34,7 @@ require (
3334
github.com/ipfs/go-ipfs-chunker v0.0.1
3435
github.com/ipfs/go-ipfs-cmdkit v0.0.1
3536
github.com/ipfs/go-ipfs-cmds v0.0.1
36-
github.com/ipfs/go-ipfs-config v0.0.1
37+
github.com/ipfs/go-ipfs-config v0.0.0-20190309024316-a0bdc3f74854
3738
github.com/ipfs/go-ipfs-ds-help v0.0.1
3839
github.com/ipfs/go-ipfs-exchange-interface v0.0.1
3940
github.com/ipfs/go-ipfs-exchange-offline v0.0.1
@@ -51,12 +52,12 @@ require (
5152
github.com/ipfs/go-metrics-prometheus v0.0.1
5253
github.com/ipfs/go-mfs v0.0.1
5354
github.com/ipfs/go-path v0.0.1
55+
github.com/ipfs/go-prompt v0.0.1
5456
github.com/ipfs/go-unixfs v0.0.1
5557
github.com/ipfs/go-verifcid v0.0.1
5658
github.com/ipfs/hang-fds v0.0.1
5759
github.com/ipfs/interface-go-ipfs-core v0.0.2
5860
github.com/ipfs/iptb v1.4.0
59-
github.com/ipfs/iptb-plugins v0.0.1
6061
github.com/jbenet/go-is-domain v0.0.0-20160119110217-ba9815c809e0
6162
github.com/jbenet/go-random v0.0.0-20190219211222-123a90aedc0c
6263
github.com/jbenet/go-random-files v0.0.0-20190219210431-31b3f20ebded
@@ -67,6 +68,7 @@ require (
6768
github.com/libp2p/go-libp2p-circuit v0.0.1
6869
github.com/libp2p/go-libp2p-connmgr v0.0.1
6970
github.com/libp2p/go-libp2p-crypto v0.0.1
71+
github.com/libp2p/go-libp2p-daemon v0.0.1 // indirect
7072
github.com/libp2p/go-libp2p-host v0.0.1
7173
github.com/libp2p/go-libp2p-interface-connmgr v0.0.1
7274
github.com/libp2p/go-libp2p-kad-dht v0.0.3
@@ -102,6 +104,7 @@ require (
102104
github.com/prometheus/client_golang v0.9.2
103105
github.com/syndtr/goleveldb v1.0.0
104106
github.com/whyrusleeping/base32 v0.0.0-20170828182744-c30ac30633cc
107+
github.com/whyrusleeping/go-ctrlnet v0.0.0-20180313164037-f564fbbdaa95 // indirect
105108
github.com/whyrusleeping/go-smux-multiplex v3.0.16+incompatible
106109
github.com/whyrusleeping/go-smux-yamux v2.0.8+incompatible
107110
github.com/whyrusleeping/go-sysinfo v0.0.0-20190219211824-4a357d4b90b1

go.sum

+12
Original file line numberDiff line numberDiff line change
@@ -6,6 +6,8 @@ github.com/Kubuxu/go-os-helper v0.0.1 h1:EJiD2VUQyh5A9hWJLmc6iWg6yIcJ7jpBcwC8GMG
66
github.com/Kubuxu/go-os-helper v0.0.1/go.mod h1:N8B+I7vPCT80IcP58r50u4+gEEcsZETFUpAzWW2ep1Y=
77
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd h1:HNhzThEtZW714v8Eda8sWWRcu9WSzJC+oCyjRjvZgRA=
88
github.com/Kubuxu/gocovmerge v0.0.0-20161216165753-7ecaa51963cd/go.mod h1:bqoB8kInrTeEtYAwaIXoSRqdwnjQmFhsfusnzyui6yY=
9+
github.com/Songmu/prompter v0.0.0-20181014095714-d227c68538bd h1:WPP3dYxBYZBo0q3t14UIvD0Myr848agWCVSlScH17E0=
10+
github.com/Songmu/prompter v0.0.0-20181014095714-d227c68538bd/go.mod h1:fNhSFBGC+sg+dZ7AqDHgq+xYiom23TeTESzUbO7PIrE=
911
github.com/Stebalien/go-bitfield v0.0.0-20180330043415-076a62f9ce6e h1:2Z+EBRrOJsA3psnUPcEWMIH2EIga1xHflQcr/EZslx8=
1012
github.com/Stebalien/go-bitfield v0.0.0-20180330043415-076a62f9ce6e/go.mod h1:3oM7gXIttpYDAJXpVNnSCiUMYBLIZ6cb1t+Ip982MRo=
1113
github.com/aead/siphash v1.0.1/go.mod h1:Nywa3cDsYNNK3gaciGTWPwHt0wlpNV15vwmswBAUSII=
@@ -139,6 +141,8 @@ github.com/ipfs/go-ipfs-cmdkit v0.0.1 h1:X6YXEAjUljTzevE6DPUKXSqcgf+4FXzcn5B957F
139141
github.com/ipfs/go-ipfs-cmdkit v0.0.1/go.mod h1:9FtbMdUabcSqv/G4/8WCxSLxkZxn/aZEFrxxqnVcRbg=
140142
github.com/ipfs/go-ipfs-cmds v0.0.1 h1:wPTynLMa+JImcTsPaVmrUDP8mJ3S8HQVUWixnKi7+k4=
141143
github.com/ipfs/go-ipfs-cmds v0.0.1/go.mod h1:k7I8PptE2kCJchR3ta546LRyxl4/uBYbLQHOJM0sUQ8=
144+
github.com/ipfs/go-ipfs-config v0.0.0-20190309024316-a0bdc3f74854 h1:/egI93XR8dai8cn+W2epdY0rCppfFsGjt8UQ741Qhy8=
145+
github.com/ipfs/go-ipfs-config v0.0.0-20190309024316-a0bdc3f74854/go.mod h1:yqJG+KMcBiqX2v2EhxeTi30T0lLGZx6uY3f5B3gD4HI=
142146
github.com/ipfs/go-ipfs-config v0.0.1 h1:6ED08emzI1imdsAjixFi2pEyZxTVD5ECKtCOxLBx+Uc=
143147
github.com/ipfs/go-ipfs-config v0.0.1/go.mod h1:KDbHjNyg4e6LLQSQpkgQMBz6Jf4LXiWAcmnkcwmH0DU=
144148
github.com/ipfs/go-ipfs-delay v0.0.0-20181109222059-70721b86a9a8/go.mod h1:8SP1YXK1M1kXuc4KJZINY3TQQ03J2rwBG9QfXmbRPrw=
@@ -182,6 +186,10 @@ github.com/ipfs/go-mfs v0.0.1 h1:EcQeaxW2DO3LmRXEtbWCGxhZ7YmMR+X7nzjYChBzG8s=
182186
github.com/ipfs/go-mfs v0.0.1/go.mod h1:rUT0dKNWkKa1T+MobpBL2zANn7p8Y6unXANC0PV2FLk=
183187
github.com/ipfs/go-path v0.0.1 h1:6UskTq8xYVs3zVnHjXDvoCqw22dKWK1BwD1cy1cuHyc=
184188
github.com/ipfs/go-path v0.0.1/go.mod h1:ztzG4iSBN2/CJa93rtHAv/I+mpK+BGALeUoJzhclhw0=
189+
github.com/ipfs/go-prompt v0.0.0-20190309021543-60746b51630e h1:rClV9cZC54BQr25YltQRNc3a1GSxiCZSF7jwEWltFVU=
190+
github.com/ipfs/go-prompt v0.0.0-20190309021543-60746b51630e/go.mod h1:9sBllkC9Vc4oYBP/S8iR8IRPbyVSwottiVgKsKA1atE=
191+
github.com/ipfs/go-prompt v0.0.1 h1:yWWyaH5YKSkU9Gejgg8d41wzh5onvckStxK6CuLGSEo=
192+
github.com/ipfs/go-prompt v0.0.1/go.mod h1:9sBllkC9Vc4oYBP/S8iR8IRPbyVSwottiVgKsKA1atE=
185193
github.com/ipfs/go-todocounter v0.0.1 h1:kITWA5ZcQZfrUnDNkRn04Xzh0YFaDFXsoO2A81Eb6Lw=
186194
github.com/ipfs/go-todocounter v0.0.1/go.mod h1:l5aErvQc8qKE2r7NDMjmq5UNAvuZy0rC8BHOplkWvZ4=
187195
github.com/ipfs/go-unixfs v0.0.1 h1:CTTGqLxU5+PRkkeA+w1peStqRWFD1Kya+yZgIT4Xy1w=
@@ -342,6 +350,8 @@ github.com/mattn/go-colorable v0.1.1/go.mod h1:FuOcm+DKB9mbwrcAfNl7/TZVBZ6rcncea
342350
github.com/mattn/go-isatty v0.0.3/go.mod h1:M+lRXTBqGeGNdLjl/ufCoiOlB5xdOkqRJdNxMWT7Zi4=
343351
github.com/mattn/go-isatty v0.0.5 h1:tHXDdz1cpzGaovsTB+TVB8q90WEokoVmfMqoVcrLUgw=
344352
github.com/mattn/go-isatty v0.0.5/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
353+
github.com/mattn/go-isatty v0.0.6 h1:SrwhHcpV4nWrMGdNcC2kXpMfcBVYGDuTArqyhocJgvA=
354+
github.com/mattn/go-isatty v0.0.6/go.mod h1:Iq45c/XA43vh69/j3iqttzPXn0bhXyGjM0Hdxcsrc5s=
345355
github.com/mattn/go-runewidth v0.0.4 h1:2BvfKmzob6Bmd4YsL0zygOqfdFnK7GR4QL06Do4/p7Y=
346356
github.com/mattn/go-runewidth v0.0.4/go.mod h1:LwmH8dsx7+W8Uxz3IHJYH5QSwggIsqBzpuz5H//U1FU=
347357
github.com/matttproud/golang_protobuf_extensions v1.0.1 h1:4hp9jkHxhMHkqkrB3Ix0jegS5sx/RkqARlsWZ6pIwiU=
@@ -461,6 +471,8 @@ golang.org/x/crypto v0.0.0-20190211182817-74369b46fc67/go.mod h1:6SG95UA2DQfeDnf
461471
golang.org/x/crypto v0.0.0-20190225124518-7f87c0fbb88b/go.mod h1:6SG95UA2DQfeDnfUPMdvaQW0Q7yPrPDi9nlGo2tz2b4=
462472
golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25 h1:jsG6UpNLt9iAsb0S2AGW28DveNzzgmbXR+ENoPjUeIU=
463473
golang.org/x/crypto v0.0.0-20190228161510-8dd112bcdc25/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
474+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2 h1:VklqNMn3ovrHsnt90PveolxSbWFaJdECFbxSq0Mqo2M=
475+
golang.org/x/crypto v0.0.0-20190308221718-c2843e01d9a2/go.mod h1:djNgcEr1/C05ACkg1iLfiJU5Ep61QUkGW8qpdssI0+w=
464476
golang.org/x/net v0.0.0-20180524181706-dfa909b99c79/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
465477
golang.org/x/net v0.0.0-20180906233101-161cd47e91fd/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=
466478
golang.org/x/net v0.0.0-20181102091132-c10e9556a7bc/go.mod h1:mL1N/T3taQHkDXs73rZJwtUhF3w3ftmwwsq0BUmARs4=

plugin/keystore.go

+24
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,24 @@
1+
package plugin
2+
3+
import (
4+
keystore "github.com/ipfs/go-ipfs/keystore"
5+
prompt "github.com/ipfs/go-prompt"
6+
)
7+
8+
// PluginKeystore is an interface that can be implemented to new keystore
9+
// backends.
10+
type PluginKeystore interface {
11+
Plugin
12+
13+
// KeystoreTypeName returns the the keystore's type. In addition to
14+
// loading the keystore plugin, the user must configure their go-ipfs
15+
// node to use the specified keystore backend.
16+
KeystoreTypeName() string
17+
18+
// Open opens the keystore. Prompter may be nil if non-interactive.
19+
Open(
20+
repoPath string,
21+
config map[string]interface{},
22+
prompter prompt.Prompter,
23+
) (keystore.Keystore, error)
24+
}

plugin/loader/loader.go

+10
Original file line numberDiff line numberDiff line change
@@ -105,6 +105,12 @@ func (loader *PluginLoader) Inject() error {
105105
return err
106106
}
107107
}
108+
if pl, ok := pl.(plugin.PluginKeystore); ok {
109+
err := injectKeystorePlugin(pl)
110+
if err != nil {
111+
return err
112+
}
113+
}
108114
}
109115
return nil
110116
}
@@ -160,6 +166,10 @@ func injectIPLDPlugin(pl plugin.PluginIPLD) error {
160166
return pl.RegisterInputEncParsers(coredag.DefaultInputEncParsers)
161167
}
162168

169+
func injectKeystorePlugin(pl plugin.PluginKeystore) error {
170+
return fsrepo.AddKeystore(pl.KeystoreTypeName(), pl.Open)
171+
}
172+
163173
func injectTracerPlugin(pl plugin.PluginTracer) error {
164174
tracer, err := pl.InitTracer()
165175
if err != nil {

repo/fsrepo/fsrepo.go

+17-2
Original file line numberDiff line numberDiff line change
@@ -384,8 +384,23 @@ func (r *FSRepo) openConfig() error {
384384
}
385385

386386
func (r *FSRepo) openKeystore() error {
387-
ksp := filepath.Join(r.path, "keystore")
388-
ks, err := keystore.NewFSKeystore(ksp)
387+
spec := r.config.Keystore
388+
if spec == nil {
389+
spec = map[string]interface{}{"type": "files", "path": "keystore"}
390+
}
391+
392+
ksType, ok := spec["type"]
393+
if !ok {
394+
return fmt.Errorf("keystore config lacks a type")
395+
}
396+
397+
ksCtor, ok := keystores[ksType]
398+
if !ok {
399+
return fmt.Errorf("couldn't find keystore of type %q", ksType)
400+
}
401+
402+
// TODO: feed through a prompter.
403+
ks, err := ksCtor(r.path, spec, nil)
389404
if err != nil {
390405
return err
391406
}

repo/fsrepo/keystores.go

+52
Original file line numberDiff line numberDiff line change
@@ -0,0 +1,52 @@
1+
package fsrepo
2+
3+
import (
4+
"fmt"
5+
"path/filepath"
6+
7+
keystore "github.com/ipfs/go-ipfs/keystore"
8+
9+
prompt "github.com/ipfs/go-prompt"
10+
)
11+
12+
type KeystoreConstructor func(
13+
repoPath string,
14+
cfg map[string]interface{},
15+
prompter prompt.Prompter,
16+
) (keystore.Keystore, error)
17+
18+
var keystores = map[string]KeystoreConstructor{
19+
"memory": MemKeystoreFromConfig,
20+
"files": FSKeystoreFromConfig,
21+
}
22+
23+
func AddKeystore(name string, ctor KeystoreConstructor) error {
24+
_, ok := keystores[name]
25+
if ok {
26+
return fmt.Errorf("keystore %q registered more than once", name)
27+
}
28+
keystores[name] = ctor
29+
return nil
30+
}
31+
32+
func FSKeystoreFromConfig(
33+
repo string,
34+
cfg map[string]interface{},
35+
_ prompt.Prompter,
36+
) (keystore.Keystore, error) {
37+
path, ok := cfg["path"].(string)
38+
if !ok {
39+
return nil, fmt.Errorf("'path' field is missing or not a string")
40+
}
41+
return keystore.NewFSKeystore(filepath.Join(repo, path))
42+
}
43+
44+
// MemKeystoreFromConfig opens an in-memory keystore based on the current
45+
// config.
46+
func MemKeystoreFromConfig(
47+
_ string,
48+
_ map[string]interface{},
49+
_ prompt.Prompter,
50+
) (keystore.Keystore, error) {
51+
return keystore.NewMemKeystore(), nil
52+
}

0 commit comments

Comments
 (0)