mirror of
https://github.com/go-gitea/gitea.git
synced 2026-11-26 10:47:04 -06:00
Use provider-neutral S3 naming for object storage and switch CI to RustFS. 1. Rename `STORAGE_TYPE = minio` to `s3`, `[storage.minio]` to `[storage.s3]`, and `MINIO_*` keys to `S3_*`. Legacy configs resolve as before and log deprecation warnings. Explicit `S3_*` keys take precedence in the same section. 2. Rename `migrate-storage` options to `--storage=s3` and `--s3-*`, retaining deprecated `minio` aliases. 3. Replace MinIO in CI with RustFS `1.0.6` and rename `TEST_MINIO_ENDPOINT` to `TEST_S3_ENDPOINT`. Docs: https://gitea.com/gitea/docs/pulls/659 Assisted-by: Claude Code:claude-opus-5-5 --------- Signed-off-by: silverwind <me@silverwind.io> Co-authored-by: Claude (Opus 4.7) <noreply@anthropic.com> Co-authored-by: Copilot Autofix powered by AI <175728472+Copilot@users.noreply.github.com>
392 lines
14 KiB
Go
392 lines
14 KiB
Go
// Copyright 2020 The Gitea Authors. All rights reserved.
|
|
// SPDX-License-Identifier: MIT
|
|
|
|
package setting
|
|
|
|
import (
|
|
"errors"
|
|
"fmt"
|
|
"path/filepath"
|
|
"slices"
|
|
"strings"
|
|
|
|
"gitea.dev/modules/log"
|
|
)
|
|
|
|
// StorageType is a type of Storage
|
|
type StorageType string
|
|
|
|
const (
|
|
// LocalStorageType is the type descriptor for local storage
|
|
LocalStorageType StorageType = "local"
|
|
// S3StorageType is the type descriptor for S3-compatible storage
|
|
S3StorageType StorageType = "s3"
|
|
// AzureBlobStorageType is the type descriptor for azure blob storage
|
|
AzureBlobStorageType StorageType = "azureblob"
|
|
legacyMinioStorageType StorageType = "minio" // deprecated alias of S3StorageType
|
|
)
|
|
|
|
var storageTypes = []StorageType{
|
|
LocalStorageType,
|
|
S3StorageType,
|
|
AzureBlobStorageType,
|
|
legacyMinioStorageType,
|
|
}
|
|
|
|
// IsValidStorageType returns true if the given storage type is valid
|
|
func IsValidStorageType(storageType StorageType) bool {
|
|
return slices.Contains(storageTypes, storageType)
|
|
}
|
|
|
|
// S3StorageConfig represents the configuration for an S3-compatible storage
|
|
type S3StorageConfig struct {
|
|
Endpoint string `ini:"S3_ENDPOINT" json:",omitempty"`
|
|
AccessKeyID string `ini:"S3_ACCESS_KEY_ID" json:",omitempty"`
|
|
SecretAccessKey string `ini:"S3_SECRET_ACCESS_KEY" json:",omitempty"`
|
|
IamEndpoint string `ini:"S3_IAM_ENDPOINT" json:",omitempty"`
|
|
Bucket string `ini:"S3_BUCKET" json:",omitempty"`
|
|
Location string `ini:"S3_LOCATION" json:",omitempty"`
|
|
BasePath string `ini:"S3_BASE_PATH" json:",omitempty"`
|
|
UseSSL bool `ini:"S3_USE_SSL"`
|
|
InsecureSkipVerify bool `ini:"S3_INSECURE_SKIP_VERIFY"`
|
|
ChecksumAlgorithm string `ini:"S3_CHECKSUM_ALGORITHM" json:",omitempty"`
|
|
ServeDirect bool `ini:"SERVE_DIRECT"`
|
|
BucketLookUpType string `ini:"S3_BUCKET_LOOKUP_TYPE" json:",omitempty"`
|
|
}
|
|
|
|
// minioToS3KeyRenames is a slice, not a map, for stable startup-log order
|
|
var minioToS3KeyRenames = []struct{ oldKey, newKey string }{
|
|
{"MINIO_ACCESS_KEY_ID", "S3_ACCESS_KEY_ID"},
|
|
{"MINIO_BASE_PATH", "S3_BASE_PATH"},
|
|
{"MINIO_BUCKET", "S3_BUCKET"},
|
|
{"MINIO_BUCKET_LOOKUP_TYPE", "S3_BUCKET_LOOKUP_TYPE"},
|
|
{"MINIO_CHECKSUM_ALGORITHM", "S3_CHECKSUM_ALGORITHM"},
|
|
{"MINIO_ENDPOINT", "S3_ENDPOINT"},
|
|
{"MINIO_IAM_ENDPOINT", "S3_IAM_ENDPOINT"},
|
|
{"MINIO_INSECURE_SKIP_VERIFY", "S3_INSECURE_SKIP_VERIFY"},
|
|
{"MINIO_LOCATION", "S3_LOCATION"},
|
|
{"MINIO_SECRET_ACCESS_KEY", "S3_SECRET_ACCESS_KEY"},
|
|
{"MINIO_USE_SSL", "S3_USE_SSL"},
|
|
}
|
|
|
|
// migrateDeprecatedStorageKeys copies all keys before deleting any, so `%(MINIO_*)s` interpolations still resolve
|
|
func migrateDeprecatedStorageKeys(rootCfg ConfigProvider) {
|
|
sections := rootCfg.Sections()
|
|
for _, sec := range sections {
|
|
for _, rename := range minioToS3KeyRenames {
|
|
if oldKey := ConfigSectionKey(sec, rename.oldKey); oldKey != nil && ConfigSectionKey(sec, rename.newKey) == nil {
|
|
_, _ = sec.NewKey(rename.newKey, oldKey.String())
|
|
}
|
|
}
|
|
}
|
|
for _, sec := range sections {
|
|
for _, rename := range minioToS3KeyRenames {
|
|
if ConfigSectionKey(sec, rename.oldKey) != nil {
|
|
LogStartupProblem(1, log.WARN, "Deprecation: config option `[%s].%s` present, please use `[%s].%s` instead", sec.Name(), rename.oldKey, sec.Name(), rename.newKey)
|
|
sec.DeleteKey(rename.oldKey)
|
|
}
|
|
}
|
|
}
|
|
}
|
|
|
|
func (cfg *S3StorageConfig) ToShadow() {
|
|
if cfg.AccessKeyID != "" {
|
|
cfg.AccessKeyID = "******"
|
|
}
|
|
if cfg.SecretAccessKey != "" {
|
|
cfg.SecretAccessKey = "******"
|
|
}
|
|
}
|
|
|
|
type AzureBlobStorageConfig struct {
|
|
Endpoint string `ini:"AZURE_BLOB_ENDPOINT" json:",omitempty"`
|
|
AccountName string `ini:"AZURE_BLOB_ACCOUNT_NAME" json:",omitempty"`
|
|
AccountKey string `ini:"AZURE_BLOB_ACCOUNT_KEY" json:",omitempty"`
|
|
Container string `ini:"AZURE_BLOB_CONTAINER" json:",omitempty"`
|
|
BasePath string `ini:"AZURE_BLOB_BASE_PATH" json:",omitempty"`
|
|
ServeDirect bool `ini:"SERVE_DIRECT"`
|
|
}
|
|
|
|
func (cfg *AzureBlobStorageConfig) ToShadow() {
|
|
if cfg.AccountKey != "" {
|
|
cfg.AccountKey = "******"
|
|
}
|
|
if cfg.AccountName != "" {
|
|
cfg.AccountName = "******"
|
|
}
|
|
}
|
|
|
|
// Storage represents configuration of storages
|
|
type Storage struct {
|
|
Type StorageType // local or s3 or azureblob
|
|
Path string `json:",omitempty"` // for local type
|
|
TemporaryPath string `json:",omitempty"`
|
|
S3Config S3StorageConfig // for s3 type
|
|
AzureBlobConfig AzureBlobStorageConfig // for azureblob type
|
|
}
|
|
|
|
func (storage *Storage) ToShadowCopy() Storage {
|
|
shadowStorage := *storage
|
|
shadowStorage.S3Config.ToShadow()
|
|
shadowStorage.AzureBlobConfig.ToShadow()
|
|
return shadowStorage
|
|
}
|
|
|
|
func (storage *Storage) ServeDirect() bool {
|
|
return (storage.Type == S3StorageType && storage.S3Config.ServeDirect) ||
|
|
(storage.Type == AzureBlobStorageType && storage.AzureBlobConfig.ServeDirect)
|
|
}
|
|
|
|
const storageSectionName = "storage"
|
|
|
|
func getDefaultStorageSection(rootCfg ConfigProvider) ConfigSection {
|
|
storageSec := rootCfg.Section(storageSectionName)
|
|
// Global Defaults
|
|
storageSec.Key("STORAGE_TYPE").MustString("local") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_ENDPOINT").MustString("localhost:9000") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_ACCESS_KEY_ID").MustString("") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_SECRET_ACCESS_KEY").MustString("") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_BUCKET").MustString("gitea") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_LOCATION").MustString("us-east-1") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_USE_SSL").MustBool(false) // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_INSECURE_SKIP_VERIFY").MustBool(false) // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_CHECKSUM_ALGORITHM").MustString("default") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("S3_BUCKET_LOOKUP_TYPE").MustString("auto") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("AZURE_BLOB_ENDPOINT").MustString("") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("AZURE_BLOB_ACCOUNT_NAME").MustString("") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("AZURE_BLOB_ACCOUNT_KEY").MustString("") // FIXME: INI-MUST-SIDE-EFFECT
|
|
storageSec.Key("AZURE_BLOB_CONTAINER").MustString("gitea") // FIXME: INI-MUST-SIDE-EFFECT
|
|
return storageSec
|
|
}
|
|
|
|
// getStorage will find target section and extra special section first and then read override
|
|
// items from extra section
|
|
func getStorage(rootCfg ConfigProvider, name, typ string, sec ConfigSection) (*Storage, error) {
|
|
if name == "" {
|
|
return nil, errors.New("no name for storage")
|
|
}
|
|
|
|
migrateDeprecatedStorageKeys(rootCfg)
|
|
targetSec, tp, err := getStorageTargetSection(rootCfg, name, typ, sec)
|
|
if err != nil {
|
|
return nil, err
|
|
}
|
|
|
|
overrideSec := getStorageOverrideSection(rootCfg, sec, tp, name)
|
|
|
|
targetType := targetSec.Key("STORAGE_TYPE").String()
|
|
switch targetType {
|
|
case string(LocalStorageType):
|
|
return getStorageForLocal(targetSec, overrideSec, tp, name)
|
|
case string(legacyMinioStorageType):
|
|
LogStartupProblem(1, log.WARN, "Deprecation: storage type `%s` for `%s` is deprecated, please use `%s` instead", legacyMinioStorageType, name, S3StorageType)
|
|
fallthrough
|
|
case string(S3StorageType):
|
|
return getStorageForS3(targetSec, overrideSec, tp, name)
|
|
case string(AzureBlobStorageType):
|
|
return getStorageForAzureBlob(targetSec, overrideSec, tp, name)
|
|
default:
|
|
return nil, fmt.Errorf("unsupported storage type %q", targetType)
|
|
}
|
|
}
|
|
|
|
type targetSecType int
|
|
|
|
const (
|
|
targetSecIsTyp targetSecType = iota // target section is [storage.type] which the type from parameter
|
|
targetSecIsStorage // target section is [storage]
|
|
targetSecIsDefault // target section is the default value
|
|
targetSecIsStorageWithName // target section is [storage.name]
|
|
targetSecIsSec // target section is from the name seciont [name]
|
|
)
|
|
|
|
func getStorageSectionByType(rootCfg ConfigProvider, typ string) (ConfigSection, targetSecType, error) { //nolint:unparam // FIXME: targetSecType is always 0, wrong design?
|
|
legacySecName := storageSectionName + "." + string(legacyMinioStorageType)
|
|
targetSec, err := rootCfg.GetSection(storageSectionName + "." + typ)
|
|
if err != nil && typ == string(S3StorageType) {
|
|
targetSec, err = rootCfg.GetSection(legacySecName)
|
|
}
|
|
if err == nil && targetSec.Name() == legacySecName {
|
|
LogStartupProblem(1, log.WARN, "Deprecation: storage section `[%s]` is deprecated, please rename it to `[%s.%s]` and use `STORAGE_TYPE = %s`", legacySecName, storageSectionName, S3StorageType, S3StorageType)
|
|
}
|
|
if err != nil {
|
|
if !IsValidStorageType(StorageType(typ)) {
|
|
return nil, 0, fmt.Errorf("get section via storage type %q failed: %v", typ, err)
|
|
}
|
|
// if typ is a valid storage type, but there is no [storage.local] or [storage.s3] section
|
|
// it's not an error
|
|
return nil, 0, nil
|
|
}
|
|
|
|
targetType := targetSec.Key("STORAGE_TYPE").String()
|
|
if targetType == "" {
|
|
if !IsValidStorageType(StorageType(typ)) {
|
|
return nil, 0, fmt.Errorf("unknown storage type %q", typ)
|
|
}
|
|
targetSec.Key("STORAGE_TYPE").SetValue(typ)
|
|
} else if !IsValidStorageType(StorageType(targetType)) {
|
|
return nil, 0, fmt.Errorf("unknown storage type %q for section storage.%v", targetType, typ)
|
|
}
|
|
|
|
return targetSec, targetSecIsTyp, nil
|
|
}
|
|
|
|
func getStorageTargetSection(rootCfg ConfigProvider, name, typ string, sec ConfigSection) (ConfigSection, targetSecType, error) {
|
|
// check typ first
|
|
if typ == "" {
|
|
if sec != nil { // check sec's type secondly
|
|
typ = sec.Key("STORAGE_TYPE").String()
|
|
if IsValidStorageType(StorageType(typ)) {
|
|
if targetSec, tp, err := getStorageSectionByType(rootCfg, typ); targetSec != nil || err != nil {
|
|
return targetSec, tp, err
|
|
}
|
|
return sec, targetSecIsSec, nil
|
|
}
|
|
}
|
|
}
|
|
|
|
if typ != "" {
|
|
targetSec, tp, err := getStorageSectionByType(rootCfg, typ)
|
|
if targetSec != nil || err != nil {
|
|
return targetSec, tp, err
|
|
}
|
|
}
|
|
|
|
// check storage name thirdly
|
|
targetSec, _ := rootCfg.GetSection(storageSectionName + "." + name)
|
|
if targetSec != nil {
|
|
targetType := targetSec.Key("STORAGE_TYPE").String()
|
|
switch targetType {
|
|
case "":
|
|
if targetSec.Key("PATH").String() == "" { // both storage type and path are empty, use default
|
|
return getDefaultStorageSection(rootCfg), targetSecIsDefault, nil
|
|
}
|
|
|
|
targetSec.Key("STORAGE_TYPE").SetValue("local")
|
|
default:
|
|
targetSec, tp, err := getStorageSectionByType(rootCfg, targetType)
|
|
if targetSec != nil || err != nil {
|
|
return targetSec, tp, err
|
|
}
|
|
}
|
|
|
|
return targetSec, targetSecIsStorageWithName, nil
|
|
}
|
|
|
|
return getDefaultStorageSection(rootCfg), targetSecIsDefault, nil
|
|
}
|
|
|
|
// getStorageOverrideSection override section will be read SERVE_DIRECT, PATH, S3_BASE_PATH, S3_BUCKET to override the targetsec when possible
|
|
func getStorageOverrideSection(rootConfig ConfigProvider, sec ConfigSection, targetSecType targetSecType, name string) ConfigSection {
|
|
if targetSecType == targetSecIsSec {
|
|
return nil
|
|
}
|
|
|
|
if sec != nil {
|
|
return sec
|
|
}
|
|
|
|
if targetSecType != targetSecIsStorageWithName {
|
|
nameSec, _ := rootConfig.GetSection(storageSectionName + "." + name)
|
|
return nameSec
|
|
}
|
|
return nil
|
|
}
|
|
|
|
func getStorageForLocal(targetSec, overrideSec ConfigSection, tp targetSecType, name string) (*Storage, error) {
|
|
storage := Storage{
|
|
Type: StorageType(targetSec.Key("STORAGE_TYPE").String()),
|
|
}
|
|
|
|
targetPath := ConfigSectionKeyString(targetSec, "PATH", "")
|
|
var fallbackPath string
|
|
if targetPath == "" { // no path
|
|
fallbackPath = filepath.Join(AppDataPath, name)
|
|
} else {
|
|
if tp == targetSecIsStorage || tp == targetSecIsDefault {
|
|
fallbackPath = filepath.Join(targetPath, name)
|
|
} else {
|
|
fallbackPath = targetPath
|
|
}
|
|
if !filepath.IsAbs(fallbackPath) {
|
|
fallbackPath = filepath.Join(AppDataPath, fallbackPath)
|
|
}
|
|
}
|
|
|
|
if overrideSec == nil { // no override section
|
|
storage.Path = fallbackPath
|
|
} else {
|
|
storage.Path = ConfigSectionKeyString(overrideSec, "PATH", "")
|
|
if storage.Path == "" { // overrideSec has no path
|
|
storage.Path = fallbackPath
|
|
} else if !filepath.IsAbs(storage.Path) {
|
|
if targetPath == "" {
|
|
storage.Path = filepath.Join(AppDataPath, storage.Path)
|
|
} else {
|
|
storage.Path = filepath.Join(targetPath, storage.Path)
|
|
}
|
|
}
|
|
}
|
|
|
|
checkOverlappedPath("[storage."+name+"].PATH", storage.Path)
|
|
|
|
return &storage, nil
|
|
}
|
|
|
|
func getStorageForS3(targetSec, overrideSec ConfigSection, tp targetSecType, name string) (*Storage, error) {
|
|
storage := Storage{Type: S3StorageType}
|
|
if err := targetSec.MapTo(&storage.S3Config); err != nil {
|
|
return nil, fmt.Errorf("map S3 config failed: %v", err)
|
|
}
|
|
|
|
var defaultPath string
|
|
if storage.S3Config.BasePath != "" {
|
|
if tp == targetSecIsStorage || tp == targetSecIsDefault {
|
|
defaultPath = strings.TrimSuffix(storage.S3Config.BasePath, "/") + "/" + name + "/"
|
|
} else {
|
|
defaultPath = storage.S3Config.BasePath
|
|
}
|
|
}
|
|
if defaultPath == "" {
|
|
defaultPath = name + "/"
|
|
}
|
|
|
|
if overrideSec != nil {
|
|
storage.S3Config.ServeDirect = ConfigSectionKeyBool(overrideSec, "SERVE_DIRECT", storage.S3Config.ServeDirect)
|
|
storage.S3Config.BasePath = ConfigSectionKeyString(overrideSec, "S3_BASE_PATH", defaultPath)
|
|
storage.S3Config.Bucket = ConfigSectionKeyString(overrideSec, "S3_BUCKET", storage.S3Config.Bucket)
|
|
} else {
|
|
storage.S3Config.BasePath = defaultPath
|
|
}
|
|
return &storage, nil
|
|
}
|
|
|
|
func getStorageForAzureBlob(targetSec, overrideSec ConfigSection, tp targetSecType, name string) (*Storage, error) {
|
|
var storage Storage
|
|
storage.Type = StorageType(targetSec.Key("STORAGE_TYPE").String())
|
|
if err := targetSec.MapTo(&storage.AzureBlobConfig); err != nil {
|
|
return nil, fmt.Errorf("map azure blob config failed: %v", err)
|
|
}
|
|
|
|
var defaultPath string
|
|
if storage.AzureBlobConfig.BasePath != "" {
|
|
if tp == targetSecIsStorage || tp == targetSecIsDefault {
|
|
defaultPath = strings.TrimSuffix(storage.AzureBlobConfig.BasePath, "/") + "/" + name + "/"
|
|
} else {
|
|
defaultPath = storage.AzureBlobConfig.BasePath
|
|
}
|
|
}
|
|
if defaultPath == "" {
|
|
defaultPath = name + "/"
|
|
}
|
|
|
|
if overrideSec != nil {
|
|
storage.AzureBlobConfig.ServeDirect = ConfigSectionKeyBool(overrideSec, "SERVE_DIRECT", storage.AzureBlobConfig.ServeDirect)
|
|
storage.AzureBlobConfig.BasePath = ConfigSectionKeyString(overrideSec, "AZURE_BLOB_BASE_PATH", defaultPath)
|
|
storage.AzureBlobConfig.Container = ConfigSectionKeyString(overrideSec, "AZURE_BLOB_CONTAINER", storage.AzureBlobConfig.Container)
|
|
} else {
|
|
storage.AzureBlobConfig.BasePath = defaultPath
|
|
}
|
|
return &storage, nil
|
|
}
|