2019-08-26 17:49:42 +01:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package satellitedb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
|
|
|
"database/sql"
|
2020-07-14 14:04:38 +01:00
|
|
|
"errors"
|
2019-08-26 17:49:42 +01:00
|
|
|
"strings"
|
|
|
|
|
|
|
|
"github.com/zeebo/errs"
|
|
|
|
|
2019-12-27 11:48:47 +00:00
|
|
|
"storj.io/common/identity"
|
|
|
|
"storj.io/common/storj"
|
2020-01-15 02:29:51 +00:00
|
|
|
"storj.io/storj/satellite/satellitedb/dbx"
|
2019-08-26 17:49:42 +01:00
|
|
|
)
|
|
|
|
|
|
|
|
type peerIdentities struct {
|
2019-12-14 02:29:54 +00:00
|
|
|
db *satelliteDB
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Set adds a peer identity entry.
|
2019-08-26 17:49:42 +01:00
|
|
|
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")
|
|
|
|
}
|
|
|
|
|
2019-12-19 09:57:02 +00:00
|
|
|
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 {
|
2020-07-14 14:04:38 +01:00
|
|
|
if errors.Is(err, sql.ErrNoRows) || serial == nil {
|
2019-12-19 09:57:02 +00:00
|
|
|
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
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
2019-12-19 09:57:02 +00:00
|
|
|
if !bytes.Equal(serial.LeafSerialNumber, ident.Leaf.SerialNumber.Bytes()) {
|
|
|
|
return tx.UpdateNoReturn_PeerIdentity_By_NodeId(ctx,
|
2019-08-26 17:49:42 +01:00
|
|
|
dbx.PeerIdentity_NodeId(nodeID.Bytes()),
|
2019-12-19 09:57:02 +00:00
|
|
|
dbx.PeerIdentity_Update_Fields{
|
|
|
|
LeafSerialNumber: dbx.PeerIdentity_LeafSerialNumber(ident.Leaf.SerialNumber.Bytes()),
|
|
|
|
Chain: dbx.PeerIdentity_Chain(identity.EncodePeerIdentity(ident)),
|
|
|
|
},
|
|
|
|
)
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|
2019-12-19 09:57:02 +00:00
|
|
|
return nil
|
|
|
|
})
|
2019-08-26 17:49:42 +01:00
|
|
|
return Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// Get gets the peer identity based on the certificate's nodeID.
|
2019-08-26 17:49:42 +01:00
|
|
|
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)
|
|
|
|
}
|
|
|
|
|
2020-07-16 15:18:02 +01:00
|
|
|
// BatchGet gets the peer idenities based on the certificate's nodeID.
|
2019-08-26 17:49:42 +01:00
|
|
|
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
|
|
|
|
|
2020-01-17 20:07:00 +00:00
|
|
|
rows, err := idents.db.Query(ctx, idents.db.Rebind(`
|
2020-01-16 14:27:24 +00:00
|
|
|
SELECT chain
|
|
|
|
FROM peer_identities
|
|
|
|
WHERE node_id IN (?`+strings.Repeat(", ?", len(nodeIDs)-1)+`)
|
|
|
|
`), args...)
|
2019-08-26 17:49:42 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2020-01-16 14:27:24 +00:00
|
|
|
defer func() { err = errs.Combine(err, rows.Close()) }()
|
2019-08-26 17:49:42 +01:00
|
|
|
|
|
|
|
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)
|
|
|
|
}
|
2020-01-16 14:27:24 +00:00
|
|
|
return peerIdents, Error.Wrap(rows.Err())
|
2019-08-26 17:49:42 +01:00
|
|
|
}
|