2018-11-05 15:23:54 +00:00
|
|
|
// Copyright (C) 2018 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2018-11-12 21:59:30 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2018-11-08 13:20:23 +00:00
|
|
|
"sync"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
"github.com/golang/protobuf/proto"
|
|
|
|
"github.com/gtank/cryptopasta"
|
2018-11-05 15:23:54 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
"google.golang.org/grpc/codes"
|
|
|
|
"google.golang.org/grpc/status"
|
|
|
|
|
2018-11-09 22:15:35 +00:00
|
|
|
"storj.io/storj/internal/migrate"
|
2018-11-05 15:23:54 +00:00
|
|
|
dbx "storj.io/storj/pkg/bwagreement/dbx"
|
|
|
|
"storj.io/storj/pkg/pb"
|
2018-11-12 21:59:30 +00:00
|
|
|
"storj.io/storj/pkg/peertls"
|
|
|
|
"storj.io/storj/pkg/provider"
|
2018-11-05 15:23:54 +00:00
|
|
|
)
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
// OK - Success!
|
|
|
|
const OK = "OK"
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2018-11-12 21:59:30 +00:00
|
|
|
dbm *DBManager
|
|
|
|
pkey crypto.PublicKey
|
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// DBManager is an implementation of the database access interface
|
|
|
|
type DBManager struct {
|
2018-11-08 13:20:23 +00:00
|
|
|
DB *dbx.DB
|
|
|
|
mu sync.Mutex
|
2018-11-05 15:23:54 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
2018-11-08 13:20:23 +00:00
|
|
|
// Agreement is a struct that contains a bandwidth agreement and the associated signature
|
|
|
|
type Agreement struct {
|
|
|
|
Agreement []byte
|
|
|
|
Signature []byte
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
// NewDBManager creates a new instance of a DatabaseManager
|
|
|
|
func NewDBManager(driver, source string, logger *zap.Logger) (*DBManager, error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
db, err := dbx.Open(driver, source)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2018-11-09 22:15:35 +00:00
|
|
|
err = migrate.Create("bwagreement", db)
|
|
|
|
if err != nil {
|
2018-11-05 15:23:54 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-12 21:59:30 +00:00
|
|
|
return &DBManager{
|
|
|
|
DB: db,
|
|
|
|
logger: logger,
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
|
|
|
func NewServer(driver, source string, logger *zap.Logger, pkey crypto.PublicKey) (*Server, error) {
|
|
|
|
dbm, err := NewDBManager(driver, source, logger)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
return &Server{
|
2018-11-12 21:59:30 +00:00
|
|
|
dbm: dbm,
|
2018-11-05 15:23:54 +00:00
|
|
|
logger: logger,
|
2018-11-12 21:59:30 +00:00
|
|
|
pkey: pkey,
|
2018-11-05 15:23:54 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
func (dbm *DBManager) locked() func() {
|
|
|
|
dbm.mu.Lock()
|
|
|
|
return dbm.mu.Unlock
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Create a db entry for the provided storagenode
|
2018-11-12 21:59:30 +00:00
|
|
|
func (dbm *DBManager) Create(ctx context.Context, createBwAgreement *pb.RenterBandwidthAllocation) (bwagreement *dbx.Bwagreement, err error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-12 21:59:30 +00:00
|
|
|
dbm.logger.Debug("entering bwagreement Create")
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
signature := createBwAgreement.GetSignature()
|
|
|
|
data := createBwAgreement.GetData()
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
bwagreement, err = dbm.DB.Create_Bwagreement(
|
2018-11-05 15:23:54 +00:00
|
|
|
ctx,
|
|
|
|
dbx.Bwagreement_Signature(signature),
|
|
|
|
dbx.Bwagreement_Data(data),
|
|
|
|
)
|
|
|
|
if err != nil {
|
|
|
|
return nil, status.Errorf(codes.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return bwagreement, nil
|
|
|
|
}
|
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
// GetBandwidthAllocations all bandwidth agreements and sorts by satellite
|
|
|
|
func (dbm *DBManager) GetBandwidthAllocations(ctx context.Context) (rows []*dbx.Bwagreement, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
defer dbm.locked()()
|
|
|
|
rows, err = dbm.DB.All_Bwagreement(ctx)
|
|
|
|
return rows, err
|
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
|
|
|
|
func (s *Server) BandwidthAgreements(stream pb.Bandwidth_BandwidthAgreementsServer) (err error) {
|
|
|
|
ctx := stream.Context()
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-12 21:59:30 +00:00
|
|
|
defer s.dbm.locked()()
|
2018-11-05 15:23:54 +00:00
|
|
|
|
|
|
|
ch := make(chan *pb.RenterBandwidthAllocation, 1)
|
|
|
|
errch := make(chan error, 1)
|
|
|
|
go func() {
|
|
|
|
for {
|
|
|
|
msg, err := stream.Recv()
|
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("Grpc Receive Error", zap.Error(err))
|
|
|
|
errch <- err
|
|
|
|
return
|
|
|
|
}
|
|
|
|
ch <- msg
|
|
|
|
}
|
|
|
|
}()
|
|
|
|
|
|
|
|
for {
|
|
|
|
select {
|
|
|
|
case err := <-errch:
|
|
|
|
return err
|
|
|
|
case <-ctx.Done():
|
|
|
|
return nil
|
|
|
|
case agreement := <-ch:
|
2018-11-12 21:59:30 +00:00
|
|
|
if err = s.verifySignature(ctx, agreement); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
_, err = s.dbm.Create(ctx, agreement)
|
2018-11-08 13:20:23 +00:00
|
|
|
if err != nil {
|
|
|
|
s.logger.Error("DB entry creation Error", zap.Error(err))
|
|
|
|
return err
|
|
|
|
}
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
}
|
2018-11-08 13:20:23 +00:00
|
|
|
|
2018-11-12 21:59:30 +00:00
|
|
|
func (s *Server) verifySignature(ctx context.Context, ba *pb.RenterBandwidthAllocation) error {
|
|
|
|
// TODO(security): detect replay attacks
|
|
|
|
pi, err := provider.PeerIdentityFromContext(ctx)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k, ok := pi.Leaf.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return peertls.ErrUnsupportedKey.New("%T", pi.Leaf.PublicKey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify Renter's (uplink) signature
|
|
|
|
if ok := cryptopasta.Verify(ba.GetData(), ba.GetSignature(), k); !ok {
|
|
|
|
return BwAgreementError.New("Failed to verify Renter's Signature")
|
|
|
|
}
|
|
|
|
|
|
|
|
// deserializing pbad you get satelliteID, uplinkID, max size, exp, serial# & action
|
|
|
|
pba := &pb.PayerBandwidthAllocation{}
|
|
|
|
if err := proto.Unmarshal(ba.GetData(), pba); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
k, ok = s.pkey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return peertls.ErrUnsupportedKey.New("%T", s.pkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify Payer's (satellite) signature
|
|
|
|
if ok := cryptopasta.Verify(pba.GetData(), pba.GetSignature(), k); !ok {
|
|
|
|
return BwAgreementError.New("Failed to verify Payer's Signature")
|
|
|
|
}
|
|
|
|
return nil
|
2018-11-08 13:20:23 +00:00
|
|
|
}
|