[#641] Remove old CORS versions after putting new one
All checks were successful
/ Vulncheck (push) Successful in 1m10s
/ Builds (push) Successful in 1m1s
/ OCI image (push) Successful in 2m3s
/ Lint (push) Successful in 2m15s
/ Tests (push) Successful in 1m21s

Signed-off-by: Marina Biryukova <m.biryukova@yadro.com>
This commit is contained in:
Marina Biryukova 2025-03-17 17:41:07 +03:00 committed by Alexey Vanin
parent c0c4bdb366
commit 01d95d8cf4
3 changed files with 39 additions and 30 deletions

View file

@ -228,33 +228,6 @@ func TestPreflightWildcardOrigin(t *testing.T) {
}
}
func TestDeleteAllCORSVersions(t *testing.T) {
body := `
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedMethod>GET</AllowedMethod>
<AllowedMethod>PUT</AllowedMethod>
<AllowedOrigin>*</AllowedOrigin>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
`
hc := prepareHandlerContext(t)
bktName := "bucket-delete-all-cors-version"
createBucket(hc, bktName)
require.Len(t, hc.tp.Objects(), 0)
for range 5 {
putBucketCORS(hc, bktName, body)
}
require.Len(t, hc.tp.Objects(), 5)
deleteBucketCORS(hc, bktName)
require.Len(t, hc.tp.Objects(), 0)
}
func TestGetLatestCORSVersion(t *testing.T) {
bodyTree := `
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
@ -295,7 +268,7 @@ func TestGetLatestCORSVersion(t *testing.T) {
requireEqualCORS(hc.t, bodyTree, w.Body.String())
}
func TestDeleteTreeCORSVersions(t *testing.T) {
func TestDeleteCORSVersions(t *testing.T) {
body := `
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
@ -305,6 +278,15 @@ func TestDeleteTreeCORSVersions(t *testing.T) {
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
`
newBody := `
<CORSConfiguration xmlns="http://s3.amazonaws.com/doc/2006-03-01/">
<CORSRule>
<AllowedMethod>HEAD</AllowedMethod>
<AllowedOrigin>*</AllowedOrigin>
<AllowedHeader>*</AllowedHeader>
</CORSRule>
</CORSConfiguration>
`
hc := prepareHandlerContext(t)
@ -317,10 +299,19 @@ func TestDeleteTreeCORSVersions(t *testing.T) {
putBucketCORS(hc, bktName, body)
require.Len(t, hc.tp.Objects(), 1)
require.Equal(t, body, string(hc.tp.Objects()[0].Payload()))
hc.tp.AddCORSObject(info.BktInfo, hc.corsCnrID, body)
require.Len(t, hc.tp.Objects(), 2)
putBucketCORS(hc, bktName, newBody)
require.Len(t, hc.tp.Objects(), 1)
require.Equal(t, newBody, string(hc.tp.Objects()[0].Payload()))
addCORSToTree(hc, body, info.BktInfo, info.BktInfo.CID)
addCORSToTree(hc, body, info.BktInfo, hc.corsCnrID)
require.Len(t, hc.tp.Objects(), 3)
hc.tp.AddCORSObject(info.BktInfo, hc.corsCnrID, body)
require.Len(t, hc.tp.Objects(), 4)
deleteBucketCORS(hc, bktName)
require.Len(t, hc.tp.Objects(), 0)

View file

@ -45,6 +45,11 @@ func (n *Layer) PutBucketCORS(ctx context.Context, p *PutCORSParams) error {
return err
}
corsVersions, err := n.getCORSVersions(ctx, p.BktInfo)
if err != nil {
n.reqLogger(ctx).Error(logs.CouldntGetCORSObjectVersions, zap.Error(err), logs.TagField(logs.TagExternalStorage))
}
prm := frostfs.PrmObjectCreate{
Container: n.corsCnrInfo.CID,
Payload: &buf,
@ -56,7 +61,7 @@ func (n *Layer) PutBucketCORS(ctx context.Context, p *PutCORSParams) error {
},
}
_, err := n.objectPutAndHash(ctx, prm, n.corsCnrInfo)
_, err = n.objectPutAndHash(ctx, prm, n.corsCnrInfo)
if err != nil {
return fmt.Errorf("put cors object: %w", err)
}
@ -76,6 +81,15 @@ func (n *Layer) PutBucketCORS(ctx context.Context, p *PutCORSParams) error {
}
}
if corsVersions != nil {
var addr oid.Address
addr.SetContainer(n.corsCnrInfo.CID)
for _, obj := range corsVersions.GetObjects() {
addr.SetObject(obj.ObjID)
n.deleteCORSObject(ctx, p.BktInfo, addr)
}
}
return nil
}

View file

@ -92,6 +92,10 @@ func (v *ObjectVersions) GetSorted() []*ObjectVersion {
return v.objects
}
func (v *ObjectVersions) GetObjects() []*ObjectVersion {
return v.objects
}
func (v *ObjectVersions) SetLessFunc(less func(*ObjectVersion, *ObjectVersion) bool) {
v.less = less
}