storj/pkg/bwagreement/testbwagreement/common.go

80 lines
2.5 KiB
Go
Raw Normal View History

2019-01-24 20:15:10 +00:00
// Copyright (C) 2019 Storj Labs, Inc.
Satellite bw usage v3 121 (#547) * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * added postgres create/read/delete test function Co-authored-by: kishore <kishore@storj.io Co-authored-by: cam <cameron@storj.io> * edit comment * removed sqlite3 driver from dbx * remove generated sqlite code, add dbx read limitoffset * remove getServerAndDB function, rename getDBPath to getPSQLInfo * WIP writing server endpoint test * code review changes
2018-11-05 15:23:54 +00:00
// See LICENSE for copying information.
2019-01-25 23:06:38 +00:00
package testbwagreement
Satellite bw usage v3 121 (#547) * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * added postgres create/read/delete test function Co-authored-by: kishore <kishore@storj.io Co-authored-by: cam <cameron@storj.io> * edit comment * removed sqlite3 driver from dbx * remove generated sqlite code, add dbx read limitoffset * remove getServerAndDB function, rename getDBPath to getPSQLInfo * WIP writing server endpoint test * code review changes
2018-11-05 15:23:54 +00:00
import (
"crypto/ecdsa"
"time"
Satellite bw usage v3 121 (#547) * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * added postgres create/read/delete test function Co-authored-by: kishore <kishore@storj.io Co-authored-by: cam <cameron@storj.io> * edit comment * removed sqlite3 driver from dbx * remove generated sqlite code, add dbx read limitoffset * remove getServerAndDB function, rename getDBPath to getPSQLInfo * WIP writing server endpoint test * code review changes
2018-11-05 15:23:54 +00:00
"github.com/gogo/protobuf/proto"
"github.com/gtank/cryptopasta"
"github.com/skyrings/skyring-common/tools/uuid"
"storj.io/storj/pkg/auth"
"storj.io/storj/pkg/identity"
Satellite bw usage v3 121 (#547) * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * added postgres create/read/delete test function Co-authored-by: kishore <kishore@storj.io Co-authored-by: cam <cameron@storj.io> * edit comment * removed sqlite3 driver from dbx * remove generated sqlite code, add dbx read limitoffset * remove getServerAndDB function, rename getDBPath to getPSQLInfo * WIP writing server endpoint test * code review changes
2018-11-05 15:23:54 +00:00
"storj.io/storj/pkg/pb"
"storj.io/storj/pkg/storj"
Satellite bw usage v3 121 (#547) * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * protobuf for sending bandwidth agreements to satellite from storage nodes * Setup process for sending agreements * Add payer_id to db with bandwidth agreements for better sorting * Linter errors * Read agreements from PSDB * Try writing message to server * Cleanup * Basic functionality * Better error handelling * Fix test * setup config and server structure for receiving bandwidth agreements * Resolve linter issues * Optional commit for if we want to handle deletes all at once * add identity to Server, add logic for receiving bandwidth messsages * Bandwidth agreement DBX creation and integration with bw agreement endpoint Co-authored-by: Kishore <kishore@storj.io> Co-authored-by: Cam <cameron@storj.io> * added postgres create/read/delete test function Co-authored-by: kishore <kishore@storj.io Co-authored-by: cam <cameron@storj.io> * edit comment * removed sqlite3 driver from dbx * remove generated sqlite code, add dbx read limitoffset * remove getServerAndDB function, rename getDBPath to getPSQLInfo * WIP writing server endpoint test * code review changes
2018-11-05 15:23:54 +00:00
)
//GeneratePayerBandwidthAllocation creates a signed PayerBandwidthAllocation from a PayerBandwidthAllocation_Action
func GeneratePayerBandwidthAllocation(action pb.PayerBandwidthAllocation_Action, satID *identity.FullIdentity, upID *identity.FullIdentity, expiration time.Duration) (*pb.PayerBandwidthAllocation, error) {
serialNum, err := uuid.New()
if err != nil {
return nil, err
}
// Generate PayerBandwidthAllocation_Data
data, _ := proto.Marshal(
&pb.PayerBandwidthAllocation_Data{
SatelliteId: satID.ID,
UplinkId: upID.ID,
ExpirationUnixSec: time.Now().Add(expiration).Unix(),
SerialNumber: serialNum.String(),
Action: action,
CreatedUnixSec: time.Now().Unix(),
},
)
// Sign the PayerBandwidthAllocation_Data with the "Satellite" Private Key
satPrivECDSA, ok := satID.Key.(*ecdsa.PrivateKey)
if !ok {
return nil, pb.Payer.Wrap(auth.ECDSA)
}
s, err := cryptopasta.Sign(data, satPrivECDSA)
if err != nil {
return nil, pb.Payer.Wrap(auth.Sign.Wrap(err))
}
// Combine Signature and Data for PayerBandwidthAllocation
return &pb.PayerBandwidthAllocation{
Data: data,
Signature: s,
Certs: satID.ChainRaw(),
}, nil
}
//GenerateRenterBandwidthAllocation creates a signed RenterBandwidthAllocation from a PayerBandwidthAllocation
func GenerateRenterBandwidthAllocation(pba *pb.PayerBandwidthAllocation, storageNodeID storj.NodeID, upID *identity.FullIdentity, total int64) (*pb.RenterBandwidthAllocation, error) {
// Generate RenterBandwidthAllocation_Data
data, _ := proto.Marshal(
&pb.RenterBandwidthAllocation_Data{
PayerAllocation: pba,
StorageNodeId: storageNodeID,
Total: total,
},
)
// Sign the PayerBandwidthAllocation_Data with the "Uplink" Private Key
upPrivECDSA, ok := upID.Key.(*ecdsa.PrivateKey)
if !ok {
return nil, pb.Payer.Wrap(auth.ECDSA)
}
s, err := cryptopasta.Sign(data, upPrivECDSA)
if err != nil {
return nil, pb.Payer.Wrap(auth.Sign.Wrap(err))
}
// Combine Signature and Data for RenterBandwidthAllocation
return &pb.RenterBandwidthAllocation{
Signature: s,
Data: data,
Certs: upID.ChainRaw(),
}, nil
}