2020-07-21 20:49:56 -04:00
|
|
|
/*
|
|
|
|
* MinIO Cloud Storage, (C) 2020 MinIO, Inc.
|
|
|
|
*
|
|
|
|
* 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 (
|
|
|
|
"encoding/json"
|
|
|
|
"io"
|
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
2021-04-03 12:03:42 -04:00
|
|
|
"path"
|
2020-07-21 20:49:56 -04:00
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/minio/cmd/logger"
|
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
|
|
|
"github.com/minio/minio/pkg/madmin"
|
|
|
|
)
|
|
|
|
|
|
|
|
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-03 12:03:42 -04:00
|
|
|
bucket := path.Clean(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
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err = parseBucketQuota(bucket, data); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if err = globalBucketMetadataSys.Update(bucket, bucketQuotaConfigFile, data); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// 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
|
|
|
|
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.GetBucketQuotaAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
2021-04-03 12:03:42 -04:00
|
|
|
bucket := path.Clean(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
|
|
|
|
}
|
|
|
|
|
|
|
|
config, err := globalBucketMetadataSys.GetQuotaConfig(bucket)
|
|
|
|
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-03 12:03:42 -04:00
|
|
|
bucket := path.Clean(vars["bucket"])
|
2020-11-24 22:09:05 -05:00
|
|
|
update := r.URL.Query().Get("update") == "true"
|
2020-07-21 20:49:56 -04:00
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
if !globalIsErasure {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2020-07-28 14:50:47 -04:00
|
|
|
objectAPI, _ := validateAdminUsersReq(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
|
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
|
2020-11-24 22:09:05 -05:00
|
|
|
if !update {
|
|
|
|
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
|
|
|
|
}
|
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 {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
|
|
|
tgtBytes, err := json.Marshal(&targets)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErrWithErr(ErrAdminConfigBadJSON, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
if err = globalBucketMetadataSys.Update(bucket, bucketTargetsFile, tgtBytes); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
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-03 12:03:42 -04:00
|
|
|
bucket := path.Clean(vars["bucket"])
|
2020-07-30 22:55:22 -04:00
|
|
|
arnType := vars["type"]
|
2020-11-12 15:12:09 -05:00
|
|
|
if !globalIsErasure {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2020-07-28 14:50:47 -04:00
|
|
|
objectAPI, _ := validateAdminUsersReq(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-03 12:03:42 -04:00
|
|
|
bucket := path.Clean(vars["bucket"])
|
2020-07-30 22:55:22 -04:00
|
|
|
arn := vars["arn"]
|
|
|
|
|
2020-11-12 15:12:09 -05:00
|
|
|
if !globalIsErasure {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-07-21 20:49:56 -04:00
|
|
|
// Get current object layer instance.
|
2020-07-30 22:55:22 -04:00
|
|
|
objectAPI, _ := validateAdminUsersReq(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 {
|
2020-07-30 22:55:22 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
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
|
|
|
|
}
|
2020-07-30 22:55:22 -04:00
|
|
|
if err = globalBucketMetadataSys.Update(bucket, bucketTargetsFile, tgtBytes); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
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
|
|
|
}
|