|
4 | 4 | package setting
|
5 | 5 |
|
6 | 6 | import (
|
| 7 | + "fmt" |
| 8 | + "os" |
| 9 | + "path/filepath" |
| 10 | + "strings" |
| 11 | + |
7 | 12 | "code.gitea.io/gitea/modules/log"
|
| 13 | + "code.gitea.io/gitea/modules/util" |
8 | 14 |
|
9 | 15 | ini "gopkg.in/ini.v1"
|
10 | 16 | )
|
11 | 17 |
|
| 18 | +type ConfigSection interface { |
| 19 | + Name() string |
| 20 | + MapTo(interface{}) error |
| 21 | + HasKey(key string) bool |
| 22 | + NewKey(name, value string) (*ini.Key, error) |
| 23 | + Key(key string) *ini.Key |
| 24 | + Keys() []*ini.Key |
| 25 | + ChildSections() []*ini.Section |
| 26 | +} |
| 27 | + |
12 | 28 | // ConfigProvider represents a config provider
|
13 | 29 | type ConfigProvider interface {
|
14 |
| - Section(section string) *ini.Section |
15 |
| - NewSection(name string) (*ini.Section, error) |
16 |
| - GetSection(name string) (*ini.Section, error) |
| 30 | + Section(section string) ConfigSection |
| 31 | + NewSection(name string) (ConfigSection, error) |
| 32 | + GetSection(name string) (ConfigSection, error) |
| 33 | + DeleteSection(name string) error |
| 34 | + Save() error |
| 35 | +} |
| 36 | + |
| 37 | +type iniFileConfigProvider struct { |
| 38 | + *ini.File |
| 39 | + filepath string // the ini file path |
| 40 | + newFile bool // whether the file has not existed previously |
| 41 | + allowEmpty bool // whether not finding configuration files is allowed (only true for the tests) |
| 42 | +} |
| 43 | + |
| 44 | +// NewEmptyConfigProvider create a new empty config provider |
| 45 | +func NewEmptyConfigProvider() ConfigProvider { |
| 46 | + cp, _ := newConfigProviderFromData("") |
| 47 | + return cp |
| 48 | +} |
| 49 | + |
| 50 | +// newConfigProviderFromData this function is only for testing |
| 51 | +func newConfigProviderFromData(configContent string) (ConfigProvider, error) { |
| 52 | + var cfg *ini.File |
| 53 | + var err error |
| 54 | + if configContent == "" { |
| 55 | + cfg = ini.Empty() |
| 56 | + } else { |
| 57 | + cfg, err = ini.Load(strings.NewReader(configContent)) |
| 58 | + if err != nil { |
| 59 | + return nil, err |
| 60 | + } |
| 61 | + } |
| 62 | + cfg.NameMapper = ini.SnackCase |
| 63 | + return &iniFileConfigProvider{ |
| 64 | + File: cfg, |
| 65 | + newFile: true, |
| 66 | + }, nil |
| 67 | +} |
| 68 | + |
| 69 | +// newConfigProviderFromFile load configuration from file. |
| 70 | +// NOTE: do not print any log except error. |
| 71 | +func newConfigProviderFromFile(customConf string, allowEmpty bool, extraConfig string) (*iniFileConfigProvider, error) { |
| 72 | + cfg := ini.Empty() |
| 73 | + newFile := true |
| 74 | + |
| 75 | + if customConf != "" { |
| 76 | + isFile, err := util.IsFile(customConf) |
| 77 | + if err != nil { |
| 78 | + return nil, fmt.Errorf("unable to check if %s is a file. Error: %v", customConf, err) |
| 79 | + } |
| 80 | + if isFile { |
| 81 | + if err := cfg.Append(customConf); err != nil { |
| 82 | + return nil, fmt.Errorf("failed to load custom conf '%s': %v", customConf, err) |
| 83 | + } |
| 84 | + newFile = false |
| 85 | + } |
| 86 | + } |
| 87 | + |
| 88 | + if newFile && !allowEmpty { |
| 89 | + return nil, fmt.Errorf("unable to find configuration file: %q, please ensure you are running in the correct environment or set the correct configuration file with -c", CustomConf) |
| 90 | + } |
| 91 | + |
| 92 | + if extraConfig != "" { |
| 93 | + if err := cfg.Append([]byte(extraConfig)); err != nil { |
| 94 | + return nil, fmt.Errorf("unable to append more config: %v", err) |
| 95 | + } |
| 96 | + } |
| 97 | + |
| 98 | + cfg.NameMapper = ini.SnackCase |
| 99 | + return &iniFileConfigProvider{ |
| 100 | + File: cfg, |
| 101 | + filepath: customConf, |
| 102 | + newFile: newFile, |
| 103 | + allowEmpty: allowEmpty, |
| 104 | + }, nil |
| 105 | +} |
| 106 | + |
| 107 | +func (p *iniFileConfigProvider) Section(section string) ConfigSection { |
| 108 | + return p.File.Section(section) |
| 109 | +} |
| 110 | + |
| 111 | +func (p *iniFileConfigProvider) NewSection(name string) (ConfigSection, error) { |
| 112 | + return p.File.NewSection(name) |
| 113 | +} |
| 114 | + |
| 115 | +func (p *iniFileConfigProvider) GetSection(name string) (ConfigSection, error) { |
| 116 | + return p.File.GetSection(name) |
| 117 | +} |
| 118 | + |
| 119 | +func (p *iniFileConfigProvider) DeleteSection(name string) error { |
| 120 | + p.File.DeleteSection(name) |
| 121 | + return nil |
| 122 | +} |
| 123 | + |
| 124 | +// Save save the content into file |
| 125 | +func (p *iniFileConfigProvider) Save() error { |
| 126 | + if p.filepath == "" { |
| 127 | + if !p.allowEmpty { |
| 128 | + return fmt.Errorf("custom config path must not be empty") |
| 129 | + } |
| 130 | + return nil |
| 131 | + } |
| 132 | + |
| 133 | + if p.newFile { |
| 134 | + if err := os.MkdirAll(filepath.Dir(CustomConf), os.ModePerm); err != nil { |
| 135 | + return fmt.Errorf("failed to create '%s': %v", CustomConf, err) |
| 136 | + } |
| 137 | + } |
| 138 | + if err := p.SaveTo(p.filepath); err != nil { |
| 139 | + return fmt.Errorf("failed to save '%s': %v", p.filepath, err) |
| 140 | + } |
| 141 | + |
| 142 | + // Change permissions to be more restrictive |
| 143 | + fi, err := os.Stat(CustomConf) |
| 144 | + if err != nil { |
| 145 | + return fmt.Errorf("failed to determine current conf file permissions: %v", err) |
| 146 | + } |
| 147 | + |
| 148 | + if fi.Mode().Perm() > 0o600 { |
| 149 | + if err = os.Chmod(CustomConf, 0o600); err != nil { |
| 150 | + log.Warn("Failed changing conf file permissions to -rw-------. Consider changing them manually.") |
| 151 | + } |
| 152 | + } |
| 153 | + return nil |
17 | 154 | }
|
18 | 155 |
|
19 | 156 | // a file is an implementation ConfigProvider and other implementations are possible, i.e. from docker, k8s, …
|
20 |
| -var _ ConfigProvider = &ini.File{} |
| 157 | +var _ ConfigProvider = &iniFileConfigProvider{} |
21 | 158 |
|
22 | 159 | func mustMapSetting(rootCfg ConfigProvider, sectionName string, setting interface{}) {
|
23 | 160 | if err := rootCfg.Section(sectionName).MapTo(setting); err != nil {
|
|
0 commit comments