@cryptotaxi247 / kubo / commits / 7fb63d7e4

move signing options into a validation checker struct

Jeromy committed Feb 25, 2015 at 14:56 UTC 7fb63d7e43d96809c679617bebed4dbb4fde3fc5
13 files changed +65 -21
core/commands/dht.go
+1 -1
@@ -508,7 +508,7 @@ PutValue will store the given key value pair in the dht.
508
509 go func() {
510 defer close(events)
511 - err := dht.PutValue(ctx, key, []byte(data), true)
511 + err := dht.PutValue(ctx, key, []byte(data))
512 if err != nil {
513 notif.PublishQueryEvent(ctx, &notif.QueryEvent{
514 Type: notif.QueryError,
core/core.go
+1 -1
@@ -488,7 +488,7 @@ func startListening(ctx context.Context, host p2phost.Host, cfg *config.Config)
488
489 func constructDHTRouting(ctx context.Context, host p2phost.Host, dstore ds.ThreadSafeDatastore) (routing.IpfsRouting, error) {
490 dhtRouting := dht.NewDHT(ctx, host, dstore)
491 - dhtRouting.Validator[IpnsValidatorTag] = namesys.ValidateIpnsRecord
491 + dhtRouting.Validator[IpnsValidatorTag] = namesys.IpnsRecordValidator
492 return dhtRouting, nil
493 }
494
namesys/publisher.go
+8 -2
@@ -13,6 +13,7 @@ import (
13 pb "github.com/jbenet/go-ipfs/namesys/internal/pb"
14 ci "github.com/jbenet/go-ipfs/p2p/crypto"
15 routing "github.com/jbenet/go-ipfs/routing"
16 + record "github.com/jbenet/go-ipfs/routing/record"
17 u "github.com/jbenet/go-ipfs/util"
18 )
19
@@ -62,7 +63,7 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value u.Key)
63 log.Debugf("Storing pubkey at: %s", namekey)
64 // Store associated public key
65 timectx, _ := context.WithDeadline(ctx, time.Now().Add(time.Second*10))
65 - err = p.routing.PutValue(timectx, namekey, pkbytes, false)
66 + err = p.routing.PutValue(timectx, namekey, pkbytes)
67 if err != nil {
68 return err
69 }
@@ -72,7 +73,7 @@ func (p *ipnsPublisher) Publish(ctx context.Context, k ci.PrivKey, value u.Key)
73 log.Debugf("Storing ipns entry at: %s", ipnskey)
74 // Store ipns entry at "/ipns/"+b58(h(pubkey))
75 timectx, _ = context.WithDeadline(ctx, time.Now().Add(time.Second*10))
75 - err = p.routing.PutValue(timectx, ipnskey, data, true)
76 + err = p.routing.PutValue(timectx, ipnskey, data)
77 if err != nil {
78 return err
79 }
@@ -105,6 +106,11 @@ func ipnsEntryDataForSig(e *pb.IpnsEntry) []byte {
106 []byte{})
107 }
108
109 +var IpnsRecordValidator = &record.ValidChecker{
110 + Func: ValidateIpnsRecord,
111 + Sign: true,
112 +}
113 +
114 // ValidateIpnsRecord implements ValidatorFunc and verifies that the
115 // given 'val' is an IpnsEntry and that that entry is valid.
116 func ValidateIpnsRecord(k u.Key, val []byte) error {
routing/dht/dht.go
+1 -1
@@ -84,7 +84,7 @@ func NewDHT(ctx context.Context, h host.Host, dstore ds.ThreadSafeDatastore) *Ip
84 dht.birth = time.Now()
85
86 dht.Validator = make(record.Validator)
87 - dht.Validator["pk"] = record.ValidatePublicKeyRecord
87 + dht.Validator["pk"] = record.PublicKeyValidator
88
89 if doPinging {
90 dht.Children().Add(1)
routing/dht/dht_test.go
+11 -5
@@ -41,8 +41,11 @@ func setupDHT(ctx context.Context, t *testing.T) *IpfsDHT {
41 dss := dssync.MutexWrap(ds.NewMapDatastore())
42 d := NewDHT(ctx, h, dss)
43
44 - d.Validator["v"] = func(u.Key, []byte) error {
45 - return nil
44 + d.Validator["v"] = &record.ValidChecker{
45 + Func: func(u.Key, []byte) error {
46 + return nil
47 + },
48 + Sign: false,
49 }
50 return d
51 }
@@ -139,8 +142,11 @@ func TestValueGetSet(t *testing.T) {
142 defer dhtA.host.Close()
143 defer dhtB.host.Close()
144
142 - vf := func(u.Key, []byte) error {
143 - return nil
145 + vf := &record.ValidChecker{
146 + Func: func(u.Key, []byte) error {
147 + return nil
148 + },
149 + Sign: false,
150 }
151 dhtA.Validator["v"] = vf
152 dhtB.Validator["v"] = vf
@@ -148,7 +154,7 @@ func TestValueGetSet(t *testing.T) {
154 connect(t, ctx, dhtA, dhtB)
155
156 ctxT, _ := context.WithTimeout(ctx, time.Second)
151 - dhtA.PutValue(ctxT, "/v/hello", []byte("world"), false)
157 + dhtA.PutValue(ctxT, "/v/hello", []byte("world"))
158
159 ctxT, _ = context.WithTimeout(ctx, time.Second*2)
160 val, err := dhtA.GetValue(ctxT, "/v/hello")
routing/dht/routing.go
+6 -1
@@ -29,13 +29,18 @@ var asyncQueryBuffer = 10
29
30 // PutValue adds value corresponding to given Key.
31 // This is the top level "Store" operation of the DHT
32 -func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte, sign bool) error {
32 +func (dht *IpfsDHT) PutValue(ctx context.Context, key u.Key, value []byte) error {
33 log.Debugf("PutValue %s", key)
34 sk, err := dht.getOwnPrivateKey()
35 if err != nil {
36 return err
37 }
38
39 + sign, err := dht.Validator.IsSigned(key)
40 + if err != nil {
41 + return err
42 + }
43 +
44 rec, err := record.MakePutRecord(sk, key, value, sign)
45 if err != nil {
46 log.Debug("Creation of record failed!")
routing/mock/centralized_client.go
+1 -1
@@ -22,7 +22,7 @@ type client struct {
22 }
23
24 // FIXME(brian): is this method meant to simulate putting a value into the network?
25 -func (c *client) PutValue(ctx context.Context, key u.Key, val []byte, sign bool) error {
25 +func (c *client) PutValue(ctx context.Context, key u.Key, val []byte) error {
26 log.Debugf("PutValue: %s", key)
27 return c.datastore.Put(key.DsKey(), val)
28 }
routing/offline/offline.go
+2 -2
@@ -35,8 +35,8 @@ type offlineRouting struct {
35 sk ci.PrivKey
36 }
37
38 -func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte, sign bool) error {
39 - rec, err := record.MakePutRecord(c.sk, key, val, sign)
38 +func (c *offlineRouting) PutValue(ctx context.Context, key u.Key, val []byte) error {
39 + rec, err := record.MakePutRecord(c.sk, key, val, false)
40 if err != nil {
41 return err
42 }
routing/record/validation.go
+30 -3
@@ -25,7 +25,12 @@ var ErrInvalidRecordType = errors.New("invalid record keytype")
25 // Validator is an object that helps ensure routing records are valid.
26 // It is a collection of validator functions, each of which implements
27 // its own notion of validity.
28 -type Validator map[string]ValidatorFunc
28 +type Validator map[string]*ValidChecker
29 +
30 +type ValidChecker struct {
31 + Func ValidatorFunc
32 + Sign bool
33 +}
34
35 // VerifyRecord checks a record and ensures it is still valid.
36 // It runs needed validators
@@ -37,13 +42,30 @@ func (v Validator) VerifyRecord(r *pb.Record) error {
42 return nil
43 }
44
40 - fnc, ok := v[parts[1]]
45 + val, ok := v[parts[1]]
46 if !ok {
47 log.Infof("Unrecognized key prefix: %s", parts[1])
48 return ErrInvalidRecordType
49 }
50
46 - return fnc(u.Key(r.GetKey()), r.GetValue())
51 + return val.Func(u.Key(r.GetKey()), r.GetValue())
52 +}
53 +
54 +func (v Validator) IsSigned(k u.Key) (bool, error) {
55 + // Now, check validity func
56 + parts := strings.Split(string(k), "/")
57 + if len(parts) < 3 {
58 + log.Infof("Record key does not have validator: %s", k)
59 + return false, nil
60 + }
61 +
62 + val, ok := v[parts[1]]
63 + if !ok {
64 + log.Infof("Unrecognized key prefix: %s", parts[1])
65 + return false, ErrInvalidRecordType
66 + }
67 +
68 + return val.Sign, nil
69 }
70
71 // ValidatePublicKeyRecord implements ValidatorFunc and
@@ -62,6 +84,11 @@ func ValidatePublicKeyRecord(k u.Key, val []byte) error {
84 return nil
85 }
86
87 +var PublicKeyValidator = &ValidChecker{
88 + Func: ValidatePublicKeyRecord,
89 + Sign: false,
90 +}
91 +
92 func CheckRecordSig(r *pb.Record, pk ci.PubKey) error {
93 blob := RecordBlobForSig(r)
94 good, err := pk.Verify(blob, r.Signature)
routing/routing.go
+1 -1
@@ -21,7 +21,7 @@ type IpfsRouting interface {
21 // Basic Put/Get
22
23 // PutValue adds value corresponding to given Key.
24 - PutValue(context.Context, u.Key, []byte, bool) error
24 + PutValue(context.Context, u.Key, []byte) error
25
26 // GetValue searches for the value corresponding to given Key.
27 GetValue(context.Context, u.Key) ([]byte, error)
routing/supernode/client.go
+1 -1
@@ -59,7 +59,7 @@ func (c *Client) FindProvidersAsync(ctx context.Context, k u.Key, max int) <-cha
59 return ch
60 }
61
62 -func (c *Client) PutValue(ctx context.Context, k u.Key, v []byte, sign bool) error {
62 +func (c *Client) PutValue(ctx context.Context, k u.Key, v []byte) error {
63 defer log.EventBegin(ctx, "putValue", &k).Done()
64 r, err := makeRecord(c.peerstore, c.local, k, v)
65 if err != nil {
routing/supernode/server.go
+1 -1
@@ -204,7 +204,7 @@ func providerKey(k util.Key) datastore.Key {
204
205 func verify(ps peer.Peerstore, r *dhtpb.Record) error {
206 v := make(record.Validator)
207 - v["pk"] = record.ValidatePublicKeyRecord
207 + v["pk"] = record.PublicKeyValidator
208 p := peer.ID(r.GetAuthor())
209 pk := ps.PubKey(p)
210 if pk == nil {
test/integration/grandcentral_test.go
+1 -1
@@ -168,7 +168,7 @@ func RunSupernodePutRecordGetRecord(conf testutil.LatencyConfig) error {
168 k := util.Key("key")
169 note := []byte("a note from putter")
170
171 - if err := putter.Routing.PutValue(ctx, k, note, false); err != nil {
171 + if err := putter.Routing.PutValue(ctx, k, note); err != nil {
172 return fmt.Errorf("failed to put value: %s", err)
173 }
174