2019-01-24 20:15:10 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
2018-11-05 15:23:54 +00:00
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package bwagreement
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-07 19:22:49 +00:00
|
|
|
"crypto"
|
|
|
|
"crypto/ecdsa"
|
2019-02-01 18:50:12 +00:00
|
|
|
"strings"
|
2018-12-07 09:59:31 +00:00
|
|
|
"time"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2019-02-07 19:22:49 +00:00
|
|
|
"github.com/gogo/protobuf/proto"
|
|
|
|
"github.com/gtank/cryptopasta"
|
2019-01-23 19:58:44 +00:00
|
|
|
"github.com/zeebo/errs"
|
2018-11-05 15:23:54 +00:00
|
|
|
"go.uber.org/zap"
|
2019-01-23 19:58:44 +00:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2018-11-05 15:23:54 +00:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/auth"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/certdb"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
2018-11-05 15:23:54 +00:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-01-25 18:05:21 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2018-11-05 15:23:54 +00:00
|
|
|
)
|
|
|
|
|
2019-01-23 19:58:44 +00:00
|
|
|
var (
|
|
|
|
// Error the default bwagreement errs class
|
|
|
|
Error = errs.Class("bwagreement error")
|
|
|
|
mon = monkit.Package()
|
|
|
|
)
|
|
|
|
|
|
|
|
// Config is a configuration struct that is everything you need to start an
|
|
|
|
// agreement receiver responsibility
|
|
|
|
type Config struct {
|
|
|
|
}
|
|
|
|
|
2019-02-01 18:50:12 +00:00
|
|
|
//UplinkStat contains information about an uplink's returned bandwidth agreement
|
|
|
|
type UplinkStat struct {
|
|
|
|
NodeID storj.NodeID
|
|
|
|
TotalBytes int64
|
|
|
|
PutActionCount int
|
|
|
|
GetActionCount int
|
|
|
|
TotalTransactions int
|
|
|
|
}
|
|
|
|
|
2019-01-02 17:53:27 +00:00
|
|
|
// DB stores bandwidth agreements.
|
2018-12-07 09:59:31 +00:00
|
|
|
type DB interface {
|
2019-01-02 17:53:27 +00:00
|
|
|
// CreateAgreement adds a new bandwidth agreement.
|
2019-01-28 19:45:25 +00:00
|
|
|
CreateAgreement(context.Context, *pb.RenterBandwidthAllocation) error
|
2019-02-01 18:50:12 +00:00
|
|
|
// GetTotalsSince returns the sum of each bandwidth type after (exluding) a given date range
|
|
|
|
GetTotals(context.Context, time.Time, time.Time) (map[storj.NodeID][]int64, error)
|
|
|
|
//GetTotals returns stats about an uplink
|
|
|
|
GetUplinkStats(context.Context, time.Time, time.Time) ([]UplinkStat, error)
|
2018-12-07 09:59:31 +00:00
|
|
|
}
|
|
|
|
|
2018-11-05 15:23:54 +00:00
|
|
|
// Server is an implementation of the pb.BandwidthServer interface
|
|
|
|
type Server struct {
|
2019-02-07 19:22:49 +00:00
|
|
|
bwdb DB
|
|
|
|
certdb certdb.DB
|
|
|
|
pkey crypto.PublicKey
|
2019-01-25 18:05:21 +00:00
|
|
|
NodeID storj.NodeID
|
2018-11-12 21:59:30 +00:00
|
|
|
logger *zap.Logger
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewServer creates instance of Server
|
2019-02-07 19:22:49 +00:00
|
|
|
func NewServer(db DB, upldb certdb.DB, pkey crypto.PublicKey, logger *zap.Logger, nodeID storj.NodeID) *Server {
|
2019-01-18 13:54:08 +00:00
|
|
|
// TODO: reorder arguments, rename logger -> log
|
2019-02-07 19:22:49 +00:00
|
|
|
return &Server{bwdb: db, certdb: upldb, pkey: pkey, logger: logger, NodeID: nodeID}
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
|
|
|
|
2019-01-18 13:54:08 +00:00
|
|
|
// Close closes resources
|
|
|
|
func (s *Server) Close() error { return nil }
|
|
|
|
|
2018-11-15 19:06:09 +00:00
|
|
|
// BandwidthAgreements receives and stores bandwidth agreements from storage nodes
|
2019-01-25 18:05:21 +00:00
|
|
|
func (s *Server) BandwidthAgreements(ctx context.Context, rba *pb.RenterBandwidthAllocation) (reply *pb.AgreementsSummary, err error) {
|
2018-11-05 15:23:54 +00:00
|
|
|
defer mon.Task()(&ctx)(&err)
|
2018-11-15 19:06:09 +00:00
|
|
|
s.logger.Debug("Received Agreement...")
|
|
|
|
reply = &pb.AgreementsSummary{
|
2019-01-11 18:15:49 +00:00
|
|
|
Status: pb.AgreementsSummary_REJECTED,
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
pba := rba.PayerAllocation
|
2019-01-25 18:05:21 +00:00
|
|
|
//verify message content
|
|
|
|
pi, err := identity.PeerIdentityFromContext(ctx)
|
2019-01-28 19:45:25 +00:00
|
|
|
if err != nil || rba.StorageNodeId != pi.ID {
|
2019-02-01 18:50:12 +00:00
|
|
|
return reply, auth.ErrBadID.New("Storage Node ID: %v vs %v", rba.StorageNodeId, pi.ID)
|
2019-01-09 15:02:03 +00:00
|
|
|
}
|
2019-01-25 18:05:21 +00:00
|
|
|
//todo: use whitelist for uplinks?
|
2019-01-28 19:45:25 +00:00
|
|
|
if pba.SatelliteId != s.NodeID {
|
2019-02-01 18:50:12 +00:00
|
|
|
return reply, pb.ErrPayer.New("Satellite ID: %v vs %v", pba.SatelliteId, s.NodeID)
|
2019-01-11 03:31:47 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
exp := time.Unix(pba.GetExpirationUnixSec(), 0).UTC()
|
2019-01-10 18:30:55 +00:00
|
|
|
if exp.Before(time.Now().UTC()) {
|
2019-01-28 19:45:25 +00:00
|
|
|
return reply, pb.ErrPayer.Wrap(auth.ErrExpired.New("%v vs %v", exp, time.Now().UTC()))
|
2019-01-10 18:30:55 +00:00
|
|
|
}
|
2019-02-07 19:22:49 +00:00
|
|
|
|
|
|
|
if err = s.verifySignature(ctx, rba); err != nil {
|
|
|
|
return reply, err
|
2019-01-25 18:05:21 +00:00
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
|
2019-01-25 18:05:21 +00:00
|
|
|
//save and return rersults
|
2019-02-07 19:22:49 +00:00
|
|
|
if err = s.bwdb.CreateAgreement(ctx, rba); err != nil {
|
2019-02-01 18:50:12 +00:00
|
|
|
if strings.Contains(err.Error(), "UNIQUE constraint failed") ||
|
|
|
|
strings.Contains(err.Error(), "violates unique constraint") {
|
|
|
|
return reply, pb.ErrPayer.Wrap(auth.ErrSerial.Wrap(err))
|
|
|
|
}
|
|
|
|
reply.Status = pb.AgreementsSummary_FAIL
|
|
|
|
return reply, pb.ErrPayer.Wrap(err)
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2018-11-15 19:06:09 +00:00
|
|
|
reply.Status = pb.AgreementsSummary_OK
|
|
|
|
s.logger.Debug("Stored Agreement...")
|
|
|
|
return reply, nil
|
2018-11-05 15:23:54 +00:00
|
|
|
}
|
2019-02-07 19:22:49 +00:00
|
|
|
|
|
|
|
func (s *Server) verifySignature(ctx context.Context, rba *pb.RenterBandwidthAllocation) error {
|
|
|
|
pba := rba.GetPayerAllocation()
|
|
|
|
|
|
|
|
// Get renter's public key from uplink agreement db
|
|
|
|
uplinkInfo, err := s.certdb.GetPublicKey(ctx, pba.UplinkId)
|
|
|
|
if err != nil {
|
|
|
|
return pb.ErrRenter.Wrap(auth.ErrVerify.New("Failed to unmarshal PayerBandwidthAllocation: %+v", err))
|
|
|
|
}
|
|
|
|
|
|
|
|
signatureLength := uplinkInfo.Curve.Params().P.BitLen() / 8
|
|
|
|
if len(rba.GetSignature()) < signatureLength {
|
|
|
|
return pb.ErrRenter.Wrap(auth.ErrSigLen.New("%d vs %d", len(rba.GetSignature()), signatureLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify Renter's (uplink) signature
|
|
|
|
rbad := *rba
|
|
|
|
rbad.SetSignature(nil)
|
|
|
|
rbad.SetCerts(nil)
|
|
|
|
rbadBytes, err := proto.Marshal(&rbad)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("marshalling error: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := cryptopasta.Verify(rbadBytes, rba.GetSignature(), uplinkInfo); !ok {
|
|
|
|
return pb.ErrRenter.Wrap(auth.ErrVerify.New("%+v", ok))
|
|
|
|
}
|
|
|
|
|
|
|
|
// satellite public key
|
|
|
|
k, ok := s.pkey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return Error.New("UnsupportedKey error: %+v", s.pkey)
|
|
|
|
}
|
|
|
|
|
|
|
|
signatureLength = k.Curve.Params().P.BitLen() / 8
|
|
|
|
if len(pba.GetSignature()) < signatureLength {
|
|
|
|
return pb.ErrPayer.Wrap(auth.ErrSigLen.New("%d vs %d", len(pba.GetSignature()), signatureLength))
|
|
|
|
}
|
|
|
|
|
|
|
|
// verify Payer's (satellite) signature
|
|
|
|
pbad := pba
|
|
|
|
pbad.SetSignature(nil)
|
|
|
|
pbad.SetCerts(nil)
|
|
|
|
pbadBytes, err := proto.Marshal(&pbad)
|
|
|
|
if err != nil {
|
|
|
|
return Error.New("marshalling error: %+v", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if ok := cryptopasta.Verify(pbadBytes, pba.GetSignature(), k); !ok {
|
|
|
|
return pb.ErrPayer.Wrap(auth.ErrVerify.New("%+v", ok))
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|