2020-03-30 15:19:36 +01:00
|
|
|
// Copyright (C) 2019 Storj Labs, Inc.
|
|
|
|
// See LICENSE for copying information.
|
|
|
|
|
|
|
|
package metainfo
|
|
|
|
|
|
|
|
import (
|
|
|
|
"context"
|
|
|
|
"strings"
|
2020-04-14 12:50:50 +01:00
|
|
|
"sync"
|
2020-04-14 09:27:43 +01:00
|
|
|
|
|
|
|
"go.uber.org/zap"
|
2020-03-30 15:19:36 +01:00
|
|
|
|
2020-04-14 12:50:50 +01:00
|
|
|
"storj.io/common/errs2"
|
2020-03-30 15:19:36 +01:00
|
|
|
"storj.io/common/pb"
|
|
|
|
"storj.io/common/rpc/rpcstatus"
|
2020-04-14 09:27:43 +01:00
|
|
|
"storj.io/common/storj"
|
2020-03-30 15:19:36 +01:00
|
|
|
"storj.io/common/useragent"
|
2020-03-30 10:08:50 +01:00
|
|
|
"storj.io/common/uuid"
|
2020-04-14 12:50:50 +01:00
|
|
|
"storj.io/drpc/drpccache"
|
2020-04-14 09:27:43 +01:00
|
|
|
"storj.io/storj/satellite/attribution"
|
2020-07-24 10:40:17 +01:00
|
|
|
"storj.io/storj/satellite/console"
|
2020-03-30 15:19:36 +01:00
|
|
|
)
|
|
|
|
|
2020-07-24 10:40:17 +01:00
|
|
|
// ensureAttribution ensures that the bucketName has the partner information specified by keyInfo partner ID or the header user agent.
|
|
|
|
// PartnerID from keyInfo is a value associated with registered user and prevails over header user agent.
|
2020-04-14 12:50:50 +01:00
|
|
|
//
|
|
|
|
// Assumes that the user has permissions sufficient for authenticating.
|
2020-07-24 10:40:17 +01:00
|
|
|
func (endpoint *Endpoint) ensureAttribution(ctx context.Context, header *pb.RequestHeader, keyInfo *console.APIKeyInfo, bucketName []byte) error {
|
2020-04-14 12:50:50 +01:00
|
|
|
if header == nil {
|
|
|
|
return rpcstatus.Error(rpcstatus.InvalidArgument, "header is nil")
|
|
|
|
}
|
2021-09-23 00:38:18 +01:00
|
|
|
if len(header.UserAgent) == 0 && keyInfo.PartnerID.IsZero() && keyInfo.UserAgent == nil {
|
2020-04-14 12:50:50 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
if conncache := drpccache.FromContext(ctx); conncache != nil {
|
|
|
|
cache := conncache.LoadOrCreate(attributionCheckCacheKey{},
|
|
|
|
func() interface{} {
|
|
|
|
return &attributionCheckCache{}
|
|
|
|
}).(*attributionCheckCache)
|
|
|
|
if !cache.needsCheck(string(bucketName)) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-07-24 10:40:17 +01:00
|
|
|
partnerID := keyInfo.PartnerID
|
2021-09-23 00:38:18 +01:00
|
|
|
userAgent := keyInfo.UserAgent
|
|
|
|
// first check keyInfo (user) attribution
|
|
|
|
if partnerID.IsZero() && userAgent == nil {
|
|
|
|
// otherwise, use header (partner tool) as attribution
|
|
|
|
userAgent = header.UserAgent
|
|
|
|
if userAgent == nil {
|
2020-07-24 10:40:17 +01:00
|
|
|
return nil
|
|
|
|
}
|
2020-04-14 12:50:50 +01:00
|
|
|
}
|
|
|
|
|
2021-09-23 00:38:18 +01:00
|
|
|
err := endpoint.tryUpdateBucketAttribution(ctx, header, keyInfo.ProjectID, bucketName, partnerID, userAgent)
|
2020-04-14 12:50:50 +01:00
|
|
|
if errs2.IsRPC(err, rpcstatus.NotFound) || errs2.IsRPC(err, rpcstatus.AlreadyExists) {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
2020-03-30 15:19:36 +01:00
|
|
|
// ResolvePartnerID returns partnerIDBytes as parsed or UUID corresponding to header.UserAgent.
|
|
|
|
// returns empty uuid when neither is defined.
|
2020-06-15 13:26:21 +01:00
|
|
|
func (endpoint *Endpoint) ResolvePartnerID(ctx context.Context, header *pb.RequestHeader) (uuid.UUID, error) {
|
2020-03-30 15:19:36 +01:00
|
|
|
if header == nil {
|
|
|
|
return uuid.UUID{}, rpcstatus.Error(rpcstatus.InvalidArgument, "header is nil")
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(header.UserAgent) == 0 {
|
|
|
|
return uuid.UUID{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
entries, err := useragent.ParseEntries(header.UserAgent)
|
|
|
|
if err != nil {
|
|
|
|
return uuid.UUID{}, rpcstatus.Errorf(rpcstatus.InvalidArgument, "invalid user agent %q: %v", string(header.UserAgent), err)
|
|
|
|
}
|
|
|
|
entries = removeUplinkUserAgent(entries)
|
|
|
|
|
|
|
|
// no user agent defined
|
|
|
|
if len(entries) == 0 {
|
|
|
|
return uuid.UUID{}, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Use the first partner product entry as the PartnerID.
|
|
|
|
for _, entry := range entries {
|
|
|
|
if entry.Product != "" {
|
|
|
|
partner, err := endpoint.partners.ByUserAgent(ctx, entry.Product)
|
2020-04-02 13:30:43 +01:00
|
|
|
if err != nil || partner.UUID.IsZero() {
|
2020-03-30 15:19:36 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-04-02 13:30:43 +01:00
|
|
|
return partner.UUID, nil
|
2020-03-30 15:19:36 +01:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-04-14 12:50:50 +01:00
|
|
|
return uuid.UUID{}, nil
|
2020-03-30 15:19:36 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
func removeUplinkUserAgent(entries []useragent.Entry) []useragent.Entry {
|
|
|
|
var xs []useragent.Entry
|
|
|
|
for i := 0; i < len(entries); i++ {
|
|
|
|
// If it's "uplink" then skip it.
|
2021-02-09 22:40:23 +00:00
|
|
|
if strings.EqualFold(entries[i].Product, uplinkProduct) {
|
2020-03-30 15:19:36 +01:00
|
|
|
// also skip any associated comments
|
|
|
|
for i+1 < len(entries) && entries[i+1].Comment != "" {
|
|
|
|
i++
|
|
|
|
}
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
xs = append(xs, entries[i])
|
|
|
|
}
|
|
|
|
return xs
|
|
|
|
}
|
2020-04-14 09:27:43 +01:00
|
|
|
|
2021-09-23 00:38:18 +01:00
|
|
|
func (endpoint *Endpoint) tryUpdateBucketAttribution(ctx context.Context, header *pb.RequestHeader, projectID uuid.UUID, bucketName []byte, partnerID uuid.UUID, userAgent []byte) error {
|
2020-04-14 12:50:50 +01:00
|
|
|
if header == nil {
|
|
|
|
return rpcstatus.Error(rpcstatus.InvalidArgument, "header is nil")
|
|
|
|
}
|
|
|
|
|
2020-04-14 09:27:43 +01:00
|
|
|
// check if attribution is set for given bucket
|
2020-04-14 12:50:50 +01:00
|
|
|
_, err := endpoint.attributions.Get(ctx, projectID, bucketName)
|
2020-04-14 09:27:43 +01:00
|
|
|
if err == nil {
|
2020-04-14 12:50:50 +01:00
|
|
|
// bucket has already an attribution, no need to update
|
2020-04-14 09:27:43 +01:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if !attribution.ErrBucketNotAttributed.Has(err) {
|
|
|
|
// try only to set the attribution, when it's missing
|
|
|
|
endpoint.log.Error("error while getting attribution from DB", zap.Error(err))
|
|
|
|
return rpcstatus.Error(rpcstatus.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
2021-09-09 16:21:42 +01:00
|
|
|
empty, err := endpoint.isBucketEmpty(ctx, projectID, bucketName)
|
2020-04-14 09:27:43 +01:00
|
|
|
if err != nil {
|
2021-09-09 16:21:42 +01:00
|
|
|
endpoint.log.Error("internal", zap.Error(err))
|
|
|
|
return rpcstatus.Error(rpcstatus.Internal, Error.Wrap(err).Error())
|
2020-04-14 09:27:43 +01:00
|
|
|
}
|
|
|
|
if !empty {
|
|
|
|
return rpcstatus.Errorf(rpcstatus.AlreadyExists, "bucket %q is not empty, PartnerID %q cannot be attributed", bucketName, partnerID)
|
|
|
|
}
|
|
|
|
|
|
|
|
// checks if bucket exists before updates it or makes a new entry
|
2021-09-09 16:21:42 +01:00
|
|
|
bucket, err := endpoint.buckets.GetBucket(ctx, bucketName, projectID)
|
2020-04-14 09:27:43 +01:00
|
|
|
if err != nil {
|
|
|
|
if storj.ErrBucketNotFound.Has(err) {
|
|
|
|
return rpcstatus.Errorf(rpcstatus.NotFound, "bucket %q does not exist", bucketName)
|
|
|
|
}
|
|
|
|
endpoint.log.Error("error while getting bucket", zap.ByteString("bucketName", bucketName), zap.Error(err))
|
|
|
|
return rpcstatus.Error(rpcstatus.Internal, "unable to set bucket attribution")
|
|
|
|
}
|
2021-09-23 00:38:18 +01:00
|
|
|
if !bucket.PartnerID.IsZero() || bucket.UserAgent != nil {
|
2020-04-14 12:50:50 +01:00
|
|
|
return rpcstatus.Errorf(rpcstatus.AlreadyExists, "bucket %q already has attribution, PartnerID %q cannot be attributed", bucketName, partnerID)
|
2020-04-14 09:27:43 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// update bucket information
|
|
|
|
bucket.PartnerID = partnerID
|
2021-09-23 00:38:18 +01:00
|
|
|
bucket.UserAgent = userAgent
|
2021-09-09 16:21:42 +01:00
|
|
|
_, err = endpoint.buckets.UpdateBucket(ctx, bucket)
|
2020-04-14 09:27:43 +01:00
|
|
|
if err != nil {
|
|
|
|
endpoint.log.Error("error while updating bucket", zap.ByteString("bucketName", bucketName), zap.Error(err))
|
|
|
|
return rpcstatus.Error(rpcstatus.Internal, "unable to set bucket attribution")
|
|
|
|
}
|
|
|
|
|
|
|
|
// update attribution table
|
|
|
|
_, err = endpoint.attributions.Insert(ctx, &attribution.Info{
|
2020-04-14 12:50:50 +01:00
|
|
|
ProjectID: projectID,
|
2020-04-14 09:27:43 +01:00
|
|
|
BucketName: bucketName,
|
|
|
|
PartnerID: partnerID,
|
2021-09-23 00:38:18 +01:00
|
|
|
UserAgent: userAgent,
|
2020-04-14 09:27:43 +01:00
|
|
|
})
|
|
|
|
if err != nil {
|
|
|
|
endpoint.log.Error("error while inserting attribution to DB", zap.Error(err))
|
|
|
|
return rpcstatus.Error(rpcstatus.Internal, err.Error())
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2020-04-14 12:50:50 +01:00
|
|
|
|
|
|
|
// maxAttributionCacheSize determines how many buckets attributionCheckCache remembers.
|
|
|
|
const maxAttributionCacheSize = 10
|
|
|
|
|
|
|
|
// attributionCheckCacheKey is used as a key for the connection cache.
|
|
|
|
type attributionCheckCacheKey struct{}
|
|
|
|
|
|
|
|
// attributionCheckCache implements a basic lru cache, with a constant size.
|
|
|
|
type attributionCheckCache struct {
|
|
|
|
mu sync.Mutex
|
|
|
|
pos int
|
|
|
|
buckets []string
|
|
|
|
}
|
|
|
|
|
|
|
|
// needsCheck returns true when the bucket should be tested for setting the useragent.
|
|
|
|
func (cache *attributionCheckCache) needsCheck(bucket string) bool {
|
|
|
|
cache.mu.Lock()
|
|
|
|
defer cache.mu.Unlock()
|
|
|
|
|
|
|
|
for _, b := range cache.buckets {
|
|
|
|
if b == bucket {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(cache.buckets) >= maxAttributionCacheSize {
|
|
|
|
cache.pos = (cache.pos + 1) % len(cache.buckets)
|
|
|
|
cache.buckets[cache.pos] = bucket
|
|
|
|
} else {
|
|
|
|
cache.pos = len(cache.buckets)
|
|
|
|
cache.buckets = append(cache.buckets, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
return true
|
|
|
|
}
|