2019-10-08 01:47:56 -04:00
|
|
|
// MinIO Cloud Storage, (C) 2017-2019 MinIO, Inc.
|
2018-08-17 15:52:14 -04:00
|
|
|
//
|
|
|
|
// 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 crypto
|
|
|
|
|
2019-10-08 01:47:56 -04:00
|
|
|
import (
|
2020-08-31 21:10:52 -04:00
|
|
|
"math/rand"
|
|
|
|
"strings"
|
2019-10-08 01:47:56 -04:00
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
"github.com/minio/minio/cmd/config"
|
2020-08-31 21:10:52 -04:00
|
|
|
"github.com/minio/minio/pkg/ellipses"
|
2019-10-08 01:47:56 -04:00
|
|
|
"github.com/minio/minio/pkg/env"
|
2019-10-31 02:39:09 -04:00
|
|
|
xnet "github.com/minio/minio/pkg/net"
|
2019-10-08 01:47:56 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
const (
|
2019-10-23 01:59:13 -04:00
|
|
|
// EnvKMSAutoEncryption is the environment variable used to en/disable
|
2019-10-08 01:47:56 -04:00
|
|
|
// SSE-S3 auto-encryption. SSE-S3 auto-encryption, if enabled,
|
|
|
|
// requires a valid KMS configuration and turns any non-SSE-C
|
|
|
|
// request into an SSE-S3 request.
|
|
|
|
// If present EnvAutoEncryption must be either "on" or "off".
|
2019-10-23 01:59:13 -04:00
|
|
|
EnvKMSAutoEncryption = "MINIO_KMS_AUTO_ENCRYPTION"
|
2019-10-08 01:47:56 -04:00
|
|
|
)
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
// ParseKESEndpoints parses the given endpoint string and
|
|
|
|
// returns a list of valid endpoint URLs. The order of the
|
|
|
|
// returned endpoints is randomized.
|
|
|
|
func ParseKESEndpoints(endpointStr string) ([]string, error) {
|
|
|
|
var rawEndpoints []string
|
2020-08-31 21:10:52 -04:00
|
|
|
for _, endpoint := range strings.Split(endpointStr, ",") {
|
2020-08-31 22:27:49 -04:00
|
|
|
if strings.TrimSpace(endpoint) == "" {
|
|
|
|
continue
|
|
|
|
}
|
2020-08-31 21:10:52 -04:00
|
|
|
if !ellipses.HasEllipses(endpoint) {
|
2021-04-22 11:45:30 -04:00
|
|
|
rawEndpoints = append(rawEndpoints, endpoint)
|
2020-08-31 21:10:52 -04:00
|
|
|
continue
|
|
|
|
}
|
|
|
|
pattern, err := ellipses.FindEllipsesPatterns(endpoint)
|
2019-12-13 15:57:11 -05:00
|
|
|
if err != nil {
|
2021-04-22 11:45:30 -04:00
|
|
|
return nil, Errorf("Invalid KES endpoint %q: %v", endpointStr, err)
|
2019-12-13 15:57:11 -05:00
|
|
|
}
|
2020-08-31 21:10:52 -04:00
|
|
|
for _, p := range pattern {
|
2021-04-22 11:45:30 -04:00
|
|
|
rawEndpoints = append(rawEndpoints, p.Expand()...)
|
2020-08-31 21:10:52 -04:00
|
|
|
}
|
2019-12-13 15:57:11 -05:00
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
if len(rawEndpoints) == 0 {
|
|
|
|
return nil, Errorf("Invalid KES endpoint %q", endpointStr)
|
2020-08-31 22:27:49 -04:00
|
|
|
}
|
2019-12-13 15:57:11 -05:00
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
var (
|
|
|
|
randNum = rand.Intn(len(rawEndpoints))
|
|
|
|
endpoints = make([]string, len(rawEndpoints))
|
|
|
|
)
|
|
|
|
for i, endpoint := range rawEndpoints {
|
2020-08-31 21:10:52 -04:00
|
|
|
endpoint, err := xnet.ParseHTTPURL(endpoint)
|
|
|
|
if err != nil {
|
2021-04-22 11:45:30 -04:00
|
|
|
return nil, Errorf("Invalid KES endpoint %q: %v", endpointStr, err)
|
2019-12-13 15:57:11 -05:00
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
endpoints[(randNum+i)%len(rawEndpoints)] = endpoint.String()
|
2019-12-13 15:57:11 -05:00
|
|
|
}
|
2021-04-22 11:45:30 -04:00
|
|
|
return endpoints, nil
|
2019-12-13 15:57:11 -05:00
|
|
|
}
|
|
|
|
|
2021-04-22 11:45:30 -04:00
|
|
|
// LookupAutoEncryption returns true if and only if
|
|
|
|
// the MINIO_KMS_AUTO_ENCRYPTION env. variable is
|
|
|
|
// set to "on".
|
|
|
|
func LookupAutoEncryption() bool {
|
|
|
|
auto, _ := config.ParseBool(env.Get(EnvKMSAutoEncryption, config.EnableOff))
|
|
|
|
return auto
|
2019-10-08 01:47:56 -04:00
|
|
|
}
|