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-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2022-06-18 09:55:39 -04:00
|
|
|
"bytes"
|
|
|
|
"encoding/base64"
|
2020-07-21 20:49:56 -04:00
|
|
|
"encoding/json"
|
2022-06-18 09:55:39 -04:00
|
|
|
"encoding/xml"
|
|
|
|
"errors"
|
|
|
|
"fmt"
|
2020-07-21 20:49:56 -04:00
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2022-06-18 09:55:39 -04:00
|
|
|
"strings"
|
|
|
|
"time"
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2021-05-11 05:02:32 -04:00
|
|
|
jsoniter "github.com/json-iterator/go"
|
2022-06-18 09:55:39 -04:00
|
|
|
"github.com/klauspost/compress/zip"
|
|
|
|
"github.com/minio/kes"
|
2021-05-06 11:52:02 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2022-06-18 09:55:39 -04:00
|
|
|
"github.com/minio/minio-go/v7/pkg/tags"
|
|
|
|
"github.com/minio/minio/internal/bucket/lifecycle"
|
|
|
|
objectlock "github.com/minio/minio/internal/bucket/object/lock"
|
|
|
|
"github.com/minio/minio/internal/bucket/versioning"
|
|
|
|
"github.com/minio/minio/internal/event"
|
|
|
|
"github.com/minio/minio/internal/kms"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2022-06-18 09:55:39 -04:00
|
|
|
"github.com/minio/pkg/bucket/policy"
|
2021-05-30 00:16:42 -04:00
|
|
|
iampolicy "github.com/minio/pkg/iam/policy"
|
2020-07-21 20:49:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2020-07-28 14:50:47 -04:00
|
|
|
bucketQuotaConfigFile = "quota.json"
|
|
|
|
bucketTargetsFile = "bucket-targets.json"
|
2020-07-21 20:49:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// PutBucketQuotaConfigHandler - PUT Bucket quota configuration.
|
|
|
|
// ----------
|
|
|
|
// Places a quota configuration on the specified bucket. The quota
|
|
|
|
// specified in the quota configuration will be applied by default
|
|
|
|
// to enforce total quota for the specified bucket.
|
|
|
|
func (a adminAPIHandlers) PutBucketQuotaConfigHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "PutBucketQuotaConfig")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.SetBucketQuotaAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
bucket := pathClean(vars["bucket"])
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:02:24 -05:00
|
|
|
quotaConfig, err := parseBucketQuota(bucket, data)
|
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-21 20:49:56 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-02-17 15:49:46 -05:00
|
|
|
if quotaConfig.Type == "fifo" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketQuotaConfigFile, data)
|
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-21 20:49:56 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-01-19 23:02:24 -05:00
|
|
|
bucketMeta := madmin.SRBucketMeta{
|
2022-06-28 21:09:20 -04:00
|
|
|
Type: madmin.SRBucketMetaTypeQuotaConfig,
|
|
|
|
Bucket: bucket,
|
|
|
|
Quota: data,
|
|
|
|
UpdatedAt: updatedAt,
|
2022-01-19 23:02:24 -05:00
|
|
|
}
|
|
|
|
if quotaConfig.Quota == 0 {
|
|
|
|
bucketMeta.Quota = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call site replication hook.
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, bucketMeta); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// Write success response.
|
|
|
|
writeSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketQuotaConfigHandler - gets bucket quota configuration
|
|
|
|
func (a adminAPIHandlers) GetBucketQuotaConfigHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetBucketQuotaConfig")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2021-08-09 21:14:38 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.GetBucketQuotaAdminAction)
|
2020-07-21 20:49:56 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
bucket := pathClean(vars["bucket"])
|
2021-04-03 12:03:42 -04:00
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-24 05:36:31 -04:00
|
|
|
config, _, err := globalBucketMetadataSys.GetQuotaConfig(ctx, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write success response.
|
|
|
|
writeSuccessResponseJSON(w, configData)
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// SetRemoteTargetHandler - sets a remote target for bucket
|
|
|
|
func (a adminAPIHandlers) SetRemoteTargetHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-28 14:50:47 -04:00
|
|
|
ctx := newContext(r, w, "SetBucketTarget")
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-07-21 20:49:56 -04:00
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
bucket := pathClean(vars["bucket"])
|
2021-08-08 01:43:01 -04:00
|
|
|
update := r.Form.Get("update") == "true"
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2022-05-30 13:58:37 -04:00
|
|
|
if globalIsGateway {
|
2020-11-12 15:12:09 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2021-08-09 21:14:38 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.SetBucketTargetAction)
|
2020-07-21 20:49:56 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if bucket exists.
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
cred, _, _, s3Err := validateAdminSignature(ctx, r, "")
|
|
|
|
if s3Err != ErrNone {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(s3Err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
password := cred.SecretKey
|
|
|
|
|
|
|
|
reqBytes, err := madmin.DecryptData(password, io.LimitReader(r.Body, r.ContentLength))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-28 14:50:47 -04:00
|
|
|
var target madmin.BucketTarget
|
2022-01-02 12:15:06 -05:00
|
|
|
json := jsoniter.ConfigCompatibleWithStandardLibrary
|
2020-07-21 20:49:56 -04:00
|
|
|
if err = json.Unmarshal(reqBytes, &target); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-11-25 14:24:50 -05:00
|
|
|
sameTarget, _ := isLocalHost(target.URL().Hostname(), target.URL().Port(), globalMinioPort)
|
2020-07-30 22:55:22 -04:00
|
|
|
if sameTarget && bucket == target.TargetBucket {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrBucketRemoteIdenticalToSource), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-11-24 22:09:05 -05:00
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
target.SourceBucket = bucket
|
2021-04-28 18:26:20 -04:00
|
|
|
var ops []madmin.TargetUpdateType
|
|
|
|
if update {
|
2021-08-08 01:43:01 -04:00
|
|
|
ops = madmin.GetTargetUpdateOps(r.Form)
|
2021-04-28 18:26:20 -04:00
|
|
|
} else {
|
2020-11-24 22:09:05 -05:00
|
|
|
target.Arn = globalBucketTargetSys.getRemoteARN(bucket, &target)
|
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
if target.Arn == "" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
2020-07-21 20:49:56 -04:00
|
|
|
return
|
|
|
|
}
|
2021-04-28 18:26:20 -04:00
|
|
|
if update {
|
|
|
|
// overlay the updates on existing target
|
|
|
|
tgt := globalBucketTargetSys.GetRemoteBucketTargetByArn(ctx, bucket, target.Arn)
|
|
|
|
if tgt.Empty() {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrRemoteTargetNotFoundError, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
for _, op := range ops {
|
|
|
|
switch op {
|
|
|
|
case madmin.CredentialsUpdateType:
|
|
|
|
tgt.Credentials = target.Credentials
|
|
|
|
tgt.TargetBucket = target.TargetBucket
|
|
|
|
tgt.Secure = target.Secure
|
|
|
|
tgt.Endpoint = target.Endpoint
|
|
|
|
case madmin.SyncUpdateType:
|
|
|
|
tgt.ReplicationSync = target.ReplicationSync
|
|
|
|
case madmin.ProxyUpdateType:
|
|
|
|
tgt.DisableProxy = target.DisableProxy
|
|
|
|
case madmin.PathUpdateType:
|
|
|
|
tgt.Path = target.Path
|
|
|
|
case madmin.BandwidthLimitUpdateType:
|
|
|
|
tgt.BandwidthLimit = target.BandwidthLimit
|
|
|
|
case madmin.HealthCheckDurationUpdateType:
|
|
|
|
tgt.HealthCheckDuration = target.HealthCheckDuration
|
|
|
|
}
|
|
|
|
}
|
|
|
|
target = tgt
|
|
|
|
}
|
2021-06-24 21:29:30 -04:00
|
|
|
|
|
|
|
// enforce minimum bandwidth limit as 100MBps
|
2021-06-27 13:11:13 -04:00
|
|
|
if target.BandwidthLimit > 0 && target.BandwidthLimit < 100*1000*1000 {
|
2021-06-24 21:29:30 -04:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrReplicationBandwidthLimitError, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-11-24 22:09:05 -05:00
|
|
|
if err = globalBucketTargetSys.SetTarget(ctx, bucket, &target, update); err != nil {
|
2021-03-26 21:58:13 -04:00
|
|
|
switch err.(type) {
|
|
|
|
case BucketRemoteConnectionErr:
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrReplicationRemoteConnectionError, err), r.URL)
|
|
|
|
default:
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
return
|
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
targets, err := globalBucketTargetSys.ListBucketTargets(ctx, bucket)
|
2020-07-30 22:55:22 -04:00
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-30 22:55:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tgtBytes, err := json.Marshal(&targets)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
if _, err = globalBucketMetadataSys.Update(ctx, bucket, bucketTargetsFile, tgtBytes); err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-30 22:55:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
data, err := json.Marshal(target.Arn)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
// Write success response.
|
2020-07-30 22:55:22 -04:00
|
|
|
writeSuccessResponseJSON(w, data)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// ListRemoteTargetsHandler - lists remote target(s) for a bucket or gets a target
|
2020-07-30 22:55:22 -04:00
|
|
|
// for a particular ARN type
|
2020-08-06 20:10:21 -04:00
|
|
|
func (a adminAPIHandlers) ListRemoteTargetsHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-30 22:55:22 -04:00
|
|
|
ctx := newContext(r, w, "ListBucketTargets")
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-07-21 20:49:56 -04:00
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
bucket := pathClean(vars["bucket"])
|
2020-07-30 22:55:22 -04:00
|
|
|
arnType := vars["type"]
|
2022-05-30 13:58:37 -04:00
|
|
|
|
|
|
|
if globalIsGateway {
|
2020-11-12 15:12:09 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2021-08-09 21:14:38 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.GetBucketTargetAction)
|
2020-07-21 20:49:56 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
if bucket != "" {
|
2020-09-23 13:37:54 -04:00
|
|
|
// Check if bucket exists.
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
if _, err := globalBucketMetadataSys.GetBucketTargetsConfig(bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
2020-07-30 22:55:22 -04:00
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
targets := globalBucketTargetSys.ListTargets(ctx, bucket, arnType)
|
2020-07-30 22:55:22 -04:00
|
|
|
data, err := json.Marshal(targets)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Write success response.
|
|
|
|
writeSuccessResponseJSON(w, data)
|
|
|
|
}
|
|
|
|
|
2020-08-06 20:10:21 -04:00
|
|
|
// RemoveRemoteTargetHandler - removes a remote target for bucket with specified ARN
|
|
|
|
func (a adminAPIHandlers) RemoveRemoteTargetHandler(w http.ResponseWriter, r *http.Request) {
|
2020-07-30 22:55:22 -04:00
|
|
|
ctx := newContext(r, w, "RemoveBucketTarget")
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-07-21 20:49:56 -04:00
|
|
|
vars := mux.Vars(r)
|
2021-04-15 19:32:13 -04:00
|
|
|
bucket := pathClean(vars["bucket"])
|
2020-07-30 22:55:22 -04:00
|
|
|
arn := vars["arn"]
|
|
|
|
|
2022-05-30 13:58:37 -04:00
|
|
|
if globalIsGateway {
|
2020-11-12 15:12:09 -05:00
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2021-08-09 21:14:38 -04:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.SetBucketTargetAction)
|
2020-07-21 20:49:56 -04:00
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
|
|
|
|
// Check if bucket exists.
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-30 22:55:22 -04:00
|
|
|
if err := globalBucketTargetSys.RemoveTarget(ctx, bucket, arn); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-08-06 20:10:21 -04:00
|
|
|
targets, err := globalBucketTargetSys.ListBucketTargets(ctx, bucket)
|
2020-07-21 20:49:56 -04:00
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-30 22:55:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
tgtBytes, err := json.Marshal(&targets)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
2020-07-21 20:49:56 -04:00
|
|
|
return
|
|
|
|
}
|
2022-06-28 21:09:20 -04:00
|
|
|
if _, err = globalBucketMetadataSys.Update(ctx, bucket, bucketTargetsFile, tgtBytes); err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-07-30 22:55:22 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// Write success response.
|
2020-07-30 22:55:22 -04:00
|
|
|
writeSuccessNoContent(w)
|
2020-07-21 20:49:56 -04:00
|
|
|
}
|
2022-06-18 09:55:39 -04:00
|
|
|
|
|
|
|
// ExportBucketMetadataHandler - exports all bucket metadata as a zipped file
|
|
|
|
func (a adminAPIHandlers) ExportBucketMetadataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ExportBucketMetadata")
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
|
|
|
|
bucket := pathClean(r.Form.Get("bucket"))
|
|
|
|
if !globalIsErasure {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.ExportBucketMetadataAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
var (
|
|
|
|
buckets []BucketInfo
|
|
|
|
err error
|
|
|
|
)
|
|
|
|
if bucket != "" {
|
|
|
|
// Check if bucket exists.
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
buckets = append(buckets, BucketInfo{Name: bucket})
|
|
|
|
} else {
|
|
|
|
buckets, err = objectAPI.ListBuckets(ctx)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Initialize a zip writer which will provide a zipped content
|
|
|
|
// of bucket metadata
|
|
|
|
zipWriter := zip.NewWriter(w)
|
|
|
|
defer zipWriter.Close()
|
|
|
|
rawDataFn := func(r io.Reader, filename string, sz int) error {
|
|
|
|
header, zerr := zip.FileInfoHeader(dummyFileInfo{
|
|
|
|
name: filename,
|
|
|
|
size: int64(sz),
|
|
|
|
mode: 0o600,
|
|
|
|
modTime: time.Now(),
|
|
|
|
isDir: false,
|
|
|
|
sys: nil,
|
|
|
|
})
|
|
|
|
if zerr != nil {
|
|
|
|
logger.LogIf(ctx, zerr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
header.Method = zip.Deflate
|
|
|
|
zwriter, zerr := zipWriter.CreateHeader(header)
|
|
|
|
if zerr != nil {
|
|
|
|
logger.LogIf(ctx, zerr)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
if _, err := io.Copy(zwriter, r); err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
|
|
|
}
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
cfgFiles := []string{
|
|
|
|
bucketPolicyConfig,
|
|
|
|
bucketNotificationConfig,
|
|
|
|
bucketLifecycleConfig,
|
|
|
|
bucketSSEConfig,
|
|
|
|
bucketTaggingConfig,
|
|
|
|
bucketQuotaConfigFile,
|
|
|
|
objectLockConfig,
|
|
|
|
bucketVersioningConfig,
|
|
|
|
bucketReplicationConfig,
|
|
|
|
bucketTargetsFile,
|
|
|
|
}
|
|
|
|
for _, bi := range buckets {
|
|
|
|
for _, cfgFile := range cfgFiles {
|
|
|
|
cfgPath := pathJoin(bi.Name, cfgFile)
|
|
|
|
bucket := bi.Name
|
|
|
|
switch cfgFile {
|
|
|
|
case bucketNotificationConfig:
|
|
|
|
config, err := globalBucketMetadataSys.GetNotificationConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
logger.LogIf(ctx, err)
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketLifecycleConfig:
|
|
|
|
config, err := globalBucketMetadataSys.GetLifecycleConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketLifecycleNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
logger.LogIf(ctx, err)
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketQuotaConfigFile:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetQuotaConfig(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketQuotaConfigNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := json.Marshal(config)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketSSEConfig:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetSSEConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketSSEConfigNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketTaggingConfig:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetTaggingConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketTaggingNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case objectLockConfig:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetObjectLockConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketObjectLockConfigNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketVersioningConfig:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetVersioningConfig(bucket)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// ignore empty versioning configs
|
|
|
|
if config.Status != versioning.Enabled && config.Status != versioning.Suspended {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketReplicationConfig:
|
|
|
|
config, _, err := globalBucketMetadataSys.GetReplicationConfig(ctx, bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketReplicationConfigNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketTargetsFile:
|
|
|
|
config, err := globalBucketMetadataSys.GetBucketTargetsConfig(bucket)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, BucketRemoteTargetNotFound{Bucket: bucket}) {
|
|
|
|
continue
|
|
|
|
}
|
|
|
|
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = rawDataFn(bytes.NewReader(configData), cfgPath, len(configData)); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, exportError(ctx, err, cfgFile, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// ImportBucketMetadataHandler - imports all bucket metadata from a zipped file and overwrite bucket metadata config
|
|
|
|
// There are some caveats regarding the following:
|
|
|
|
// 1. object lock config - object lock should have been specified at time of bucket creation. Only default retention settings are imported here.
|
|
|
|
// 2. Replication config - is omitted from import as remote target credentials are not available from exported data for security reasons.
|
|
|
|
// 3. lifecycle config - if transition rules are present, tier name needs to have been defined.
|
|
|
|
func (a adminAPIHandlers) ImportBucketMetadataHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "ImportBucketMetadata")
|
|
|
|
|
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
|
|
|
bucket := pathClean(r.Form.Get("bucket"))
|
|
|
|
|
|
|
|
if !globalIsErasure {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
// Get current object layer instance.
|
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.ImportBucketMetadataAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
reader := bytes.NewReader(data)
|
|
|
|
zr, err := zip.NewReader(reader, int64(len(data)))
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
bucketMap := make(map[string]struct{}, 1)
|
|
|
|
|
|
|
|
// import object lock config if any - order of import matters here.
|
|
|
|
for _, file := range zr.File {
|
|
|
|
slc := strings.Split(file.Name, slashSeparator)
|
|
|
|
if len(slc) != 2 { // expecting bucket/configfile in the zipfile
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, fileName := slc[0], slc[1]
|
|
|
|
if bucket == "" { // use bucket requested in query parameters if specified. Otherwise default bucket name to directory name within zip
|
|
|
|
bucket = b
|
|
|
|
}
|
|
|
|
switch fileName {
|
|
|
|
case objectLockConfig:
|
|
|
|
reader, err := file.Open()
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
config, err := objectlock.ParseObjectLockConfig(reader)
|
|
|
|
if err != nil {
|
|
|
|
apiErr := errorCodes.ToAPIErr(ErrMalformedXML)
|
|
|
|
apiErr.Description = err.Error()
|
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := bucketMap[bucket]; !ok {
|
|
|
|
opts := BucketOptions{
|
|
|
|
LockEnabled: config.ObjectLockEnabled == "Enabled",
|
|
|
|
}
|
|
|
|
err = objectAPI.MakeBucketWithLocation(ctx, bucket, opts)
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BucketExists); !ok {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bucketMap[bucket] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
// Deny object locking configuration settings on existing buckets without object lock enabled.
|
|
|
|
if _, _, err = globalBucketMetadataSys.GetObjectLockConfig(bucket); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, objectLockConfig, configData)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call site replication hook.
|
|
|
|
//
|
|
|
|
// We encode the xml bytes as base64 to ensure there are no encoding
|
|
|
|
// errors.
|
|
|
|
cfgStr := base64.StdEncoding.EncodeToString(configData)
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, madmin.SRBucketMeta{
|
|
|
|
Type: madmin.SRBucketMetaTypeObjectLockConfig,
|
|
|
|
Bucket: bucket,
|
|
|
|
ObjectLockConfig: &cfgStr,
|
2022-06-28 21:09:20 -04:00
|
|
|
UpdatedAt: updatedAt,
|
2022-06-18 09:55:39 -04:00
|
|
|
}); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
// import versioning metadata
|
|
|
|
for _, file := range zr.File {
|
|
|
|
slc := strings.Split(file.Name, slashSeparator)
|
|
|
|
if len(slc) != 2 { // expecting bucket/configfile in the zipfile
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, fileName := slc[0], slc[1]
|
|
|
|
if bucket == "" { // use bucket requested in query parameters if specified. Otherwise default bucket name to directory name within zip
|
|
|
|
bucket = b
|
|
|
|
}
|
|
|
|
switch fileName {
|
|
|
|
case bucketVersioningConfig:
|
|
|
|
reader, err := file.Open()
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
v, err := versioning.ParseConfig(io.LimitReader(reader, maxBucketVersioningConfigSize))
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, ok := bucketMap[bucket]; !ok {
|
|
|
|
err = objectAPI.MakeBucketWithLocation(ctx, bucket, BucketOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BucketExists); !ok {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bucketMap[bucket] = struct{}{}
|
|
|
|
}
|
|
|
|
|
|
|
|
if globalSiteReplicationSys.isEnabled() && v.Suspended() {
|
|
|
|
writeErrorResponse(ctx, w, APIError{
|
|
|
|
Code: "InvalidBucketState",
|
|
|
|
Description: "Cluster replication is enabled for this site, so the versioning state cannot be changed.",
|
|
|
|
HTTPStatusCode: http.StatusConflict,
|
|
|
|
}, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if rcfg, _ := globalBucketObjectLockSys.Get(bucket); rcfg.LockEnabled && v.Suspended() {
|
|
|
|
writeErrorResponse(ctx, w, APIError{
|
|
|
|
Code: "InvalidBucketState",
|
|
|
|
Description: "An Object Lock configuration is present on this bucket, so the versioning state cannot be changed.",
|
|
|
|
HTTPStatusCode: http.StatusConflict,
|
|
|
|
}, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if _, err := getReplicationConfig(ctx, bucket); err == nil && v.Suspended() {
|
|
|
|
writeErrorResponse(ctx, w, APIError{
|
|
|
|
Code: "InvalidBucketState",
|
|
|
|
Description: "A replication configuration is present on this bucket, so the versioning state cannot be changed.",
|
|
|
|
HTTPStatusCode: http.StatusConflict,
|
|
|
|
}, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(v)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
if _, err = globalBucketMetadataSys.Update(ctx, bucket, bucketVersioningConfig, configData); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
for _, file := range zr.File {
|
|
|
|
reader, err := file.Open()
|
|
|
|
if err != nil {
|
2022-06-23 12:25:15 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, ""), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
sz := file.FileInfo().Size()
|
|
|
|
slc := strings.Split(file.Name, slashSeparator)
|
|
|
|
if len(slc) != 2 { // expecting bucket/configfile in the zipfile
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
b, fileName := slc[0], slc[1]
|
|
|
|
if bucket == "" { // use bucket requested in query parameters if specified. Otherwise default bucket name to directory name within zip
|
|
|
|
bucket = b
|
|
|
|
}
|
|
|
|
// create bucket if it does not exist yet.
|
|
|
|
if _, ok := bucketMap[bucket]; !ok {
|
|
|
|
err = objectAPI.MakeBucketWithLocation(ctx, bucket, BucketOptions{})
|
|
|
|
if err != nil {
|
|
|
|
if _, ok := err.(BucketExists); !ok {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
bucketMap[bucket] = struct{}{}
|
|
|
|
}
|
|
|
|
switch fileName {
|
|
|
|
case bucketNotificationConfig:
|
|
|
|
config, err := event.ParseConfig(io.LimitReader(reader, sz), globalSite.Region, globalNotificationSys.targetList)
|
|
|
|
if err != nil {
|
|
|
|
apiErr := errorCodes.ToAPIErr(ErrMalformedXML)
|
|
|
|
if event.IsEventError(err) {
|
2022-06-20 19:13:45 -04:00
|
|
|
apiErr = importError(ctx, err, file.Name, bucket)
|
2022-06-18 09:55:39 -04:00
|
|
|
}
|
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(config)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
if _, err = globalBucketMetadataSys.Update(ctx, bucket, bucketNotificationConfig, configData); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
rulesMap := config.ToRulesMap()
|
|
|
|
globalNotificationSys.AddRulesMap(bucket, rulesMap)
|
|
|
|
case bucketPolicyConfig:
|
|
|
|
// Error out if Content-Length is beyond allowed size.
|
|
|
|
if sz > maxBucketPolicySize {
|
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrPolicyTooLarge), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketPolicyBytes, err := ioutil.ReadAll(io.LimitReader(reader, sz))
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketPolicy, err := policy.ParseConfig(bytes.NewReader(bucketPolicyBytes), bucket)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Version in policy must not be empty
|
|
|
|
if bucketPolicy.Version == "" {
|
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrMalformedPolicy), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := json.Marshal(bucketPolicy)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketPolicyConfig, configData)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Call site replication hook.
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, madmin.SRBucketMeta{
|
2022-06-28 21:09:20 -04:00
|
|
|
Type: madmin.SRBucketMetaTypePolicy,
|
|
|
|
Bucket: bucket,
|
|
|
|
Policy: bucketPolicyBytes,
|
|
|
|
UpdatedAt: updatedAt,
|
2022-06-18 09:55:39 -04:00
|
|
|
}); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case bucketLifecycleConfig:
|
|
|
|
bucketLifecycle, err := lifecycle.ParseLifecycleConfig(io.LimitReader(reader, sz))
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the received bucket policy document
|
|
|
|
if err = bucketLifecycle.Validate(); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Validate the transition storage ARNs
|
|
|
|
if err = validateTransitionTier(bucketLifecycle); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(bucketLifecycle)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
if _, err = globalBucketMetadataSys.Update(ctx, bucket, bucketLifecycleConfig, configData); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketSSEConfig:
|
|
|
|
// Parse bucket encryption xml
|
|
|
|
encConfig, err := validateBucketSSEConfig(io.LimitReader(reader, maxBucketSSEConfigSize))
|
|
|
|
if err != nil {
|
|
|
|
apiErr := APIError{
|
|
|
|
Code: "MalformedXML",
|
|
|
|
Description: fmt.Sprintf("%s (%s)", errorCodes[ErrMalformedXML].Description, err),
|
|
|
|
HTTPStatusCode: errorCodes[ErrMalformedXML].HTTPStatusCode,
|
|
|
|
}
|
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error if KMS is not initialized
|
|
|
|
if GlobalKMS == nil {
|
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrKMSNotConfigured), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
kmsKey := encConfig.KeyID()
|
|
|
|
if kmsKey != "" {
|
|
|
|
kmsContext := kms.Context{"MinIO admin API": "ServerInfoHandler"} // Context for a test key operation
|
|
|
|
_, err := GlobalKMS.GenerateKey(kmsKey, kmsContext)
|
|
|
|
if err != nil {
|
|
|
|
if errors.Is(err, kes.ErrKeyNotFound) {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, errKMSKeyNotFound, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(encConfig)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Store the bucket encryption configuration in the object layer
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketSSEConfig, configData)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call site replication hook.
|
|
|
|
//
|
|
|
|
// We encode the xml bytes as base64 to ensure there are no encoding
|
|
|
|
// errors.
|
|
|
|
cfgStr := base64.StdEncoding.EncodeToString(configData)
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, madmin.SRBucketMeta{
|
|
|
|
Type: madmin.SRBucketMetaTypeSSEConfig,
|
|
|
|
Bucket: bucket,
|
|
|
|
SSEConfig: &cfgStr,
|
2022-06-28 21:09:20 -04:00
|
|
|
UpdatedAt: updatedAt,
|
2022-06-18 09:55:39 -04:00
|
|
|
}); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
case bucketTaggingConfig:
|
|
|
|
tags, err := tags.ParseBucketXML(io.LimitReader(reader, sz))
|
|
|
|
if err != nil {
|
|
|
|
apiErr := errorCodes.ToAPIErrWithErr(ErrMalformedXML, fmt.Errorf("error importing %s with %w", file.Name, err))
|
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(tags)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketTaggingConfig, configData)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
// Call site replication hook.
|
|
|
|
//
|
|
|
|
// We encode the xml bytes as base64 to ensure there are no encoding
|
|
|
|
// errors.
|
|
|
|
cfgStr := base64.StdEncoding.EncodeToString(configData)
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, madmin.SRBucketMeta{
|
2022-06-28 21:09:20 -04:00
|
|
|
Type: madmin.SRBucketMetaTypeTags,
|
|
|
|
Bucket: bucket,
|
|
|
|
Tags: &cfgStr,
|
|
|
|
UpdatedAt: updatedAt,
|
2022-06-18 09:55:39 -04:00
|
|
|
}); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
case bucketQuotaConfigFile:
|
|
|
|
data, err := ioutil.ReadAll(reader)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
quotaConfig, err := parseBucketQuota(bucket, data)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if quotaConfig.Type == "fifo" {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketQuotaConfigFile, data)
|
|
|
|
if err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
bucketMeta := madmin.SRBucketMeta{
|
2022-06-28 21:09:20 -04:00
|
|
|
Type: madmin.SRBucketMetaTypeQuotaConfig,
|
|
|
|
Bucket: bucket,
|
|
|
|
Quota: data,
|
|
|
|
UpdatedAt: updatedAt,
|
2022-06-18 09:55:39 -04:00
|
|
|
}
|
|
|
|
if quotaConfig.Quota == 0 {
|
|
|
|
bucketMeta.Quota = nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// Call site replication hook.
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, bucketMeta); err != nil {
|
2022-06-20 19:13:45 -04:00
|
|
|
writeErrorResponse(ctx, w, importError(ctx, err, file.Name, bucket), r.URL)
|
2022-06-18 09:55:39 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|
|
|
|
}
|