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-02-05 04:42:34 -05:00
|
|
|
|
|
|
|
package cmd
|
|
|
|
|
|
|
|
import (
|
2021-10-06 19:36:31 -04:00
|
|
|
"encoding/base64"
|
2020-02-05 04:42:34 -05:00
|
|
|
"encoding/xml"
|
2022-02-17 14:36:14 -05:00
|
|
|
"errors"
|
2020-05-04 12:42:58 -04:00
|
|
|
"fmt"
|
2020-02-05 04:42:34 -05:00
|
|
|
"io"
|
|
|
|
"net/http"
|
|
|
|
|
|
|
|
"github.com/gorilla/mux"
|
2022-02-17 14:36:14 -05:00
|
|
|
"github.com/minio/kes"
|
2021-10-06 19:36:31 -04:00
|
|
|
"github.com/minio/madmin-go"
|
2022-02-17 14:36:14 -05:00
|
|
|
"github.com/minio/minio/internal/kms"
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/logger"
|
2021-05-30 00:16:42 -04:00
|
|
|
"github.com/minio/pkg/bucket/policy"
|
2020-02-05 04:42:34 -05:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
|
|
|
// Bucket Encryption configuration file name.
|
|
|
|
bucketSSEConfig = "bucket-encryption.xml"
|
|
|
|
)
|
|
|
|
|
|
|
|
// PutBucketEncryptionHandler - Stores given bucket encryption configuration
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_PutBucketEncryption.html
|
|
|
|
func (api objectAPIHandlers) PutBucketEncryptionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "PutBucketEncryption")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-02-05 04:42:34 -05:00
|
|
|
|
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:08:19 -04:00
|
|
|
if !objAPI.IsEncryptionSupported() {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrNotImplemented), r.URL)
|
2020-02-16 01:07:52 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-06-01 21:08:19 -04:00
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
2020-02-05 04:42:34 -05:00
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.PutBucketEncryptionAction, bucket, ""); s3Error != ErrNone {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if bucket exists.
|
|
|
|
if _, err := objAPI.GetBucketInfo(ctx, bucket); err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Parse bucket encryption xml
|
|
|
|
encConfig, err := validateBucketSSEConfig(io.LimitReader(r.Body, maxBucketSSEConfigSize))
|
|
|
|
if err != nil {
|
2020-05-04 12:42:58 -04:00
|
|
|
apiErr := APIError{
|
|
|
|
Code: "MalformedXML",
|
|
|
|
Description: fmt.Sprintf("%s (%s)", errorCodes[ErrMalformedXML].Description, err),
|
|
|
|
HTTPStatusCode: errorCodes[ErrMalformedXML].HTTPStatusCode,
|
|
|
|
}
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, apiErr, r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Return error if KMS is not initialized
|
|
|
|
if GlobalKMS == nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrKMSNotConfigured), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
2022-02-17 14:36:14 -05:00
|
|
|
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) {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, errKMSKeyNotFound), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
}
|
2020-02-05 04:42:34 -05:00
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
configData, err := xml.Marshal(encConfig)
|
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-05-19 16:53:54 -04:00
|
|
|
// 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 {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-05-19 16:53:54 -04:00
|
|
|
return
|
|
|
|
}
|
2020-02-05 04:42:34 -05:00
|
|
|
|
2021-10-06 19:36:31 -04:00
|
|
|
// 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,
|
2021-10-06 19:36:31 -04:00
|
|
|
}); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2020-02-05 04:42:34 -05:00
|
|
|
writeSuccessResponseHeadersOnly(w)
|
|
|
|
}
|
|
|
|
|
|
|
|
// GetBucketEncryptionHandler - Returns bucket policy configuration
|
|
|
|
// https://docs.aws.amazon.com/AmazonS3/latest/API/API_GetBucketEncryption.html
|
|
|
|
func (api objectAPIHandlers) GetBucketEncryptionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "GetBucketEncryption")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-02-05 04:42:34 -05:00
|
|
|
|
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.GetBucketEncryptionAction, bucket, ""); s3Error != ErrNone {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if bucket exists
|
|
|
|
var err error
|
|
|
|
if _, err = objAPI.GetBucketInfo(ctx, bucket); err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
2022-04-24 05:36:31 -04:00
|
|
|
config, _, err := globalBucketMetadataSys.GetSSEConfig(bucket)
|
2020-05-20 13:18:15 -04:00
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-05-20 13:18:15 -04:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
configData, err := xml.Marshal(config)
|
2020-05-19 16:53:54 -04:00
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Write bucket encryption configuration to client
|
2020-05-19 16:53:54 -04:00
|
|
|
writeSuccessResponseXML(w, configData)
|
2020-02-05 04:42:34 -05:00
|
|
|
}
|
|
|
|
|
|
|
|
// DeleteBucketEncryptionHandler - Removes bucket encryption configuration
|
|
|
|
func (api objectAPIHandlers) DeleteBucketEncryptionHandler(w http.ResponseWriter, r *http.Request) {
|
|
|
|
ctx := newContext(r, w, "DeleteBucketEncryption")
|
|
|
|
|
2021-01-26 16:21:51 -05:00
|
|
|
defer logger.AuditLog(ctx, w, r, mustGetClaimsFromToken(r))
|
2020-02-05 04:42:34 -05:00
|
|
|
|
|
|
|
objAPI := api.ObjectAPI()
|
|
|
|
if objAPI == nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(ErrServerNotInitialized), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
vars := mux.Vars(r)
|
|
|
|
bucket := vars["bucket"]
|
|
|
|
|
|
|
|
if s3Error := checkRequestAuthType(ctx, r, policy.PutBucketEncryptionAction, bucket, ""); s3Error != ErrNone {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, errorCodes.ToAPIErr(s3Error), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Check if bucket exists
|
|
|
|
var err error
|
|
|
|
if _, err = objAPI.GetBucketInfo(ctx, bucket); err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
|
|
|
|
|
|
|
// Delete bucket encryption config from object layer
|
2022-06-28 21:09:20 -04:00
|
|
|
updatedAt, err := globalBucketMetadataSys.Update(ctx, bucket, bucketSSEConfig, nil)
|
|
|
|
if err != nil {
|
2021-06-17 23:27:04 -04:00
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
2020-02-05 04:42:34 -05:00
|
|
|
return
|
|
|
|
}
|
2022-03-10 03:04:34 -05:00
|
|
|
// Call site replication hook.
|
|
|
|
//
|
|
|
|
if err = globalSiteReplicationSys.BucketMetaHook(ctx, madmin.SRBucketMeta{
|
|
|
|
Type: madmin.SRBucketMetaTypeSSEConfig,
|
|
|
|
Bucket: bucket,
|
|
|
|
SSEConfig: nil,
|
2022-06-28 21:09:20 -04:00
|
|
|
UpdatedAt: updatedAt,
|
2022-03-10 03:04:34 -05:00
|
|
|
}); err != nil {
|
|
|
|
writeErrorResponse(ctx, w, toAPIError(ctx, err), r.URL)
|
|
|
|
return
|
|
|
|
}
|
2020-02-05 04:42:34 -05:00
|
|
|
writeSuccessNoContent(w)
|
|
|
|
}
|