github.com/minio/minio@v0.0.0-20240328213742-3f72439b8a27/internal/config/compress/compress.go (about)

     1  // Copyright (c) 2015-2021 MinIO, Inc.
     2  //
     3  // This file is part of MinIO Object Storage stack
     4  //
     5  // This program is free software: you can redistribute it and/or modify
     6  // it under the terms of the GNU Affero General Public License as published by
     7  // the Free Software Foundation, either version 3 of the License, or
     8  // (at your option) any later version.
     9  //
    10  // This program is distributed in the hope that it will be useful
    11  // but WITHOUT ANY WARRANTY; without even the implied warranty of
    12  // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.  See the
    13  // GNU Affero General Public License for more details.
    14  //
    15  // You should have received a copy of the GNU Affero General Public License
    16  // along with this program.  If not, see <http://www.gnu.org/licenses/>.
    17  
    18  package compress
    19  
    20  import (
    21  	"fmt"
    22  	"strings"
    23  
    24  	"github.com/minio/minio/internal/config"
    25  	"github.com/minio/pkg/v2/env"
    26  )
    27  
    28  // Config represents the compression settings.
    29  type Config struct {
    30  	Enabled        bool     `json:"enabled"`
    31  	AllowEncrypted bool     `json:"allow_encryption"`
    32  	Extensions     []string `json:"extensions"`
    33  	MimeTypes      []string `json:"mime-types"`
    34  }
    35  
    36  // Compression environment variables
    37  const (
    38  	Extensions     = "extensions"
    39  	AllowEncrypted = "allow_encryption"
    40  	MimeTypes      = "mime_types"
    41  
    42  	EnvCompressState           = "MINIO_COMPRESSION_ENABLE"
    43  	EnvCompressAllowEncryption = "MINIO_COMPRESSION_ALLOW_ENCRYPTION"
    44  	EnvCompressExtensions      = "MINIO_COMPRESSION_EXTENSIONS"
    45  	EnvCompressMimeTypes       = "MINIO_COMPRESSION_MIME_TYPES"
    46  
    47  	// Include-list for compression.
    48  	DefaultExtensions = ".txt,.log,.csv,.json,.tar,.xml,.bin"
    49  	DefaultMimeTypes  = "text/*,application/json,application/xml,binary/octet-stream"
    50  )
    51  
    52  // DefaultKVS - default KV config for compression settings
    53  var (
    54  	DefaultKVS = config.KVS{
    55  		config.KV{
    56  			Key:   config.Enable,
    57  			Value: config.EnableOff,
    58  		},
    59  		config.KV{
    60  			Key:   AllowEncrypted,
    61  			Value: config.EnableOff,
    62  		},
    63  		config.KV{
    64  			Key:   Extensions,
    65  			Value: DefaultExtensions,
    66  		},
    67  		config.KV{
    68  			Key:   MimeTypes,
    69  			Value: DefaultMimeTypes,
    70  		},
    71  	}
    72  )
    73  
    74  // Parses the given compression exclude list `extensions` or `content-types`.
    75  func parseCompressIncludes(include string) ([]string, error) {
    76  	includes := strings.Split(include, config.ValueSeparator)
    77  	for _, e := range includes {
    78  		if len(e) == 0 {
    79  			return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be empty")
    80  		}
    81  		if e == "/" {
    82  			return nil, config.ErrInvalidCompressionIncludesValue(nil).Msg("extension/mime-type cannot be '/'")
    83  		}
    84  	}
    85  	return includes, nil
    86  }
    87  
    88  // LookupConfig - lookup compression config.
    89  func LookupConfig(kvs config.KVS) (Config, error) {
    90  	var err error
    91  	cfg := Config{}
    92  	if err = config.CheckValidKeys(config.CompressionSubSys, kvs, DefaultKVS); err != nil {
    93  		return cfg, err
    94  	}
    95  
    96  	compress := env.Get(EnvCompressState, kvs.Get(config.Enable))
    97  	if compress == "" {
    98  		compress = env.Get(EnvCompress, "")
    99  	}
   100  	cfg.Enabled, err = config.ParseBool(compress)
   101  	if err != nil {
   102  		// Parsing failures happen due to empty KVS, ignore it.
   103  		if kvs.Empty() {
   104  			return cfg, nil
   105  		}
   106  		return cfg, err
   107  	}
   108  	if !cfg.Enabled {
   109  		return cfg, nil
   110  	}
   111  
   112  	allowEnc := env.Get(EnvCompressAllowEncryption, kvs.Get(AllowEncrypted))
   113  	if allowEnc == "" {
   114  		allowEnc = env.Get(EnvCompressAllowEncryptionLegacy, "")
   115  	}
   116  
   117  	cfg.AllowEncrypted, err = config.ParseBool(allowEnc)
   118  	if err != nil {
   119  		return cfg, err
   120  	}
   121  
   122  	compressExtensions := env.Get(EnvCompressExtensions, kvs.Get(Extensions))
   123  	compressExtensionsLegacy := env.Get(EnvCompressExtensionsLegacy, "")
   124  	compressMimeTypes := env.Get(EnvCompressMimeTypes, kvs.Get(MimeTypes))
   125  	compressMimeTypesLegacy1 := env.Get(EnvCompressMimeTypesLegacy1, "")
   126  	compressMimeTypesLegacy2 := env.Get(EnvCompressMimeTypesLegacy2, "")
   127  	if compressExtensions != "" {
   128  		extensions, err := parseCompressIncludes(compressExtensions)
   129  		if err != nil {
   130  			return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESSION_EXTENSIONS value (`%s`)", err, extensions)
   131  		}
   132  		cfg.Extensions = extensions
   133  	}
   134  
   135  	if compressExtensionsLegacy != "" {
   136  		extensions, err := parseCompressIncludes(compressExtensions)
   137  		if err != nil {
   138  			return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_EXTENSIONS value (`%s`)", err, extensions)
   139  		}
   140  		cfg.Extensions = extensions
   141  	}
   142  
   143  	if compressMimeTypes != "" {
   144  		mimeTypes, err := parseCompressIncludes(compressMimeTypes)
   145  		if err != nil {
   146  			return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESSION_MIME_TYPES value (`%s`)", err, mimeTypes)
   147  		}
   148  		cfg.MimeTypes = mimeTypes
   149  	}
   150  
   151  	if compressMimeTypesLegacy1 != "" {
   152  		mimeTypes, err := parseCompressIncludes(compressMimeTypesLegacy1)
   153  		if err != nil {
   154  			return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIMETYPES value (`%s`)", err, mimeTypes)
   155  		}
   156  		cfg.MimeTypes = mimeTypes
   157  	}
   158  
   159  	if compressMimeTypesLegacy2 != "" {
   160  		mimeTypes, err := parseCompressIncludes(compressMimeTypesLegacy2)
   161  		if err != nil {
   162  			return cfg, fmt.Errorf("%s: Invalid MINIO_COMPRESS_MIME_TYPES value (`%s`)", err, mimeTypes)
   163  		}
   164  		cfg.MimeTypes = mimeTypes
   165  	}
   166  
   167  	return cfg, nil
   168  }