2020-05-01 00:55:54 +02: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 (
|
2020-05-19 22:53:54 +02:00
|
|
|
"errors"
|
2020-05-01 00:55:54 +02:00
|
|
|
"io/ioutil"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
|
|
|
"github.com/minio/minio/cmd/config"
|
2020-05-11 19:34:08 +02:00
|
|
|
"github.com/minio/minio/cmd/logger"
|
2020-05-01 00:55:54 +02:00
|
|
|
"github.com/minio/minio/pkg/env"
|
|
|
|
iampolicy "github.com/minio/minio/pkg/iam/policy"
|
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
bucketQuotaConfigFile = "quota.json"
|
|
|
|
)
|
|
|
|
|
|
|
|
// 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")
|
2020-05-11 19:34:08 +02:00
|
|
|
|
|
|
|
defer logger.AuditLog(w, r, "PutBucketQuotaConfig", mustGetClaimsFromToken(r))
|
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.SetBucketQuotaAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
// Turn off quota commands if data usage info is unavailable.
|
|
|
|
if env.Get(envDataUsageCrawlConf, config.EnableOn) == config.EnableOff {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrAdminBucketQuotaDisabled), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
data, err := ioutil.ReadAll(r.Body)
|
|
|
|
if err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrInvalidRequest), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
|
|
|
if _, err = parseBucketQuota(bucket, data); err != nil {
|
2020-05-01 00:55:54 +02:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL, guessIsBrowserReq(r))
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
|
|
|
if err = globalBucketMetadataSys.Update(bucket, bucketQuotaConfigFile, data); err != nil {
|
2020-05-01 00:55:54 +02:00
|
|
|
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")
|
|
|
|
|
2020-05-11 19:34:08 +02:00
|
|
|
defer logger.AuditLog(w, r, "GetBucketQuotaConfig", mustGetClaimsFromToken(r))
|
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
objectAPI, _ := validateAdminUsersReq(ctx, w, r, iampolicy.GetBucketQuotaAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
|
|
|
configData, err := globalBucketMetadataSys.GetConfig(bucket, bucketQuotaConfigFile)
|
2020-05-01 00:55:54 +02:00
|
|
|
if err != nil {
|
2020-05-19 22:53:54 +02:00
|
|
|
if errors.Is(err, errConfigNotFound) {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, BucketQuotaConfigNotFound{Bucket: bucket}), r.URL)
|
|
|
|
} else {
|
2020-05-01 00:55:54 +02:00
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
|
|
|
}
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
// Write success response.
|
|
|
|
writeSuccessResponseJSON(w, configData)
|
|
|
|
}
|
|
|
|
|
|
|
|
// RemoveBucketQuotaConfigHandler - removes Bucket quota configuration.
|
|
|
|
// ----------
|
|
|
|
// Removes quota configuration on the specified bucket.
|
|
|
|
func (a adminAPIHandlers) RemoveBucketQuotaConfigHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "RemoveBucketQuotaConfig")
|
2020-05-11 19:34:08 +02:00
|
|
|
|
|
|
|
defer logger.AuditLog(w, r, "RemoveBucketQuotaConfig", mustGetClaimsFromToken(r))
|
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
objectAPI, _ := validateAdminReq(ctx, w, r, iampolicy.SetBucketQuotaAdminAction)
|
|
|
|
if objectAPI == nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if _, err := objectAPI.GetBucketInfo(ctx, bucket); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
|
|
|
if err := globalBucketMetadataSys.Update(bucket, bucketQuotaConfigFile, nil); err != nil {
|
|
|
|
writeErrorResponseJSON(ctx, w, toAdminAPIErr(ctx, err), r.URL)
|
2020-05-01 00:55:54 +02:00
|
|
|
return
|
|
|
|
}
|
2020-05-19 22:53:54 +02:00
|
|
|
|
2020-05-01 00:55:54 +02:00
|
|
|
// Write success response.
|
|
|
|
writeSuccessNoContent(w)
|
|
|
|
}
|