2019-03-18 10:55:06 +00:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package trust
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"sync"
|
|
|
|
|
2019-04-12 15:28:27 +01:00
|
|
|
"github.com/zeebo/errs"
|
2019-06-04 13:31:39 +01:00
|
|
|
monkit "gopkg.in/spacemonkeygo/monkit.v2"
|
2019-04-12 15:28:27 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/auth/signing"
|
|
|
|
"storj.io/storj/pkg/identity"
|
2019-07-17 19:14:44 +01:00
|
|
|
"storj.io/storj/pkg/pb"
|
2019-03-18 10:55:06 +00:00
|
|
|
"storj.io/storj/pkg/storj"
|
2019-07-17 19:14:44 +01:00
|
|
|
"storj.io/storj/pkg/transport"
|
2019-03-18 10:55:06 +00:00
|
|
|
)
|
|
|
|
|
2019-04-12 15:28:27 +01:00
|
|
|
// Error is the default error class
|
|
|
|
var Error = errs.Class("trust:")
|
2019-07-17 19:14:44 +01:00
|
|
|
|
2019-06-04 13:31:39 +01:00
|
|
|
var mon = monkit.Package()
|
2019-04-12 15:28:27 +01:00
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// Pool implements different peer verifications.
|
|
|
|
type Pool struct {
|
2019-07-17 19:14:44 +01:00
|
|
|
mu sync.RWMutex
|
|
|
|
transport transport.Client
|
2019-03-18 10:55:06 +00:00
|
|
|
|
2019-07-17 19:14:44 +01:00
|
|
|
trustedSatellites map[storj.NodeID]*satelliteInfoCache
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// satelliteInfoCache caches identity information about a satellite
|
|
|
|
type satelliteInfoCache struct {
|
2019-04-24 09:13:48 +01:00
|
|
|
mu sync.Mutex
|
2019-07-17 19:14:44 +01:00
|
|
|
url storj.NodeURL
|
2019-03-18 10:55:06 +00:00
|
|
|
identity *identity.PeerIdentity
|
|
|
|
}
|
|
|
|
|
2019-07-17 19:14:44 +01:00
|
|
|
// NewPool creates a new trust pool of the specified list of trusted satellites.
|
|
|
|
func NewPool(transport transport.Client, trustedSatellites storj.NodeURLs) (*Pool, error) {
|
2019-03-18 10:55:06 +00:00
|
|
|
// TODO: preload all satellite peer identities
|
|
|
|
|
|
|
|
// parse the comma separated list of approved satellite IDs into an array of storj.NodeIDs
|
|
|
|
trusted := make(map[storj.NodeID]*satelliteInfoCache)
|
|
|
|
|
2019-07-03 18:29:18 +01:00
|
|
|
for _, node := range trustedSatellites {
|
2019-07-17 19:14:44 +01:00
|
|
|
trusted[node.ID] = &satelliteInfoCache{url: node}
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &Pool{
|
2019-07-17 19:14:44 +01:00
|
|
|
transport: transport,
|
|
|
|
trustedSatellites: trusted,
|
2019-03-18 10:55:06 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// VerifySatelliteID checks whether id corresponds to a trusted satellite.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (pool *Pool) VerifySatelliteID(ctx context.Context, id storj.NodeID) (err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
pool.mu.RLock()
|
|
|
|
defer pool.mu.RUnlock()
|
|
|
|
|
|
|
|
_, ok := pool.trustedSatellites[id]
|
|
|
|
if !ok {
|
2019-07-17 19:14:44 +01:00
|
|
|
return Error.New("satellite %q is untrusted", id)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSignee gets the corresponding signee for verifying signatures.
|
2019-04-08 21:31:20 +01:00
|
|
|
// It ignores passed in ctx cancellation to avoid miscaching between concurrent requests.
|
2019-06-04 13:31:39 +01:00
|
|
|
func (pool *Pool) GetSignee(ctx context.Context, id storj.NodeID) (_ signing.Signee, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
2019-03-18 10:55:06 +00:00
|
|
|
// lookup peer identity with id
|
|
|
|
pool.mu.RLock()
|
|
|
|
info, ok := pool.trustedSatellites[id]
|
|
|
|
pool.mu.RUnlock()
|
|
|
|
|
2019-07-17 19:14:44 +01:00
|
|
|
if !ok {
|
|
|
|
return nil, Error.New("signee %q is untrusted", id)
|
2019-03-18 10:55:06 +00:00
|
|
|
}
|
|
|
|
|
2019-04-24 09:13:48 +01:00
|
|
|
info.mu.Lock()
|
|
|
|
defer info.mu.Unlock()
|
2019-04-12 15:28:27 +01:00
|
|
|
|
2019-04-24 09:13:48 +01:00
|
|
|
if info.identity == nil {
|
2019-07-17 19:14:44 +01:00
|
|
|
identity, err := pool.FetchPeerIdentity(ctx, info.url)
|
2019-04-24 09:13:48 +01:00
|
|
|
if err != nil {
|
|
|
|
return nil, Error.Wrap(err)
|
|
|
|
}
|
2019-04-12 15:28:27 +01:00
|
|
|
info.identity = identity
|
2019-04-24 09:13:48 +01:00
|
|
|
}
|
2019-03-18 10:55:06 +00:00
|
|
|
|
|
|
|
return signing.SigneeFromPeerIdentity(info.identity), nil
|
|
|
|
}
|
2019-06-21 23:48:52 +01:00
|
|
|
|
2019-07-17 19:14:44 +01:00
|
|
|
// FetchPeerIdentity dials the url and fetches the identity.
|
|
|
|
func (pool *Pool) FetchPeerIdentity(ctx context.Context, url storj.NodeURL) (_ *identity.PeerIdentity, err error) {
|
|
|
|
identity, err := pool.transport.FetchPeerIdentity(ctx, &pb.Node{
|
|
|
|
Id: url.ID,
|
|
|
|
Address: &pb.NodeAddress{
|
|
|
|
Transport: pb.NodeTransport_TCP_TLS_GRPC,
|
|
|
|
Address: url.Address,
|
|
|
|
},
|
|
|
|
})
|
|
|
|
return identity, Error.Wrap(err)
|
|
|
|
}
|
|
|
|
|
2019-06-21 23:48:52 +01:00
|
|
|
// GetSatellites returns a slice containing all trusted satellites
|
|
|
|
func (pool *Pool) GetSatellites(ctx context.Context) (satellites []storj.NodeID) {
|
|
|
|
defer mon.Task()(&ctx)(nil)
|
|
|
|
for sat := range pool.trustedSatellites {
|
|
|
|
satellites = append(satellites, sat)
|
|
|
|
}
|
|
|
|
return satellites
|
|
|
|
}
|
2019-07-03 18:29:18 +01:00
|
|
|
|
|
|
|
// GetAddress returns the address of a satellite in the trusted list
|
|
|
|
func (pool *Pool) GetAddress(ctx context.Context, id storj.NodeID) (_ string, err error) {
|
|
|
|
defer mon.Task()(&ctx)(&err)
|
|
|
|
|
|
|
|
pool.mu.RLock()
|
|
|
|
defer pool.mu.RUnlock()
|
|
|
|
|
|
|
|
info, ok := pool.trustedSatellites[id]
|
|
|
|
if !ok {
|
|
|
|
return "", Error.New("ID %v not found in trusted list", id)
|
|
|
|
}
|
2019-07-17 19:14:44 +01:00
|
|
|
return info.url.Address, nil
|
2019-07-03 18:29:18 +01:00
|
|
|
}
|