2019-11-12 23:50:18 +01:00
|
|
|
/*
|
2020-01-27 23:12:34 +01:00
|
|
|
* MinIO Cloud Storage, (C) 2019-2020 MinIO, Inc.
|
2019-11-12 23:50:18 +01:00
|
|
|
*
|
|
|
|
* Licensed under the Apache License, Version 2.0 (the "License");
|
|
|
|
* you may not use this file except in compliance with the License.
|
|
|
|
* You may obtain a copy of the License at
|
|
|
|
*
|
|
|
|
* http://www.apache.org/licenses/LICENSE-2.0
|
|
|
|
*
|
|
|
|
* Unless required by applicable law or agreed to in writing, software
|
|
|
|
* distributed under the License is distributed on an "AS IS" BASIS,
|
|
|
|
* WITHOUT WARRANTIES OR CONDITIONS OF ANY KIND, either express or implied.
|
|
|
|
* See the License for the specific language governing permissions and
|
|
|
|
* limitations under the License.
|
|
|
|
*/
|
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2020-02-24 02:09:11 +01:00
|
|
|
"bytes"
|
2019-11-20 22:18:09 +01:00
|
|
|
"context"
|
2020-05-08 22:44:44 +02:00
|
|
|
"encoding/xml"
|
2020-04-01 09:04:25 +02:00
|
|
|
"errors"
|
2020-04-06 22:44:16 +02:00
|
|
|
"math"
|
2019-11-20 22:18:09 +01:00
|
|
|
"net/http"
|
2020-02-24 02:09:11 +01:00
|
|
|
"path"
|
2020-05-08 22:44:44 +02:00
|
|
|
"sync"
|
2019-11-20 22:18:09 +01:00
|
|
|
|
2019-11-21 13:52:35 +01:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-04-06 22:44:16 +02:00
|
|
|
"github.com/minio/minio/pkg/auth"
|
2020-01-27 23:12:34 +01:00
|
|
|
objectlock "github.com/minio/minio/pkg/bucket/object/lock"
|
2020-04-06 22:44:16 +02:00
|
|
|
"github.com/minio/minio/pkg/bucket/policy"
|
2019-11-12 23:50:18 +01:00
|
|
|
)
|
|
|
|
|
2020-05-08 22:44:44 +02:00
|
|
|
// BucketObjectLockSys - map of bucket and retention configuration.
|
|
|
|
type BucketObjectLockSys struct {
|
|
|
|
sync.RWMutex
|
|
|
|
retentionMap map[string]*objectlock.Retention
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set - set retention configuration.
|
|
|
|
func (sys *BucketObjectLockSys) Set(bucketName string, retention *objectlock.Retention) {
|
|
|
|
if globalIsGateway {
|
|
|
|
// no-op
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.Lock()
|
|
|
|
sys.retentionMap[bucketName] = retention
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Get - Get retention configuration.
|
|
|
|
func (sys *BucketObjectLockSys) Get(bucketName string) (r *objectlock.Retention, ok bool) {
|
|
|
|
if globalIsGateway {
|
|
|
|
// When gateway is enabled, no cached value
|
|
|
|
// is used to validate bucket object lock configuration
|
|
|
|
objAPI := newObjectLayerWithoutSafeModeFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
lc, err := objAPI.GetBucketObjectLockConfig(GlobalContext, bucketName)
|
|
|
|
if err != nil {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
return lc.ToRetention(), true
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
r, ok = sys.retentionMap[bucketName]
|
|
|
|
return r, ok
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove - removes retention sysuration.
|
|
|
|
func (sys *BucketObjectLockSys) Remove(bucketName string) {
|
|
|
|
sys.Lock()
|
|
|
|
delete(sys.retentionMap, bucketName)
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
// Similar to enforceRetentionBypassForDelete but for WebUI
|
2020-05-03 23:01:28 +02:00
|
|
|
func enforceRetentionBypassForDeleteWeb(ctx context.Context, r *http.Request, bucket, object string, getObjectInfoFn GetObjectInfoFn, govBypassPerms bool) APIErrorCode {
|
2020-04-06 22:44:16 +02:00
|
|
|
opts, err := getOpts(ctx, r, bucket, object)
|
|
|
|
if err != nil {
|
|
|
|
return toAPIErrorCode(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
oi, err := getObjectInfoFn(ctx, bucket, object, opts)
|
|
|
|
if err != nil {
|
|
|
|
return toAPIErrorCode(ctx, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
lhold := objectlock.GetObjectLegalHoldMeta(oi.UserDefined)
|
|
|
|
if lhold.Status.Valid() && lhold.Status == objectlock.LegalHoldOn {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := objectlock.GetObjectRetentionMeta(oi.UserDefined)
|
|
|
|
if ret.Mode.Valid() {
|
|
|
|
switch ret.Mode {
|
|
|
|
case objectlock.RetCompliance:
|
|
|
|
// In compliance mode, a protected object version can't be overwritten
|
|
|
|
// or deleted by any user, including the root user in your AWS account.
|
|
|
|
// When an object is locked in compliance mode, its retention mode can't
|
|
|
|
// be changed, and its retention period can't be shortened. Compliance mode
|
|
|
|
// ensures that an object version can't be overwritten or deleted for the
|
|
|
|
// duration of the retention period.
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ret.RetainUntilDate.Before(t) {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
case objectlock.RetGovernance:
|
|
|
|
// In governance mode, users can't overwrite or delete an object
|
|
|
|
// version or alter its lock settings unless they have special
|
|
|
|
// permissions. With governance mode, you protect objects against
|
|
|
|
// being deleted by most users, but you can still grant some users
|
|
|
|
// permission to alter the retention settings or delete the object
|
|
|
|
// if necessary. You can also use governance mode to test retention-period
|
|
|
|
// settings before creating a compliance-mode retention period.
|
|
|
|
// To override or remove governance-mode retention settings, a
|
|
|
|
// user must have the s3:BypassGovernanceRetention permission
|
|
|
|
// and must explicitly include x-amz-bypass-governance-retention:true
|
|
|
|
// as a request header with any request that requires overriding
|
|
|
|
// governance mode.
|
2020-05-03 23:01:28 +02:00
|
|
|
byPassSet := govBypassPerms && objectlock.IsObjectLockGovernanceBypassSet(r.Header)
|
2020-04-06 22:44:16 +02:00
|
|
|
if !byPassSet {
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ret.RetainUntilDate.Before(t) {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
2020-05-03 23:01:28 +02:00
|
|
|
|
|
|
|
if !govBypassPerms {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
// enforceRetentionForDeletion checks if it is appropriate to remove an
|
|
|
|
// object according to locking configuration when this is lifecycle/ bucket quota asking.
|
|
|
|
func enforceRetentionForDeletion(ctx context.Context, objInfo ObjectInfo) (locked bool) {
|
2020-04-09 18:28:57 +02:00
|
|
|
lhold := objectlock.GetObjectLegalHoldMeta(objInfo.UserDefined)
|
|
|
|
if lhold.Status.Valid() && lhold.Status == objectlock.LegalHoldOn {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
|
|
|
|
ret := objectlock.GetObjectRetentionMeta(objInfo.UserDefined)
|
|
|
|
if ret.Mode.Valid() && (ret.Mode == objectlock.RetCompliance || ret.Mode == objectlock.RetGovernance) {
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
if ret.RetainUntilDate.After(t) {
|
|
|
|
return true
|
|
|
|
}
|
|
|
|
}
|
|
|
|
return false
|
|
|
|
}
|
|
|
|
|
2019-11-25 19:58:39 +01:00
|
|
|
// enforceRetentionBypassForDelete enforces whether an existing object under governance can be deleted
|
2019-11-20 22:18:09 +01:00
|
|
|
// with governance bypass headers set in the request.
|
2019-11-25 19:58:39 +01:00
|
|
|
// Objects under site wide WORM can never be overwritten.
|
2019-11-20 22:18:09 +01:00
|
|
|
// For objects in "Governance" mode, overwrite is allowed if a) object retention date is past OR
|
|
|
|
// governance bypass headers are set and user has governance bypass permissions.
|
2019-11-25 19:58:39 +01:00
|
|
|
// Objects in "Compliance" mode can be overwritten only if retention date is past.
|
2020-04-06 22:44:16 +02:00
|
|
|
func enforceRetentionBypassForDelete(ctx context.Context, r *http.Request, bucket, object string, getObjectInfoFn GetObjectInfoFn) APIErrorCode {
|
|
|
|
opts, err := getOpts(ctx, r, bucket, object)
|
2019-11-20 22:18:09 +01:00
|
|
|
if err != nil {
|
2020-04-06 22:44:16 +02:00
|
|
|
return toAPIErrorCode(ctx, err)
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
|
|
|
oi, err := getObjectInfoFn(ctx, bucket, object, opts)
|
2019-11-20 22:18:09 +01:00
|
|
|
if err != nil {
|
2020-04-06 22:44:16 +02:00
|
|
|
return toAPIErrorCode(ctx, err)
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
2020-01-17 00:41:56 +01:00
|
|
|
lhold := objectlock.GetObjectLegalHoldMeta(oi.UserDefined)
|
2020-04-06 22:44:16 +02:00
|
|
|
if lhold.Status.Valid() && lhold.Status == objectlock.LegalHoldOn {
|
|
|
|
return ErrObjectLocked
|
2019-11-25 19:58:39 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
|
|
|
ret := objectlock.GetObjectRetentionMeta(oi.UserDefined)
|
|
|
|
if ret.Mode.Valid() {
|
|
|
|
switch ret.Mode {
|
|
|
|
case objectlock.RetCompliance:
|
|
|
|
// In compliance mode, a protected object version can't be overwritten
|
|
|
|
// or deleted by any user, including the root user in your AWS account.
|
|
|
|
// When an object is locked in compliance mode, its retention mode can't
|
|
|
|
// be changed, and its retention period can't be shortened. Compliance mode
|
|
|
|
// ensures that an object version can't be overwritten or deleted for the
|
|
|
|
// duration of the retention period.
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ret.RetainUntilDate.Before(t) {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
case objectlock.RetGovernance:
|
|
|
|
// In governance mode, users can't overwrite or delete an object
|
|
|
|
// version or alter its lock settings unless they have special
|
|
|
|
// permissions. With governance mode, you protect objects against
|
|
|
|
// being deleted by most users, but you can still grant some users
|
|
|
|
// permission to alter the retention settings or delete the object
|
|
|
|
// if necessary. You can also use governance mode to test retention-period
|
|
|
|
// settings before creating a compliance-mode retention period.
|
|
|
|
// To override or remove governance-mode retention settings, a
|
|
|
|
// user must have the s3:BypassGovernanceRetention permission
|
|
|
|
// and must explicitly include x-amz-bypass-governance-retention:true
|
|
|
|
// as a request header with any request that requires overriding
|
|
|
|
// governance mode.
|
|
|
|
//
|
|
|
|
byPassSet := objectlock.IsObjectLockGovernanceBypassSet(r.Header)
|
|
|
|
if !byPassSet {
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
|
|
|
|
if !ret.RetainUntilDate.Before(t) {
|
|
|
|
return ErrObjectLocked
|
|
|
|
}
|
|
|
|
return ErrNone
|
|
|
|
}
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock-overview.html#object-lock-retention-modes
|
|
|
|
// If you try to delete objects protected by governance mode and have s3:BypassGovernanceRetention
|
|
|
|
// or s3:GetBucketObjectLockConfiguration permissions, the operation will succeed.
|
|
|
|
govBypassPerms1 := checkRequestAuthType(ctx, r, policy.BypassGovernanceRetentionAction, bucket, object)
|
|
|
|
govBypassPerms2 := checkRequestAuthType(ctx, r, policy.GetBucketObjectLockConfigurationAction, bucket, object)
|
|
|
|
if govBypassPerms1 != ErrNone && govBypassPerms2 != ErrNone {
|
|
|
|
return ErrAccessDenied
|
|
|
|
}
|
|
|
|
}
|
2019-11-25 19:58:39 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
return ErrNone
|
2019-11-25 19:58:39 +01:00
|
|
|
}
|
|
|
|
|
|
|
|
// enforceRetentionBypassForPut enforces whether an existing object under governance can be overwritten
|
|
|
|
// with governance bypass headers set in the request.
|
|
|
|
// Objects under site wide WORM cannot be overwritten.
|
|
|
|
// For objects in "Governance" mode, overwrite is allowed if a) object retention date is past OR
|
|
|
|
// governance bypass headers are set and user has governance bypass permissions.
|
|
|
|
// Objects in compliance mode can be overwritten only if retention date is being extended. No mode change is permitted.
|
2020-04-06 22:44:16 +02:00
|
|
|
func enforceRetentionBypassForPut(ctx context.Context, r *http.Request, bucket, object string, getObjectInfoFn GetObjectInfoFn, objRetention *objectlock.ObjectRetention, cred auth.Credentials, owner bool, claims map[string]interface{}) (ObjectInfo, APIErrorCode) {
|
|
|
|
byPassSet := objectlock.IsObjectLockGovernanceBypassSet(r.Header)
|
|
|
|
opts, err := getOpts(ctx, r, bucket, object)
|
2019-11-25 19:58:39 +01:00
|
|
|
if err != nil {
|
2020-04-06 22:44:16 +02:00
|
|
|
return ObjectInfo{}, toAPIErrorCode(ctx, err)
|
2019-11-25 19:58:39 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
|
|
|
oi, err := getObjectInfoFn(ctx, bucket, object, opts)
|
2019-11-25 19:58:39 +01:00
|
|
|
if err != nil {
|
|
|
|
return oi, toAPIErrorCode(ctx, err)
|
|
|
|
}
|
|
|
|
|
2020-01-17 00:41:56 +01:00
|
|
|
t, err := objectlock.UTCNowNTP()
|
2019-11-25 19:58:39 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2019-11-20 22:18:09 +01:00
|
|
|
return oi, ErrObjectLocked
|
|
|
|
}
|
2019-11-25 19:58:39 +01:00
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
// Pass in relative days from current time, to additionally to verify "object-lock-remaining-retention-days" policy if any.
|
|
|
|
days := int(math.Ceil(math.Abs(objRetention.RetainUntilDate.Sub(t).Hours()) / 24))
|
|
|
|
|
|
|
|
ret := objectlock.GetObjectRetentionMeta(oi.UserDefined)
|
|
|
|
if ret.Mode.Valid() {
|
|
|
|
// Retention has expired you may change whatever you like.
|
|
|
|
if ret.RetainUntilDate.Before(t) {
|
|
|
|
perm := isPutRetentionAllowed(bucket, object,
|
|
|
|
days, objRetention.RetainUntilDate.Time,
|
|
|
|
objRetention.Mode, byPassSet, r, cred,
|
|
|
|
owner, claims)
|
|
|
|
return oi, perm
|
2019-11-25 19:58:39 +01:00
|
|
|
}
|
|
|
|
|
2020-04-06 22:44:16 +02:00
|
|
|
switch ret.Mode {
|
|
|
|
case objectlock.RetGovernance:
|
|
|
|
govPerm := isPutRetentionAllowed(bucket, object, days,
|
|
|
|
objRetention.RetainUntilDate.Time, objRetention.Mode,
|
|
|
|
byPassSet, r, cred, owner, claims)
|
|
|
|
// Governance mode retention period cannot be shortened, if x-amz-bypass-governance is not set.
|
|
|
|
if !byPassSet {
|
|
|
|
if objRetention.Mode != objectlock.RetGovernance || objRetention.RetainUntilDate.Before((ret.RetainUntilDate.Time)) {
|
|
|
|
return oi, ErrObjectLocked
|
|
|
|
}
|
2019-11-21 13:52:35 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
return oi, govPerm
|
|
|
|
case objectlock.RetCompliance:
|
|
|
|
// Compliance retention mode cannot be changed or shortened.
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock-overview.html#object-lock-retention-modes
|
|
|
|
if objRetention.Mode != objectlock.RetCompliance || objRetention.RetainUntilDate.Before((ret.RetainUntilDate.Time)) {
|
2019-11-20 22:18:09 +01:00
|
|
|
return oi, ErrObjectLocked
|
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
compliancePerm := isPutRetentionAllowed(bucket, object,
|
|
|
|
days, objRetention.RetainUntilDate.Time, objRetention.Mode,
|
|
|
|
false, r, cred, owner, claims)
|
|
|
|
return oi, compliancePerm
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
return oi, ErrNone
|
|
|
|
} // No pre-existing retention metadata present.
|
|
|
|
|
|
|
|
perm := isPutRetentionAllowed(bucket, object,
|
|
|
|
days, objRetention.RetainUntilDate.Time,
|
|
|
|
objRetention.Mode, byPassSet, r, cred, owner, claims)
|
|
|
|
return oi, perm
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
|
|
|
|
2020-01-17 00:41:56 +01:00
|
|
|
// checkPutObjectLockAllowed enforces object retention policy and legal hold policy
|
|
|
|
// for requests with WORM headers
|
2019-11-20 22:18:09 +01:00
|
|
|
// See https://docs.aws.amazon.com/AmazonS3/latest/dev/object-lock-managing.html for the spec.
|
|
|
|
// For non-existing objects with object retention headers set, this method returns ErrNone if bucket has
|
|
|
|
// locking enabled and user has requisite permissions (s3:PutObjectRetention)
|
2019-11-25 19:58:39 +01:00
|
|
|
// If object exists on object store and site wide WORM enabled - this method
|
2019-11-20 22:18:09 +01:00
|
|
|
// returns an error. For objects in "Governance" mode, overwrite is allowed if the retention date has expired.
|
2019-11-25 19:58:39 +01:00
|
|
|
// For objects in "Compliance" mode, retention date cannot be shortened, and mode cannot be altered.
|
2020-01-17 00:41:56 +01:00
|
|
|
// For objects with legal hold header set, the s3:PutObjectLegalHold permission is expected to be set
|
|
|
|
// Both legal hold and retention can be applied independently on an object
|
2020-04-06 22:44:16 +02:00
|
|
|
func checkPutObjectLockAllowed(ctx context.Context, r *http.Request, bucket, object string, getObjectInfoFn GetObjectInfoFn, retentionPermErr, legalHoldPermErr APIErrorCode) (objectlock.RetMode, objectlock.RetentionDate, objectlock.ObjectLegalHold, APIErrorCode) {
|
|
|
|
var mode objectlock.RetMode
|
2020-01-17 00:41:56 +01:00
|
|
|
var retainDate objectlock.RetentionDate
|
|
|
|
var legalHold objectlock.ObjectLegalHold
|
2019-11-25 19:58:39 +01:00
|
|
|
|
2020-01-17 00:41:56 +01:00
|
|
|
retentionRequested := objectlock.IsObjectLockRetentionRequested(r.Header)
|
|
|
|
legalHoldRequested := objectlock.IsObjectLockLegalHoldRequested(r.Header)
|
2019-11-20 22:18:09 +01:00
|
|
|
|
2020-05-08 22:44:44 +02:00
|
|
|
retentionCfg, isWORMBucket := globalBucketObjectLockSys.Get(bucket)
|
2020-04-06 22:44:16 +02:00
|
|
|
if !isWORMBucket {
|
|
|
|
if legalHoldRequested || retentionRequested {
|
|
|
|
return mode, retainDate, legalHold, ErrInvalidBucketObjectLockConfiguration
|
|
|
|
}
|
|
|
|
// If this not a WORM enabled bucket, we should return right here.
|
|
|
|
return mode, retainDate, legalHold, ErrNone
|
|
|
|
}
|
|
|
|
|
2019-11-20 22:18:09 +01:00
|
|
|
var objExists bool
|
|
|
|
opts, err := getOpts(ctx, r, bucket, object)
|
|
|
|
if err != nil {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, toAPIErrorCode(ctx, err)
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-03-31 18:15:42 +02:00
|
|
|
|
|
|
|
t, err := objectlock.UTCNowNTP()
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
2019-11-20 22:18:09 +01:00
|
|
|
if objInfo, err := getObjectInfoFn(ctx, bucket, object, opts); err == nil {
|
|
|
|
objExists = true
|
2020-01-17 00:41:56 +01:00
|
|
|
r := objectlock.GetObjectRetentionMeta(objInfo.UserDefined)
|
2020-04-25 01:37:05 +02:00
|
|
|
if r.Mode == objectlock.RetCompliance && r.RetainUntilDate.After(t) {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
|
|
|
mode = r.Mode
|
|
|
|
retainDate = r.RetainUntilDate
|
2020-01-17 00:41:56 +01:00
|
|
|
legalHold = objectlock.GetObjectLegalHoldMeta(objInfo.UserDefined)
|
|
|
|
// Disallow overwriting an object on legal hold
|
2020-04-06 22:44:16 +02:00
|
|
|
if legalHold.Status == objectlock.LegalHoldOn {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
|
|
|
}
|
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
2020-01-17 00:41:56 +01:00
|
|
|
if legalHoldRequested {
|
|
|
|
var lerr error
|
|
|
|
if legalHold, lerr = objectlock.ParseObjectLockLegalHoldHeaders(r.Header); lerr != nil {
|
|
|
|
return mode, retainDate, legalHold, toAPIErrorCode(ctx, err)
|
|
|
|
}
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
|
2019-11-20 22:18:09 +01:00
|
|
|
if retentionRequested {
|
2020-01-17 00:41:56 +01:00
|
|
|
legalHold, err := objectlock.ParseObjectLockLegalHoldHeaders(r.Header)
|
2019-11-20 22:18:09 +01:00
|
|
|
if err != nil {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, toAPIErrorCode(ctx, err)
|
|
|
|
}
|
|
|
|
rMode, rDate, err := objectlock.ParseObjectLockRetentionHeaders(r.Header)
|
|
|
|
if err != nil {
|
|
|
|
return mode, retainDate, legalHold, toAPIErrorCode(ctx, err)
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2019-11-21 13:52:35 +01:00
|
|
|
if objExists && retainDate.After(t) {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
|
|
|
if retentionPermErr != ErrNone {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, retentionPermErr
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-01-17 00:41:56 +01:00
|
|
|
return rMode, rDate, legalHold, ErrNone
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2019-11-25 19:58:39 +01:00
|
|
|
|
|
|
|
if !retentionRequested && isWORMBucket {
|
2019-11-20 22:18:09 +01:00
|
|
|
if retentionPermErr != ErrNone {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, retentionPermErr
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-01-17 00:41:56 +01:00
|
|
|
t, err := objectlock.UTCNowNTP()
|
2019-11-21 13:52:35 +01:00
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
2019-11-21 13:52:35 +01:00
|
|
|
}
|
2019-11-20 22:18:09 +01:00
|
|
|
// AWS S3 just creates a new version of object when an object is being overwritten.
|
2019-11-21 13:52:35 +01:00
|
|
|
if objExists && retainDate.After(t) {
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrObjectLocked
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-03-31 18:15:42 +02:00
|
|
|
if !legalHoldRequested && !retentionCfg.IsEmpty() {
|
2020-01-17 00:41:56 +01:00
|
|
|
// inherit retention from bucket configuration
|
2020-03-31 18:15:42 +02:00
|
|
|
return retentionCfg.Mode, objectlock.RetentionDate{Time: t.Add(retentionCfg.Validity)}, legalHold, ErrNone
|
2020-01-06 19:15:45 +01:00
|
|
|
}
|
2020-04-06 22:44:16 +02:00
|
|
|
return "", objectlock.RetentionDate{}, legalHold, ErrNone
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-01-17 00:41:56 +01:00
|
|
|
return mode, retainDate, legalHold, ErrNone
|
2019-11-20 22:18:09 +01:00
|
|
|
}
|
2020-02-24 02:09:11 +01:00
|
|
|
|
2020-05-08 22:44:44 +02:00
|
|
|
func readBucketObjectLockConfig(ctx context.Context, objAPI ObjectLayer, bucket string) (*objectlock.Config, error) {
|
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, bucket)
|
|
|
|
if err != nil && err != errMetaDataConverted {
|
|
|
|
return nil, toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
if !meta.LockEnabled {
|
|
|
|
return nil, BucketObjectLockConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
configFile := path.Join(bucketConfigPrefix, bucket, objectLockConfig)
|
|
|
|
configData, err := readConfig(ctx, objAPI, configFile)
|
|
|
|
if err != nil {
|
|
|
|
if err != errConfigNotFound {
|
|
|
|
return nil, toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
return objectlock.NewObjectLockConfig(), nil
|
|
|
|
}
|
|
|
|
cfg, err := objectlock.ParseObjectLockConfig(bytes.NewReader(configData))
|
|
|
|
if err != nil {
|
|
|
|
return nil, toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
return cfg, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
func saveBucketObjectLockConfig(ctx context.Context, objAPI ObjectLayer, bucket string, config *objectlock.Config) error {
|
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, bucket)
|
|
|
|
if err != nil && err != errMetaDataConverted {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
if !meta.LockEnabled {
|
|
|
|
return BucketObjectLockConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
|
|
|
|
configFile := path.Join(bucketConfigPrefix, bucket, objectLockConfig)
|
|
|
|
if err = saveConfig(ctx, objAPI, configFile, data); err != nil {
|
|
|
|
return toObjectErr(err, bucket)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// NewBucketObjectLockSys returns initialized BucketObjectLockSys
|
|
|
|
func NewBucketObjectLockSys() *BucketObjectLockSys {
|
|
|
|
return &BucketObjectLockSys{
|
|
|
|
retentionMap: make(map[string]*objectlock.Retention),
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initializes bucket object lock config system for all buckets
|
|
|
|
func (sys *BucketObjectLockSys) Init(buckets []BucketInfo, objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
|
|
|
// In gateway mode, we always fetch the bucket object lock configuration from the gateway backend.
|
|
|
|
// So, this is a no-op for gateway servers.
|
|
|
|
if globalIsGateway {
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Load BucketObjectLockSys once during boot.
|
|
|
|
return sys.load(buckets, objAPI)
|
|
|
|
}
|
|
|
|
|
|
|
|
func (sys *BucketObjectLockSys) load(buckets []BucketInfo, objAPI ObjectLayer) error {
|
2020-02-24 02:09:11 +01:00
|
|
|
for _, bucket := range buckets {
|
2020-04-09 18:30:02 +02:00
|
|
|
ctx := logger.SetReqInfo(GlobalContext, &logger.ReqInfo{BucketName: bucket.Name})
|
2020-05-01 18:53:14 +02:00
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, bucket.Name)
|
2020-02-24 02:09:11 +01:00
|
|
|
if err != nil {
|
2020-05-01 18:53:14 +02:00
|
|
|
if err != errMetaDataConverted {
|
|
|
|
return err
|
2020-02-24 02:09:11 +01:00
|
|
|
}
|
|
|
|
|
2020-05-01 18:53:14 +02:00
|
|
|
meta.Created = bucket.Created
|
|
|
|
logger.LogIf(ctx, meta.save(ctx, objAPI))
|
|
|
|
}
|
|
|
|
if !meta.LockEnabled {
|
2020-02-24 02:09:11 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
2020-05-01 18:53:14 +02:00
|
|
|
configFile := path.Join(bucketConfigPrefix, bucket.Name, objectLockConfig)
|
2020-02-24 02:09:11 +01:00
|
|
|
configData, err := readConfig(ctx, objAPI, configFile)
|
|
|
|
if err != nil {
|
2020-04-01 09:04:25 +02:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2020-05-08 22:44:44 +02:00
|
|
|
globalBucketObjectLockSys.Set(bucket.Name, &objectlock.Retention{})
|
2020-02-24 02:09:11 +01:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := objectlock.ParseObjectLockConfig(bytes.NewReader(configData))
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
2020-05-04 18:42:58 +02:00
|
|
|
retention := &objectlock.Retention{}
|
2020-02-24 02:09:11 +01:00
|
|
|
if config.Rule != nil {
|
|
|
|
retention = config.ToRetention()
|
|
|
|
}
|
2020-05-08 22:44:44 +02:00
|
|
|
globalBucketObjectLockSys.Set(bucket.Name, retention)
|
2020-02-24 02:09:11 +01:00
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|