2021-04-18 15:41:13 -04:00
|
|
|
// Copyright (c) 2015-2021 MinIO, Inc.
|
|
|
|
//
|
|
|
|
// This file is part of MinIO Object Storage stack
|
|
|
|
//
|
|
|
|
// This program is free software: you can redistribute it and/or modify
|
|
|
|
// it under the terms of the GNU Affero General Public License as published by
|
|
|
|
// the Free Software Foundation, either version 3 of the License, or
|
|
|
|
// (at your option) any later version.
|
|
|
|
//
|
|
|
|
// This program is distributed in the hope that it will be useful
|
|
|
|
// but WITHOUT ANY WARRANTY; without even the implied warranty of
|
|
|
|
// MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
|
|
|
|
// GNU Affero General Public License for more details.
|
|
|
|
//
|
|
|
|
// You should have received a copy of the GNU Affero General Public License
|
|
|
|
// along with this program. If not, see <http://www.gnu.org/licenses/>.
|
2020-05-19 16:53:54 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
|
|
|
"bytes"
|
|
|
|
"context"
|
2020-05-20 13:18:15 -04:00
|
|
|
"errors"
|
2020-05-19 16:53:54 -04:00
|
|
|
"fmt"
|
|
|
|
"sync"
|
2022-04-24 05:36:31 -04:00
|
|
|
"time"
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2020-07-14 12:38:05 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
2021-06-01 17:59:40 -04:00
|
|
|
bucketsse "github.com/minio/minio/internal/bucket/encryption"
|
|
|
|
"github.com/minio/minio/internal/bucket/lifecycle"
|
|
|
|
objectlock "github.com/minio/minio/internal/bucket/object/lock"
|
|
|
|
"github.com/minio/minio/internal/bucket/replication"
|
|
|
|
"github.com/minio/minio/internal/bucket/versioning"
|
|
|
|
"github.com/minio/minio/internal/event"
|
|
|
|
"github.com/minio/minio/internal/kms"
|
|
|
|
"github.com/minio/minio/internal/logger"
|
|
|
|
"github.com/minio/minio/internal/sync/errgroup"
|
2021-05-30 00:16:42 -04:00
|
|
|
"github.com/minio/pkg/bucket/policy"
|
2020-05-19 16:53:54 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// BucketMetadataSys captures all bucket metadata for a given cluster.
|
|
|
|
type BucketMetadataSys struct {
|
|
|
|
sync.RWMutex
|
|
|
|
metadataMap map[string]BucketMetadata
|
|
|
|
}
|
|
|
|
|
|
|
|
// Remove bucket metadata from memory.
|
|
|
|
func (sys *BucketMetadataSys) Remove(bucket string) {
|
2020-06-03 16:18:54 -04:00
|
|
|
if globalIsGateway {
|
2020-05-19 16:53:54 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sys.Lock()
|
|
|
|
delete(sys.metadataMap, bucket)
|
2020-10-16 20:59:31 -04:00
|
|
|
globalBucketMonitor.DeleteBucket(bucket)
|
2020-05-19 16:53:54 -04:00
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
|
|
|
// Set - sets a new metadata in-memory.
|
|
|
|
// Only a shallow copy is saved and fields with references
|
|
|
|
// cannot be modified without causing a race condition,
|
|
|
|
// so they should be replaced atomically and not appended to, etc.
|
|
|
|
// Data is not persisted to disk.
|
|
|
|
func (sys *BucketMetadataSys) Set(bucket string, meta BucketMetadata) {
|
|
|
|
if globalIsGateway {
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket != minioMetaBucket {
|
|
|
|
sys.Lock()
|
|
|
|
sys.metadataMap[bucket] = meta
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Update update bucket metadata for the specified config file.
|
|
|
|
// The configData data should not be modified after being sent here.
|
2022-06-28 21:09:20 -04:00
|
|
|
func (sys *BucketMetadataSys) Update(ctx context.Context, bucket string, configFile string, configData []byte) (updatedAt time.Time, err error) {
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-05-19 16:53:54 -04:00
|
|
|
if objAPI == nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, errServerNotInitialized
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2021-09-20 20:41:01 -04:00
|
|
|
if globalIsGateway && globalGatewayName != NASBackendGateway {
|
|
|
|
if configFile == bucketPolicyConfig {
|
2020-06-03 16:18:54 -04:00
|
|
|
if configData == nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, objAPI.DeleteBucketPolicy(ctx, bucket)
|
2020-06-03 16:18:54 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
config, err := policy.ParseConfig(bytes.NewReader(configData), bucket)
|
|
|
|
if err != nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, objAPI.SetBucketPolicy(ctx, bucket, config)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, NotImplemented{}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if bucket == minioMetaBucket {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, errInvalidArgument
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2021-09-20 20:41:01 -04:00
|
|
|
if !globalIsErasure && !globalIsDistErasure && errors.Is(err, errVolumeNotFound) {
|
|
|
|
// Only single drive mode needs this fallback.
|
|
|
|
meta = newBucketMetadata(bucket)
|
|
|
|
} else {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, err
|
2021-09-20 20:41:01 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt = UTCNow()
|
2020-05-19 16:53:54 -04:00
|
|
|
switch configFile {
|
|
|
|
case bucketPolicyConfig:
|
|
|
|
meta.PolicyConfigJSON = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.PolicyConfigUpdatedAt = updatedAt
|
2020-05-19 16:53:54 -04:00
|
|
|
case bucketNotificationConfig:
|
2020-05-21 14:03:59 -04:00
|
|
|
meta.NotificationConfigXML = configData
|
2020-05-19 16:53:54 -04:00
|
|
|
case bucketLifecycleConfig:
|
|
|
|
meta.LifecycleConfigXML = configData
|
|
|
|
case bucketSSEConfig:
|
|
|
|
meta.EncryptionConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.EncryptionConfigUpdatedAt = updatedAt
|
2020-06-16 01:09:39 -04:00
|
|
|
case bucketTaggingConfig:
|
2020-05-19 16:53:54 -04:00
|
|
|
meta.TaggingConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.TaggingConfigUpdatedAt = updatedAt
|
2020-08-20 16:18:06 -04:00
|
|
|
case bucketQuotaConfigFile:
|
|
|
|
meta.QuotaConfigJSON = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.QuotaConfigUpdatedAt = updatedAt
|
2020-05-19 16:53:54 -04:00
|
|
|
case objectLockConfig:
|
2020-05-21 14:03:59 -04:00
|
|
|
meta.ObjectLockConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.ObjectLockConfigUpdatedAt = updatedAt
|
2020-06-12 23:04:01 -04:00
|
|
|
case bucketVersioningConfig:
|
|
|
|
meta.VersioningConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.VersioningConfigUpdatedAt = updatedAt
|
2020-07-21 20:49:56 -04:00
|
|
|
case bucketReplicationConfig:
|
|
|
|
meta.ReplicationConfigXML = configData
|
2022-06-28 21:09:20 -04:00
|
|
|
meta.ReplicationConfigUpdatedAt = updatedAt
|
2020-07-28 14:50:47 -04:00
|
|
|
case bucketTargetsFile:
|
2022-07-18 21:54:27 -04:00
|
|
|
meta.BucketTargetsConfigJSON, meta.BucketTargetsConfigMetaJSON, err = encryptBucketMetadata(ctx, meta.Name, configData, kms.Context{
|
2021-04-03 12:03:42 -04:00
|
|
|
bucket: meta.Name,
|
|
|
|
bucketTargetsFile: bucketTargetsFile,
|
|
|
|
})
|
2020-12-21 19:21:33 -05:00
|
|
|
if err != nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, fmt.Errorf("Error encrypting bucket target metadata %w", err)
|
2020-12-21 19:21:33 -05:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
default:
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, fmt.Errorf("Unknown bucket %s metadata update requested %s", bucket, configFile)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2022-01-31 20:27:43 -05:00
|
|
|
if err := meta.Save(ctx, objAPI); err != nil {
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
sys.Set(bucket, meta)
|
2022-01-31 20:27:43 -05:00
|
|
|
globalNotificationSys.LoadBucketMetadata(bgContext(ctx), bucket) // Do not use caller context here
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
return updatedAt, nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Get metadata for a bucket.
|
|
|
|
// If no metadata exists errConfigNotFound is returned and a new metadata is returned.
|
|
|
|
// Only a shallow copy is returned, so referenced data should not be modified,
|
|
|
|
// but can be replaced atomically.
|
2020-08-20 13:38:53 -04:00
|
|
|
//
|
|
|
|
// This function should only be used with
|
|
|
|
// - GetBucketInfo
|
|
|
|
// - ListBuckets
|
|
|
|
// For all other bucket specific metadata, use the relevant
|
|
|
|
// calls implemented specifically for each of those features.
|
2020-05-19 16:53:54 -04:00
|
|
|
func (sys *BucketMetadataSys) Get(bucket string) (BucketMetadata, error) {
|
|
|
|
if globalIsGateway || bucket == minioMetaBucket {
|
|
|
|
return newBucketMetadata(bucket), errConfigNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
sys.RLock()
|
|
|
|
defer sys.RUnlock()
|
|
|
|
|
|
|
|
meta, ok := sys.metadataMap[bucket]
|
|
|
|
if !ok {
|
|
|
|
return newBucketMetadata(bucket), errConfigNotFound
|
|
|
|
}
|
|
|
|
|
|
|
|
return meta, nil
|
|
|
|
}
|
|
|
|
|
2020-06-12 23:04:01 -04:00
|
|
|
// GetVersioningConfig returns configured versioning config
|
|
|
|
// The returned object may not be modified.
|
2022-05-31 05:57:57 -04:00
|
|
|
func (sys *BucketMetadataSys) GetVersioningConfig(bucket string) (*versioning.Versioning, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-06-12 23:04:01 -04:00
|
|
|
if err != nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return &versioning.Versioning{XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/"}, meta.Created, nil
|
|
|
|
}
|
|
|
|
return &versioning.Versioning{XMLNS: "http://s3.amazonaws.com/doc/2006-03-01/"}, time.Time{}, err
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return meta.versioningConfig, meta.VersioningConfigUpdatedAt, nil
|
2020-06-12 23:04:01 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetTaggingConfig returns configured tagging config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetTaggingConfig(bucket string) (*tags.Tags, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketTaggingNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
if meta.taggingConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketTaggingNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.taggingConfig, meta.TaggingConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetObjectLockConfig returns configured object lock config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetObjectLockConfig(bucket string) (*objectlock.Config, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketObjectLockConfigNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.objectLockConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketObjectLockConfigNotFound{Bucket: bucket}
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.objectLockConfig, meta.ObjectLockConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetLifecycleConfig returns configured lifecycle config
|
|
|
|
// The returned object may not be modified.
|
|
|
|
func (sys *BucketMetadataSys) GetLifecycleConfig(bucket string) (*lifecycle.Lifecycle, error) {
|
2021-06-08 23:11:27 -04:00
|
|
|
if globalIsGateway && globalGatewayName == NASBackendGateway {
|
|
|
|
// Only needed in case of NAS gateway.
|
|
|
|
objAPI := newObjectLayerFn()
|
|
|
|
if objAPI == nil {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if meta.lifecycleConfig == nil {
|
|
|
|
return nil, BucketLifecycleNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
return meta.lifecycleConfig, nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return nil, BucketLifecycleNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2020-05-20 13:18:15 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
if meta.lifecycleConfig == nil {
|
|
|
|
return nil, BucketLifecycleNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
return meta.lifecycleConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetNotificationConfig returns configured notification config
|
|
|
|
// The returned object may not be modified.
|
|
|
|
func (sys *BucketMetadataSys) GetNotificationConfig(bucket string) (*event.Config, error) {
|
2020-08-26 11:52:46 -04:00
|
|
|
if globalIsGateway && globalGatewayName == NASBackendGateway {
|
2020-06-03 16:18:54 -04:00
|
|
|
// Only needed in case of NAS gateway.
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-06-03 16:18:54 -04:00
|
|
|
if objAPI == nil {
|
|
|
|
return nil, errServerNotInitialized
|
|
|
|
}
|
|
|
|
meta, err := loadBucketMetadata(GlobalContext, objAPI, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta.notificationConfig, nil
|
|
|
|
}
|
|
|
|
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
return meta.notificationConfig, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetSSEConfig returns configured SSE config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetSSEConfig(bucket string) (*bucketsse.BucketSSEConfig, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketSSEConfigNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.sseConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketSSEConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
|
|
|
return meta.sseConfig, meta.EncryptionConfigUpdatedAt, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// CreatedAt returns the time of creation of bucket
|
|
|
|
func (sys *BucketMetadataSys) CreatedAt(bucket string) (time.Time, error) {
|
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
|
|
|
if err != nil {
|
|
|
|
return time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.Created.UTC(), nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetPolicyConfig returns configured bucket policy
|
|
|
|
// The returned object may not be modified.
|
2022-05-31 05:57:57 -04:00
|
|
|
func (sys *BucketMetadataSys) GetPolicyConfig(bucket string) (*policy.Policy, time.Time, error) {
|
2020-06-03 16:18:54 -04:00
|
|
|
if globalIsGateway {
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-06-03 16:18:54 -04:00
|
|
|
if objAPI == nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, errServerNotInitialized
|
2020-06-03 16:18:54 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
p, err := objAPI.GetBucketPolicy(GlobalContext, bucket)
|
|
|
|
return p, UTCNow(), err
|
2020-06-03 16:18:54 -04:00
|
|
|
}
|
|
|
|
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, BucketPolicyNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
|
|
|
if meta.policyConfig == nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
return nil, time.Time{}, BucketPolicyNotFound{Bucket: bucket}
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-05-31 05:57:57 -04:00
|
|
|
return meta.policyConfig, meta.PolicyConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
// GetQuotaConfig returns configured bucket quota
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetQuotaConfig(ctx context.Context, bucket string) (*madmin.BucketQuota, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(ctx, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2022-06-18 09:55:39 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return nil, time.Time{}, BucketQuotaConfigNotFound{Bucket: bucket}
|
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.quotaConfig, meta.QuotaConfigUpdatedAt, nil
|
2020-05-20 13:18:15 -04:00
|
|
|
}
|
2020-05-19 16:53:54 -04:00
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// GetReplicationConfig returns configured bucket replication config
|
|
|
|
// The returned object may not be modified.
|
2022-04-24 05:36:31 -04:00
|
|
|
func (sys *BucketMetadataSys) GetReplicationConfig(ctx context.Context, bucket string) (*replication.Config, time.Time, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(ctx, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, errConfigNotFound) {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketReplicationConfigNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, err
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
if meta.replicationConfig == nil {
|
2022-04-24 05:36:31 -04:00
|
|
|
return nil, time.Time{}, BucketReplicationConfigNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2022-04-24 05:36:31 -04:00
|
|
|
return meta.replicationConfig, meta.ReplicationConfigUpdatedAt, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
// GetBucketTargetsConfig returns configured bucket targets for this bucket
|
2020-07-21 20:49:56 -04:00
|
|
|
// The returned object may not be modified.
|
2020-07-30 22:55:22 -04:00
|
|
|
func (sys *BucketMetadataSys) GetBucketTargetsConfig(bucket string) (*madmin.BucketTargets, error) {
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := sys.GetConfig(GlobalContext, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
2022-05-31 05:57:57 -04:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
return nil, BucketRemoteTargetNotFound{Bucket: bucket}
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
return nil, err
|
|
|
|
}
|
2020-07-28 14:50:47 -04:00
|
|
|
if meta.bucketTargetConfig == nil {
|
2020-07-30 22:55:22 -04:00
|
|
|
return nil, BucketRemoteTargetNotFound{Bucket: bucket}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-07-28 14:50:47 -04:00
|
|
|
return meta.bucketTargetConfig, nil
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// GetConfig returns a specific configuration from the bucket metadata.
|
2020-05-20 13:18:15 -04:00
|
|
|
// The returned object may not be modified.
|
2022-01-31 20:27:43 -05:00
|
|
|
func (sys *BucketMetadataSys) GetConfig(ctx context.Context, bucket string) (BucketMetadata, error) {
|
2020-10-09 12:59:52 -04:00
|
|
|
objAPI := newObjectLayerFn()
|
2020-05-20 13:18:15 -04:00
|
|
|
if objAPI == nil {
|
|
|
|
return newBucketMetadata(bucket), errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2020-06-03 16:18:54 -04:00
|
|
|
if globalIsGateway {
|
2020-05-20 13:18:15 -04:00
|
|
|
return newBucketMetadata(bucket), NotImplemented{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if bucket == minioMetaBucket {
|
|
|
|
return newBucketMetadata(bucket), errInvalidArgument
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.RLock()
|
2020-05-20 13:18:15 -04:00
|
|
|
meta, ok := sys.metadataMap[bucket]
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.RUnlock()
|
2020-05-20 13:18:15 -04:00
|
|
|
if ok {
|
|
|
|
return meta, nil
|
|
|
|
}
|
2022-01-31 20:27:43 -05:00
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
|
|
|
return meta, err
|
|
|
|
}
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.Lock()
|
2020-05-20 13:18:15 -04:00
|
|
|
sys.metadataMap[bucket] = meta
|
2020-05-20 17:11:13 -04:00
|
|
|
sys.Unlock()
|
2021-06-08 23:11:27 -04:00
|
|
|
|
2020-05-20 13:18:15 -04:00
|
|
|
return meta, nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Init - initializes bucket metadata system for all buckets.
|
|
|
|
func (sys *BucketMetadataSys) Init(ctx context.Context, buckets []BucketInfo, objAPI ObjectLayer) error {
|
|
|
|
if objAPI == nil {
|
|
|
|
return errServerNotInitialized
|
|
|
|
}
|
|
|
|
|
2021-09-20 20:41:01 -04:00
|
|
|
// In gateway mode, we don't need to load bucket metadata except
|
|
|
|
// NAS gateway backend.
|
|
|
|
if globalIsGateway && !objAPI.IsNotificationSupported() {
|
2020-05-19 16:53:54 -04:00
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2020-08-20 13:38:53 -04:00
|
|
|
// Load bucket metadata sys in background
|
2020-10-22 16:36:24 -04:00
|
|
|
go sys.load(ctx, buckets, objAPI)
|
2020-08-20 13:38:53 -04:00
|
|
|
return nil
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
|
2020-06-02 01:32:53 -04:00
|
|
|
// concurrently load bucket metadata to speed up loading bucket metadata.
|
2020-10-22 16:36:24 -04:00
|
|
|
func (sys *BucketMetadataSys) concurrentLoad(ctx context.Context, buckets []BucketInfo, objAPI ObjectLayer) {
|
2020-06-02 01:32:53 -04:00
|
|
|
g := errgroup.WithNErrs(len(buckets))
|
|
|
|
for index := range buckets {
|
|
|
|
index := index
|
|
|
|
g.Go(func() error {
|
2020-12-14 15:07:07 -05:00
|
|
|
_, _ = objAPI.HealBucket(ctx, buckets[index].Name, madmin.HealOpts{
|
|
|
|
// Ensure heal opts for bucket metadata be deep healed all the time.
|
|
|
|
ScanMode: madmin.HealDeepScan,
|
2022-02-07 13:39:57 -05:00
|
|
|
Recreate: true,
|
2020-12-14 15:07:07 -05:00
|
|
|
})
|
2020-06-02 01:32:53 -04:00
|
|
|
meta, err := loadBucketMetadata(ctx, objAPI, buckets[index].Name)
|
|
|
|
if err != nil {
|
2021-09-20 20:41:01 -04:00
|
|
|
if !globalIsErasure && !globalIsDistErasure && errors.Is(err, errVolumeNotFound) {
|
|
|
|
meta = newBucketMetadata(buckets[index].Name)
|
|
|
|
} else {
|
|
|
|
return err
|
|
|
|
}
|
2020-06-02 01:32:53 -04:00
|
|
|
}
|
|
|
|
sys.Lock()
|
|
|
|
sys.metadataMap[buckets[index].Name] = meta
|
|
|
|
sys.Unlock()
|
2021-09-20 20:41:01 -04:00
|
|
|
|
2022-08-24 09:42:36 -04:00
|
|
|
globalEventNotifier.set(buckets[index], meta) // set notification targets
|
2021-09-20 20:41:01 -04:00
|
|
|
|
|
|
|
globalBucketTargetSys.set(buckets[index], meta) // set remote replication targets
|
|
|
|
|
2020-06-02 01:32:53 -04:00
|
|
|
return nil
|
|
|
|
}, index)
|
|
|
|
}
|
|
|
|
for _, err := range g.Wait() {
|
2020-05-19 16:53:54 -04:00
|
|
|
if err != nil {
|
2020-10-22 16:36:24 -04:00
|
|
|
logger.LogIf(ctx, err)
|
2020-05-19 16:53:54 -04:00
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2020-06-02 01:32:53 -04:00
|
|
|
// Loads bucket metadata for all buckets into BucketMetadataSys.
|
2020-10-22 16:36:24 -04:00
|
|
|
func (sys *BucketMetadataSys) load(ctx context.Context, buckets []BucketInfo, objAPI ObjectLayer) {
|
2020-06-02 01:32:53 -04:00
|
|
|
count := 100 // load 100 bucket metadata at a time.
|
|
|
|
for {
|
|
|
|
if len(buckets) < count {
|
2020-10-22 16:36:24 -04:00
|
|
|
sys.concurrentLoad(ctx, buckets, objAPI)
|
|
|
|
return
|
2020-06-02 01:32:53 -04:00
|
|
|
}
|
2020-10-22 16:36:24 -04:00
|
|
|
sys.concurrentLoad(ctx, buckets[:count], objAPI)
|
2020-06-02 01:32:53 -04:00
|
|
|
buckets = buckets[count:]
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
2021-01-05 13:45:26 -05:00
|
|
|
// Reset the state of the BucketMetadataSys.
|
|
|
|
func (sys *BucketMetadataSys) Reset() {
|
|
|
|
sys.Lock()
|
|
|
|
for k := range sys.metadataMap {
|
|
|
|
delete(sys.metadataMap, k)
|
|
|
|
}
|
|
|
|
sys.Unlock()
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// NewBucketMetadataSys - creates new policy system.
|
|
|
|
func NewBucketMetadataSys() *BucketMetadataSys {
|
|
|
|
return &BucketMetadataSys{
|
|
|
|
metadataMap: make(map[string]BucketMetadata),
|
|
|
|
}
|
|
|
|
}
|