2023-11-30 08:25:05 +00:00
|
|
|
package middleware
|
|
|
|
|
|
|
|
import (
|
2024-02-27 09:45:37 +00:00
|
|
|
"context"
|
2023-11-30 08:25:05 +00:00
|
|
|
"crypto/elliptic"
|
2024-04-10 06:41:07 +00:00
|
|
|
"encoding/xml"
|
2023-11-30 08:25:05 +00:00
|
|
|
"fmt"
|
2024-04-10 06:41:07 +00:00
|
|
|
"io"
|
2023-11-30 08:25:05 +00:00
|
|
|
"net/http"
|
2024-04-10 06:41:07 +00:00
|
|
|
"net/url"
|
2023-11-30 08:25:05 +00:00
|
|
|
"strings"
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2023-11-30 08:25:05 +00:00
|
|
|
apiErr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2024-04-10 06:41:07 +00:00
|
|
|
frostfsErrors "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/frostfs/errors"
|
2023-11-30 08:25:05 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/chain"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/engine"
|
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/pkg/resource/testutil"
|
2023-12-13 14:44:18 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/common"
|
2023-11-30 08:25:05 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/policy-engine/schema/s3"
|
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2023-12-13 14:44:18 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/util"
|
2023-11-30 08:25:05 +00:00
|
|
|
"go.uber.org/zap"
|
|
|
|
)
|
|
|
|
|
2024-04-04 15:06:16 +00:00
|
|
|
const (
|
|
|
|
QueryVersionID = "versionId"
|
|
|
|
QueryPrefix = "prefix"
|
|
|
|
QueryDelimiter = "delimiter"
|
|
|
|
QueryMaxKeys = "max-keys"
|
2024-04-10 06:41:07 +00:00
|
|
|
amzTagging = "x-amz-tagging"
|
2024-04-04 15:06:16 +00:00
|
|
|
)
|
|
|
|
|
2024-06-26 13:26:10 +00:00
|
|
|
// In these operations we don't check resource tags because
|
|
|
|
// * they haven't been created yet
|
|
|
|
// * resource tags shouldn't be checked by AWS spec.
|
2024-04-10 06:41:07 +00:00
|
|
|
var withoutResourceOps = []string{
|
|
|
|
CreateBucketOperation,
|
|
|
|
CreateMultipartUploadOperation,
|
|
|
|
AbortMultipartUploadOperation,
|
|
|
|
CompleteMultipartUploadOperation,
|
|
|
|
UploadPartOperation,
|
|
|
|
UploadPartCopyOperation,
|
|
|
|
ListPartsOperation,
|
|
|
|
PutObjectOperation,
|
|
|
|
CopyObjectOperation,
|
2024-06-26 13:26:10 +00:00
|
|
|
DeleteObjectOperation,
|
|
|
|
DeleteMultipleObjectsOperation,
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2023-11-30 08:25:05 +00:00
|
|
|
type PolicySettings interface {
|
2023-12-05 12:49:13 +00:00
|
|
|
PolicyDenyByDefault() bool
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2023-12-13 14:44:18 +00:00
|
|
|
type FrostFSIDInformer interface {
|
2024-04-12 08:59:05 +00:00
|
|
|
GetUserGroupIDsAndClaims(userHash util.Uint160) ([]string, map[string]string, error)
|
2023-12-13 14:44:18 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
type XMLDecoder interface {
|
|
|
|
NewXMLDecoder(io.Reader) *xml.Decoder
|
|
|
|
}
|
|
|
|
|
|
|
|
type ResourceTagging interface {
|
|
|
|
GetBucketTagging(ctx context.Context, bktInfo *data.BucketInfo) (map[string]string, error)
|
|
|
|
GetObjectTagging(ctx context.Context, p *data.GetObjectTaggingParams) (string, map[string]string, error)
|
|
|
|
}
|
|
|
|
|
2024-03-01 14:07:08 +00:00
|
|
|
// BucketResolveFunc is a func to resolve bucket info by name.
|
|
|
|
type BucketResolveFunc func(ctx context.Context, bucket string) (*data.BucketInfo, error)
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
type PolicyConfig struct {
|
|
|
|
Storage engine.ChainRouter
|
|
|
|
FrostfsID FrostFSIDInformer
|
|
|
|
Settings PolicySettings
|
|
|
|
Domains []string
|
|
|
|
Log *zap.Logger
|
|
|
|
BucketResolver BucketResolveFunc
|
2024-04-10 06:41:07 +00:00
|
|
|
Decoder XMLDecoder
|
|
|
|
Tagging ResourceTagging
|
2024-02-27 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func PolicyCheck(cfg PolicyConfig) Func {
|
2023-11-30 08:25:05 +00:00
|
|
|
return func(h http.Handler) http.Handler {
|
|
|
|
return http.HandlerFunc(func(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := r.Context()
|
2024-02-27 09:45:37 +00:00
|
|
|
if err := policyCheck(r, cfg); err != nil {
|
|
|
|
reqLogOrDefault(ctx, cfg.Log).Error(logs.PolicyValidationFailed, zap.Error(err))
|
2024-04-10 06:41:07 +00:00
|
|
|
err = frostfsErrors.UnwrapErr(err)
|
2024-03-04 12:53:00 +00:00
|
|
|
if _, wrErr := WriteErrorResponse(w, GetReqInfo(ctx), err); wrErr != nil {
|
|
|
|
reqLogOrDefault(ctx, cfg.Log).Error(logs.FailedToWriteResponse, zap.Error(wrErr))
|
|
|
|
}
|
2023-11-30 08:25:05 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
h.ServeHTTP(w, r)
|
|
|
|
})
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
func policyCheck(r *http.Request, cfg PolicyConfig) error {
|
|
|
|
reqType, bktName, objName := getBucketObject(r, cfg.Domains)
|
2024-04-11 11:31:53 +00:00
|
|
|
req, userKey, userGroups, err := getPolicyRequest(r, cfg, reqType, bktName, objName)
|
2023-11-30 08:25:05 +00:00
|
|
|
if err != nil {
|
2024-02-27 09:45:37 +00:00
|
|
|
return err
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-10 12:53:36 +00:00
|
|
|
var bktInfo *data.BucketInfo
|
|
|
|
if reqType != noneType && !strings.HasSuffix(req.Operation(), CreateBucketOperation) {
|
|
|
|
bktInfo, err = cfg.BucketResolver(r.Context(), bktName)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2023-11-30 08:25:05 +00:00
|
|
|
reqInfo := GetReqInfo(r.Context())
|
2024-04-10 13:26:39 +00:00
|
|
|
target := engine.NewRequestTargetWithNamespace(reqInfo.Namespace)
|
2024-04-10 12:53:36 +00:00
|
|
|
if bktInfo != nil {
|
2024-04-10 13:26:39 +00:00
|
|
|
cnrTarget := engine.ContainerTarget(bktInfo.CID.EncodeToString())
|
|
|
|
target.Container = &cnrTarget
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:31:53 +00:00
|
|
|
if userKey != nil {
|
|
|
|
entityName := fmt.Sprintf("%s:%s", reqInfo.Namespace, userKey.Address())
|
|
|
|
uTarget := engine.UserTarget(entityName)
|
|
|
|
target.User = &uTarget
|
|
|
|
}
|
|
|
|
|
|
|
|
gts := make([]engine.Target, len(userGroups))
|
|
|
|
for i, group := range userGroups {
|
|
|
|
entityName := fmt.Sprintf("%s:%s", reqInfo.Namespace, group)
|
|
|
|
gts[i] = engine.GroupTarget(entityName)
|
|
|
|
}
|
|
|
|
target.Groups = gts
|
|
|
|
|
2024-04-10 13:26:39 +00:00
|
|
|
st, found, err := cfg.Storage.IsAllowed(chain.S3, target, req)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
st = chain.NoRuleFound
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
switch {
|
|
|
|
case st == chain.Allow:
|
|
|
|
return nil
|
|
|
|
case st != chain.NoRuleFound:
|
|
|
|
return apiErr.GetAPIErrorWithError(apiErr.ErrAccessDenied, fmt.Errorf("policy check: %s", st.String()))
|
|
|
|
}
|
|
|
|
|
2024-05-28 12:50:34 +00:00
|
|
|
if cfg.Settings.PolicyDenyByDefault() {
|
2024-02-27 09:45:37 +00:00
|
|
|
return apiErr.GetAPIErrorWithError(apiErr.ErrAccessDenied, fmt.Errorf("policy check: %s", st.String()))
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-11 11:31:53 +00:00
|
|
|
func getPolicyRequest(r *http.Request, cfg PolicyConfig, reqType ReqType, bktName string, objName string) (*testutil.Request, *keys.PublicKey, []string, error) {
|
2023-12-13 14:44:18 +00:00
|
|
|
var (
|
|
|
|
owner string
|
|
|
|
groups []string
|
2024-04-12 08:59:05 +00:00
|
|
|
tags map[string]string
|
2024-04-11 11:31:53 +00:00
|
|
|
pk *keys.PublicKey
|
2023-12-13 14:44:18 +00:00
|
|
|
)
|
|
|
|
|
2023-11-30 08:25:05 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
bd, err := GetBoxData(ctx)
|
|
|
|
if err == nil && bd.Gate.BearerToken != nil {
|
2024-04-11 11:31:53 +00:00
|
|
|
pk, err = keys.NewPublicKeyFromBytes(bd.Gate.BearerToken.SigningKeyBytes(), elliptic.P256())
|
2023-11-30 08:25:05 +00:00
|
|
|
if err != nil {
|
2024-04-11 11:31:53 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("parse pubclic key from btoken: %w", err)
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
owner = pk.Address()
|
2023-12-13 14:44:18 +00:00
|
|
|
|
2024-04-12 08:59:05 +00:00
|
|
|
groups, tags, err = cfg.FrostfsID.GetUserGroupIDsAndClaims(pk.GetScriptHash())
|
2023-12-13 14:44:18 +00:00
|
|
|
if err != nil {
|
2024-04-11 11:31:53 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("get group ids: %w", err)
|
2023-12-13 14:44:18 +00:00
|
|
|
}
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
op := determineOperation(r, reqType)
|
|
|
|
var res string
|
|
|
|
switch reqType {
|
|
|
|
case objectType:
|
|
|
|
res = fmt.Sprintf(s3.ResourceFormatS3BucketObject, bktName, objName)
|
|
|
|
default:
|
|
|
|
res = fmt.Sprintf(s3.ResourceFormatS3Bucket, bktName)
|
|
|
|
}
|
2023-11-30 08:25:05 +00:00
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProps, resourceProps, err := determineProperties(r, cfg.Decoder, cfg.BucketResolver, cfg.Tagging, reqType, op, bktName, objName, owner, groups, tags)
|
2024-04-10 06:41:07 +00:00
|
|
|
if err != nil {
|
2024-04-11 11:31:53 +00:00
|
|
|
return nil, nil, nil, fmt.Errorf("determine properties: %w", err)
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
2024-04-04 15:06:16 +00:00
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
reqLogOrDefault(r.Context(), cfg.Log).Debug(logs.PolicyRequest, zap.String("action", op),
|
2024-06-11 08:46:49 +00:00
|
|
|
zap.String("resource", res), zap.Any("request properties", requestProps),
|
|
|
|
zap.Any("resource properties", resourceProps))
|
2024-04-04 15:06:16 +00:00
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
return testutil.NewRequest(op, testutil.NewResource(res, resourceProps), requestProps), pk, groups, nil
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type ReqType int
|
|
|
|
|
|
|
|
const (
|
|
|
|
noneType ReqType = iota
|
|
|
|
bucketType
|
|
|
|
objectType
|
|
|
|
)
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
func getBucketObject(r *http.Request, domains []string) (reqType ReqType, bktName string, objName string) {
|
2023-11-30 08:25:05 +00:00
|
|
|
for _, domain := range domains {
|
2023-12-19 09:47:28 +00:00
|
|
|
ind := strings.Index(r.Host, "."+domain)
|
|
|
|
if ind == -1 {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
bkt := r.Host[:ind]
|
|
|
|
if obj := strings.TrimPrefix(r.URL.Path, "/"); obj != "" {
|
2024-02-27 09:45:37 +00:00
|
|
|
return objectType, bkt, obj
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
2023-12-19 09:47:28 +00:00
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
return bucketType, bkt, ""
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
bktObj := strings.TrimPrefix(r.URL.Path, "/")
|
|
|
|
if bktObj == "" {
|
|
|
|
return noneType, "", ""
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
2024-05-06 08:42:08 +00:00
|
|
|
if ind := strings.IndexByte(bktObj, '/'); ind != -1 && bktObj[ind+1:] != "" {
|
2024-02-27 09:45:37 +00:00
|
|
|
return objectType, bktObj[:ind], bktObj[ind+1:]
|
|
|
|
}
|
|
|
|
|
2024-05-06 08:42:08 +00:00
|
|
|
return bucketType, strings.TrimSuffix(bktObj, "/"), ""
|
2024-02-27 09:45:37 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func determineOperation(r *http.Request, reqType ReqType) (operation string) {
|
2023-11-30 08:25:05 +00:00
|
|
|
switch reqType {
|
|
|
|
case objectType:
|
|
|
|
operation = determineObjectOperation(r)
|
|
|
|
case bucketType:
|
|
|
|
operation = determineBucketOperation(r)
|
|
|
|
default:
|
|
|
|
operation = determineGeneralOperation(r)
|
|
|
|
}
|
|
|
|
|
2024-02-27 09:45:37 +00:00
|
|
|
return "s3:" + operation
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func determineBucketOperation(r *http.Request) string {
|
|
|
|
query := r.URL.Query()
|
|
|
|
switch r.Method {
|
|
|
|
case http.MethodOptions:
|
2024-05-31 13:55:37 +00:00
|
|
|
return OptionsBucketOperation
|
2023-11-30 08:25:05 +00:00
|
|
|
case http.MethodHead:
|
|
|
|
return HeadBucketOperation
|
|
|
|
case http.MethodGet:
|
|
|
|
switch {
|
|
|
|
case query.Has(UploadsQuery):
|
|
|
|
return ListMultipartUploadsOperation
|
|
|
|
case query.Has(LocationQuery):
|
|
|
|
return GetBucketLocationOperation
|
|
|
|
case query.Has(PolicyQuery):
|
|
|
|
return GetBucketPolicyOperation
|
|
|
|
case query.Has(LifecycleQuery):
|
|
|
|
return GetBucketLifecycleOperation
|
|
|
|
case query.Has(EncryptionQuery):
|
|
|
|
return GetBucketEncryptionOperation
|
|
|
|
case query.Has(CorsQuery):
|
|
|
|
return GetBucketCorsOperation
|
|
|
|
case query.Has(ACLQuery):
|
|
|
|
return GetBucketACLOperation
|
|
|
|
case query.Has(WebsiteQuery):
|
|
|
|
return GetBucketWebsiteOperation
|
|
|
|
case query.Has(AccelerateQuery):
|
|
|
|
return GetBucketAccelerateOperation
|
|
|
|
case query.Has(RequestPaymentQuery):
|
|
|
|
return GetBucketRequestPaymentOperation
|
|
|
|
case query.Has(LoggingQuery):
|
|
|
|
return GetBucketLoggingOperation
|
|
|
|
case query.Has(ReplicationQuery):
|
|
|
|
return GetBucketReplicationOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return GetBucketTaggingOperation
|
|
|
|
case query.Has(ObjectLockQuery):
|
|
|
|
return GetBucketObjectLockConfigOperation
|
|
|
|
case query.Has(VersioningQuery):
|
|
|
|
return GetBucketVersioningOperation
|
|
|
|
case query.Has(NotificationQuery):
|
|
|
|
return GetBucketNotificationOperation
|
|
|
|
case query.Has(EventsQuery):
|
|
|
|
return ListenBucketNotificationOperation
|
|
|
|
case query.Has(VersionsQuery):
|
|
|
|
return ListBucketObjectVersionsOperation
|
|
|
|
case query.Get(ListTypeQuery) == "2" && query.Get(MetadataQuery) == "true":
|
|
|
|
return ListObjectsV2MOperation
|
|
|
|
case query.Get(ListTypeQuery) == "2":
|
|
|
|
return ListObjectsV2Operation
|
|
|
|
default:
|
|
|
|
return ListObjectsV1Operation
|
|
|
|
}
|
|
|
|
case http.MethodPut:
|
|
|
|
switch {
|
|
|
|
case query.Has(CorsQuery):
|
|
|
|
return PutBucketCorsOperation
|
|
|
|
case query.Has(ACLQuery):
|
|
|
|
return PutBucketACLOperation
|
|
|
|
case query.Has(LifecycleQuery):
|
|
|
|
return PutBucketLifecycleOperation
|
|
|
|
case query.Has(EncryptionQuery):
|
|
|
|
return PutBucketEncryptionOperation
|
|
|
|
case query.Has(PolicyQuery):
|
|
|
|
return PutBucketPolicyOperation
|
|
|
|
case query.Has(ObjectLockQuery):
|
|
|
|
return PutBucketObjectLockConfigOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return PutBucketTaggingOperation
|
|
|
|
case query.Has(VersioningQuery):
|
|
|
|
return PutBucketVersioningOperation
|
|
|
|
case query.Has(NotificationQuery):
|
|
|
|
return PutBucketNotificationOperation
|
|
|
|
default:
|
|
|
|
return CreateBucketOperation
|
|
|
|
}
|
|
|
|
case http.MethodPost:
|
|
|
|
switch {
|
|
|
|
case query.Has(DeleteQuery):
|
|
|
|
return DeleteMultipleObjectsOperation
|
|
|
|
default:
|
|
|
|
return PostObjectOperation
|
|
|
|
}
|
|
|
|
case http.MethodDelete:
|
|
|
|
switch {
|
|
|
|
case query.Has(CorsQuery):
|
|
|
|
return DeleteBucketCorsOperation
|
|
|
|
case query.Has(WebsiteQuery):
|
|
|
|
return DeleteBucketWebsiteOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return DeleteBucketTaggingOperation
|
|
|
|
case query.Has(PolicyQuery):
|
|
|
|
return DeleteBucketPolicyOperation
|
|
|
|
case query.Has(LifecycleQuery):
|
|
|
|
return DeleteBucketLifecycleOperation
|
|
|
|
case query.Has(EncryptionQuery):
|
|
|
|
return DeleteBucketEncryptionOperation
|
|
|
|
default:
|
|
|
|
return DeleteBucketOperation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 14:32:58 +00:00
|
|
|
return "UnmatchedBucketOperation"
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func determineObjectOperation(r *http.Request) string {
|
|
|
|
query := r.URL.Query()
|
|
|
|
switch r.Method {
|
2024-05-31 13:55:37 +00:00
|
|
|
case http.MethodOptions:
|
|
|
|
return OptionsObjectOperation
|
2023-11-30 08:25:05 +00:00
|
|
|
case http.MethodHead:
|
|
|
|
return HeadObjectOperation
|
|
|
|
case http.MethodGet:
|
|
|
|
switch {
|
|
|
|
case query.Has(UploadIDQuery):
|
|
|
|
return ListPartsOperation
|
|
|
|
case query.Has(ACLQuery):
|
|
|
|
return GetObjectACLOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return GetObjectTaggingOperation
|
|
|
|
case query.Has(RetentionQuery):
|
|
|
|
return GetObjectRetentionOperation
|
|
|
|
case query.Has(LegalQuery):
|
|
|
|
return GetObjectLegalHoldOperation
|
|
|
|
case query.Has(AttributesQuery):
|
|
|
|
return GetObjectAttributesOperation
|
|
|
|
default:
|
|
|
|
return GetObjectOperation
|
|
|
|
}
|
|
|
|
case http.MethodPut:
|
|
|
|
switch {
|
|
|
|
case query.Has(PartNumberQuery) && query.Has(UploadIDQuery) && r.Header.Get("X-Amz-Copy-Source") != "":
|
|
|
|
return UploadPartCopyOperation
|
|
|
|
case query.Has(PartNumberQuery) && query.Has(UploadIDQuery):
|
|
|
|
return UploadPartOperation
|
|
|
|
case query.Has(ACLQuery):
|
|
|
|
return PutObjectACLOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return PutObjectTaggingOperation
|
|
|
|
case r.Header.Get("X-Amz-Copy-Source") != "":
|
|
|
|
return CopyObjectOperation
|
|
|
|
case query.Has(RetentionQuery):
|
|
|
|
return PutObjectRetentionOperation
|
|
|
|
case query.Has(LegalHoldQuery):
|
|
|
|
return PutObjectLegalHoldOperation
|
|
|
|
default:
|
|
|
|
return PutObjectOperation
|
|
|
|
}
|
|
|
|
case http.MethodPost:
|
|
|
|
switch {
|
|
|
|
case query.Has(UploadIDQuery):
|
|
|
|
return CompleteMultipartUploadOperation
|
|
|
|
case query.Has(UploadsQuery):
|
|
|
|
return CreateMultipartUploadOperation
|
|
|
|
default:
|
|
|
|
return SelectObjectContentOperation
|
|
|
|
}
|
|
|
|
case http.MethodDelete:
|
|
|
|
switch {
|
|
|
|
case query.Has(UploadIDQuery):
|
|
|
|
return AbortMultipartUploadOperation
|
|
|
|
case query.Has(TaggingQuery):
|
|
|
|
return DeleteObjectTaggingOperation
|
|
|
|
default:
|
|
|
|
return DeleteObjectOperation
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-02-21 14:32:58 +00:00
|
|
|
return "UnmatchedObjectOperation"
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func determineGeneralOperation(r *http.Request) string {
|
|
|
|
if r.Method == http.MethodGet {
|
|
|
|
return ListBucketsOperation
|
|
|
|
}
|
2024-02-21 14:32:58 +00:00
|
|
|
return "UnmatchedOperation"
|
2023-11-30 08:25:05 +00:00
|
|
|
}
|
2024-04-04 15:06:16 +00:00
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
func determineProperties(r *http.Request, decoder XMLDecoder, resolver BucketResolveFunc, tagging ResourceTagging, reqType ReqType,
|
2024-06-11 08:46:49 +00:00
|
|
|
op, bktName, objName, owner string, groups []string, userClaims map[string]string) (requestProperties map[string]string, resourceProperties map[string]string, err error) {
|
|
|
|
requestProperties = map[string]string{
|
2024-04-04 15:06:16 +00:00
|
|
|
s3.PropertyKeyOwner: owner,
|
|
|
|
common.PropertyKeyFrostFSIDGroupID: chain.FormCondSliceContainsValue(groups),
|
2024-04-17 15:21:21 +00:00
|
|
|
common.PropertyKeyFrostFSSourceIP: GetReqInfo(r.Context()).RemoteHost,
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|
2024-04-10 06:41:07 +00:00
|
|
|
queries := GetReqInfo(r.Context()).URL.Query()
|
2024-04-04 15:06:16 +00:00
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
for k, v := range userClaims {
|
|
|
|
requestProperties[fmt.Sprintf(common.PropertyKeyFormatFrostFSIDUserClaim, k)] = v
|
2024-04-12 08:59:05 +00:00
|
|
|
}
|
|
|
|
|
2024-04-04 15:06:16 +00:00
|
|
|
if reqType == objectType {
|
|
|
|
if versionID := queries.Get(QueryVersionID); len(versionID) > 0 {
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[s3.PropertyKeyVersionID] = versionID
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqType == bucketType && (strings.HasSuffix(op, ListObjectsV1Operation) || strings.HasSuffix(op, ListObjectsV2Operation) ||
|
|
|
|
strings.HasSuffix(op, ListBucketObjectVersionsOperation) || strings.HasSuffix(op, ListMultipartUploadsOperation)) {
|
|
|
|
if prefix := queries.Get(QueryPrefix); len(prefix) > 0 {
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[s3.PropertyKeyPrefix] = prefix
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|
|
|
|
if delimiter := queries.Get(QueryDelimiter); len(delimiter) > 0 {
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[s3.PropertyKeyDelimiter] = delimiter
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|
|
|
|
if maxKeys := queries.Get(QueryMaxKeys); len(maxKeys) > 0 {
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[s3.PropertyKeyMaxKeys] = maxKeys
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[s3.PropertyKeyAccessBoxAttrMFA] = "false"
|
2024-04-16 08:20:35 +00:00
|
|
|
attrs, err := GetAccessBoxAttrs(r.Context())
|
|
|
|
if err == nil {
|
|
|
|
for _, attr := range attrs {
|
2024-06-11 08:46:49 +00:00
|
|
|
requestProperties[fmt.Sprintf(s3.PropertyKeyFormatAccessBoxAttr, attr.Key())] = attr.Value()
|
2024-04-16 08:20:35 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
reqTags, err := determineRequestTags(r, decoder, op)
|
2024-04-10 06:41:07 +00:00
|
|
|
if err != nil {
|
2024-06-11 08:46:49 +00:00
|
|
|
return nil, nil, fmt.Errorf("determine request tags: %w", err)
|
|
|
|
}
|
|
|
|
for k, v := range reqTags {
|
|
|
|
requestProperties[k] = v
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
resourceProperties, err = determineResourceTags(r.Context(), reqType, op, bktName, objName, queries.Get(QueryVersionID), resolver, tagging)
|
2024-04-10 06:41:07 +00:00
|
|
|
if err != nil {
|
2024-06-11 08:46:49 +00:00
|
|
|
return nil, nil, fmt.Errorf("determine resource tags: %w", err)
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
2024-06-11 08:46:49 +00:00
|
|
|
return requestProperties, resourceProperties, nil
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func determineRequestTags(r *http.Request, decoder XMLDecoder, op string) (map[string]string, error) {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
|
|
|
|
if strings.HasSuffix(op, PutObjectTaggingOperation) || strings.HasSuffix(op, PutBucketTaggingOperation) {
|
|
|
|
tagging := new(data.Tagging)
|
|
|
|
if err := decoder.NewXMLDecoder(r.Body).Decode(tagging); err != nil {
|
2024-04-24 12:20:11 +00:00
|
|
|
return nil, fmt.Errorf("%w: %s", apiErr.GetAPIError(apiErr.ErrMalformedXML), err.Error())
|
2024-04-10 06:41:07 +00:00
|
|
|
}
|
|
|
|
GetReqInfo(r.Context()).Tagging = tagging
|
|
|
|
|
|
|
|
for _, tag := range tagging.TagSet {
|
|
|
|
tags[fmt.Sprintf(s3.PropertyKeyFormatRequestTag, tag.Key)] = tag.Value
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if tagging := r.Header.Get(amzTagging); len(tagging) > 0 {
|
|
|
|
queries, err := url.ParseQuery(tagging)
|
|
|
|
if err != nil {
|
|
|
|
return nil, apiErr.GetAPIError(apiErr.ErrInvalidArgument)
|
|
|
|
}
|
|
|
|
for key := range queries {
|
|
|
|
tags[fmt.Sprintf(s3.PropertyKeyFormatRequestTag, key)] = queries.Get(key)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func determineResourceTags(ctx context.Context, reqType ReqType, op, bktName, objName, versionID string, resolver BucketResolveFunc,
|
|
|
|
tagging ResourceTagging) (map[string]string, error) {
|
|
|
|
tags := make(map[string]string)
|
|
|
|
|
|
|
|
if reqType != bucketType && reqType != objectType {
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, withoutResOp := range withoutResourceOps {
|
|
|
|
if strings.HasSuffix(op, withoutResOp) {
|
|
|
|
return tags, nil
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
bktInfo, err := resolver(ctx, bktName)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get bucket info: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqType == bucketType {
|
|
|
|
tags, err = tagging.GetBucketTagging(ctx, bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get bucket tagging: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if reqType == objectType {
|
|
|
|
tagPrm := &data.GetObjectTaggingParams{
|
|
|
|
ObjectVersion: &data.ObjectVersion{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: objName,
|
|
|
|
VersionID: versionID,
|
|
|
|
},
|
|
|
|
}
|
|
|
|
_, tags, err = tagging.GetObjectTagging(ctx, tagPrm)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("get object tagging: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res := make(map[string]string, len(tags))
|
|
|
|
for k, v := range tags {
|
|
|
|
res[fmt.Sprintf(s3.PropertyKeyFormatResourceTag, k)] = v
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
2024-04-04 15:06:16 +00:00
|
|
|
}
|