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/>.
|
2019-10-04 13:35:33 -04:00
|
|
|
|
|
|
|
package compress
|
|
|
|
|
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"strings"
|
|
|
|
|
2021-06-01 17:59:40 -04:00
|
|
|
"github.com/minio/minio/internal/config"
|
2021-05-28 18:17:01 -04:00
|
|
|
"github.com/minio/pkg/env"
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Config represents the compression settings.
|
|
|
|
type Config struct {
|
2021-01-05 23:08:35 -05:00
|
|
|
Enabled bool `json:"enabled"`
|
|
|
|
AllowEncrypted bool `json:"allow_encryption"`
|
|
|
|
Extensions []string `json:"extensions"`
|
|
|
|
MimeTypes []string `json:"mime-types"`
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
// Compression environment variables
|
|
|
|
const (
|
2021-01-05 23:08:35 -05:00
|
|
|
Extensions = "extensions"
|
|
|
|
AllowEncrypted = "allow_encryption"
|
|
|
|
MimeTypes = "mime_types"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
2022-07-17 11:43:14 -04:00
|
|
|
EnvCompressState = "MINIO_COMPRESSION_ENABLE"
|
|
|
|
EnvCompressAllowEncryption = "MINIO_COMPRESSION_ALLOW_ENCRYPTION"
|
|
|
|
EnvCompressExtensions = "MINIO_COMPRESSION_EXTENSIONS"
|
|
|
|
EnvCompressMimeTypes = "MINIO_COMPRESSION_MIME_TYPES"
|
2019-10-23 01:59:13 -04:00
|
|
|
|
|
|
|
// Include-list for compression.
|
|
|
|
DefaultExtensions = ".txt,.log,.csv,.json,.tar,.xml,.bin"
|
2021-02-10 11:56:37 -05:00
|
|
|
DefaultMimeTypes = "text/*,application/json,application/xml,binary/octet-stream"
|
2019-10-23 01:59:13 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// DefaultKVS - default KV config for compression settings
|
|
|
|
var (
|
|
|
|
DefaultKVS = config.KVS{
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
2019-12-04 18:32:37 -05:00
|
|
|
Key: config.Enable,
|
|
|
|
Value: config.EnableOff,
|
2019-11-20 18:10:24 -05:00
|
|
|
},
|
2021-01-05 23:08:35 -05:00
|
|
|
config.KV{
|
|
|
|
Key: AllowEncrypted,
|
|
|
|
Value: config.EnableOff,
|
|
|
|
},
|
2019-11-20 18:10:24 -05:00
|
|
|
config.KV{
|
|
|
|
Key: Extensions,
|
|
|
|
Value: DefaultExtensions,
|
|
|
|
},
|
|
|
|
config.KV{
|
|
|
|
Key: MimeTypes,
|
|
|
|
Value: DefaultMimeTypes,
|
|
|
|
},
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
2019-10-04 13:35:33 -04:00
|
|
|
)
|
|
|
|
|
|
|
|
// Parses the given compression exclude list `extensions` or `content-types`.
|
2019-10-31 02:39:09 -04:00
|
|
|
func parseCompressIncludes(include string) ([]string, error) {
|
|
|
|
includes := strings.Split(include, config.ValueSeparator)
|
2019-10-04 13:35:33 -04:00
|
|
|
for _, e := range includes {
|
|
|
|
if len(e) == 0 {
|
2019-10-23 01:59:13 -04:00
|
|
|
return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be empty")
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
2019-10-31 02:39:09 -04:00
|
|
|
if e == "/" {
|
|
|
|
return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be '/'")
|
|
|
|
}
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
return includes, nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LookupConfig - lookup compression config.
|
2019-10-23 01:59:13 -04:00
|
|
|
func LookupConfig(kvs config.KVS) (Config, error) {
|
|
|
|
var err error
|
|
|
|
cfg := Config{}
|
|
|
|
if err = config.CheckValidKeys(config.CompressionSubSys, kvs, DefaultKVS); err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2022-07-17 11:43:14 -04:00
|
|
|
compress := env.Get(EnvCompressState, kvs.Get(config.Enable))
|
2019-10-23 01:59:13 -04:00
|
|
|
if compress == "" {
|
2022-07-17 11:43:14 -04:00
|
|
|
compress = env.Get(EnvCompress, "")
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
|
|
|
cfg.Enabled, err = config.ParseBool(compress)
|
|
|
|
if err != nil {
|
2019-11-13 20:38:05 -05:00
|
|
|
// Parsing failures happen due to empty KVS, ignore it.
|
|
|
|
if kvs.Empty() {
|
|
|
|
return cfg, nil
|
|
|
|
}
|
2019-10-23 01:59:13 -04:00
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
if !cfg.Enabled {
|
|
|
|
return cfg, nil
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
|
2021-01-05 23:08:35 -05:00
|
|
|
allowEnc := env.Get(EnvCompressAllowEncryption, kvs.Get(AllowEncrypted))
|
2022-07-17 11:43:14 -04:00
|
|
|
if allowEnc == "" {
|
|
|
|
allowEnc = env.Get(EnvCompressAllowEncryptionLegacy, "")
|
|
|
|
}
|
|
|
|
|
2021-01-05 23:08:35 -05:00
|
|
|
cfg.AllowEncrypted, err = config.ParseBool(allowEnc)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, err
|
|
|
|
}
|
|
|
|
|
2019-10-23 01:59:13 -04:00
|
|
|
compressExtensions := env.Get(EnvCompressExtensions, kvs.Get(Extensions))
|
2022-07-17 11:43:14 -04:00
|
|
|
compressExtensionsLegacy := env.Get(EnvCompressExtensionsLegacy, "")
|
2019-10-23 01:59:13 -04:00
|
|
|
compressMimeTypes := env.Get(EnvCompressMimeTypes, kvs.Get(MimeTypes))
|
2022-07-17 11:43:14 -04:00
|
|
|
compressMimeTypesLegacy1 := env.Get(EnvCompressMimeTypesLegacy1, "")
|
|
|
|
compressMimeTypesLegacy2 := env.Get(EnvCompressMimeTypesLegacy2, "")
|
|
|
|
if compressExtensions != "" {
|
|
|
|
extensions, err := parseCompressIncludes(compressExtensions)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESSION_EXTENSIONS value (`%s`)", err, extensions)
|
|
|
|
}
|
|
|
|
cfg.Extensions = extensions
|
|
|
|
}
|
|
|
|
|
|
|
|
if compressExtensionsLegacy != "" {
|
|
|
|
extensions, err := parseCompressIncludes(compressExtensions)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_EXTENSIONS value (`%s`)", err, extensions)
|
|
|
|
}
|
|
|
|
cfg.Extensions = extensions
|
|
|
|
}
|
|
|
|
|
|
|
|
if compressMimeTypes != "" {
|
|
|
|
mimeTypes, err := parseCompressIncludes(compressMimeTypes)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESSION_MIME_TYPES value (`%s`)", err, mimeTypes)
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
2022-07-17 11:43:14 -04:00
|
|
|
cfg.MimeTypes = mimeTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
if compressMimeTypesLegacy1 != "" {
|
|
|
|
mimeTypes, err := parseCompressIncludes(compressMimeTypesLegacy1)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIMETYPES value (`%s`)", err, mimeTypes)
|
2019-10-23 01:59:13 -04:00
|
|
|
}
|
2022-07-17 11:43:14 -04:00
|
|
|
cfg.MimeTypes = mimeTypes
|
|
|
|
}
|
|
|
|
|
|
|
|
if compressMimeTypesLegacy2 != "" {
|
|
|
|
mimeTypes, err := parseCompressIncludes(compressMimeTypesLegacy2)
|
|
|
|
if err != nil {
|
|
|
|
return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIME_TYPES value (`%s`)", err, mimeTypes)
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
2022-07-17 11:43:14 -04:00
|
|
|
cfg.MimeTypes = mimeTypes
|
2019-10-04 13:35:33 -04:00
|
|
|
}
|
|
|
|
|
|
|
|
return cfg, nil
|
|
|
|
}
|