mirror of
				https://github.com/go-gitea/gitea.git
				synced 2025-10-31 10:56:10 +01:00 
			
		
		
		
	Fix all possible setting error related storages and added some tests (#23911)
Follow up #22405 Fix #20703 This PR rewrites storage configuration read sequences with some breaks and tests. It becomes more strict than before and also fixed some inherit problems. - Move storage's MinioConfig struct into setting, so after the configuration loading, the values will be stored into the struct but not still on some section. - All storages configurations should be stored on one section, configuration items cannot be overrided by multiple sections. The prioioty of configuration is `[attachment]` > `[storage.attachments]` | `[storage.customized]` > `[storage]` > `default` - For extra override configuration items, currently are `SERVE_DIRECT`, `MINIO_BASE_PATH`, `MINIO_BUCKET`, which could be configured in another section. The prioioty of the override configuration is `[attachment]` > `[storage.attachments]` > `default`. - Add more tests for storages configurations. - Update the storage documentations. --------- Co-authored-by: wxiaoguang <wxiaoguang@gmail.com>
This commit is contained in:
		| @@ -5,10 +5,10 @@ package setting | ||||
|  | ||||
| import ( | ||||
| 	"encoding/base64" | ||||
| 	"fmt" | ||||
| 	"time" | ||||
|  | ||||
| 	"code.gitea.io/gitea/modules/generate" | ||||
| 	"code.gitea.io/gitea/modules/log" | ||||
| ) | ||||
|  | ||||
| // LFS represents the configuration for Git LFS | ||||
| @@ -20,25 +20,27 @@ var LFS = struct { | ||||
| 	MaxFileSize     int64         `ini:"LFS_MAX_FILE_SIZE"` | ||||
| 	LocksPagingNum  int           `ini:"LFS_LOCKS_PAGING_NUM"` | ||||
|  | ||||
| 	Storage | ||||
| 	Storage *Storage | ||||
| }{} | ||||
|  | ||||
| func loadLFSFrom(rootCfg ConfigProvider) { | ||||
| func loadLFSFrom(rootCfg ConfigProvider) error { | ||||
| 	sec := rootCfg.Section("server") | ||||
| 	if err := sec.MapTo(&LFS); err != nil { | ||||
| 		log.Fatal("Failed to map LFS settings: %v", err) | ||||
| 		return fmt.Errorf("failed to map LFS settings: %v", err) | ||||
| 	} | ||||
|  | ||||
| 	lfsSec := rootCfg.Section("lfs") | ||||
| 	storageType := lfsSec.Key("STORAGE_TYPE").MustString("") | ||||
| 	lfsSec, _ := rootCfg.GetSection("lfs") | ||||
|  | ||||
| 	// Specifically default PATH to LFS_CONTENT_PATH | ||||
| 	// DEPRECATED should not be removed because users maybe upgrade from lower version to the latest version | ||||
| 	// if these are removed, the warning will not be shown | ||||
| 	deprecatedSetting(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0") | ||||
| 	lfsSec.Key("PATH").MustString(sec.Key("LFS_CONTENT_PATH").String()) | ||||
| 	deprecatedSettingFatal(rootCfg, "server", "LFS_CONTENT_PATH", "lfs", "PATH", "v1.19.0") | ||||
|  | ||||
| 	LFS.Storage = getStorage(rootCfg, "lfs", storageType, lfsSec) | ||||
| 	var err error | ||||
| 	LFS.Storage, err = getStorage(rootCfg, "lfs", "", lfsSec) | ||||
| 	if err != nil { | ||||
| 		return err | ||||
| 	} | ||||
|  | ||||
| 	// Rest of LFS service settings | ||||
| 	if LFS.LocksPagingNum == 0 { | ||||
| @@ -47,23 +49,25 @@ func loadLFSFrom(rootCfg ConfigProvider) { | ||||
|  | ||||
| 	LFS.HTTPAuthExpiry = sec.Key("LFS_HTTP_AUTH_EXPIRY").MustDuration(24 * time.Hour) | ||||
|  | ||||
| 	if LFS.StartServer { | ||||
| 		LFS.JWTSecretBytes = make([]byte, 32) | ||||
| 		n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64)) | ||||
| 	if !LFS.StartServer { | ||||
| 		return nil | ||||
| 	} | ||||
|  | ||||
| 		if err != nil || n != 32 { | ||||
| 			LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64() | ||||
| 			if err != nil { | ||||
| 				log.Fatal("Error generating JWT Secret for custom config: %v", err) | ||||
| 				return | ||||
| 			} | ||||
| 	LFS.JWTSecretBytes = make([]byte, 32) | ||||
| 	n, err := base64.RawURLEncoding.Decode(LFS.JWTSecretBytes, []byte(LFS.JWTSecretBase64)) | ||||
|  | ||||
| 			// Save secret | ||||
| 			sec.Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64) | ||||
| 			if err := rootCfg.Save(); err != nil { | ||||
| 				log.Fatal("Error saving JWT Secret for custom config: %v", err) | ||||
| 				return | ||||
| 			} | ||||
| 	if err != nil || n != 32 { | ||||
| 		LFS.JWTSecretBase64, err = generate.NewJwtSecretBase64() | ||||
| 		if err != nil { | ||||
| 			return fmt.Errorf("Error generating JWT Secret for custom config: %v", err) | ||||
| 		} | ||||
|  | ||||
| 		// Save secret | ||||
| 		sec.Key("LFS_JWT_SECRET").SetValue(LFS.JWTSecretBase64) | ||||
| 		if err := rootCfg.Save(); err != nil { | ||||
| 			return fmt.Errorf("Error saving JWT Secret for custom config: %v", err) | ||||
| 		} | ||||
| 	} | ||||
|  | ||||
| 	return nil | ||||
| } | ||||
|   | ||||
		Reference in New Issue
	
	Block a user