2022-02-11 12:25:05 +00:00
|
|
|
package v2
|
|
|
|
|
|
|
|
import (
|
|
|
|
"crypto/ecdsa"
|
|
|
|
"crypto/elliptic"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
|
|
|
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
|
|
|
objectV2 "github.com/nspcc-dev/neofs-api-go/v2/object"
|
|
|
|
refsV2 "github.com/nspcc-dev/neofs-api-go/v2/refs"
|
|
|
|
sessionV2 "github.com/nspcc-dev/neofs-api-go/v2/session"
|
2022-05-12 07:22:02 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/bearer"
|
2022-02-11 12:25:05 +00:00
|
|
|
containerIDSDK "github.com/nspcc-dev/neofs-sdk-go/container/id"
|
|
|
|
eaclSDK "github.com/nspcc-dev/neofs-sdk-go/eacl"
|
|
|
|
oidSDK "github.com/nspcc-dev/neofs-sdk-go/object/id"
|
|
|
|
sessionSDK "github.com/nspcc-dev/neofs-sdk-go/session"
|
2022-05-17 13:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neofs-sdk-go/user"
|
2022-02-11 12:25:05 +00:00
|
|
|
)
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
var errMissingContainerID = errors.New("missing container ID")
|
|
|
|
|
2022-05-24 15:28:48 +00:00
|
|
|
func getContainerIDFromRequest(req interface{}) (containerIDSDK.ID, error) {
|
2022-05-12 16:37:46 +00:00
|
|
|
var idV2 *refsV2.ContainerID
|
2022-05-24 15:28:48 +00:00
|
|
|
var id containerIDSDK.ID
|
2022-05-12 16:37:46 +00:00
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
switch v := req.(type) {
|
|
|
|
case *objectV2.GetRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetAddress().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.PutRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
part, ok := v.GetBody().GetObjectPart().(*objectV2.PutObjectPartInit)
|
|
|
|
if !ok {
|
2022-05-24 15:28:48 +00:00
|
|
|
return containerIDSDK.ID{}, errors.New("can't get container ID in chunk")
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = part.GetHeader().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.HeadRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetAddress().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.SearchRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.DeleteRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetAddress().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.GetRangeRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetAddress().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case *objectV2.GetRangeHashRequest:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetBody().GetAddress().GetContainerID()
|
2022-02-11 12:25:05 +00:00
|
|
|
default:
|
2022-05-24 15:28:48 +00:00
|
|
|
return containerIDSDK.ID{}, errors.New("unknown request type")
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
if idV2 == nil {
|
2022-05-24 15:28:48 +00:00
|
|
|
return containerIDSDK.ID{}, errMissingContainerID
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return id, id.ReadFromV2(*idV2)
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// originalBearerToken goes down to original request meta header and fetches
|
|
|
|
// bearer token from there.
|
2022-05-12 07:22:02 +00:00
|
|
|
func originalBearerToken(header *sessionV2.RequestMetaHeader) *bearer.Token {
|
2022-02-11 12:25:05 +00:00
|
|
|
for header.GetOrigin() != nil {
|
|
|
|
header = header.GetOrigin()
|
|
|
|
}
|
|
|
|
|
2022-05-12 07:22:02 +00:00
|
|
|
tokV2 := header.GetBearerToken()
|
|
|
|
if tokV2 == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var tok bearer.Token
|
|
|
|
tok.ReadFromV2(*tokV2)
|
|
|
|
|
|
|
|
return &tok
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
// originalSessionToken goes down to original request meta header and fetches
|
|
|
|
// session token from there.
|
2022-05-18 15:20:08 +00:00
|
|
|
func originalSessionToken(header *sessionV2.RequestMetaHeader) (*sessionSDK.Object, error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
for header.GetOrigin() != nil {
|
|
|
|
header = header.GetOrigin()
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
tokV2 := header.GetSessionToken()
|
|
|
|
if tokV2 == nil {
|
|
|
|
return nil, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
var tok sessionSDK.Object
|
|
|
|
|
|
|
|
err := tok.ReadFromV2(*tokV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("invalid session token: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return &tok, nil
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-12 16:37:46 +00:00
|
|
|
func getObjectIDFromRequestBody(body interface{}) (*oidSDK.ID, error) {
|
|
|
|
var idV2 *refsV2.ObjectID
|
|
|
|
|
2022-02-11 12:25:05 +00:00
|
|
|
switch v := body.(type) {
|
|
|
|
default:
|
2022-05-12 16:37:46 +00:00
|
|
|
return nil, nil
|
2022-02-11 12:25:05 +00:00
|
|
|
case interface {
|
|
|
|
GetObjectID() *refsV2.ObjectID
|
|
|
|
}:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetObjectID()
|
2022-02-11 12:25:05 +00:00
|
|
|
case interface {
|
|
|
|
GetAddress() *refsV2.Address
|
|
|
|
}:
|
2022-05-12 16:37:46 +00:00
|
|
|
idV2 = v.GetAddress().GetObjectID()
|
|
|
|
}
|
|
|
|
|
|
|
|
if idV2 == nil {
|
|
|
|
return nil, nil
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
2022-05-12 16:37:46 +00:00
|
|
|
|
|
|
|
var id oidSDK.ID
|
|
|
|
|
|
|
|
err := id.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
return &id, nil
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
func useObjectIDFromSession(req *RequestInfo, token *sessionSDK.Object) {
|
2022-02-11 12:25:05 +00:00
|
|
|
if token == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
// TODO(@cthulhu-rider): It'd be nice to not pull object identifiers from
|
|
|
|
// the token, but assert them. Track #1420
|
|
|
|
var tokV2 sessionV2.Token
|
|
|
|
token.WriteToV2(&tokV2)
|
|
|
|
|
|
|
|
ctx, ok := tokV2.GetBody().GetContext().(*sessionV2.ObjectSessionContext)
|
2022-02-11 12:25:05 +00:00
|
|
|
if !ok {
|
2022-05-18 15:20:08 +00:00
|
|
|
panic(fmt.Sprintf("wrong object session context %T, is it verified?", tokV2.GetBody().GetContext()))
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
idV2 := ctx.GetAddress().GetObjectID()
|
|
|
|
if idV2 == nil {
|
|
|
|
return
|
2022-05-12 16:37:46 +00:00
|
|
|
}
|
2022-02-11 12:25:05 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
req.oid = new(oidSDK.ID)
|
|
|
|
|
|
|
|
err := req.oid.ReadFromV2(*idV2)
|
|
|
|
if err != nil {
|
|
|
|
panic(fmt.Sprintf("unexpected protocol violation error after correct session token decoding: %v", err))
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
func ownerFromToken(token *sessionSDK.Object) (*user.ID, *keys.PublicKey, error) {
|
2022-02-11 12:25:05 +00:00
|
|
|
// 1. First check signature of session token.
|
|
|
|
if !token.VerifySignature() {
|
|
|
|
return nil, nil, fmt.Errorf("%w: invalid session token signature", ErrMalformedRequest)
|
|
|
|
}
|
|
|
|
|
|
|
|
// 2. Then check if session token owner issued the session token
|
2022-05-16 13:15:31 +00:00
|
|
|
// TODO(@cthulhu-rider): #1387 implement and use another approach to avoid conversion
|
2022-05-18 15:20:08 +00:00
|
|
|
var tokV2 sessionV2.Token
|
|
|
|
token.WriteToV2(&tokV2)
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
tokenIssuerKey, err := unmarshalPublicKey(tokV2.GetSignature().GetKey())
|
|
|
|
if err != nil {
|
|
|
|
return nil, nil, fmt.Errorf("invalid key in session token signature: %w", err)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:09:12 +00:00
|
|
|
tokenIssuer := token.Issuer()
|
|
|
|
|
|
|
|
if !isOwnerFromKey(&tokenIssuer, tokenIssuerKey) {
|
2022-02-11 12:25:05 +00:00
|
|
|
// TODO: #767 in this case we can issue all owner keys from neofs.id and check once again
|
|
|
|
return nil, nil, fmt.Errorf("%w: invalid session token owner", ErrMalformedRequest)
|
|
|
|
}
|
|
|
|
|
2022-05-25 16:09:12 +00:00
|
|
|
return &tokenIssuer, tokenIssuerKey, nil
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
func originalBodySignature(v *sessionV2.RequestVerificationHeader) *refsV2.Signature {
|
2022-02-11 12:25:05 +00:00
|
|
|
if v == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for v.GetOrigin() != nil {
|
|
|
|
v = v.GetOrigin()
|
|
|
|
}
|
|
|
|
|
2022-05-16 13:15:31 +00:00
|
|
|
return v.GetBodySignature()
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
func unmarshalPublicKey(bs []byte) (*keys.PublicKey, error) {
|
|
|
|
return keys.NewPublicKeyFromBytes(bs, elliptic.P256())
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
func isOwnerFromKey(id *user.ID, key *keys.PublicKey) bool {
|
2022-02-11 12:25:05 +00:00
|
|
|
if id == nil || key == nil {
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2022-05-17 13:59:46 +00:00
|
|
|
var id2 user.ID
|
|
|
|
user.IDFromKey(&id2, (ecdsa.PublicKey)(*key))
|
|
|
|
|
|
|
|
return id2.Equals(*id)
|
2022-02-11 12:25:05 +00:00
|
|
|
}
|
2022-02-28 12:35:10 +00:00
|
|
|
|
2022-05-18 15:20:08 +00:00
|
|
|
// assertVerb checks that token verb corresponds to op.
|
|
|
|
func assertVerb(tok sessionSDK.Object, op eaclSDK.Operation) bool {
|
|
|
|
//nolint:exhaustive
|
|
|
|
switch op {
|
|
|
|
case eaclSDK.OperationPut:
|
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectPut, sessionSDK.VerbObjectDelete)
|
2022-02-28 12:35:10 +00:00
|
|
|
case eaclSDK.OperationDelete:
|
2022-05-18 15:20:08 +00:00
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectDelete)
|
|
|
|
case eaclSDK.OperationGet:
|
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectGet)
|
|
|
|
case eaclSDK.OperationHead:
|
|
|
|
return tok.AssertVerb(
|
|
|
|
sessionSDK.VerbObjectHead,
|
|
|
|
sessionSDK.VerbObjectGet,
|
|
|
|
sessionSDK.VerbObjectDelete,
|
|
|
|
sessionSDK.VerbObjectRange,
|
|
|
|
sessionSDK.VerbObjectRangeHash)
|
|
|
|
case eaclSDK.OperationSearch:
|
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectSearch, sessionSDK.VerbObjectDelete)
|
|
|
|
case eaclSDK.OperationRange:
|
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectRange, sessionSDK.VerbObjectRangeHash)
|
|
|
|
case eaclSDK.OperationRangeHash:
|
|
|
|
return tok.AssertVerb(sessionSDK.VerbObjectRangeHash)
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
2022-02-28 12:35:10 +00:00
|
|
|
}
|