2019-01-19 18:58:53 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package pointerdb
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
2019-02-07 19:22:49 +00:00
|
|
|
"crypto/ecdsa"
|
2019-01-19 18:58:53 +00:00
|
|
|
"time"
|
|
|
|
|
|
|
|
"github.com/skyrings/skyring-common/tools/uuid"
|
|
|
|
|
|
|
|
"storj.io/storj/pkg/auth"
|
2019-02-07 19:22:49 +00:00
|
|
|
"storj.io/storj/pkg/certdb"
|
2019-01-19 18:58:53 +00:00
|
|
|
"storj.io/storj/pkg/identity"
|
|
|
|
"storj.io/storj/pkg/pb"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AllocationSigner structure
|
|
|
|
type AllocationSigner struct {
|
|
|
|
satelliteIdentity *identity.FullIdentity
|
|
|
|
bwExpiration int
|
2019-02-07 19:22:49 +00:00
|
|
|
certdb certdb.DB
|
2019-01-19 18:58:53 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// NewAllocationSigner creates new instance
|
2019-02-07 19:22:49 +00:00
|
|
|
func NewAllocationSigner(satelliteIdentity *identity.FullIdentity, bwExpiration int, upldb certdb.DB) *AllocationSigner {
|
2019-01-19 18:58:53 +00:00
|
|
|
return &AllocationSigner{
|
|
|
|
satelliteIdentity: satelliteIdentity,
|
|
|
|
bwExpiration: bwExpiration,
|
2019-02-07 19:22:49 +00:00
|
|
|
certdb: upldb,
|
2019-01-19 18:58:53 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// PayerBandwidthAllocation returns generated payer bandwidth allocation
|
2019-01-28 19:45:25 +00:00
|
|
|
func (allocation *AllocationSigner) PayerBandwidthAllocation(ctx context.Context, peerIdentity *identity.PeerIdentity, action pb.BandwidthAction) (pba *pb.PayerBandwidthAllocation, err error) {
|
2019-01-19 18:58:53 +00:00
|
|
|
if peerIdentity == nil {
|
|
|
|
return nil, Error.New("missing peer identity")
|
|
|
|
}
|
|
|
|
serialNum, err := uuid.New()
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
created := time.Now().Unix()
|
|
|
|
// convert ttl from days to seconds
|
|
|
|
ttl := allocation.bwExpiration
|
|
|
|
ttl *= 86400
|
2019-02-07 19:22:49 +00:00
|
|
|
|
|
|
|
pk, ok := peerIdentity.Leaf.PublicKey.(*ecdsa.PublicKey)
|
|
|
|
if !ok {
|
|
|
|
return nil, Error.New("UnsupportedKey error: %+v", peerIdentity.Leaf.PublicKey)
|
|
|
|
}
|
|
|
|
// store the corresponding uplink's id and public key into certDB db
|
|
|
|
err = allocation.certdb.SavePublicKey(ctx, peerIdentity.ID, pk)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2019-01-28 19:45:25 +00:00
|
|
|
pba = &pb.PayerBandwidthAllocation{
|
2019-01-19 18:58:53 +00:00
|
|
|
SatelliteId: allocation.satelliteIdentity.ID,
|
|
|
|
UplinkId: peerIdentity.ID,
|
|
|
|
CreatedUnixSec: created,
|
|
|
|
ExpirationUnixSec: created + int64(ttl),
|
|
|
|
Action: action,
|
|
|
|
SerialNumber: serialNum.String(),
|
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
if err := auth.SignMessage(pba, *allocation.satelliteIdentity); err != nil {
|
2019-01-19 18:58:53 +00:00
|
|
|
return nil, err
|
|
|
|
}
|
2019-01-28 19:45:25 +00:00
|
|
|
return pba, nil
|
2019-01-19 18:58:53 +00:00
|
|
|
}
|