You can not select more than 25 topics Topics must start with a letter or number, can include dashes ('-') and can be up to 35 characters long.

setting.go 7.6KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228
  1. // Copyright 2014 The Gogs Authors. All rights reserved.
  2. // Copyright 2017 The Gitea Authors. All rights reserved.
  3. // SPDX-License-Identifier: MIT
  4. package setting
  5. import (
  6. "fmt"
  7. "os"
  8. "runtime"
  9. "strings"
  10. "time"
  11. "code.gitea.io/gitea/modules/log"
  12. "code.gitea.io/gitea/modules/user"
  13. )
  14. // settings
  15. var (
  16. // AppVer is the version of the current build of Gitea. It is set in main.go from main.Version.
  17. AppVer string
  18. // AppBuiltWith represents a human-readable version go runtime build version and build tags. (See main.go formatBuiltWith().)
  19. AppBuiltWith string
  20. // AppStartTime store time gitea has started
  21. AppStartTime time.Time
  22. // Other global setting objects
  23. CfgProvider ConfigProvider
  24. RunMode string
  25. RunUser string
  26. IsProd bool
  27. IsWindows bool
  28. // IsInTesting indicates whether the testing is running. A lot of unreliable code causes a lot of nonsense error logs during testing
  29. // TODO: this is only a temporary solution, we should make the test code more reliable
  30. IsInTesting = false
  31. )
  32. func init() {
  33. IsWindows = runtime.GOOS == "windows"
  34. if AppVer == "" {
  35. AppVer = "dev"
  36. }
  37. // We can rely on log.CanColorStdout being set properly because modules/log/console_windows.go comes before modules/setting/setting.go lexicographically
  38. // By default set this logger at Info - we'll change it later, but we need to start with something.
  39. log.SetConsoleLogger(log.DEFAULT, "console", log.INFO)
  40. }
  41. // IsRunUserMatchCurrentUser returns false if configured run user does not match
  42. // actual user that runs the app. The first return value is the actual user name.
  43. // This check is ignored under Windows since SSH remote login is not the main
  44. // method to login on Windows.
  45. func IsRunUserMatchCurrentUser(runUser string) (string, bool) {
  46. if IsWindows || SSH.StartBuiltinServer {
  47. return "", true
  48. }
  49. currentUser := user.CurrentUsername()
  50. return currentUser, runUser == currentUser
  51. }
  52. // PrepareAppDataPath creates app data directory if necessary
  53. func PrepareAppDataPath() error {
  54. // FIXME: There are too many calls to MkdirAll in old code. It is incorrect.
  55. // For example, if someDir=/mnt/vol1/gitea-home/data, if the mount point /mnt/vol1 is not mounted when Gitea runs,
  56. // then gitea will make new empty directories in /mnt/vol1, all are stored in the root filesystem.
  57. // The correct behavior should be: creating parent directories is end users' duty. We only create sub-directories in existing parent directories.
  58. // For quickstart, the parent directories should be created automatically for first startup (eg: a flag or a check of INSTALL_LOCK).
  59. // Now we can take the first step to do correctly (using Mkdir) in other packages, and prepare the AppDataPath here, then make a refactor in future.
  60. st, err := os.Stat(AppDataPath)
  61. if os.IsNotExist(err) {
  62. err = os.MkdirAll(AppDataPath, os.ModePerm)
  63. if err != nil {
  64. return fmt.Errorf("unable to create the APP_DATA_PATH directory: %q, Error: %w", AppDataPath, err)
  65. }
  66. return nil
  67. }
  68. if err != nil {
  69. return fmt.Errorf("unable to use APP_DATA_PATH %q. Error: %w", AppDataPath, err)
  70. }
  71. if !st.IsDir() /* also works for symlink */ {
  72. return fmt.Errorf("the APP_DATA_PATH %q is not a directory (or symlink to a directory) and can't be used", AppDataPath)
  73. }
  74. return nil
  75. }
  76. func InitCfgProvider(file string, extraConfigs ...string) {
  77. var err error
  78. if CfgProvider, err = NewConfigProviderFromFile(file, extraConfigs...); err != nil {
  79. log.Fatal("Unable to init config provider from %q: %v", file, err)
  80. }
  81. CfgProvider.DisableSaving() // do not allow saving the CfgProvider into file, it will be polluted by the "MustXxx" calls
  82. }
  83. func MustInstalled() {
  84. if !InstallLock {
  85. log.Fatal(`Unable to load config file for a installed Gitea instance, you should either use "--config" to set your config file (app.ini), or run "gitea web" command to install Gitea.`)
  86. }
  87. }
  88. func LoadCommonSettings() {
  89. if err := loadCommonSettingsFrom(CfgProvider); err != nil {
  90. log.Fatal("Unable to load settings from config: %v", err)
  91. }
  92. }
  93. // loadCommonSettingsFrom loads common configurations from a configuration provider.
  94. func loadCommonSettingsFrom(cfg ConfigProvider) error {
  95. // WARNING: don't change the sequence except you know what you are doing.
  96. loadRunModeFrom(cfg)
  97. loadLogGlobalFrom(cfg)
  98. loadServerFrom(cfg)
  99. loadSSHFrom(cfg)
  100. mustCurrentRunUserMatch(cfg) // it depends on the SSH config, only non-builtin SSH server requires this check
  101. loadOAuth2From(cfg)
  102. loadSecurityFrom(cfg)
  103. if err := loadAttachmentFrom(cfg); err != nil {
  104. return err
  105. }
  106. if err := loadLFSFrom(cfg); err != nil {
  107. return err
  108. }
  109. loadTimeFrom(cfg)
  110. loadRepositoryFrom(cfg)
  111. if err := loadAvatarsFrom(cfg); err != nil {
  112. return err
  113. }
  114. if err := loadRepoAvatarFrom(cfg); err != nil {
  115. return err
  116. }
  117. if err := loadPackagesFrom(cfg); err != nil {
  118. return err
  119. }
  120. if err := loadActionsFrom(cfg); err != nil {
  121. return err
  122. }
  123. loadUIFrom(cfg)
  124. loadAdminFrom(cfg)
  125. loadAPIFrom(cfg)
  126. loadMetricsFrom(cfg)
  127. loadCamoFrom(cfg)
  128. loadI18nFrom(cfg)
  129. loadGitFrom(cfg)
  130. loadMirrorFrom(cfg)
  131. loadMarkupFrom(cfg)
  132. loadOtherFrom(cfg)
  133. return nil
  134. }
  135. func loadRunModeFrom(rootCfg ConfigProvider) {
  136. rootSec := rootCfg.Section("")
  137. RunUser = rootSec.Key("RUN_USER").MustString(user.CurrentUsername())
  138. // The following is a purposefully undocumented option. Please do not run Gitea as root. It will only cause future headaches.
  139. // Please don't use root as a bandaid to "fix" something that is broken, instead the broken thing should instead be fixed properly.
  140. unsafeAllowRunAsRoot := ConfigSectionKeyBool(rootSec, "I_AM_BEING_UNSAFE_RUNNING_AS_ROOT")
  141. RunMode = os.Getenv("GITEA_RUN_MODE")
  142. if RunMode == "" {
  143. RunMode = rootSec.Key("RUN_MODE").MustString("prod")
  144. }
  145. // non-dev mode is treated as prod mode, to protect users from accidentally running in dev mode if there is a typo in this value.
  146. RunMode = strings.ToLower(RunMode)
  147. if RunMode != "dev" {
  148. RunMode = "prod"
  149. }
  150. IsProd = RunMode != "dev"
  151. // check if we run as root
  152. if os.Getuid() == 0 {
  153. if !unsafeAllowRunAsRoot {
  154. // Special thanks to VLC which inspired the wording of this messaging.
  155. log.Fatal("Gitea is not supposed to be run as root. Sorry. If you need to use privileged TCP ports please instead use setcap and the `cap_net_bind_service` permission")
  156. }
  157. log.Critical("You are running Gitea using the root user, and have purposely chosen to skip built-in protections around this. You have been warned against this.")
  158. }
  159. }
  160. // HasInstallLock checks the install-lock in ConfigProvider directly, because sometimes the config file is not loaded into setting variables yet.
  161. func HasInstallLock(rootCfg ConfigProvider) bool {
  162. return rootCfg.Section("security").Key("INSTALL_LOCK").MustBool(false)
  163. }
  164. func mustCurrentRunUserMatch(rootCfg ConfigProvider) {
  165. // Does not check run user when the "InstallLock" is off.
  166. if HasInstallLock(rootCfg) {
  167. currentUser, match := IsRunUserMatchCurrentUser(RunUser)
  168. if !match {
  169. log.Fatal("Expect user '%s' but current user is: %s", RunUser, currentUser)
  170. }
  171. }
  172. }
  173. // LoadSettings initializes the settings for normal start up
  174. func LoadSettings() {
  175. initAllLoggers()
  176. loadDBSetting(CfgProvider)
  177. loadServiceFrom(CfgProvider)
  178. loadOAuth2ClientFrom(CfgProvider)
  179. loadCacheFrom(CfgProvider)
  180. loadSessionFrom(CfgProvider)
  181. loadCorsFrom(CfgProvider)
  182. loadMailsFrom(CfgProvider)
  183. loadProxyFrom(CfgProvider)
  184. loadWebhookFrom(CfgProvider)
  185. loadMigrationsFrom(CfgProvider)
  186. loadIndexerFrom(CfgProvider)
  187. loadTaskFrom(CfgProvider)
  188. LoadQueueSettings()
  189. loadProjectFrom(CfgProvider)
  190. loadMimeTypeMapFrom(CfgProvider)
  191. loadFederationFrom(CfgProvider)
  192. }
  193. // LoadSettingsForInstall initializes the settings for install
  194. func LoadSettingsForInstall() {
  195. loadDBSetting(CfgProvider)
  196. loadServiceFrom(CfgProvider)
  197. loadMailerFrom(CfgProvider)
  198. }