-
Notifications
You must be signed in to change notification settings - Fork 393
/
peeridentities.go
110 lines (95 loc) · 3.1 KB
/
peeridentities.go
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"bytes"
"context"
"database/sql"
"errors"
"strings"
"github.com/zeebo/errs"
"storj.io/common/identity"
"storj.io/common/storj"
"storj.io/storj/satellite/satellitedb/dbx"
)
type peerIdentities struct {
db *satelliteDB
}
// Set adds a peer identity entry.
func (idents *peerIdentities) Set(ctx context.Context, nodeID storj.NodeID, ident *identity.PeerIdentity) (err error) {
defer mon.Task()(&ctx)(&err)
if ident == nil {
return Error.New("identitiy is nil")
}
err = idents.db.WithTx(ctx, func(ctx context.Context, tx *dbx.Tx) (err error) {
serial, err := tx.Get_PeerIdentity_LeafSerialNumber_By_NodeId(ctx, dbx.PeerIdentity_NodeId(nodeID.Bytes()))
if serial == nil || err != nil {
if errors.Is(err, sql.ErrNoRows) || serial == nil {
return tx.CreateNoReturn_PeerIdentity(ctx,
dbx.PeerIdentity_NodeId(nodeID.Bytes()),
dbx.PeerIdentity_LeafSerialNumber(ident.Leaf.SerialNumber.Bytes()),
dbx.PeerIdentity_Chain(identity.EncodePeerIdentity(ident)),
)
}
return err
}
if !bytes.Equal(serial.LeafSerialNumber, ident.Leaf.SerialNumber.Bytes()) {
return tx.UpdateNoReturn_PeerIdentity_By_NodeId(ctx,
dbx.PeerIdentity_NodeId(nodeID.Bytes()),
dbx.PeerIdentity_Update_Fields{
LeafSerialNumber: dbx.PeerIdentity_LeafSerialNumber(ident.Leaf.SerialNumber.Bytes()),
Chain: dbx.PeerIdentity_Chain(identity.EncodePeerIdentity(ident)),
},
)
}
return nil
})
return Error.Wrap(err)
}
// Get gets the peer identity based on the certificate's nodeID.
func (idents *peerIdentities) Get(ctx context.Context, nodeID storj.NodeID) (_ *identity.PeerIdentity, err error) {
defer mon.Task()(&ctx)(&err)
dbxIdent, err := idents.db.Get_PeerIdentity_By_NodeId(ctx, dbx.PeerIdentity_NodeId(nodeID.Bytes()))
if err != nil {
return nil, Error.Wrap(err)
}
if dbxIdent == nil {
return nil, Error.New("missing node id: %v", nodeID)
}
ident, err := identity.DecodePeerIdentity(ctx, dbxIdent.Chain)
return ident, Error.Wrap(err)
}
// BatchGet gets the peer idenities based on the certificate's nodeID.
func (idents *peerIdentities) BatchGet(ctx context.Context, nodeIDs storj.NodeIDList) (peerIdents []*identity.PeerIdentity, err error) {
defer mon.Task()(&ctx)(&err)
if len(nodeIDs) == 0 {
return nil, nil
}
args := make([]interface{}, 0, nodeIDs.Len())
for _, nodeID := range nodeIDs {
args = append(args, nodeID)
}
// TODO: optimize using arrays like overlay
rows, err := idents.db.Query(ctx, idents.db.Rebind(`
SELECT chain
FROM peer_identities
WHERE node_id IN (?`+strings.Repeat(", ?", len(nodeIDs)-1)+`)
`), args...)
if err != nil {
return nil, Error.Wrap(err)
}
defer func() { err = errs.Combine(err, rows.Close()) }()
for rows.Next() {
var peerChain []byte
err := rows.Scan(&peerChain)
if err != nil {
return nil, Error.Wrap(err)
}
ident, err := identity.DecodePeerIdentity(ctx, peerChain)
if err != nil {
return nil, Error.Wrap(err)
}
peerIdents = append(peerIdents, ident)
}
return peerIdents, Error.Wrap(rows.Err())
}