2021-07-21 11:59:46 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2021-10-19 15:08:07 +00:00
|
|
|
"context"
|
2021-07-21 11:59:46 +00:00
|
|
|
"crypto/ecdsa"
|
2022-04-06 08:27:47 +00:00
|
|
|
"crypto/elliptic"
|
2021-07-21 11:59:46 +00:00
|
|
|
"encoding/hex"
|
|
|
|
"encoding/json"
|
|
|
|
"encoding/xml"
|
2022-06-30 11:51:56 +00:00
|
|
|
stderrors "errors"
|
2021-07-21 11:59:46 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-07-15 14:35:11 +00:00
|
|
|
"sort"
|
|
|
|
"strconv"
|
2021-07-21 11:59:46 +00:00
|
|
|
"strings"
|
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
v2acl "git.frostfs.info/TrueCloudLab/frostfs-api-go/v2/acl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/layer"
|
2023-07-05 14:05:45 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/middleware"
|
2023-08-23 11:07:52 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/internal/logs"
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/eacl"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object"
|
|
|
|
oid "git.frostfs.info/TrueCloudLab/frostfs-sdk-go/object/id"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-sdk-go/session"
|
2021-07-21 11:59:46 +00:00
|
|
|
"github.com/nspcc-dev/neo-go/pkg/crypto/keys"
|
2022-06-09 20:05:50 +00:00
|
|
|
"go.uber.org/zap"
|
2021-07-21 11:59:46 +00:00
|
|
|
)
|
|
|
|
|
|
|
|
var (
|
|
|
|
writeOps = []eacl.Operation{eacl.OperationPut, eacl.OperationDelete}
|
|
|
|
readOps = []eacl.Operation{eacl.OperationGet, eacl.OperationHead,
|
|
|
|
eacl.OperationSearch, eacl.OperationRange, eacl.OperationRangeHash}
|
|
|
|
fullOps = []eacl.Operation{eacl.OperationGet, eacl.OperationHead, eacl.OperationPut,
|
|
|
|
eacl.OperationDelete, eacl.OperationSearch, eacl.OperationRange, eacl.OperationRangeHash}
|
|
|
|
)
|
|
|
|
|
|
|
|
var actionToOpMap = map[string][]eacl.Operation{
|
|
|
|
s3DeleteObject: {eacl.OperationDelete},
|
|
|
|
s3GetObject: readOps,
|
|
|
|
s3PutObject: {eacl.OperationPut},
|
|
|
|
s3ListBucket: readOps,
|
|
|
|
}
|
|
|
|
|
|
|
|
const (
|
|
|
|
arnAwsPrefix = "arn:aws:s3:::"
|
|
|
|
allUsersWildcard = "*"
|
|
|
|
allUsersGroup = "http://acs.amazonaws.com/groups/global/AllUsers"
|
|
|
|
|
|
|
|
s3DeleteObject = "s3:DeleteObject"
|
|
|
|
s3GetObject = "s3:GetObject"
|
|
|
|
s3PutObject = "s3:PutObject"
|
|
|
|
s3ListBucket = "s3:ListBucket"
|
|
|
|
s3ListBucketVersions = "s3:ListBucketVersions"
|
|
|
|
s3ListBucketMultipartUploads = "s3:ListBucketMultipartUploads"
|
|
|
|
s3GetObjectVersion = "s3:GetObjectVersion"
|
|
|
|
)
|
|
|
|
|
|
|
|
// AWSACL is aws permission constants.
|
|
|
|
type AWSACL string
|
|
|
|
|
|
|
|
const (
|
|
|
|
aclFullControl AWSACL = "FULL_CONTROL"
|
|
|
|
aclWrite AWSACL = "WRITE"
|
|
|
|
aclRead AWSACL = "READ"
|
|
|
|
)
|
|
|
|
|
|
|
|
// GranteeType is aws grantee permission type constants.
|
|
|
|
type GranteeType string
|
|
|
|
|
|
|
|
const (
|
|
|
|
acpCanonicalUser GranteeType = "CanonicalUser"
|
|
|
|
acpAmazonCustomerByEmail GranteeType = "AmazonCustomerByEmail"
|
|
|
|
acpGroup GranteeType = "Group"
|
|
|
|
)
|
|
|
|
|
|
|
|
type bucketPolicy struct {
|
|
|
|
Version string `json:"Version"`
|
|
|
|
ID string `json:"Id"`
|
|
|
|
Statement []statement `json:"Statement"`
|
2021-08-30 07:55:42 +00:00
|
|
|
Bucket string `json:"-"`
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
type statement struct {
|
|
|
|
Sid string `json:"Sid"`
|
|
|
|
Effect string `json:"Effect"`
|
|
|
|
Principal principal `json:"Principal"`
|
|
|
|
Action []string `json:"Action"`
|
|
|
|
Resource []string `json:"Resource"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type principal struct {
|
|
|
|
AWS string `json:"AWS,omitempty"`
|
|
|
|
CanonicalUser string `json:"CanonicalUser,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
type orderedAstResource struct {
|
|
|
|
Index int
|
|
|
|
Resource *astResource
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
type ast struct {
|
|
|
|
Resources []*astResource
|
|
|
|
}
|
|
|
|
|
|
|
|
type astResource struct {
|
2021-08-30 07:55:42 +00:00
|
|
|
resourceInfo
|
2021-07-21 11:59:46 +00:00
|
|
|
Operations []*astOperation
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
type resourceInfo struct {
|
|
|
|
Bucket string
|
|
|
|
Object string
|
|
|
|
Version string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resourceInfo) Name() string {
|
|
|
|
if len(r.Object) == 0 {
|
|
|
|
return r.Bucket
|
|
|
|
}
|
|
|
|
if len(r.Version) == 0 {
|
|
|
|
return r.Bucket + "/" + r.Object
|
|
|
|
}
|
|
|
|
return r.Bucket + "/" + r.Object + ":" + r.Version
|
|
|
|
}
|
|
|
|
|
|
|
|
func (r *resourceInfo) IsBucket() bool {
|
|
|
|
return len(r.Object) == 0
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
type astOperation struct {
|
2022-07-06 13:28:28 +00:00
|
|
|
Users []string
|
|
|
|
Op eacl.Operation
|
|
|
|
Action eacl.Action
|
|
|
|
}
|
|
|
|
|
|
|
|
func (a astOperation) IsGroupGrantee() bool {
|
|
|
|
return len(a.Users) == 0
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
const (
|
|
|
|
serviceRecordResourceKey = "Resource"
|
|
|
|
serviceRecordGroupLengthKey = "GroupLength"
|
|
|
|
)
|
|
|
|
|
|
|
|
type ServiceRecord struct {
|
|
|
|
Resource string
|
|
|
|
GroupRecordsLength int
|
|
|
|
}
|
|
|
|
|
|
|
|
func (s ServiceRecord) ToEACLRecord() *eacl.Record {
|
|
|
|
serviceRecord := eacl.NewRecord()
|
2022-07-20 13:38:22 +00:00
|
|
|
serviceRecord.SetAction(eacl.ActionAllow)
|
|
|
|
serviceRecord.SetOperation(eacl.OperationGet)
|
2022-07-15 14:35:11 +00:00
|
|
|
serviceRecord.AddFilter(eacl.HeaderFromService, eacl.MatchUnknown, serviceRecordResourceKey, s.Resource)
|
|
|
|
serviceRecord.AddFilter(eacl.HeaderFromService, eacl.MatchUnknown, serviceRecordGroupLengthKey, strconv.Itoa(s.GroupRecordsLength))
|
2022-07-20 13:38:22 +00:00
|
|
|
eacl.AddFormedTarget(serviceRecord, eacl.RoleSystem)
|
2022-07-15 14:35:11 +00:00
|
|
|
return serviceRecord
|
|
|
|
}
|
|
|
|
|
2023-02-17 12:52:52 +00:00
|
|
|
var (
|
|
|
|
errInvalidStatement = stderrors.New("invalid statement")
|
|
|
|
errInvalidPrincipal = stderrors.New("invalid principal")
|
|
|
|
)
|
|
|
|
|
|
|
|
func (s *statement) UnmarshalJSON(data []byte) error {
|
|
|
|
var statementMap map[string]interface{}
|
|
|
|
if err := json.Unmarshal(data, &statementMap); err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
sidField, ok := statementMap["Sid"]
|
|
|
|
if ok {
|
|
|
|
if s.Sid, ok = sidField.(string); !ok {
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
effectField, ok := statementMap["Effect"]
|
|
|
|
if ok {
|
|
|
|
if s.Effect, ok = effectField.(string); !ok {
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
principalField, ok := statementMap["Principal"]
|
|
|
|
if ok {
|
|
|
|
principalMap, ok := principalField.(map[string]interface{})
|
|
|
|
if !ok {
|
|
|
|
return errInvalidPrincipal
|
|
|
|
}
|
|
|
|
|
|
|
|
awsField, ok := principalMap["AWS"]
|
|
|
|
if ok {
|
|
|
|
if s.Principal.AWS, ok = awsField.(string); !ok {
|
|
|
|
return fmt.Errorf("%w: 'AWS' field must be string", errInvalidPrincipal)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
canonicalUserField, ok := principalMap["CanonicalUser"]
|
|
|
|
if ok {
|
|
|
|
if s.Principal.CanonicalUser, ok = canonicalUserField.(string); !ok {
|
|
|
|
return errInvalidPrincipal
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
actionField, ok := statementMap["Action"]
|
|
|
|
if ok {
|
|
|
|
switch actionField := actionField.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
s.Action = make([]string, len(actionField))
|
|
|
|
for i, action := range actionField {
|
|
|
|
if s.Action[i], ok = action.(string); !ok {
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
s.Action = []string{actionField}
|
|
|
|
default:
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceField, ok := statementMap["Resource"]
|
|
|
|
if ok {
|
|
|
|
switch resourceField := resourceField.(type) {
|
|
|
|
case []interface{}:
|
|
|
|
s.Resource = make([]string, len(resourceField))
|
|
|
|
for i, action := range resourceField {
|
|
|
|
if s.Resource[i], ok = action.(string); !ok {
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
case string:
|
|
|
|
s.Resource = []string{resourceField}
|
|
|
|
default:
|
|
|
|
return errInvalidStatement
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func (h *handler) GetBucketACLHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-03-18 13:04:09 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
bucketACL, err := h.obj.GetBucketACL(ctx, bktInfo)
|
2022-03-18 13:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not fetch bucket acl", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, h.encodeBucketACL(ctx, bktInfo.Name, bucketACL)); err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
2022-03-18 13:04:09 +00:00
|
|
|
return
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:27:47 +00:00
|
|
|
func (h *handler) bearerTokenIssuerKey(ctx context.Context) (*keys.PublicKey, error) {
|
2023-08-14 15:34:41 +00:00
|
|
|
box, err := middleware.GetBoxData(ctx)
|
2022-04-06 08:27:47 +00:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2022-04-25 09:57:58 +00:00
|
|
|
var btoken v2acl.BearerToken
|
|
|
|
box.Gate.BearerToken.WriteToV2(&btoken)
|
|
|
|
|
|
|
|
key, err := keys.NewPublicKeyFromBytes(btoken.GetSignature().GetKey(), elliptic.P256())
|
2022-04-06 08:27:47 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("public key from bytes: %w", err)
|
2021-10-19 15:08:07 +00:00
|
|
|
}
|
|
|
|
|
2022-04-06 08:27:47 +00:00
|
|
|
return key, nil
|
2021-10-19 15:08:07 +00:00
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func (h *handler) PutBucketACLHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-04-06 08:27:47 +00:00
|
|
|
key, err := h.bearerTokenIssuerKey(r.Context())
|
2021-10-19 15:08:07 +00:00
|
|
|
if err != nil {
|
2022-04-06 08:27:47 +00:00
|
|
|
h.logAndSendError(w, "couldn't get bearer token issuer key", reqInfo, err)
|
2021-10-19 15:08:07 +00:00
|
|
|
return
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2022-04-18 15:35:25 +00:00
|
|
|
token, err := getSessionTokenSetEACL(r.Context())
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get eacl token", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
list := &AccessControlPolicy{}
|
|
|
|
if r.ContentLength == 0 {
|
2022-04-06 08:27:47 +00:00
|
|
|
list, err = parseACLHeaders(r.Header, key)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-22 19:40:52 +00:00
|
|
|
} else if err = xml.NewDecoder(r.Body).Decode(list); err != nil {
|
2022-01-19 20:20:07 +00:00
|
|
|
h.logAndSendError(w, "could not parse bucket acl", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
resInfo := &resourceInfo{Bucket: reqInfo.BucketName}
|
|
|
|
astBucket, err := aclToAst(list, resInfo)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not translate acl to policy", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:05:50 +00:00
|
|
|
if _, err = h.updateBucketACL(r, astBucket, bktInfo, token); err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could not update bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-09-21 09:31:23 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 20:05:50 +00:00
|
|
|
func (h *handler) updateBucketACL(r *http.Request, astChild *ast, bktInfo *data.BucketInfo, sessionToken *session.Container) (bool, error) {
|
2022-03-18 13:04:09 +00:00
|
|
|
bucketACL, err := h.obj.GetBucketACL(r.Context(), bktInfo)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-06-09 20:05:50 +00:00
|
|
|
return false, fmt.Errorf("could not get bucket eacl: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
parentAst := tableToAst(bucketACL.EACL, bktInfo.Name)
|
2022-05-25 17:25:43 +00:00
|
|
|
strCID := bucketACL.Info.CID.EncodeToString()
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, resource := range parentAst.Resources {
|
2022-05-25 17:25:43 +00:00
|
|
|
if resource.Bucket == strCID {
|
2022-03-18 13:04:09 +00:00
|
|
|
resource.Bucket = bktInfo.Name
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
resAst, updated := mergeAst(parentAst, astChild)
|
|
|
|
if !updated {
|
2022-06-09 20:05:50 +00:00
|
|
|
return false, nil
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
table, err := astToTable(resAst)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-06-09 20:05:50 +00:00
|
|
|
return false, fmt.Errorf("could not translate ast to table: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.PutBucketACLParams{
|
2022-06-21 15:21:20 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
EACL: table,
|
|
|
|
SessionToken: sessionToken,
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if err = h.obj.PutBucketACL(r.Context(), p); err != nil {
|
2022-06-09 20:05:50 +00:00
|
|
|
return false, fmt.Errorf("could not put bucket acl: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-06-09 20:05:50 +00:00
|
|
|
return true, nil
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetObjectACLHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-03-18 13:04:09 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
bucketACL, err := h.obj.GetBucketACL(ctx, bktInfo)
|
2022-03-18 13:04:09 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not fetch bucket acl", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
prm := &layer.HeadObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
objInfo, err := h.obj.GetObjectInfo(ctx, prm)
|
2022-07-15 14:35:11 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, h.encodeObjectACL(ctx, bucketACL, reqInfo.BucketName, objInfo.VersionID())); err != nil {
|
2022-07-15 14:35:11 +00:00
|
|
|
h.logAndSendError(w, "failed to encode response", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) PutObjectACLHandler(w http.ResponseWriter, r *http.Request) {
|
2023-06-09 13:19:23 +00:00
|
|
|
ctx := r.Context()
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2021-10-19 15:08:07 +00:00
|
|
|
versionID := reqInfo.URL.Query().Get(api.QueryVersionID)
|
2023-06-09 13:19:23 +00:00
|
|
|
key, err := h.bearerTokenIssuerKey(ctx)
|
2021-10-19 15:08:07 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get gate key", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
token, err := getSessionTokenSetEACL(ctx)
|
2022-04-18 15:35:25 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "couldn't get eacl token", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:22:18 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
p := &layer.HeadObjectParams{
|
|
|
|
BktInfo: bktInfo,
|
|
|
|
Object: reqInfo.ObjectName,
|
|
|
|
VersionID: versionID,
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
objInfo, err := h.obj.GetObjectInfo(ctx, p)
|
2022-08-24 15:22:18 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get object info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
list := &AccessControlPolicy{}
|
|
|
|
if r.ContentLength == 0 {
|
2022-04-06 08:27:47 +00:00
|
|
|
list, err = parseACLHeaders(r.Header, key)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not parse bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-22 19:40:52 +00:00
|
|
|
} else if err = xml.NewDecoder(r.Body).Decode(list); err != nil {
|
2022-01-19 20:20:07 +00:00
|
|
|
h.logAndSendError(w, "could not parse bucket acl", reqInfo, errors.GetAPIError(errors.ErrMalformedXML))
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
resInfo := &resourceInfo{
|
|
|
|
Bucket: reqInfo.BucketName,
|
|
|
|
Object: reqInfo.ObjectName,
|
2022-08-24 15:22:18 +00:00
|
|
|
Version: objInfo.VersionID(),
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
astObject, err := aclToAst(list, resInfo)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2021-08-30 07:55:42 +00:00
|
|
|
h.logAndSendError(w, "could not translate acl to ast", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:05:50 +00:00
|
|
|
updated, err := h.updateBucketACL(r, astObject, bktInfo, token)
|
|
|
|
if err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could not update bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
2022-06-09 20:05:50 +00:00
|
|
|
if updated {
|
|
|
|
s := &SendNotificationParams{
|
2022-07-18 14:51:34 +00:00
|
|
|
Event: EventObjectACLPut,
|
2023-10-02 08:52:07 +00:00
|
|
|
NotificationInfo: data.NotificationInfoFromObject(objInfo, h.cfg.Features.MD5Enabled()),
|
2022-07-18 14:51:34 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ReqInfo: reqInfo,
|
2022-06-09 20:05:50 +00:00
|
|
|
}
|
2023-06-09 13:19:23 +00:00
|
|
|
if err = h.sendNotifications(ctx, s); err != nil {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Error(logs.CouldntSendNotification, zap.Error(err))
|
2022-06-09 20:05:50 +00:00
|
|
|
}
|
|
|
|
}
|
2021-09-21 09:31:23 +00:00
|
|
|
w.WriteHeader(http.StatusOK)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetBucketPolicyHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-03-18 13:04:09 +00:00
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bucketACL, err := h.obj.GetBucketACL(r.Context(), bktInfo)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not fetch bucket acl", reqInfo, err)
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
ast := tableToAst(bucketACL.EACL, reqInfo.BucketName)
|
|
|
|
bktPolicy := astToPolicy(ast)
|
|
|
|
|
|
|
|
w.WriteHeader(http.StatusOK)
|
|
|
|
|
|
|
|
if err = json.NewEncoder(w).Encode(bktPolicy); err != nil {
|
|
|
|
h.logAndSendError(w, "something went wrong", reqInfo, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-09-10 06:56:56 +00:00
|
|
|
func checkOwner(info *data.BucketInfo, owner string) error {
|
2021-07-21 11:59:46 +00:00
|
|
|
if owner == "" {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2022-04-13 16:56:58 +00:00
|
|
|
// may need to convert owner to appropriate format
|
2021-07-21 11:59:46 +00:00
|
|
|
if info.Owner.String() != owner {
|
2023-06-30 09:03:55 +00:00
|
|
|
return fmt.Errorf("%w: mismatch owner", errors.GetAPIError(errors.ErrAccessDenied))
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) PutBucketPolicyHandler(w http.ResponseWriter, r *http.Request) {
|
2023-07-05 14:05:45 +00:00
|
|
|
reqInfo := middleware.GetReqInfo(r.Context())
|
2022-03-18 13:04:09 +00:00
|
|
|
|
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not get bucket info", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-18 15:35:25 +00:00
|
|
|
token, err := getSessionTokenSetEACL(r.Context())
|
2022-05-24 13:17:45 +00:00
|
|
|
if err != nil {
|
2022-04-18 15:35:25 +00:00
|
|
|
h.logAndSendError(w, "couldn't get eacl token", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
bktPolicy := &bucketPolicy{Bucket: reqInfo.BucketName}
|
2022-06-22 19:40:52 +00:00
|
|
|
if err = json.NewDecoder(r.Body).Decode(bktPolicy); err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could not parse bucket policy", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
astPolicy, err := policyToAst(bktPolicy)
|
|
|
|
if err != nil {
|
|
|
|
h.logAndSendError(w, "could not translate policy to ast", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-09 20:05:50 +00:00
|
|
|
if _, err = h.updateBucketACL(r, astPolicy, bktInfo, token); err != nil {
|
2021-07-21 11:59:46 +00:00
|
|
|
h.logAndSendError(w, "could not update bucket acl", reqInfo, err)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-04-06 08:27:47 +00:00
|
|
|
func parseACLHeaders(header http.Header, key *keys.PublicKey) (*AccessControlPolicy, error) {
|
2021-07-21 11:59:46 +00:00
|
|
|
var err error
|
|
|
|
acp := &AccessControlPolicy{Owner: Owner{
|
2022-04-06 08:27:47 +00:00
|
|
|
ID: hex.EncodeToString(key.Bytes()),
|
|
|
|
DisplayName: key.Address(),
|
2021-07-21 11:59:46 +00:00
|
|
|
}}
|
|
|
|
acp.AccessControlList = []*Grant{{
|
|
|
|
Grantee: &Grantee{
|
2022-04-06 08:27:47 +00:00
|
|
|
ID: hex.EncodeToString(key.Bytes()),
|
|
|
|
DisplayName: key.Address(),
|
2021-07-21 11:59:46 +00:00
|
|
|
Type: acpCanonicalUser,
|
|
|
|
},
|
|
|
|
Permission: aclFullControl,
|
|
|
|
}}
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
cannedACL := header.Get(api.AmzACL)
|
2021-07-21 11:59:46 +00:00
|
|
|
if cannedACL != "" {
|
|
|
|
return addPredefinedACP(acp, cannedACL)
|
|
|
|
}
|
|
|
|
|
2021-10-19 15:08:07 +00:00
|
|
|
if acp.AccessControlList, err = addGrantees(acp.AccessControlList, header, api.AmzGrantFullControl); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("add grantees full control: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2021-10-19 15:08:07 +00:00
|
|
|
if acp.AccessControlList, err = addGrantees(acp.AccessControlList, header, api.AmzGrantRead); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("add grantees read: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2021-10-19 15:08:07 +00:00
|
|
|
if acp.AccessControlList, err = addGrantees(acp.AccessControlList, header, api.AmzGrantWrite); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("add grantees write: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return acp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func addGrantees(list []*Grant, headers http.Header, hdr string) ([]*Grant, error) {
|
|
|
|
grant := headers.Get(hdr)
|
|
|
|
if grant == "" {
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
permission, err := grantHdrToPermission(hdr)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("parse header: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
grantees, err := parseGrantee(grant)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("parse grantee: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, grantee := range grantees {
|
|
|
|
if grantee.Type == acpAmazonCustomerByEmail || (grantee.Type == acpGroup && grantee.URI != allUsersGroup) {
|
2022-06-30 11:51:56 +00:00
|
|
|
return nil, stderrors.New("unsupported grantee type")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, &Grant{
|
|
|
|
Grantee: grantee,
|
|
|
|
Permission: permission,
|
|
|
|
})
|
|
|
|
}
|
|
|
|
return list, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func grantHdrToPermission(grant string) (AWSACL, error) {
|
|
|
|
switch grant {
|
|
|
|
case api.AmzGrantFullControl:
|
|
|
|
return aclFullControl, nil
|
|
|
|
case api.AmzGrantRead:
|
|
|
|
return aclRead, nil
|
|
|
|
case api.AmzGrantWrite:
|
|
|
|
return aclWrite, nil
|
|
|
|
}
|
|
|
|
return "", fmt.Errorf("unsuppoted header: %s", grant)
|
|
|
|
}
|
|
|
|
|
|
|
|
func parseGrantee(grantees string) ([]*Grantee, error) {
|
|
|
|
var result []*Grantee
|
|
|
|
|
|
|
|
split := strings.Split(grantees, ", ")
|
|
|
|
for _, pair := range split {
|
|
|
|
split2 := strings.Split(pair, "=")
|
|
|
|
if len(split2) != 2 {
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidArgument)
|
|
|
|
}
|
|
|
|
|
|
|
|
grantee, err := formGrantee(split2[0], split2[1])
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("form grantee: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
result = append(result, grantee)
|
|
|
|
}
|
|
|
|
|
|
|
|
return result, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func formGrantee(granteeType, value string) (*Grantee, error) {
|
|
|
|
value = strings.Trim(value, "\"")
|
|
|
|
switch granteeType {
|
|
|
|
case "id":
|
|
|
|
return &Grantee{
|
|
|
|
ID: value,
|
|
|
|
Type: acpCanonicalUser,
|
|
|
|
}, nil
|
|
|
|
case "uri":
|
|
|
|
return &Grantee{
|
|
|
|
URI: value,
|
|
|
|
Type: acpGroup,
|
|
|
|
}, nil
|
|
|
|
case "emailAddress":
|
|
|
|
return &Grantee{
|
|
|
|
EmailAddress: value,
|
|
|
|
Type: acpAmazonCustomerByEmail,
|
|
|
|
}, nil
|
|
|
|
}
|
2022-06-02 14:31:28 +00:00
|
|
|
// do not return grantee type to avoid sensitive data logging (#489)
|
|
|
|
return nil, fmt.Errorf("unknown grantee type")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func addPredefinedACP(acp *AccessControlPolicy, cannedACL string) (*AccessControlPolicy, error) {
|
|
|
|
switch cannedACL {
|
|
|
|
case basicACLPrivate:
|
|
|
|
case basicACLPublic:
|
|
|
|
acp.AccessControlList = append(acp.AccessControlList, &Grant{
|
|
|
|
Grantee: &Grantee{
|
|
|
|
URI: allUsersGroup,
|
|
|
|
Type: acpGroup,
|
|
|
|
},
|
|
|
|
Permission: aclFullControl,
|
|
|
|
})
|
|
|
|
case cannedACLAuthRead:
|
|
|
|
fallthrough
|
|
|
|
case basicACLReadOnly:
|
|
|
|
acp.AccessControlList = append(acp.AccessControlList, &Grant{
|
|
|
|
Grantee: &Grantee{
|
|
|
|
URI: allUsersGroup,
|
|
|
|
Type: acpGroup,
|
|
|
|
},
|
|
|
|
Permission: aclRead,
|
|
|
|
})
|
|
|
|
default:
|
|
|
|
return nil, errors.GetAPIError(errors.ErrInvalidArgument)
|
|
|
|
}
|
|
|
|
|
|
|
|
return acp, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func tableToAst(table *eacl.Table, bktName string) *ast {
|
2022-07-15 14:35:11 +00:00
|
|
|
resourceMap := make(map[string]orderedAstResource)
|
|
|
|
|
|
|
|
var groupRecordsLeft int
|
|
|
|
var currentResource orderedAstResource
|
|
|
|
for i, record := range table.Records() {
|
|
|
|
if serviceRec := tryServiceRecord(record); serviceRec != nil {
|
|
|
|
resInfo := resourceInfoFromName(serviceRec.Resource, bktName)
|
|
|
|
groupRecordsLeft = serviceRec.GroupRecordsLength
|
|
|
|
|
|
|
|
currentResource = getResourceOrCreate(resourceMap, i, resInfo)
|
|
|
|
resourceMap[resInfo.Name()] = currentResource
|
|
|
|
} else if groupRecordsLeft != 0 {
|
|
|
|
groupRecordsLeft--
|
|
|
|
addOperationsAndUpdateMap(currentResource, record, resourceMap)
|
|
|
|
} else {
|
|
|
|
resInfo := resInfoFromFilters(bktName, record.Filters())
|
|
|
|
resource := getResourceOrCreate(resourceMap, i, resInfo)
|
|
|
|
addOperationsAndUpdateMap(resource, record, resourceMap)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-15 14:35:11 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &ast{
|
|
|
|
Resources: formReverseOrderResources(resourceMap),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func formReverseOrderResources(resourceMap map[string]orderedAstResource) []*astResource {
|
|
|
|
orderedResources := make([]orderedAstResource, 0, len(resourceMap))
|
|
|
|
for _, resource := range resourceMap {
|
|
|
|
orderedResources = append(orderedResources, resource)
|
|
|
|
}
|
|
|
|
sort.Slice(orderedResources, func(i, j int) bool {
|
|
|
|
return orderedResources[i].Index >= orderedResources[j].Index // reverse order
|
|
|
|
})
|
|
|
|
|
|
|
|
result := make([]*astResource, len(orderedResources))
|
|
|
|
for i, ordered := range orderedResources {
|
|
|
|
res := ordered.Resource
|
|
|
|
for j, k := 0, len(res.Operations)-1; j < k; j, k = j+1, k-1 {
|
|
|
|
res.Operations[j], res.Operations[k] = res.Operations[k], res.Operations[j]
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-17 02:26:20 +00:00
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
result[i] = res
|
|
|
|
}
|
|
|
|
|
|
|
|
return result
|
|
|
|
}
|
|
|
|
|
|
|
|
func addOperationsAndUpdateMap(orderedRes orderedAstResource, record eacl.Record, resMap map[string]orderedAstResource) {
|
|
|
|
for _, target := range record.Targets() {
|
|
|
|
orderedRes.Resource.Operations = addToList(orderedRes.Resource.Operations, record, target)
|
|
|
|
}
|
|
|
|
resMap[orderedRes.Resource.Name()] = orderedRes
|
|
|
|
}
|
|
|
|
|
|
|
|
func getResourceOrCreate(resMap map[string]orderedAstResource, index int, resInfo resourceInfo) orderedAstResource {
|
|
|
|
resource, ok := resMap[resInfo.Name()]
|
|
|
|
if !ok {
|
|
|
|
resource = orderedAstResource{
|
|
|
|
Index: index,
|
|
|
|
Resource: &astResource{resourceInfo: resInfo},
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-15 14:35:11 +00:00
|
|
|
return resource
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
func resInfoFromFilters(bucketName string, filters []eacl.Filter) resourceInfo {
|
|
|
|
resInfo := resourceInfo{Bucket: bucketName}
|
|
|
|
for _, filter := range filters {
|
|
|
|
if filter.Matcher() == eacl.MatchStringEqual {
|
2022-09-07 06:59:24 +00:00
|
|
|
if filter.Key() == object.AttributeFilePath {
|
2022-07-15 14:35:11 +00:00
|
|
|
resInfo.Object = filter.Value()
|
|
|
|
} else if filter.Key() == v2acl.FilterObjectID {
|
|
|
|
resInfo.Version = filter.Value()
|
|
|
|
}
|
2022-07-17 02:26:20 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
return resInfo
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func mergeAst(parent, child *ast) (*ast, bool) {
|
|
|
|
updated := false
|
|
|
|
for _, resource := range child.Resources {
|
2021-08-30 07:55:42 +00:00
|
|
|
parentResource := getParentResource(parent, resource)
|
2021-07-21 11:59:46 +00:00
|
|
|
if parentResource == nil {
|
|
|
|
parent.Resources = append(parent.Resources, resource)
|
|
|
|
updated = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
var newOps []*astOperation
|
|
|
|
for _, astOp := range resource.Operations {
|
2022-09-06 07:43:04 +00:00
|
|
|
// get parent matched operations
|
2021-07-21 11:59:46 +00:00
|
|
|
ops := getAstOps(parentResource, astOp)
|
|
|
|
switch len(ops) {
|
2022-09-06 07:43:04 +00:00
|
|
|
case 2: // parent contains different actions for the same child operation
|
2021-07-21 11:59:46 +00:00
|
|
|
// potential inconsistency
|
2022-07-06 13:28:28 +00:00
|
|
|
if groupGrantee := astOp.IsGroupGrantee(); groupGrantee {
|
2021-07-21 11:59:46 +00:00
|
|
|
// it is not likely (such state must be detected early)
|
|
|
|
// inconsistency
|
|
|
|
action := eacl.ActionAllow
|
|
|
|
if astOp.Action == eacl.ActionAllow {
|
|
|
|
action = eacl.ActionDeny
|
|
|
|
}
|
2022-07-06 13:28:28 +00:00
|
|
|
removeAstOp(parentResource, groupGrantee, astOp.Op, action)
|
2021-07-21 11:59:46 +00:00
|
|
|
updated = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
opToAdd, opToDelete := ops[0], ops[1]
|
|
|
|
if ops[1].Action == astOp.Action {
|
|
|
|
opToAdd, opToDelete = ops[1], ops[0]
|
|
|
|
}
|
|
|
|
|
|
|
|
if handleAddOperations(parentResource, astOp, opToAdd) {
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
if handleRemoveOperations(parentResource, astOp, opToDelete) {
|
|
|
|
updated = true
|
|
|
|
}
|
2022-09-06 07:43:04 +00:00
|
|
|
case 1: // parent contains some action for the same child operation
|
2021-07-21 11:59:46 +00:00
|
|
|
if astOp.Action != ops[0].Action {
|
|
|
|
// potential inconsistency
|
2022-07-06 13:28:28 +00:00
|
|
|
if groupGrantee := astOp.IsGroupGrantee(); groupGrantee {
|
2021-07-21 11:59:46 +00:00
|
|
|
// inconsistency
|
2022-09-06 07:43:04 +00:00
|
|
|
ops[0].Action = astOp.Action
|
2021-07-21 11:59:46 +00:00
|
|
|
updated = true
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if handleRemoveOperations(parentResource, astOp, ops[0]) {
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
parentResource.Operations = append(parentResource.Operations, astOp)
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
if handleAddOperations(parentResource, astOp, ops[0]) {
|
|
|
|
updated = true
|
|
|
|
}
|
2022-09-06 07:43:04 +00:00
|
|
|
case 0: // parent doesn't contain actions for the same child operation
|
2021-07-21 11:59:46 +00:00
|
|
|
newOps = append(newOps, astOp)
|
|
|
|
updated = true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if newOps != nil {
|
|
|
|
parentResource.Operations = append(newOps, parentResource.Operations...)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return parent, updated
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleAddOperations(parentResource *astResource, astOp, existedOp *astOperation) bool {
|
|
|
|
var needToAdd []string
|
|
|
|
for _, user := range astOp.Users {
|
|
|
|
if !containsStr(existedOp.Users, user) {
|
|
|
|
needToAdd = append(needToAdd, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(needToAdd) != 0 {
|
|
|
|
addUsers(parentResource, existedOp, needToAdd)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleRemoveOperations(parentResource *astResource, astOp, existedOp *astOperation) bool {
|
|
|
|
var needToRemove []string
|
|
|
|
for _, user := range astOp.Users {
|
|
|
|
if containsStr(existedOp.Users, user) {
|
|
|
|
needToRemove = append(needToRemove, user)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
if len(needToRemove) != 0 {
|
|
|
|
removeUsers(parentResource, existedOp, needToRemove)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func containsStr(list []string, element string) bool {
|
|
|
|
for _, str := range list {
|
|
|
|
if str == element {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAstOps(resource *astResource, childOp *astOperation) []*astOperation {
|
|
|
|
var res []*astOperation
|
|
|
|
for _, astOp := range resource.Operations {
|
2022-07-06 13:28:28 +00:00
|
|
|
if astOp.IsGroupGrantee() == childOp.IsGroupGrantee() && astOp.Op == childOp.Op {
|
2021-07-21 11:59:46 +00:00
|
|
|
res = append(res, astOp)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2022-07-06 12:39:15 +00:00
|
|
|
func removeAstOp(resource *astResource, group bool, op eacl.Operation, action eacl.Action) {
|
2021-07-21 11:59:46 +00:00
|
|
|
for i, astOp := range resource.Operations {
|
2022-07-06 13:28:28 +00:00
|
|
|
if astOp.IsGroupGrantee() == group && astOp.Op == op && astOp.Action == action {
|
2021-07-21 11:59:46 +00:00
|
|
|
resource.Operations = append(resource.Operations[:i], resource.Operations[i+1:]...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func addUsers(resource *astResource, astO *astOperation, users []string) {
|
|
|
|
for _, astOp := range resource.Operations {
|
2022-07-06 13:28:28 +00:00
|
|
|
if astOp.IsGroupGrantee() == astO.IsGroupGrantee() && astOp.Op == astO.Op && astOp.Action == astO.Action {
|
2021-07-21 11:59:46 +00:00
|
|
|
astOp.Users = append(astO.Users, users...)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func removeUsers(resource *astResource, astOperation *astOperation, users []string) {
|
2022-07-06 13:28:28 +00:00
|
|
|
for ind, astOp := range resource.Operations {
|
|
|
|
if !astOp.IsGroupGrantee() && astOp.Op == astOperation.Op && astOp.Action == astOperation.Action {
|
2022-07-06 13:01:47 +00:00
|
|
|
filteredUsers := astOp.Users[:0] // new slice without allocation
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, user := range astOp.Users {
|
2022-07-06 13:01:47 +00:00
|
|
|
if !containsStr(users, user) {
|
|
|
|
filteredUsers = append(filteredUsers, user)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
2022-07-06 13:28:28 +00:00
|
|
|
if len(filteredUsers) == 0 { // remove ast resource
|
|
|
|
resource.Operations = append(resource.Operations[:ind], resource.Operations[ind+1:]...)
|
|
|
|
} else {
|
|
|
|
astOp.Users = filteredUsers
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
func getParentResource(parent *ast, resource *astResource) *astResource {
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, parentResource := range parent.Resources {
|
2021-08-30 07:55:42 +00:00
|
|
|
if resource.Bucket == parentResource.Bucket && resource.Object == parentResource.Object &&
|
|
|
|
resource.Version == parentResource.Version {
|
2021-07-21 11:59:46 +00:00
|
|
|
return parentResource
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
func astToTable(ast *ast) (*eacl.Table, error) {
|
2021-07-21 11:59:46 +00:00
|
|
|
table := eacl.NewTable()
|
|
|
|
|
2022-07-17 03:33:42 +00:00
|
|
|
for i := len(ast.Resources) - 1; i >= 0; i-- {
|
2022-07-10 20:24:02 +00:00
|
|
|
records, err := formRecords(ast.Resources[i])
|
2021-07-21 11:59:46 +00:00
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("form records: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-15 14:35:11 +00:00
|
|
|
|
|
|
|
serviceRecord := ServiceRecord{
|
|
|
|
Resource: ast.Resources[i].Name(),
|
|
|
|
GroupRecordsLength: len(records),
|
|
|
|
}
|
|
|
|
table.AddRecord(serviceRecord.ToEACLRecord())
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, rec := range records {
|
|
|
|
table.AddRecord(rec)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return table, nil
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
func tryServiceRecord(record eacl.Record) *ServiceRecord {
|
2022-07-20 13:38:22 +00:00
|
|
|
if record.Action() != eacl.ActionAllow || record.Operation() != eacl.OperationGet ||
|
|
|
|
len(record.Targets()) != 1 || len(record.Filters()) != 2 {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
target := record.Targets()[0]
|
|
|
|
if target.Role() != eacl.RoleSystem {
|
2022-07-15 14:35:11 +00:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
resourceFilter := record.Filters()[0]
|
|
|
|
recordsFilter := record.Filters()[1]
|
|
|
|
if resourceFilter.From() != eacl.HeaderFromService || recordsFilter.From() != eacl.HeaderFromService ||
|
|
|
|
resourceFilter.Matcher() != eacl.MatchUnknown || recordsFilter.Matcher() != eacl.MatchUnknown ||
|
|
|
|
resourceFilter.Key() != serviceRecordResourceKey || recordsFilter.Key() != serviceRecordGroupLengthKey {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
groupLength, err := strconv.Atoi(recordsFilter.Value())
|
|
|
|
if err != nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
return &ServiceRecord{
|
|
|
|
Resource: resourceFilter.Value(),
|
|
|
|
GroupRecordsLength: groupLength,
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-07-10 20:24:02 +00:00
|
|
|
func formRecords(resource *astResource) ([]*eacl.Record, error) {
|
2021-07-21 11:59:46 +00:00
|
|
|
var res []*eacl.Record
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
for i := len(resource.Operations) - 1; i >= 0; i-- {
|
|
|
|
astOp := resource.Operations[i]
|
2021-07-21 11:59:46 +00:00
|
|
|
record := eacl.NewRecord()
|
|
|
|
record.SetOperation(astOp.Op)
|
|
|
|
record.SetAction(astOp.Action)
|
2022-07-06 13:28:28 +00:00
|
|
|
if astOp.IsGroupGrantee() {
|
2021-07-21 11:59:46 +00:00
|
|
|
eacl.AddFormedTarget(record, eacl.RoleOthers)
|
|
|
|
} else {
|
2022-07-06 13:32:53 +00:00
|
|
|
targetKeys := make([]ecdsa.PublicKey, 0, len(astOp.Users))
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, user := range astOp.Users {
|
|
|
|
pk, err := keys.NewPublicKeyFromString(user)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("public key from string: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-06 13:32:53 +00:00
|
|
|
targetKeys = append(targetKeys, (ecdsa.PublicKey)(*pk))
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-07 09:00:09 +00:00
|
|
|
// Unknown role is used, because it is ignored when keys are set
|
2022-07-06 13:32:53 +00:00
|
|
|
eacl.AddFormedTarget(record, eacl.RoleUnknown, targetKeys...)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2021-08-30 07:55:42 +00:00
|
|
|
if len(resource.Object) != 0 {
|
|
|
|
if len(resource.Version) != 0 {
|
2022-04-25 09:57:58 +00:00
|
|
|
var id oid.ID
|
|
|
|
if err := id.DecodeString(resource.Version); err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("parse object version (oid): %w", err)
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
2022-02-08 16:54:04 +00:00
|
|
|
record.AddObjectIDFilter(eacl.MatchStringEqual, id)
|
2022-07-12 10:39:25 +00:00
|
|
|
} else {
|
2022-09-07 06:59:24 +00:00
|
|
|
record.AddObjectAttributeFilter(eacl.MatchStringEqual, object.AttributeFilePath, resource.Object)
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
res = append(res, record)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-03-16 09:02:16 +00:00
|
|
|
func addToList(operations []*astOperation, rec eacl.Record, target eacl.Target) []*astOperation {
|
2022-07-06 12:39:15 +00:00
|
|
|
var (
|
|
|
|
found *astOperation
|
|
|
|
groupTarget = target.Role() == eacl.RoleOthers
|
|
|
|
)
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, astOp := range operations {
|
2022-07-06 13:28:28 +00:00
|
|
|
if astOp.Op == rec.Operation() && astOp.IsGroupGrantee() == groupTarget {
|
2021-07-21 11:59:46 +00:00
|
|
|
found = astOp
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found != nil {
|
2022-07-06 12:39:15 +00:00
|
|
|
if !groupTarget {
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, key := range target.BinaryKeys() {
|
|
|
|
found.Users = append(found.Users, hex.EncodeToString(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
astOperation := &astOperation{
|
2022-07-06 13:28:28 +00:00
|
|
|
Op: rec.Operation(),
|
|
|
|
Action: rec.Action(),
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-06 12:39:15 +00:00
|
|
|
if !groupTarget {
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, key := range target.BinaryKeys() {
|
|
|
|
astOperation.Users = append(astOperation.Users, hex.EncodeToString(key))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
operations = append(operations, astOperation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return operations
|
|
|
|
}
|
|
|
|
|
|
|
|
func policyToAst(bktPolicy *bucketPolicy) (*ast, error) {
|
|
|
|
res := &ast{}
|
|
|
|
|
|
|
|
rr := make(map[string]*astResource)
|
|
|
|
|
|
|
|
for _, state := range bktPolicy.Statement {
|
|
|
|
if state.Principal.AWS != "" && state.Principal.AWS != allUsersWildcard ||
|
|
|
|
state.Principal.AWS == "" && state.Principal.CanonicalUser == "" {
|
|
|
|
return nil, fmt.Errorf("unsupported principal: %v", state.Principal)
|
|
|
|
}
|
2022-07-06 12:39:15 +00:00
|
|
|
var groupGrantee bool
|
2021-07-21 11:59:46 +00:00
|
|
|
if state.Principal.AWS == allUsersWildcard {
|
2022-07-06 12:39:15 +00:00
|
|
|
groupGrantee = true
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, resource := range state.Resource {
|
|
|
|
trimmedResource := strings.TrimPrefix(resource, arnAwsPrefix)
|
|
|
|
r, ok := rr[trimmedResource]
|
|
|
|
if !ok {
|
2022-11-09 14:25:02 +00:00
|
|
|
if !strings.HasPrefix(trimmedResource, bktPolicy.Bucket) {
|
|
|
|
return nil, fmt.Errorf("resource '%s' must be in the same bucket '%s'", trimmedResource, bktPolicy.Bucket)
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
r = &astResource{
|
|
|
|
resourceInfo: resourceInfoFromName(trimmedResource, bktPolicy.Bucket),
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
for _, action := range state.Action {
|
|
|
|
for _, op := range actionToOpMap[action] {
|
|
|
|
toAction := effectToAction(state.Effect)
|
2022-07-06 12:39:15 +00:00
|
|
|
r.Operations = addTo(r.Operations, state.Principal.CanonicalUser, op, groupGrantee, toAction)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
rr[trimmedResource] = r
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, val := range rr {
|
|
|
|
res.Resources = append(res.Resources, val)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
func resourceInfoFromName(name, bucketName string) resourceInfo {
|
|
|
|
resInfo := resourceInfo{Bucket: bucketName}
|
|
|
|
if name != bucketName {
|
|
|
|
versionedObject := strings.TrimPrefix(name, bucketName+"/")
|
|
|
|
objVersion := strings.Split(versionedObject, ":")
|
|
|
|
if len(objVersion) <= 2 {
|
|
|
|
resInfo.Object = objVersion[0]
|
|
|
|
if len(objVersion) == 2 {
|
|
|
|
resInfo.Version = objVersion[1]
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
resInfo.Object = strings.Join(objVersion[:len(objVersion)-1], ":")
|
|
|
|
resInfo.Version = objVersion[len(objVersion)-1]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return resInfo
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func astToPolicy(ast *ast) *bucketPolicy {
|
|
|
|
bktPolicy := &bucketPolicy{}
|
|
|
|
|
|
|
|
for _, resource := range ast.Resources {
|
|
|
|
allowed, denied := triageOperations(resource.Operations)
|
2021-08-30 07:55:42 +00:00
|
|
|
handleResourceOperations(bktPolicy, allowed, eacl.ActionAllow, resource.Name())
|
|
|
|
handleResourceOperations(bktPolicy, denied, eacl.ActionDeny, resource.Name())
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return bktPolicy
|
|
|
|
}
|
|
|
|
|
|
|
|
func handleResourceOperations(bktPolicy *bucketPolicy, list []*astOperation, eaclAction eacl.Action, resourceName string) {
|
|
|
|
userOpsMap := make(map[string][]eacl.Operation)
|
|
|
|
|
|
|
|
for _, op := range list {
|
2022-07-06 13:28:28 +00:00
|
|
|
if !op.IsGroupGrantee() {
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, user := range op.Users {
|
|
|
|
userOps := userOpsMap[user]
|
|
|
|
userOps = append(userOps, op.Op)
|
|
|
|
userOpsMap[user] = userOps
|
|
|
|
}
|
|
|
|
} else {
|
|
|
|
userOps := userOpsMap[allUsersGroup]
|
|
|
|
userOps = append(userOps, op.Op)
|
|
|
|
userOpsMap[allUsersGroup] = userOps
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for user, userOps := range userOpsMap {
|
|
|
|
var actions []string
|
|
|
|
LOOP:
|
|
|
|
for action, ops := range actionToOpMap {
|
|
|
|
for _, op := range ops {
|
|
|
|
if !contains(userOps, op) {
|
2022-11-09 14:25:02 +00:00
|
|
|
continue LOOP
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
actions = append(actions, action)
|
|
|
|
}
|
|
|
|
if len(actions) != 0 {
|
|
|
|
state := statement{
|
|
|
|
Effect: actionToEffect(eaclAction),
|
|
|
|
Principal: principal{CanonicalUser: user},
|
|
|
|
Action: actions,
|
|
|
|
Resource: []string{arnAwsPrefix + resourceName},
|
|
|
|
}
|
|
|
|
if user == allUsersGroup {
|
|
|
|
state.Principal = principal{AWS: allUsersWildcard}
|
|
|
|
}
|
|
|
|
bktPolicy.Statement = append(bktPolicy.Statement, state)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func triageOperations(operations []*astOperation) ([]*astOperation, []*astOperation) {
|
|
|
|
var allowed, denied []*astOperation
|
|
|
|
for _, op := range operations {
|
|
|
|
if op.Action == eacl.ActionAllow {
|
|
|
|
allowed = append(allowed, op)
|
|
|
|
} else {
|
|
|
|
denied = append(denied, op)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return allowed, denied
|
|
|
|
}
|
|
|
|
|
2022-07-06 12:39:15 +00:00
|
|
|
func addTo(list []*astOperation, userID string, op eacl.Operation, groupGrantee bool, action eacl.Action) []*astOperation {
|
2021-07-21 11:59:46 +00:00
|
|
|
var found *astOperation
|
|
|
|
for _, astop := range list {
|
2022-07-06 13:28:28 +00:00
|
|
|
if astop.Op == op && astop.IsGroupGrantee() == groupGrantee {
|
2021-07-21 11:59:46 +00:00
|
|
|
found = astop
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if found != nil {
|
2022-07-06 12:39:15 +00:00
|
|
|
if !groupGrantee {
|
2021-08-30 07:55:42 +00:00
|
|
|
found.Users = append(found.Users, userID)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
} else {
|
|
|
|
astoperation := &astOperation{
|
2022-07-06 13:28:28 +00:00
|
|
|
Op: op,
|
|
|
|
Action: action,
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
2022-07-06 12:39:15 +00:00
|
|
|
if !groupGrantee {
|
2021-08-30 07:55:42 +00:00
|
|
|
astoperation.Users = append(astoperation.Users, userID)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
list = append(list, astoperation)
|
|
|
|
}
|
|
|
|
|
|
|
|
return list
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
func aclToAst(acl *AccessControlPolicy, resInfo *resourceInfo) (*ast, error) {
|
|
|
|
res := &ast{}
|
|
|
|
|
|
|
|
resource := &astResource{resourceInfo: *resInfo}
|
|
|
|
|
|
|
|
ops := readOps
|
|
|
|
if resInfo.IsBucket() {
|
|
|
|
ops = append(ops, writeOps...)
|
|
|
|
}
|
|
|
|
|
2022-07-21 13:06:52 +00:00
|
|
|
// Expect to have at least 1 full control grant for owner which is set in
|
|
|
|
// parseACLHeaders(). If there is no other grants, then user sets private
|
|
|
|
// canned ACL, which is processed in this branch.
|
|
|
|
if len(acl.AccessControlList) < 2 {
|
|
|
|
for _, op := range ops {
|
|
|
|
operation := &astOperation{
|
|
|
|
Op: op,
|
|
|
|
Action: eacl.ActionDeny,
|
|
|
|
}
|
|
|
|
resource.Operations = append(resource.Operations, operation)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
for _, op := range ops {
|
|
|
|
operation := &astOperation{
|
|
|
|
Users: []string{acl.Owner.ID},
|
|
|
|
Op: op,
|
|
|
|
Action: eacl.ActionAllow,
|
|
|
|
}
|
|
|
|
resource.Operations = append(resource.Operations, operation)
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, grant := range acl.AccessControlList {
|
|
|
|
if grant.Grantee.Type == acpAmazonCustomerByEmail || (grant.Grantee.Type == acpGroup && grant.Grantee.URI != allUsersGroup) {
|
2022-06-30 11:51:56 +00:00
|
|
|
return nil, stderrors.New("unsupported grantee type")
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
|
|
|
|
2022-07-06 12:39:15 +00:00
|
|
|
var groupGrantee bool
|
2021-08-30 07:55:42 +00:00
|
|
|
if grant.Grantee.Type == acpGroup {
|
2022-07-06 12:39:15 +00:00
|
|
|
groupGrantee = true
|
2021-08-30 07:55:42 +00:00
|
|
|
} else if grant.Grantee.ID == acl.Owner.ID {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, action := range getActions(grant.Permission, resInfo.IsBucket()) {
|
|
|
|
for _, op := range actionToOpMap[action] {
|
2022-07-06 12:39:15 +00:00
|
|
|
resource.Operations = addTo(resource.Operations, grant.Grantee.ID, op, groupGrantee, eacl.ActionAllow)
|
2021-08-30 07:55:42 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
res.Resources = []*astResource{resource}
|
|
|
|
return res, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func aclToPolicy(acl *AccessControlPolicy, resInfo *resourceInfo) (*bucketPolicy, error) {
|
|
|
|
if resInfo.Bucket == "" {
|
|
|
|
return nil, fmt.Errorf("resource bucket must not be empty")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
results := []statement{
|
2021-08-30 07:55:42 +00:00
|
|
|
getAllowStatement(resInfo, acl.Owner.ID, aclFullControl),
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
2022-07-21 09:55:33 +00:00
|
|
|
// Expect to have at least 1 full control grant for owner which is set in
|
|
|
|
// parseACLHeaders(). If there is no other grants, then user sets private
|
|
|
|
// canned ACL, which is processed in this branch.
|
|
|
|
if len(acl.AccessControlList) < 2 {
|
|
|
|
results = append([]statement{getDenyStatement(resInfo, allUsersWildcard, aclFullControl)}, results...)
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
for _, grant := range acl.AccessControlList {
|
|
|
|
if grant.Grantee.Type == acpAmazonCustomerByEmail || (grant.Grantee.Type == acpGroup && grant.Grantee.URI != allUsersGroup) {
|
2022-06-30 11:51:56 +00:00
|
|
|
return nil, stderrors.New("unsupported grantee type")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
user := grant.Grantee.ID
|
|
|
|
if grant.Grantee.Type == acpGroup {
|
|
|
|
user = allUsersWildcard
|
|
|
|
} else if user == acl.Owner.ID {
|
|
|
|
continue
|
|
|
|
}
|
2021-08-30 07:55:42 +00:00
|
|
|
results = append(results, getAllowStatement(resInfo, user, grant.Permission))
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return &bucketPolicy{
|
|
|
|
Statement: results,
|
2022-07-12 10:39:25 +00:00
|
|
|
Bucket: resInfo.Bucket,
|
2021-07-21 11:59:46 +00:00
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
func getAllowStatement(resInfo *resourceInfo, id string, permission AWSACL) statement {
|
2021-07-21 11:59:46 +00:00
|
|
|
state := statement{
|
|
|
|
Effect: "Allow",
|
|
|
|
Principal: principal{
|
|
|
|
CanonicalUser: id,
|
|
|
|
},
|
2021-08-30 07:55:42 +00:00
|
|
|
Action: getActions(permission, resInfo.IsBucket()),
|
|
|
|
Resource: []string{arnAwsPrefix + resInfo.Name()},
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if id == allUsersWildcard {
|
|
|
|
state.Principal = principal{AWS: allUsersWildcard}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2022-07-21 09:55:33 +00:00
|
|
|
func getDenyStatement(resInfo *resourceInfo, id string, permission AWSACL) statement {
|
|
|
|
state := statement{
|
|
|
|
Effect: "Deny",
|
|
|
|
Principal: principal{
|
|
|
|
CanonicalUser: id,
|
|
|
|
},
|
|
|
|
Action: getActions(permission, resInfo.IsBucket()),
|
|
|
|
Resource: []string{arnAwsPrefix + resInfo.Name()},
|
|
|
|
}
|
|
|
|
|
|
|
|
if id == allUsersWildcard {
|
|
|
|
state.Principal = principal{AWS: allUsersWildcard}
|
|
|
|
}
|
|
|
|
|
|
|
|
return state
|
|
|
|
}
|
|
|
|
|
2021-07-21 11:59:46 +00:00
|
|
|
func getActions(permission AWSACL, isBucket bool) []string {
|
|
|
|
var res []string
|
|
|
|
switch permission {
|
|
|
|
case aclRead:
|
|
|
|
if isBucket {
|
|
|
|
res = []string{s3ListBucket, s3ListBucketVersions, s3ListBucketMultipartUploads}
|
|
|
|
} else {
|
|
|
|
res = []string{s3GetObject, s3GetObjectVersion}
|
|
|
|
}
|
|
|
|
case aclWrite:
|
|
|
|
if isBucket {
|
|
|
|
res = []string{s3PutObject, s3DeleteObject}
|
|
|
|
}
|
|
|
|
case aclFullControl:
|
|
|
|
if isBucket {
|
|
|
|
res = []string{s3ListBucket, s3ListBucketVersions, s3ListBucketMultipartUploads, s3PutObject, s3DeleteObject}
|
|
|
|
} else {
|
|
|
|
res = []string{s3GetObject, s3GetObjectVersion}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
|
|
|
func effectToAction(effect string) eacl.Action {
|
|
|
|
switch effect {
|
|
|
|
case "Allow":
|
|
|
|
return eacl.ActionAllow
|
|
|
|
case "Deny":
|
|
|
|
return eacl.ActionDeny
|
|
|
|
}
|
|
|
|
return eacl.ActionUnknown
|
|
|
|
}
|
|
|
|
|
|
|
|
func actionToEffect(action eacl.Action) string {
|
|
|
|
switch action {
|
|
|
|
case eacl.ActionAllow:
|
|
|
|
return "Allow"
|
|
|
|
case eacl.ActionDeny:
|
|
|
|
return "Deny"
|
|
|
|
default:
|
|
|
|
return ""
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func permissionToOperations(permission AWSACL) []eacl.Operation {
|
|
|
|
switch permission {
|
|
|
|
case aclFullControl:
|
|
|
|
return fullOps
|
|
|
|
case aclRead:
|
|
|
|
return readOps
|
|
|
|
case aclWrite:
|
|
|
|
return writeOps
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func isWriteOperation(op eacl.Operation) bool {
|
|
|
|
return op == eacl.OperationDelete || op == eacl.OperationPut
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
func (h *handler) encodeObjectACL(ctx context.Context, bucketACL *layer.BucketACL, bucketName, objectVersion string) *AccessControlPolicy {
|
2021-07-21 11:59:46 +00:00
|
|
|
res := &AccessControlPolicy{
|
|
|
|
Owner: Owner{
|
|
|
|
ID: bucketACL.Info.Owner.String(),
|
|
|
|
DisplayName: bucketACL.Info.Owner.String(),
|
|
|
|
},
|
|
|
|
}
|
|
|
|
|
|
|
|
m := make(map[string][]eacl.Operation)
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
astList := tableToAst(bucketACL.EACL, bucketName)
|
|
|
|
|
|
|
|
for _, resource := range astList.Resources {
|
|
|
|
if resource.Version != objectVersion {
|
2021-07-21 11:59:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
for _, op := range resource.Operations {
|
|
|
|
if op.Action != eacl.ActionAllow {
|
2021-07-21 11:59:46 +00:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2022-07-15 14:35:11 +00:00
|
|
|
if len(op.Users) == 0 {
|
|
|
|
list := append(m[allUsersGroup], op.Op)
|
2021-07-21 11:59:46 +00:00
|
|
|
m[allUsersGroup] = list
|
2022-07-15 14:35:11 +00:00
|
|
|
} else {
|
|
|
|
for _, user := range op.Users {
|
|
|
|
list := append(m[user], op.Op)
|
|
|
|
m[user] = list
|
|
|
|
}
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for key, val := range m {
|
|
|
|
permission := aclFullControl
|
2022-08-24 15:22:18 +00:00
|
|
|
read := true
|
2021-07-21 11:59:46 +00:00
|
|
|
for op := eacl.OperationGet; op <= eacl.OperationRangeHash; op++ {
|
2022-08-24 15:22:18 +00:00
|
|
|
if !contains(val, op) && !isWriteOperation(op) {
|
|
|
|
read = false
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-08-24 15:22:18 +00:00
|
|
|
if read {
|
|
|
|
permission = aclFullControl
|
|
|
|
} else {
|
2023-08-23 11:07:52 +00:00
|
|
|
h.reqLogger(ctx).Warn(logs.SomeACLNotFullyMapped)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
var grantee *Grantee
|
|
|
|
if key == allUsersGroup {
|
|
|
|
grantee = NewGrantee(acpGroup)
|
|
|
|
grantee.URI = allUsersGroup
|
|
|
|
} else {
|
|
|
|
grantee = NewGrantee(acpCanonicalUser)
|
|
|
|
grantee.ID = key
|
|
|
|
}
|
|
|
|
|
|
|
|
grant := &Grant{
|
|
|
|
Grantee: grantee,
|
|
|
|
Permission: permission,
|
|
|
|
}
|
|
|
|
res.AccessControlList = append(res.AccessControlList, grant)
|
|
|
|
}
|
|
|
|
|
|
|
|
return res
|
|
|
|
}
|
|
|
|
|
2023-06-09 13:19:23 +00:00
|
|
|
func (h *handler) encodeBucketACL(ctx context.Context, bucketName string, bucketACL *layer.BucketACL) *AccessControlPolicy {
|
|
|
|
return h.encodeObjectACL(ctx, bucketACL, bucketName, "")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func contains(list []eacl.Operation, op eacl.Operation) bool {
|
|
|
|
for _, operation := range list {
|
|
|
|
if operation == op {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
|
|
|
type getRecordFunc func(op eacl.Operation) *eacl.Record
|
|
|
|
|
2021-08-30 07:55:42 +00:00
|
|
|
func bucketACLToTable(acp *AccessControlPolicy, resInfo *resourceInfo) (*eacl.Table, error) {
|
|
|
|
if !resInfo.IsBucket() {
|
2021-07-21 11:59:46 +00:00
|
|
|
return nil, fmt.Errorf("allowed only bucket acl")
|
|
|
|
}
|
|
|
|
|
|
|
|
var found bool
|
|
|
|
table := eacl.NewTable()
|
|
|
|
|
|
|
|
ownerKey, err := keys.NewPublicKeyFromString(acp.Owner.ID)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("public key from string: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
for _, grant := range acp.AccessControlList {
|
|
|
|
if !isValidGrant(grant) {
|
2022-06-30 11:51:56 +00:00
|
|
|
return nil, stderrors.New("unsupported grantee")
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
if grant.Grantee.ID == acp.Owner.ID {
|
|
|
|
found = true
|
|
|
|
}
|
|
|
|
|
|
|
|
getRecord, err := getRecordFunction(grant.Grantee)
|
|
|
|
if err != nil {
|
2022-06-22 19:40:52 +00:00
|
|
|
return nil, fmt.Errorf("record func from grantee: %w", err)
|
2021-07-21 11:59:46 +00:00
|
|
|
}
|
|
|
|
for _, op := range permissionToOperations(grant.Permission) {
|
|
|
|
table.AddRecord(getRecord(op))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if !found {
|
|
|
|
for _, op := range fullOps {
|
|
|
|
table.AddRecord(getAllowRecord(op, ownerKey))
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, op := range fullOps {
|
|
|
|
table.AddRecord(getOthersRecord(op, eacl.ActionDeny))
|
|
|
|
}
|
|
|
|
|
|
|
|
return table, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func getRecordFunction(grantee *Grantee) (getRecordFunc, error) {
|
|
|
|
switch grantee.Type {
|
|
|
|
case acpAmazonCustomerByEmail:
|
|
|
|
case acpCanonicalUser:
|
|
|
|
pk, err := keys.NewPublicKeyFromString(grantee.ID)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse canonical ID %s: %w", grantee.ID, err)
|
|
|
|
}
|
|
|
|
return func(op eacl.Operation) *eacl.Record {
|
|
|
|
return getAllowRecord(op, pk)
|
|
|
|
}, nil
|
|
|
|
case acpGroup:
|
|
|
|
return func(op eacl.Operation) *eacl.Record {
|
|
|
|
return getOthersRecord(op, eacl.ActionAllow)
|
|
|
|
}, nil
|
|
|
|
}
|
|
|
|
return nil, fmt.Errorf("unknown type: %s", grantee.Type)
|
|
|
|
}
|
|
|
|
|
|
|
|
func isValidGrant(grant *Grant) bool {
|
|
|
|
return (grant.Permission == aclFullControl || grant.Permission == aclRead || grant.Permission == aclWrite) &&
|
|
|
|
(grant.Grantee.Type == acpCanonicalUser || (grant.Grantee.Type == acpGroup && grant.Grantee.URI == allUsersGroup))
|
|
|
|
}
|
|
|
|
|
|
|
|
func getAllowRecord(op eacl.Operation, pk *keys.PublicKey) *eacl.Record {
|
|
|
|
record := eacl.NewRecord()
|
|
|
|
record.SetOperation(op)
|
|
|
|
record.SetAction(eacl.ActionAllow)
|
2022-07-06 12:39:15 +00:00
|
|
|
// Unknown role is used, because it is ignored when keys are set
|
|
|
|
eacl.AddFormedTarget(record, eacl.RoleUnknown, (ecdsa.PublicKey)(*pk))
|
2021-07-21 11:59:46 +00:00
|
|
|
return record
|
|
|
|
}
|
|
|
|
|
|
|
|
func getOthersRecord(op eacl.Operation, action eacl.Action) *eacl.Record {
|
|
|
|
record := eacl.NewRecord()
|
|
|
|
record.SetOperation(op)
|
|
|
|
record.SetAction(action)
|
|
|
|
eacl.AddFormedTarget(record, eacl.RoleOthers)
|
|
|
|
return record
|
|
|
|
}
|