2022-02-28 08:29:03 +00:00
|
|
|
package handler
|
|
|
|
|
|
|
|
import (
|
2022-11-08 09:12:55 +00:00
|
|
|
"context"
|
2022-02-28 08:29:03 +00:00
|
|
|
"fmt"
|
|
|
|
"net/http"
|
2022-02-28 13:39:04 +00:00
|
|
|
"strconv"
|
2022-02-28 10:22:07 +00:00
|
|
|
"time"
|
2022-02-28 08:29:03 +00:00
|
|
|
|
2023-03-07 14:38:08 +00:00
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api"
|
|
|
|
"git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/data"
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr "git.frostfs.info/TrueCloudLab/frostfs-s3-gw/api/errors"
|
2023-03-07 14:38:08 +00:00
|
|
|
"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"
|
2022-02-28 08:29:03 +00:00
|
|
|
)
|
|
|
|
|
2022-02-28 10:22:07 +00:00
|
|
|
const (
|
|
|
|
dayDuration = 24 * time.Hour
|
|
|
|
yearDuration = 365 * dayDuration
|
|
|
|
|
|
|
|
enabledValue = "Enabled"
|
|
|
|
governanceMode = "GOVERNANCE"
|
|
|
|
complianceMode = "COMPLIANCE"
|
|
|
|
legalHoldOn = "ON"
|
2022-02-28 13:39:04 +00:00
|
|
|
legalHoldOff = "OFF"
|
2022-02-28 10:22:07 +00:00
|
|
|
)
|
|
|
|
|
2022-02-28 08:29:03 +00:00
|
|
|
func (h *handler) PutBucketObjectLockConfigHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 08:29:03 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 08:29:03 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't put object locking configuration", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotAllowed))
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lockingConf := &data.ObjectLockConfiguration{}
|
2023-10-09 12:34:51 +00:00
|
|
|
if err = h.cfg.NewXMLDecoder(r.Body).Decode(lockingConf); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't parse locking configuration", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = checkLockConfiguration(lockingConf); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid lock configuration", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(ctx, bktInfo)
|
2022-02-28 08:29:03 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't get bucket settings", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-22 13:32:09 +00:00
|
|
|
// settings pointer is stored in the cache, so modify a copy of the settings
|
|
|
|
newSettings := *settings
|
|
|
|
newSettings.LockConfiguration = lockingConf
|
2022-02-28 08:29:03 +00:00
|
|
|
|
|
|
|
sp := &layer.PutSettingsParams{
|
|
|
|
BktInfo: bktInfo,
|
2022-08-22 13:32:09 +00:00
|
|
|
Settings: &newSettings,
|
2022-02-28 08:29:03 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
if err = h.obj.PutBucketSettings(ctx, sp); err != nil {
|
|
|
|
h.logAndSendError(ctx, w, "couldn't put bucket settings", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetBucketObjectLockConfigHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 08:29:03 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 08:29:03 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "object lock disabled", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound))
|
2022-02-28 08:29:03 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
settings, err := h.obj.GetBucketSettings(ctx, bktInfo)
|
2022-03-01 15:07:15 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't get bucket settings", reqInfo, err)
|
2022-03-01 15:07:15 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:29:03 +00:00
|
|
|
if settings.LockConfiguration == nil {
|
|
|
|
settings.LockConfiguration = &data.ObjectLockConfiguration{}
|
|
|
|
}
|
|
|
|
if settings.LockConfiguration.ObjectLockEnabled == "" {
|
|
|
|
settings.LockConfiguration.ObjectLockEnabled = enabledValue
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, settings.LockConfiguration); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "something went wrong", reqInfo, err)
|
2022-02-28 08:29:03 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:39:04 +00:00
|
|
|
func (h *handler) PutObjectLegalHoldHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 13:39:04 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "object lock disabled", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound))
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
legalHold := &data.LegalHold{}
|
2023-10-09 12:34:51 +00:00
|
|
|
if err = h.cfg.NewXMLDecoder(r.Body).Decode(legalHold); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't parse legal hold configuration", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if legalHold.Status != legalHoldOn && legalHold.Status != legalHoldOff {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid legal hold status", reqInfo,
|
2022-02-28 13:39:04 +00:00
|
|
|
fmt.Errorf("invalid status %s", legalHold.Status))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 22:48:56 +00:00
|
|
|
p := &layer.PutLockInfoParams{
|
2024-04-10 06:41:07 +00:00
|
|
|
ObjVersion: &data.ObjectVersion{
|
2022-08-11 22:48:56 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
|
|
|
},
|
|
|
|
NewLock: &data.ObjectLock{
|
|
|
|
LegalHold: &data.LegalHoldLock{
|
|
|
|
Enabled: legalHold.Status == legalHoldOn,
|
|
|
|
},
|
2022-05-26 13:11:14 +00:00
|
|
|
},
|
2023-04-24 23:49:12 +00:00
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
p.CopiesNumbers, err = h.pickCopiesNumbers(parseMetadata(r), reqInfo.Namespace, bktInfo.LocationConstraint)
|
2023-04-24 23:49:12 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid copies number", reqInfo, err)
|
2023-04-24 23:49:12 +00:00
|
|
|
return
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
if err = h.obj.PutLockInfo(ctx, p); err != nil {
|
|
|
|
h.logAndSendError(ctx, w, "couldn't head put legal hold", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetObjectLegalHoldHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 13:39:04 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "object lock disabled", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound))
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
p := &data.ObjectVersion{
|
2022-05-26 13:11:14 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
lockInfo, err := h.obj.GetLockInfo(ctx, p)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't head lock object", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
legalHold := &data.LegalHold{Status: legalHoldOff}
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsLegalHoldSet() {
|
2022-02-28 13:39:04 +00:00
|
|
|
legalHold.Status = legalHoldOn
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, legalHold); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "something went wrong", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) PutObjectRetentionHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 13:39:04 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "object lock disabled", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound))
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retention := &data.Retention{}
|
2023-10-09 12:34:51 +00:00
|
|
|
if err = h.cfg.NewXMLDecoder(r.Body).Decode(retention); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't parse object retention", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
lock, err := formObjectLockFromRetention(ctx, retention, r.Header)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid retention configuration", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-08-11 22:48:56 +00:00
|
|
|
p := &layer.PutLockInfoParams{
|
2024-04-10 06:41:07 +00:00
|
|
|
ObjVersion: &data.ObjectVersion{
|
2022-08-11 22:48:56 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
|
|
|
},
|
2023-04-24 23:49:12 +00:00
|
|
|
NewLock: lock,
|
|
|
|
}
|
|
|
|
|
2023-11-21 08:51:07 +00:00
|
|
|
p.CopiesNumbers, err = h.pickCopiesNumbers(parseMetadata(r), reqInfo.Namespace, bktInfo.LocationConstraint)
|
2023-04-24 23:49:12 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "invalid copies number", reqInfo, err)
|
2023-04-24 23:49:12 +00:00
|
|
|
return
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
if err = h.obj.PutLockInfo(ctx, p); err != nil {
|
|
|
|
h.logAndSendError(ctx, w, "couldn't put legal hold", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
func (h *handler) GetObjectRetentionHandler(w http.ResponseWriter, r *http.Request) {
|
2024-10-25 01:36:18 +00:00
|
|
|
ctx := r.Context()
|
|
|
|
reqInfo := middleware.GetReqInfo(ctx)
|
2022-02-28 13:39:04 +00:00
|
|
|
|
2022-03-18 13:04:09 +00:00
|
|
|
bktInfo, err := h.getBucketAndCheckOwner(r, reqInfo.BucketName)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "could not get bucket info", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if !bktInfo.ObjectLockEnabled {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "object lock disabled", reqInfo,
|
2024-09-27 09:18:41 +00:00
|
|
|
apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound))
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2024-04-10 06:41:07 +00:00
|
|
|
p := &data.ObjectVersion{
|
2022-05-26 13:11:14 +00:00
|
|
|
BktInfo: bktInfo,
|
|
|
|
ObjectName: reqInfo.ObjectName,
|
|
|
|
VersionID: reqInfo.URL.Query().Get(api.QueryVersionID),
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2024-10-25 01:36:18 +00:00
|
|
|
lockInfo, err := h.obj.GetLockInfo(ctx, p)
|
2022-02-28 13:39:04 +00:00
|
|
|
if err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "couldn't head lock object", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-27 09:33:36 +00:00
|
|
|
if !lockInfo.IsRetentionSet() {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "retention lock isn't set", reqInfo, apierr.GetAPIError(apierr.ErrNoSuchKey))
|
2022-02-28 13:39:04 +00:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
retention := &data.Retention{
|
|
|
|
Mode: governanceMode,
|
2022-06-27 09:33:36 +00:00
|
|
|
RetainUntilDate: lockInfo.UntilDate(),
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
2022-06-27 09:33:36 +00:00
|
|
|
if lockInfo.IsCompliance() {
|
2022-02-28 13:39:04 +00:00
|
|
|
retention.Mode = complianceMode
|
|
|
|
}
|
|
|
|
|
2023-07-05 14:05:45 +00:00
|
|
|
if err = middleware.EncodeToResponse(w, retention); err != nil {
|
2024-10-25 01:36:18 +00:00
|
|
|
h.logAndSendError(ctx, w, "something went wrong", reqInfo, err)
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 08:29:03 +00:00
|
|
|
func checkLockConfiguration(conf *data.ObjectLockConfiguration) error {
|
|
|
|
if conf.ObjectLockEnabled != "" && conf.ObjectLockEnabled != enabledValue {
|
|
|
|
return fmt.Errorf("invalid ObjectLockEnabled value: %s", conf.ObjectLockEnabled)
|
|
|
|
}
|
|
|
|
|
|
|
|
if conf.Rule == nil || conf.Rule.DefaultRetention == nil {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
retention := conf.Rule.DefaultRetention
|
2022-02-28 10:22:07 +00:00
|
|
|
if retention.Mode != governanceMode && retention.Mode != complianceMode {
|
2022-02-28 08:29:03 +00:00
|
|
|
return fmt.Errorf("invalid Mode value: %s", retention.Mode)
|
|
|
|
}
|
|
|
|
|
|
|
|
if retention.Days == 0 && retention.Years == 0 {
|
|
|
|
return fmt.Errorf("you must specify Days or Years")
|
|
|
|
}
|
|
|
|
|
|
|
|
if retention.Days != 0 && retention.Years != 0 {
|
|
|
|
return fmt.Errorf("you cannot specify Days and Years at the same time")
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
2022-02-28 10:22:07 +00:00
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
func formObjectLock(ctx context.Context, bktInfo *data.BucketInfo, defaultConfig *data.ObjectLockConfiguration, header http.Header) (*data.ObjectLock, error) {
|
2022-02-28 10:22:07 +00:00
|
|
|
if !bktInfo.ObjectLockEnabled {
|
|
|
|
if existLockHeaders(header) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrObjectLockConfigurationNotFound)
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
2022-03-01 15:07:15 +00:00
|
|
|
return nil, nil
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
objectLock := &data.ObjectLock{}
|
|
|
|
|
2022-02-28 10:22:07 +00:00
|
|
|
if defaultConfig == nil {
|
|
|
|
defaultConfig = &data.ObjectLockConfiguration{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if defaultConfig.Rule != nil && defaultConfig.Rule.DefaultRetention != nil {
|
2022-05-26 13:11:14 +00:00
|
|
|
retention := &data.RetentionLock{}
|
2022-02-28 10:22:07 +00:00
|
|
|
defaultRetention := defaultConfig.Rule.DefaultRetention
|
2022-05-26 13:11:14 +00:00
|
|
|
retention.IsCompliance = defaultRetention.Mode == complianceMode
|
2022-11-08 09:12:55 +00:00
|
|
|
now := layer.TimeNow(ctx)
|
2022-02-28 10:22:07 +00:00
|
|
|
if defaultRetention.Days != 0 {
|
2022-05-26 13:11:14 +00:00
|
|
|
retention.Until = now.Add(time.Duration(defaultRetention.Days) * dayDuration)
|
2022-02-28 10:22:07 +00:00
|
|
|
} else {
|
2022-05-26 13:11:14 +00:00
|
|
|
retention.Until = now.Add(time.Duration(defaultRetention.Years) * yearDuration)
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
2022-05-26 13:11:14 +00:00
|
|
|
objectLock.Retention = retention
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
if header.Get(api.AmzObjectLockLegalHold) == legalHoldOn {
|
|
|
|
objectLock.LegalHold = &data.LegalHoldLock{Enabled: true}
|
|
|
|
}
|
2022-02-28 10:22:07 +00:00
|
|
|
|
|
|
|
mode := header.Get(api.AmzObjectLockMode)
|
2022-10-07 14:11:51 +00:00
|
|
|
until := header.Get(api.AmzObjectLockRetainUntilDate)
|
|
|
|
|
|
|
|
if mode != "" && until == "" || mode == "" && until != "" {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrObjectLockInvalidHeaders)
|
2022-10-07 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-02-28 10:22:07 +00:00
|
|
|
if mode != "" {
|
2022-05-26 13:11:14 +00:00
|
|
|
if objectLock.Retention == nil {
|
|
|
|
objectLock.Retention = &data.RetentionLock{}
|
|
|
|
}
|
2022-10-07 14:11:51 +00:00
|
|
|
|
|
|
|
if mode != complianceMode && mode != governanceMode {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrUnknownWORMModeDirective)
|
2022-10-07 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
objectLock.Retention.IsCompliance = mode == complianceMode
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
if until != "" {
|
|
|
|
retentionDate, err := time.Parse(time.RFC3339, until)
|
|
|
|
if err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrInvalidRetentionDate)
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
2022-05-26 13:11:14 +00:00
|
|
|
if objectLock.Retention == nil {
|
|
|
|
objectLock.Retention = &data.RetentionLock{}
|
|
|
|
}
|
|
|
|
objectLock.Retention.Until = retentionDate
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
2022-05-31 07:55:57 +00:00
|
|
|
if objectLock.Retention != nil {
|
|
|
|
if bypassStr := header.Get(api.AmzBypassGovernanceRetention); len(bypassStr) > 0 {
|
|
|
|
bypass, err := strconv.ParseBool(bypassStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse bypass governance header: %w", err)
|
|
|
|
}
|
|
|
|
objectLock.Retention.ByPassedGovernance = bypass
|
|
|
|
}
|
2022-10-07 14:11:51 +00:00
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
if objectLock.Retention.Until.Before(layer.TimeNow(ctx)) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrPastObjectLockRetainDate)
|
2022-10-07 14:11:51 +00:00
|
|
|
}
|
2022-05-31 07:55:57 +00:00
|
|
|
}
|
|
|
|
|
2022-03-01 15:07:15 +00:00
|
|
|
return objectLock, nil
|
2022-02-28 10:22:07 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
func existLockHeaders(header http.Header) bool {
|
|
|
|
return header.Get(api.AmzObjectLockMode) != "" ||
|
|
|
|
header.Get(api.AmzObjectLockLegalHold) != "" ||
|
|
|
|
header.Get(api.AmzObjectLockRetainUntilDate) != ""
|
|
|
|
}
|
2022-02-28 13:39:04 +00:00
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
func formObjectLockFromRetention(ctx context.Context, retention *data.Retention, header http.Header) (*data.ObjectLock, error) {
|
2022-02-28 13:39:04 +00:00
|
|
|
if retention.Mode != governanceMode && retention.Mode != complianceMode {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrMalformedXML)
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
retentionDate, err := time.Parse(time.RFC3339, retention.RetainUntilDate)
|
|
|
|
if err != nil {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrMalformedXML)
|
2022-10-07 14:11:51 +00:00
|
|
|
}
|
|
|
|
|
2022-11-08 09:12:55 +00:00
|
|
|
if retentionDate.Before(layer.TimeNow(ctx)) {
|
2024-09-27 09:18:41 +00:00
|
|
|
return nil, apierr.GetAPIError(apierr.ErrPastObjectLockRetainDate)
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
2022-05-26 13:11:14 +00:00
|
|
|
var bypass bool
|
|
|
|
if bypassStr := header.Get(api.AmzBypassGovernanceRetention); len(bypassStr) > 0 {
|
|
|
|
bypass, err = strconv.ParseBool(bypassStr)
|
|
|
|
if err != nil {
|
|
|
|
return nil, fmt.Errorf("couldn't parse bypass governance header: %w", err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2022-02-28 13:39:04 +00:00
|
|
|
lock := &data.ObjectLock{
|
2022-05-26 13:11:14 +00:00
|
|
|
Retention: &data.RetentionLock{
|
|
|
|
Until: retentionDate,
|
|
|
|
IsCompliance: retention.Mode == complianceMode,
|
|
|
|
ByPassedGovernance: bypass,
|
|
|
|
},
|
2022-02-28 13:39:04 +00:00
|
|
|
}
|
|
|
|
|
|
|
|
return lock, nil
|
|
|
|
}
|