storj/satellite/satellitedb/peeridentities.go

120 lines
3.2 KiB
Go
Raw Normal View History

// Copyright (C) 2018 Storj Labs, Inc.
// See LICENSE for copying information.
package satellitedb
import (
"bytes"
"context"
"database/sql"
"strings"
"github.com/zeebo/errs"
"storj.io/storj/pkg/identity"
"storj.io/storj/pkg/storj"
dbx "storj.io/storj/satellite/satellitedb/dbx"
)
type peerIdentities struct {
satellite/satellitedb: unexport satellitedb.DB Backstory: I needed a better way to pass around information about the underlying driver and implementation to all the various db-using things in satellitedb (at least until some new "cockroach driver" support makes it to DBX). After hitting a few dead ends, I decided I wanted to have a type that could act like a *dbx.DB but which would also carry information about the implementation, etc. Then I could pass around that type to all the things in satellitedb that previously wanted *dbx.DB. But then I realized that *satellitedb.DB was, essentially, exactly that already. One thing that might have kept *satellitedb.DB from being directly usable was that embedding a *dbx.DB inside it would make a lot of dbx methods publicly available on a *satellitedb.DB instance that previously were nicely encapsulated and hidden. But after a quick look, I realized that _nothing_ outside of satellite/satellitedb even needs to use satellitedb.DB at all. It didn't even need to be exported, except for some trivially-replaceable code in migrate_postgres_test.go. And once I made it unexported, any concerns about exposing new methods on it were entirely moot. So I have here changed the exported *satellitedb.DB type into the unexported *satellitedb.satelliteDB type, and I have changed all the places here that wanted raw dbx.DB handles to use this new type instead. Now they can just take a gander at the implementation member on it and know all they need to know about the underlying database. This will make it possible for some other pending code here to differentiate between postgres and cockroach backends. Change-Id: I27af99f8ae23b50782333da5277b553b34634edc
2019-12-14 02:29:54 +00:00
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")
}
tx, err := idents.db.Open(ctx)
if err != nil {
return Error.Wrap(err)
}
defer func() {
if err == nil {
err = tx.Commit()
} else {
err = errs.Combine(err, tx.Rollback())
}
}()
serial, err := tx.Get_PeerIdentity_LeafSerialNumber_By_NodeId(ctx, dbx.PeerIdentity_NodeId(nodeID.Bytes()))
if serial == nil || err != nil {
if serial == nil || err == sql.ErrNoRows {
return Error.Wrap(tx.CreateNoReturn_PeerIdentity(ctx,
dbx.PeerIdentity_NodeId(nodeID.Bytes()),
dbx.PeerIdentity_LeafSerialNumber(ident.Leaf.SerialNumber.Bytes()),
dbx.PeerIdentity_Chain(identity.EncodePeerIdentity(ident)),
))
}
return Error.Wrap(err)
}
if !bytes.Equal(serial.LeafSerialNumber, ident.Leaf.SerialNumber.Bytes()) {
return Error.Wrap(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 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(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, nil
}