config.go 45 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310
  1. // Package config manages the configuration
  2. package config
  3. import (
  4. "errors"
  5. "fmt"
  6. "os"
  7. "path/filepath"
  8. "strconv"
  9. "strings"
  10. "github.com/spf13/viper"
  11. "github.com/drakkan/sftpgo/v2/common"
  12. "github.com/drakkan/sftpgo/v2/dataprovider"
  13. "github.com/drakkan/sftpgo/v2/ftpd"
  14. "github.com/drakkan/sftpgo/v2/httpclient"
  15. "github.com/drakkan/sftpgo/v2/httpd"
  16. "github.com/drakkan/sftpgo/v2/kms"
  17. "github.com/drakkan/sftpgo/v2/logger"
  18. "github.com/drakkan/sftpgo/v2/mfa"
  19. "github.com/drakkan/sftpgo/v2/sdk/plugin"
  20. "github.com/drakkan/sftpgo/v2/sftpd"
  21. "github.com/drakkan/sftpgo/v2/smtp"
  22. "github.com/drakkan/sftpgo/v2/telemetry"
  23. "github.com/drakkan/sftpgo/v2/util"
  24. "github.com/drakkan/sftpgo/v2/version"
  25. "github.com/drakkan/sftpgo/v2/webdavd"
  26. )
  27. const (
  28. logSender = "config"
  29. // configName defines the name for config file.
  30. // This name does not include the extension, viper will search for files
  31. // with supported extensions such as "sftpgo.json", "sftpgo.yaml" and so on
  32. configName = "sftpgo"
  33. // ConfigEnvPrefix defines a prefix that environment variables will use
  34. configEnvPrefix = "sftpgo"
  35. )
  36. var (
  37. globalConf globalConfig
  38. defaultSFTPDBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version)
  39. defaultFTPDBanner = fmt.Sprintf("SFTPGo %v ready", version.Get().Version)
  40. defaultSFTPDBinding = sftpd.Binding{
  41. Address: "",
  42. Port: 2022,
  43. ApplyProxyConfig: true,
  44. }
  45. defaultFTPDBinding = ftpd.Binding{
  46. Address: "",
  47. Port: 0,
  48. ApplyProxyConfig: true,
  49. TLSMode: 0,
  50. ForcePassiveIP: "",
  51. ClientAuthType: 0,
  52. TLSCipherSuites: nil,
  53. PassiveConnectionsSecurity: 0,
  54. ActiveConnectionsSecurity: 0,
  55. Debug: false,
  56. }
  57. defaultWebDAVDBinding = webdavd.Binding{
  58. Address: "",
  59. Port: 0,
  60. EnableHTTPS: false,
  61. ClientAuthType: 0,
  62. TLSCipherSuites: nil,
  63. Prefix: "",
  64. ProxyAllowed: nil,
  65. }
  66. defaultHTTPDBinding = httpd.Binding{
  67. Address: "127.0.0.1",
  68. Port: 8080,
  69. EnableWebAdmin: true,
  70. EnableWebClient: true,
  71. EnableHTTPS: false,
  72. ClientAuthType: 0,
  73. TLSCipherSuites: nil,
  74. ProxyAllowed: nil,
  75. HideLoginURL: 0,
  76. RenderOpenAPI: true,
  77. }
  78. defaultRateLimiter = common.RateLimiterConfig{
  79. Average: 0,
  80. Period: 1000,
  81. Burst: 1,
  82. Type: 2,
  83. Protocols: []string{common.ProtocolSSH, common.ProtocolFTP, common.ProtocolWebDAV, common.ProtocolHTTP},
  84. AllowList: []string{},
  85. GenerateDefenderEvents: false,
  86. EntriesSoftLimit: 100,
  87. EntriesHardLimit: 150,
  88. }
  89. defaultTOTP = mfa.TOTPConfig{
  90. Name: "Default",
  91. Issuer: "SFTPGo",
  92. Algo: mfa.TOTPAlgoSHA1,
  93. }
  94. )
  95. type globalConfig struct {
  96. Common common.Configuration `json:"common" mapstructure:"common"`
  97. SFTPD sftpd.Configuration `json:"sftpd" mapstructure:"sftpd"`
  98. FTPD ftpd.Configuration `json:"ftpd" mapstructure:"ftpd"`
  99. WebDAVD webdavd.Configuration `json:"webdavd" mapstructure:"webdavd"`
  100. ProviderConf dataprovider.Config `json:"data_provider" mapstructure:"data_provider"`
  101. HTTPDConfig httpd.Conf `json:"httpd" mapstructure:"httpd"`
  102. HTTPConfig httpclient.Config `json:"http" mapstructure:"http"`
  103. KMSConfig kms.Configuration `json:"kms" mapstructure:"kms"`
  104. MFAConfig mfa.Config `json:"mfa" mapstructure:"mfa"`
  105. TelemetryConfig telemetry.Conf `json:"telemetry" mapstructure:"telemetry"`
  106. PluginsConfig []plugin.Config `json:"plugins" mapstructure:"plugins"`
  107. SMTPConfig smtp.Config `json:"smtp" mapstructure:"smtp"`
  108. }
  109. func init() {
  110. Init()
  111. }
  112. // Init initializes the global configuration.
  113. // It is not supposed to be called outside of this package.
  114. // It is exported to minimize refactoring efforts. Will eventually disappear.
  115. func Init() {
  116. // create a default configuration to use if no config file is provided
  117. globalConf = globalConfig{
  118. Common: common.Configuration{
  119. IdleTimeout: 15,
  120. UploadMode: 0,
  121. Actions: common.ProtocolActions{
  122. ExecuteOn: []string{},
  123. ExecuteSync: []string{},
  124. Hook: "",
  125. },
  126. SetstatMode: 0,
  127. TempPath: "",
  128. ProxyProtocol: 0,
  129. ProxyAllowed: []string{},
  130. PostConnectHook: "",
  131. PostDisconnectHook: "",
  132. DataRetentionHook: "",
  133. MaxTotalConnections: 0,
  134. MaxPerHostConnections: 20,
  135. DefenderConfig: common.DefenderConfig{
  136. Enabled: false,
  137. BanTime: 30,
  138. BanTimeIncrement: 50,
  139. Threshold: 15,
  140. ScoreInvalid: 2,
  141. ScoreValid: 1,
  142. ScoreLimitExceeded: 3,
  143. ObservationTime: 30,
  144. EntriesSoftLimit: 100,
  145. EntriesHardLimit: 150,
  146. SafeListFile: "",
  147. BlockListFile: "",
  148. },
  149. RateLimitersConfig: []common.RateLimiterConfig{defaultRateLimiter},
  150. },
  151. SFTPD: sftpd.Configuration{
  152. Banner: defaultSFTPDBanner,
  153. Bindings: []sftpd.Binding{defaultSFTPDBinding},
  154. MaxAuthTries: 0,
  155. HostKeys: []string{},
  156. KexAlgorithms: []string{},
  157. Ciphers: []string{},
  158. MACs: []string{},
  159. TrustedUserCAKeys: []string{},
  160. LoginBannerFile: "",
  161. EnabledSSHCommands: []string{},
  162. KeyboardInteractiveAuthentication: false,
  163. KeyboardInteractiveHook: "",
  164. PasswordAuthentication: true,
  165. FolderPrefix: "",
  166. },
  167. FTPD: ftpd.Configuration{
  168. Bindings: []ftpd.Binding{defaultFTPDBinding},
  169. Banner: defaultFTPDBanner,
  170. BannerFile: "",
  171. ActiveTransfersPortNon20: true,
  172. PassivePortRange: ftpd.PortRange{
  173. Start: 50000,
  174. End: 50100,
  175. },
  176. DisableActiveMode: false,
  177. EnableSite: false,
  178. HASHSupport: 0,
  179. CombineSupport: 0,
  180. CertificateFile: "",
  181. CertificateKeyFile: "",
  182. CACertificates: []string{},
  183. CARevocationLists: []string{},
  184. },
  185. WebDAVD: webdavd.Configuration{
  186. Bindings: []webdavd.Binding{defaultWebDAVDBinding},
  187. CertificateFile: "",
  188. CertificateKeyFile: "",
  189. CACertificates: []string{},
  190. CARevocationLists: []string{},
  191. Cors: webdavd.CorsConfig{
  192. Enabled: false,
  193. AllowedOrigins: []string{},
  194. AllowedMethods: []string{},
  195. AllowedHeaders: []string{},
  196. ExposedHeaders: []string{},
  197. AllowCredentials: false,
  198. MaxAge: 0,
  199. },
  200. Cache: webdavd.Cache{
  201. Users: webdavd.UsersCacheConfig{
  202. ExpirationTime: 0,
  203. MaxSize: 50,
  204. },
  205. MimeTypes: webdavd.MimeCacheConfig{
  206. Enabled: true,
  207. MaxSize: 1000,
  208. },
  209. },
  210. },
  211. ProviderConf: dataprovider.Config{
  212. Driver: "sqlite",
  213. Name: "sftpgo.db",
  214. Host: "",
  215. Port: 0,
  216. Username: "",
  217. Password: "",
  218. ConnectionString: "",
  219. SQLTablesPrefix: "",
  220. SSLMode: 0,
  221. TrackQuota: 1,
  222. PoolSize: 0,
  223. UsersBaseDir: "",
  224. Actions: dataprovider.ObjectsActions{
  225. ExecuteOn: []string{},
  226. ExecuteFor: []string{},
  227. Hook: "",
  228. },
  229. ExternalAuthHook: "",
  230. ExternalAuthScope: 0,
  231. CredentialsPath: "credentials",
  232. PreLoginHook: "",
  233. PostLoginHook: "",
  234. PostLoginScope: 0,
  235. CheckPasswordHook: "",
  236. CheckPasswordScope: 0,
  237. PasswordHashing: dataprovider.PasswordHashing{
  238. Argon2Options: dataprovider.Argon2Options{
  239. Memory: 65536,
  240. Iterations: 1,
  241. Parallelism: 2,
  242. },
  243. BcryptOptions: dataprovider.BcryptOptions{
  244. Cost: 10,
  245. },
  246. Algo: dataprovider.HashingAlgoBcrypt,
  247. },
  248. PasswordValidation: dataprovider.PasswordValidation{
  249. Admins: dataprovider.PasswordValidationRules{
  250. MinEntropy: 0,
  251. },
  252. Users: dataprovider.PasswordValidationRules{
  253. MinEntropy: 0,
  254. },
  255. },
  256. PasswordCaching: true,
  257. UpdateMode: 0,
  258. PreferDatabaseCredentials: false,
  259. SkipNaturalKeysValidation: false,
  260. DelayedQuotaUpdate: 0,
  261. CreateDefaultAdmin: false,
  262. IsShared: 0,
  263. },
  264. HTTPDConfig: httpd.Conf{
  265. Bindings: []httpd.Binding{defaultHTTPDBinding},
  266. TemplatesPath: "templates",
  267. StaticFilesPath: "static",
  268. BackupsPath: "backups",
  269. OpenAPIPath: "openapi",
  270. WebRoot: "",
  271. CertificateFile: "",
  272. CertificateKeyFile: "",
  273. CACertificates: nil,
  274. CARevocationLists: nil,
  275. SigningPassphrase: "",
  276. MaxUploadFileSize: 1048576000,
  277. Cors: httpd.CorsConfig{
  278. Enabled: false,
  279. AllowedOrigins: []string{},
  280. AllowedMethods: []string{},
  281. AllowedHeaders: []string{},
  282. ExposedHeaders: []string{},
  283. AllowCredentials: false,
  284. MaxAge: 0,
  285. },
  286. },
  287. HTTPConfig: httpclient.Config{
  288. Timeout: 20,
  289. RetryWaitMin: 2,
  290. RetryWaitMax: 30,
  291. RetryMax: 3,
  292. CACertificates: nil,
  293. Certificates: nil,
  294. SkipTLSVerify: false,
  295. Headers: nil,
  296. },
  297. KMSConfig: kms.Configuration{
  298. Secrets: kms.Secrets{
  299. URL: "",
  300. MasterKeyString: "",
  301. MasterKeyPath: "",
  302. },
  303. },
  304. MFAConfig: mfa.Config{
  305. TOTP: nil,
  306. },
  307. TelemetryConfig: telemetry.Conf{
  308. BindPort: 10000,
  309. BindAddress: "127.0.0.1",
  310. EnableProfiler: false,
  311. AuthUserFile: "",
  312. CertificateFile: "",
  313. CertificateKeyFile: "",
  314. TLSCipherSuites: nil,
  315. },
  316. PluginsConfig: nil,
  317. SMTPConfig: smtp.Config{
  318. Host: "",
  319. Port: 25,
  320. From: "",
  321. User: "",
  322. Password: "",
  323. AuthType: 0,
  324. Encryption: 0,
  325. Domain: "",
  326. TemplatesPath: "templates",
  327. },
  328. }
  329. viper.SetEnvPrefix(configEnvPrefix)
  330. replacer := strings.NewReplacer(".", "__")
  331. viper.SetEnvKeyReplacer(replacer)
  332. viper.SetConfigName(configName)
  333. setViperDefaults()
  334. viper.AutomaticEnv()
  335. viper.AllowEmptyEnv(true)
  336. }
  337. // GetCommonConfig returns the common protocols configuration
  338. func GetCommonConfig() common.Configuration {
  339. return globalConf.Common
  340. }
  341. // SetCommonConfig sets the common protocols configuration
  342. func SetCommonConfig(config common.Configuration) {
  343. globalConf.Common = config
  344. }
  345. // GetSFTPDConfig returns the configuration for the SFTP server
  346. func GetSFTPDConfig() sftpd.Configuration {
  347. return globalConf.SFTPD
  348. }
  349. // SetSFTPDConfig sets the configuration for the SFTP server
  350. func SetSFTPDConfig(config sftpd.Configuration) {
  351. globalConf.SFTPD = config
  352. }
  353. // GetFTPDConfig returns the configuration for the FTP server
  354. func GetFTPDConfig() ftpd.Configuration {
  355. return globalConf.FTPD
  356. }
  357. // SetFTPDConfig sets the configuration for the FTP server
  358. func SetFTPDConfig(config ftpd.Configuration) {
  359. globalConf.FTPD = config
  360. }
  361. // GetWebDAVDConfig returns the configuration for the WebDAV server
  362. func GetWebDAVDConfig() webdavd.Configuration {
  363. return globalConf.WebDAVD
  364. }
  365. // SetWebDAVDConfig sets the configuration for the WebDAV server
  366. func SetWebDAVDConfig(config webdavd.Configuration) {
  367. globalConf.WebDAVD = config
  368. }
  369. // GetHTTPDConfig returns the configuration for the HTTP server
  370. func GetHTTPDConfig() httpd.Conf {
  371. return globalConf.HTTPDConfig
  372. }
  373. // SetHTTPDConfig sets the configuration for the HTTP server
  374. func SetHTTPDConfig(config httpd.Conf) {
  375. globalConf.HTTPDConfig = config
  376. }
  377. // GetProviderConf returns the configuration for the data provider
  378. func GetProviderConf() dataprovider.Config {
  379. return globalConf.ProviderConf
  380. }
  381. // SetProviderConf sets the configuration for the data provider
  382. func SetProviderConf(config dataprovider.Config) {
  383. globalConf.ProviderConf = config
  384. }
  385. // GetHTTPConfig returns the configuration for HTTP clients
  386. func GetHTTPConfig() httpclient.Config {
  387. return globalConf.HTTPConfig
  388. }
  389. // GetKMSConfig returns the KMS configuration
  390. func GetKMSConfig() kms.Configuration {
  391. return globalConf.KMSConfig
  392. }
  393. // SetKMSConfig sets the kms configuration
  394. func SetKMSConfig(config kms.Configuration) {
  395. globalConf.KMSConfig = config
  396. }
  397. // GetTelemetryConfig returns the telemetry configuration
  398. func GetTelemetryConfig() telemetry.Conf {
  399. return globalConf.TelemetryConfig
  400. }
  401. // SetTelemetryConfig sets the telemetry configuration
  402. func SetTelemetryConfig(config telemetry.Conf) {
  403. globalConf.TelemetryConfig = config
  404. }
  405. // GetPluginsConfig returns the plugins configuration
  406. func GetPluginsConfig() []plugin.Config {
  407. return globalConf.PluginsConfig
  408. }
  409. // SetPluginsConfig sets the plugin configuration
  410. func SetPluginsConfig(config []plugin.Config) {
  411. globalConf.PluginsConfig = config
  412. }
  413. // GetMFAConfig returns multi-factor authentication config
  414. func GetMFAConfig() mfa.Config {
  415. return globalConf.MFAConfig
  416. }
  417. // GetSMTPConfig returns the SMTP configuration
  418. func GetSMTPConfig() smtp.Config {
  419. return globalConf.SMTPConfig
  420. }
  421. // HasServicesToStart returns true if the config defines at least a service to start.
  422. // Supported services are SFTP, FTP and WebDAV
  423. func HasServicesToStart() bool {
  424. if globalConf.SFTPD.ShouldBind() {
  425. return true
  426. }
  427. if globalConf.FTPD.ShouldBind() {
  428. return true
  429. }
  430. if globalConf.WebDAVD.ShouldBind() {
  431. return true
  432. }
  433. return false
  434. }
  435. func getRedactedPassword() string {
  436. return "[redacted]"
  437. }
  438. func getRedactedGlobalConf() globalConfig {
  439. conf := globalConf
  440. conf.Common.Actions.Hook = util.GetRedactedURL(conf.Common.Actions.Hook)
  441. conf.Common.StartupHook = util.GetRedactedURL(conf.Common.StartupHook)
  442. conf.Common.PostConnectHook = util.GetRedactedURL(conf.Common.PostConnectHook)
  443. conf.Common.PostDisconnectHook = util.GetRedactedURL(conf.Common.PostDisconnectHook)
  444. conf.Common.DataRetentionHook = util.GetRedactedURL(conf.Common.DataRetentionHook)
  445. conf.SFTPD.KeyboardInteractiveHook = util.GetRedactedURL(conf.SFTPD.KeyboardInteractiveHook)
  446. conf.HTTPDConfig.SigningPassphrase = getRedactedPassword()
  447. conf.ProviderConf.Password = getRedactedPassword()
  448. conf.ProviderConf.Actions.Hook = util.GetRedactedURL(conf.ProviderConf.Actions.Hook)
  449. conf.ProviderConf.ExternalAuthHook = util.GetRedactedURL(conf.ProviderConf.ExternalAuthHook)
  450. conf.ProviderConf.PreLoginHook = util.GetRedactedURL(conf.ProviderConf.PreLoginHook)
  451. conf.ProviderConf.PostLoginHook = util.GetRedactedURL(conf.ProviderConf.PostLoginHook)
  452. conf.ProviderConf.CheckPasswordHook = util.GetRedactedURL(conf.ProviderConf.CheckPasswordHook)
  453. conf.SMTPConfig.Password = getRedactedPassword()
  454. return conf
  455. }
  456. func setConfigFile(configDir, configFile string) {
  457. if configFile == "" {
  458. return
  459. }
  460. if !filepath.IsAbs(configFile) && util.IsFileInputValid(configFile) {
  461. configFile = filepath.Join(configDir, configFile)
  462. }
  463. viper.SetConfigFile(configFile)
  464. }
  465. // LoadConfig loads the configuration
  466. // configDir will be added to the configuration search paths.
  467. // The search path contains by default the current directory and on linux it contains
  468. // $HOME/.config/sftpgo and /etc/sftpgo too.
  469. // configFile is an absolute or relative path (to the config dir) to the configuration file.
  470. func LoadConfig(configDir, configFile string) error {
  471. var err error
  472. viper.AddConfigPath(configDir)
  473. setViperAdditionalConfigPaths()
  474. viper.AddConfigPath(".")
  475. setConfigFile(configDir, configFile)
  476. if err = viper.ReadInConfig(); err != nil {
  477. // if the user specify a configuration file we get os.ErrNotExist.
  478. // viper.ConfigFileNotFoundError is returned if viper is unable
  479. // to find sftpgo.{json,yaml, etc..} in any of the search paths
  480. if errors.As(err, &viper.ConfigFileNotFoundError{}) {
  481. logger.Debug(logSender, "", "no configuration file found")
  482. } else {
  483. // should we return the error and not start here?
  484. logger.Warn(logSender, "", "error loading configuration file: %v", err)
  485. logger.WarnToConsole("error loading configuration file: %v", err)
  486. }
  487. }
  488. err = viper.Unmarshal(&globalConf)
  489. if err != nil {
  490. logger.Warn(logSender, "", "error parsing configuration file: %v", err)
  491. logger.WarnToConsole("error parsing configuration file: %v", err)
  492. return err
  493. }
  494. // viper only supports slice of strings from env vars, so we use our custom method
  495. loadBindingsFromEnv()
  496. if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
  497. globalConf.SFTPD.Banner = defaultSFTPDBanner
  498. }
  499. if strings.TrimSpace(globalConf.FTPD.Banner) == "" {
  500. globalConf.FTPD.Banner = defaultFTPDBanner
  501. }
  502. if globalConf.ProviderConf.UsersBaseDir != "" && !util.IsFileInputValid(globalConf.ProviderConf.UsersBaseDir) {
  503. err = fmt.Errorf("invalid users base dir %#v will be ignored", globalConf.ProviderConf.UsersBaseDir)
  504. globalConf.ProviderConf.UsersBaseDir = ""
  505. logger.Warn(logSender, "", "Configuration error: %v", err)
  506. logger.WarnToConsole("Configuration error: %v", err)
  507. }
  508. if globalConf.Common.UploadMode < 0 || globalConf.Common.UploadMode > 2 {
  509. warn := fmt.Sprintf("invalid upload_mode 0, 1 and 2 are supported, configured: %v reset upload_mode to 0",
  510. globalConf.Common.UploadMode)
  511. globalConf.Common.UploadMode = 0
  512. logger.Warn(logSender, "", "Configuration error: %v", warn)
  513. logger.WarnToConsole("Configuration error: %v", warn)
  514. }
  515. if globalConf.Common.ProxyProtocol < 0 || globalConf.Common.ProxyProtocol > 2 {
  516. warn := fmt.Sprintf("invalid proxy_protocol 0, 1 and 2 are supported, configured: %v reset proxy_protocol to 0",
  517. globalConf.Common.ProxyProtocol)
  518. globalConf.Common.ProxyProtocol = 0
  519. logger.Warn(logSender, "", "Configuration error: %v", warn)
  520. logger.WarnToConsole("Configuration error: %v", warn)
  521. }
  522. if globalConf.ProviderConf.ExternalAuthScope < 0 || globalConf.ProviderConf.ExternalAuthScope > 15 {
  523. warn := fmt.Sprintf("invalid external_auth_scope: %v reset to 0", globalConf.ProviderConf.ExternalAuthScope)
  524. globalConf.ProviderConf.ExternalAuthScope = 0
  525. logger.Warn(logSender, "", "Configuration error: %v", warn)
  526. logger.WarnToConsole("Configuration error: %v", warn)
  527. }
  528. if globalConf.ProviderConf.CredentialsPath == "" {
  529. warn := "invalid credentials path, reset to \"credentials\""
  530. globalConf.ProviderConf.CredentialsPath = "credentials"
  531. logger.Warn(logSender, "", "Configuration error: %v", warn)
  532. logger.WarnToConsole("Configuration error: %v", warn)
  533. }
  534. logger.Debug(logSender, "", "config file used: '%#v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
  535. return nil
  536. }
  537. func loadBindingsFromEnv() {
  538. for idx := 0; idx < 10; idx++ {
  539. getTOTPFromEnv(idx)
  540. getRateLimitersFromEnv(idx)
  541. getPluginsFromEnv(idx)
  542. getSFTPDBindindFromEnv(idx)
  543. getFTPDBindingFromEnv(idx)
  544. getWebDAVDBindingFromEnv(idx)
  545. getHTTPDBindingFromEnv(idx)
  546. getHTTPClientCertificatesFromEnv(idx)
  547. getHTTPClientHeadersFromEnv(idx)
  548. }
  549. }
  550. func getTOTPFromEnv(idx int) {
  551. totpConfig := defaultTOTP
  552. if len(globalConf.MFAConfig.TOTP) > idx {
  553. totpConfig = globalConf.MFAConfig.TOTP[idx]
  554. }
  555. isSet := false
  556. name, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__NAME", idx))
  557. if ok {
  558. totpConfig.Name = name
  559. isSet = true
  560. }
  561. issuer, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ISSUER", idx))
  562. if ok {
  563. totpConfig.Issuer = issuer
  564. isSet = true
  565. }
  566. algo, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ALGO", idx))
  567. if ok {
  568. totpConfig.Algo = algo
  569. isSet = true
  570. }
  571. if isSet {
  572. if len(globalConf.MFAConfig.TOTP) > idx {
  573. globalConf.MFAConfig.TOTP[idx] = totpConfig
  574. } else {
  575. globalConf.MFAConfig.TOTP = append(globalConf.MFAConfig.TOTP, totpConfig)
  576. }
  577. }
  578. }
  579. func getRateLimitersFromEnv(idx int) {
  580. rtlConfig := defaultRateLimiter
  581. if len(globalConf.Common.RateLimitersConfig) > idx {
  582. rtlConfig = globalConf.Common.RateLimitersConfig[idx]
  583. }
  584. isSet := false
  585. average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx))
  586. if ok {
  587. rtlConfig.Average = average
  588. isSet = true
  589. }
  590. period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx))
  591. if ok {
  592. rtlConfig.Period = period
  593. isSet = true
  594. }
  595. burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx))
  596. if ok {
  597. rtlConfig.Burst = int(burst)
  598. isSet = true
  599. }
  600. rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx))
  601. if ok {
  602. rtlConfig.Type = int(rtlType)
  603. isSet = true
  604. }
  605. protocols, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PROTOCOLS", idx))
  606. if ok {
  607. rtlConfig.Protocols = protocols
  608. isSet = true
  609. }
  610. allowList, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ALLOW_LIST", idx))
  611. if ok {
  612. rtlConfig.AllowList = allowList
  613. isSet = true
  614. }
  615. generateEvents, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__GENERATE_DEFENDER_EVENTS", idx))
  616. if ok {
  617. rtlConfig.GenerateDefenderEvents = generateEvents
  618. isSet = true
  619. }
  620. softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx))
  621. if ok {
  622. rtlConfig.EntriesSoftLimit = int(softLimit)
  623. isSet = true
  624. }
  625. hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx))
  626. if ok {
  627. rtlConfig.EntriesHardLimit = int(hardLimit)
  628. isSet = true
  629. }
  630. if isSet {
  631. if len(globalConf.Common.RateLimitersConfig) > idx {
  632. globalConf.Common.RateLimitersConfig[idx] = rtlConfig
  633. } else {
  634. globalConf.Common.RateLimitersConfig = append(globalConf.Common.RateLimitersConfig, rtlConfig)
  635. }
  636. }
  637. }
  638. func getKMSPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  639. isSet := false
  640. kmsScheme, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__SCHEME", idx))
  641. if ok {
  642. pluginConfig.KMSOptions.Scheme = kmsScheme
  643. isSet = true
  644. }
  645. kmsEncStatus, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__ENCRYPTED_STATUS", idx))
  646. if ok {
  647. pluginConfig.KMSOptions.EncryptedStatus = kmsEncStatus
  648. isSet = true
  649. }
  650. return isSet
  651. }
  652. func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  653. isSet := false
  654. authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx))
  655. if ok {
  656. pluginConfig.AuthOptions.Scope = int(authScope)
  657. isSet = true
  658. }
  659. return isSet
  660. }
  661. func getNotifierPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  662. isSet := false
  663. notifierFsEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__FS_EVENTS", idx))
  664. if ok {
  665. pluginConfig.NotifierOptions.FsEvents = notifierFsEvents
  666. isSet = true
  667. }
  668. notifierProviderEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_EVENTS", idx))
  669. if ok {
  670. pluginConfig.NotifierOptions.ProviderEvents = notifierProviderEvents
  671. isSet = true
  672. }
  673. notifierProviderObjects, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_OBJECTS", idx))
  674. if ok {
  675. pluginConfig.NotifierOptions.ProviderObjects = notifierProviderObjects
  676. isSet = true
  677. }
  678. notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx))
  679. if ok {
  680. pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime)
  681. isSet = true
  682. }
  683. notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx))
  684. if ok {
  685. pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize)
  686. isSet = true
  687. }
  688. return isSet
  689. }
  690. func getPluginsFromEnv(idx int) {
  691. pluginConfig := plugin.Config{}
  692. if len(globalConf.PluginsConfig) > idx {
  693. pluginConfig = globalConf.PluginsConfig[idx]
  694. }
  695. isSet := false
  696. pluginType, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__TYPE", idx))
  697. if ok {
  698. pluginConfig.Type = pluginType
  699. isSet = true
  700. }
  701. if getNotifierPluginFromEnv(idx, &pluginConfig) {
  702. isSet = true
  703. }
  704. if getKMSPluginFromEnv(idx, &pluginConfig) {
  705. isSet = true
  706. }
  707. if getAuthPluginFromEnv(idx, &pluginConfig) {
  708. isSet = true
  709. }
  710. cmd, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__CMD", idx))
  711. if ok {
  712. pluginConfig.Cmd = cmd
  713. isSet = true
  714. }
  715. cmdArgs, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__ARGS", idx))
  716. if ok {
  717. pluginConfig.Args = cmdArgs
  718. isSet = true
  719. }
  720. pluginHash, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__SHA256SUM", idx))
  721. if ok {
  722. pluginConfig.SHA256Sum = pluginHash
  723. isSet = true
  724. }
  725. autoMTLS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTO_MTLS", idx))
  726. if ok {
  727. pluginConfig.AutoMTLS = autoMTLS
  728. isSet = true
  729. }
  730. if isSet {
  731. if len(globalConf.PluginsConfig) > idx {
  732. globalConf.PluginsConfig[idx] = pluginConfig
  733. } else {
  734. globalConf.PluginsConfig = append(globalConf.PluginsConfig, pluginConfig)
  735. }
  736. }
  737. }
  738. func getSFTPDBindindFromEnv(idx int) {
  739. binding := sftpd.Binding{
  740. ApplyProxyConfig: true,
  741. }
  742. if len(globalConf.SFTPD.Bindings) > idx {
  743. binding = globalConf.SFTPD.Bindings[idx]
  744. }
  745. isSet := false
  746. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx))
  747. if ok {
  748. binding.Port = int(port)
  749. isSet = true
  750. }
  751. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__ADDRESS", idx))
  752. if ok {
  753. binding.Address = address
  754. isSet = true
  755. }
  756. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  757. if ok {
  758. binding.ApplyProxyConfig = applyProxyConfig
  759. isSet = true
  760. }
  761. if isSet {
  762. if len(globalConf.SFTPD.Bindings) > idx {
  763. globalConf.SFTPD.Bindings[idx] = binding
  764. } else {
  765. globalConf.SFTPD.Bindings = append(globalConf.SFTPD.Bindings, binding)
  766. }
  767. }
  768. }
  769. func getFTPDBindingFromEnv(idx int) {
  770. binding := ftpd.Binding{
  771. ApplyProxyConfig: true,
  772. }
  773. if len(globalConf.FTPD.Bindings) > idx {
  774. binding = globalConf.FTPD.Bindings[idx]
  775. }
  776. isSet := false
  777. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx))
  778. if ok {
  779. binding.Port = int(port)
  780. isSet = true
  781. }
  782. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ADDRESS", idx))
  783. if ok {
  784. binding.Address = address
  785. isSet = true
  786. }
  787. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  788. if ok {
  789. binding.ApplyProxyConfig = applyProxyConfig
  790. isSet = true
  791. }
  792. tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx))
  793. if ok {
  794. binding.TLSMode = int(tlsMode)
  795. isSet = true
  796. }
  797. passiveIP, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__FORCE_PASSIVE_IP", idx))
  798. if ok {
  799. binding.ForcePassiveIP = passiveIP
  800. isSet = true
  801. }
  802. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  803. if ok {
  804. binding.ClientAuthType = int(clientAuthType)
  805. isSet = true
  806. }
  807. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  808. if ok {
  809. binding.TLSCipherSuites = tlsCiphers
  810. isSet = true
  811. }
  812. pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx))
  813. if ok {
  814. binding.PassiveConnectionsSecurity = int(pasvSecurity)
  815. isSet = true
  816. }
  817. activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx))
  818. if ok {
  819. binding.ActiveConnectionsSecurity = int(activeSecurity)
  820. isSet = true
  821. }
  822. debug, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__DEBUG", idx))
  823. if ok {
  824. binding.Debug = debug
  825. isSet = true
  826. }
  827. if isSet {
  828. if len(globalConf.FTPD.Bindings) > idx {
  829. globalConf.FTPD.Bindings[idx] = binding
  830. } else {
  831. globalConf.FTPD.Bindings = append(globalConf.FTPD.Bindings, binding)
  832. }
  833. }
  834. }
  835. func getWebDAVDBindingFromEnv(idx int) {
  836. binding := webdavd.Binding{}
  837. if len(globalConf.WebDAVD.Bindings) > idx {
  838. binding = globalConf.WebDAVD.Bindings[idx]
  839. }
  840. isSet := false
  841. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx))
  842. if ok {
  843. binding.Port = int(port)
  844. isSet = true
  845. }
  846. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ADDRESS", idx))
  847. if ok {
  848. binding.Address = address
  849. isSet = true
  850. }
  851. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ENABLE_HTTPS", idx))
  852. if ok {
  853. binding.EnableHTTPS = enableHTTPS
  854. isSet = true
  855. }
  856. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  857. if ok {
  858. binding.ClientAuthType = int(clientAuthType)
  859. isSet = true
  860. }
  861. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  862. if ok {
  863. binding.TLSCipherSuites = tlsCiphers
  864. isSet = true
  865. }
  866. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PROXY_ALLOWED", idx))
  867. if ok {
  868. binding.ProxyAllowed = proxyAllowed
  869. isSet = true
  870. }
  871. prefix, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PREFIX", idx))
  872. if ok {
  873. binding.Prefix = prefix
  874. isSet = true
  875. }
  876. if isSet {
  877. if len(globalConf.WebDAVD.Bindings) > idx {
  878. globalConf.WebDAVD.Bindings[idx] = binding
  879. } else {
  880. globalConf.WebDAVD.Bindings = append(globalConf.WebDAVD.Bindings, binding)
  881. }
  882. }
  883. }
  884. func getHTTPDBindingFromEnv(idx int) {
  885. binding := httpd.Binding{
  886. EnableWebAdmin: true,
  887. EnableWebClient: true,
  888. RenderOpenAPI: true,
  889. }
  890. if len(globalConf.HTTPDConfig.Bindings) > idx {
  891. binding = globalConf.HTTPDConfig.Bindings[idx]
  892. }
  893. isSet := false
  894. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx))
  895. if ok {
  896. binding.Port = int(port)
  897. isSet = true
  898. }
  899. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ADDRESS", idx))
  900. if ok {
  901. binding.Address = address
  902. isSet = true
  903. }
  904. enableWebAdmin, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_ADMIN", idx))
  905. if ok {
  906. binding.EnableWebAdmin = enableWebAdmin
  907. isSet = true
  908. }
  909. enableWebClient, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_CLIENT", idx))
  910. if ok {
  911. binding.EnableWebClient = enableWebClient
  912. isSet = true
  913. }
  914. renderOpenAPI, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__RENDER_OPENAPI", idx))
  915. if ok {
  916. binding.RenderOpenAPI = renderOpenAPI
  917. isSet = true
  918. }
  919. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_HTTPS", idx))
  920. if ok {
  921. binding.EnableHTTPS = enableHTTPS
  922. isSet = true
  923. }
  924. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  925. if ok {
  926. binding.ClientAuthType = int(clientAuthType)
  927. isSet = true
  928. }
  929. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  930. if ok {
  931. binding.TLSCipherSuites = tlsCiphers
  932. isSet = true
  933. }
  934. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PROXY_ALLOWED", idx))
  935. if ok {
  936. binding.ProxyAllowed = proxyAllowed
  937. isSet = true
  938. }
  939. hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx))
  940. if ok {
  941. binding.HideLoginURL = int(hideLoginURL)
  942. isSet = true
  943. }
  944. if isSet {
  945. if len(globalConf.HTTPDConfig.Bindings) > idx {
  946. globalConf.HTTPDConfig.Bindings[idx] = binding
  947. } else {
  948. globalConf.HTTPDConfig.Bindings = append(globalConf.HTTPDConfig.Bindings, binding)
  949. }
  950. }
  951. }
  952. func getHTTPClientCertificatesFromEnv(idx int) {
  953. tlsCert := httpclient.TLSKeyPair{}
  954. cert, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__CERT", idx))
  955. if ok {
  956. tlsCert.Cert = cert
  957. }
  958. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__KEY", idx))
  959. if ok {
  960. tlsCert.Key = key
  961. }
  962. if tlsCert.Cert != "" && tlsCert.Key != "" {
  963. if len(globalConf.HTTPConfig.Certificates) > idx {
  964. globalConf.HTTPConfig.Certificates[idx] = tlsCert
  965. } else {
  966. globalConf.HTTPConfig.Certificates = append(globalConf.HTTPConfig.Certificates, tlsCert)
  967. }
  968. }
  969. }
  970. func getHTTPClientHeadersFromEnv(idx int) {
  971. header := httpclient.Header{}
  972. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__KEY", idx))
  973. if ok {
  974. header.Key = key
  975. }
  976. value, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__VALUE", idx))
  977. if ok {
  978. header.Value = value
  979. }
  980. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__URL", idx))
  981. if ok {
  982. header.URL = url
  983. }
  984. if header.Key != "" && header.Value != "" {
  985. if len(globalConf.HTTPConfig.Headers) > idx {
  986. globalConf.HTTPConfig.Headers[idx] = header
  987. } else {
  988. globalConf.HTTPConfig.Headers = append(globalConf.HTTPConfig.Headers, header)
  989. }
  990. }
  991. }
  992. func setViperDefaults() {
  993. viper.SetDefault("common.idle_timeout", globalConf.Common.IdleTimeout)
  994. viper.SetDefault("common.upload_mode", globalConf.Common.UploadMode)
  995. viper.SetDefault("common.actions.execute_on", globalConf.Common.Actions.ExecuteOn)
  996. viper.SetDefault("common.actions.execute_sync", globalConf.Common.Actions.ExecuteSync)
  997. viper.SetDefault("common.actions.hook", globalConf.Common.Actions.Hook)
  998. viper.SetDefault("common.setstat_mode", globalConf.Common.SetstatMode)
  999. viper.SetDefault("common.temp_path", globalConf.Common.TempPath)
  1000. viper.SetDefault("common.proxy_protocol", globalConf.Common.ProxyProtocol)
  1001. viper.SetDefault("common.proxy_allowed", globalConf.Common.ProxyAllowed)
  1002. viper.SetDefault("common.post_connect_hook", globalConf.Common.PostConnectHook)
  1003. viper.SetDefault("common.post_disconnect_hook", globalConf.Common.PostDisconnectHook)
  1004. viper.SetDefault("common.data_retention_hook", globalConf.Common.DataRetentionHook)
  1005. viper.SetDefault("common.max_total_connections", globalConf.Common.MaxTotalConnections)
  1006. viper.SetDefault("common.max_per_host_connections", globalConf.Common.MaxPerHostConnections)
  1007. viper.SetDefault("common.defender.enabled", globalConf.Common.DefenderConfig.Enabled)
  1008. viper.SetDefault("common.defender.ban_time", globalConf.Common.DefenderConfig.BanTime)
  1009. viper.SetDefault("common.defender.ban_time_increment", globalConf.Common.DefenderConfig.BanTimeIncrement)
  1010. viper.SetDefault("common.defender.threshold", globalConf.Common.DefenderConfig.Threshold)
  1011. viper.SetDefault("common.defender.score_invalid", globalConf.Common.DefenderConfig.ScoreInvalid)
  1012. viper.SetDefault("common.defender.score_valid", globalConf.Common.DefenderConfig.ScoreValid)
  1013. viper.SetDefault("common.defender.score_limit_exceeded", globalConf.Common.DefenderConfig.ScoreLimitExceeded)
  1014. viper.SetDefault("common.defender.observation_time", globalConf.Common.DefenderConfig.ObservationTime)
  1015. viper.SetDefault("common.defender.entries_soft_limit", globalConf.Common.DefenderConfig.EntriesSoftLimit)
  1016. viper.SetDefault("common.defender.entries_hard_limit", globalConf.Common.DefenderConfig.EntriesHardLimit)
  1017. viper.SetDefault("common.defender.safelist_file", globalConf.Common.DefenderConfig.SafeListFile)
  1018. viper.SetDefault("common.defender.blocklist_file", globalConf.Common.DefenderConfig.BlockListFile)
  1019. viper.SetDefault("sftpd.max_auth_tries", globalConf.SFTPD.MaxAuthTries)
  1020. viper.SetDefault("sftpd.banner", globalConf.SFTPD.Banner)
  1021. viper.SetDefault("sftpd.host_keys", globalConf.SFTPD.HostKeys)
  1022. viper.SetDefault("sftpd.kex_algorithms", globalConf.SFTPD.KexAlgorithms)
  1023. viper.SetDefault("sftpd.ciphers", globalConf.SFTPD.Ciphers)
  1024. viper.SetDefault("sftpd.macs", globalConf.SFTPD.MACs)
  1025. viper.SetDefault("sftpd.trusted_user_ca_keys", globalConf.SFTPD.TrustedUserCAKeys)
  1026. viper.SetDefault("sftpd.login_banner_file", globalConf.SFTPD.LoginBannerFile)
  1027. viper.SetDefault("sftpd.enabled_ssh_commands", sftpd.GetDefaultSSHCommands())
  1028. viper.SetDefault("sftpd.keyboard_interactive_authentication", globalConf.SFTPD.KeyboardInteractiveAuthentication)
  1029. viper.SetDefault("sftpd.keyboard_interactive_auth_hook", globalConf.SFTPD.KeyboardInteractiveHook)
  1030. viper.SetDefault("sftpd.password_authentication", globalConf.SFTPD.PasswordAuthentication)
  1031. viper.SetDefault("sftpd.folder_prefix", globalConf.SFTPD.FolderPrefix)
  1032. viper.SetDefault("ftpd.banner", globalConf.FTPD.Banner)
  1033. viper.SetDefault("ftpd.banner_file", globalConf.FTPD.BannerFile)
  1034. viper.SetDefault("ftpd.active_transfers_port_non_20", globalConf.FTPD.ActiveTransfersPortNon20)
  1035. viper.SetDefault("ftpd.passive_port_range.start", globalConf.FTPD.PassivePortRange.Start)
  1036. viper.SetDefault("ftpd.passive_port_range.end", globalConf.FTPD.PassivePortRange.End)
  1037. viper.SetDefault("ftpd.disable_active_mode", globalConf.FTPD.DisableActiveMode)
  1038. viper.SetDefault("ftpd.enable_site", globalConf.FTPD.EnableSite)
  1039. viper.SetDefault("ftpd.hash_support", globalConf.FTPD.HASHSupport)
  1040. viper.SetDefault("ftpd.combine_support", globalConf.FTPD.CombineSupport)
  1041. viper.SetDefault("ftpd.certificate_file", globalConf.FTPD.CertificateFile)
  1042. viper.SetDefault("ftpd.certificate_key_file", globalConf.FTPD.CertificateKeyFile)
  1043. viper.SetDefault("ftpd.ca_certificates", globalConf.FTPD.CACertificates)
  1044. viper.SetDefault("ftpd.ca_revocation_lists", globalConf.FTPD.CARevocationLists)
  1045. viper.SetDefault("webdavd.certificate_file", globalConf.WebDAVD.CertificateFile)
  1046. viper.SetDefault("webdavd.certificate_key_file", globalConf.WebDAVD.CertificateKeyFile)
  1047. viper.SetDefault("webdavd.ca_certificates", globalConf.WebDAVD.CACertificates)
  1048. viper.SetDefault("webdavd.ca_revocation_lists", globalConf.WebDAVD.CARevocationLists)
  1049. viper.SetDefault("webdavd.cors.enabled", globalConf.WebDAVD.Cors.Enabled)
  1050. viper.SetDefault("webdavd.cors.allowed_origins", globalConf.WebDAVD.Cors.AllowedOrigins)
  1051. viper.SetDefault("webdavd.cors.allowed_methods", globalConf.WebDAVD.Cors.AllowedMethods)
  1052. viper.SetDefault("webdavd.cors.allowed_headers", globalConf.WebDAVD.Cors.AllowedHeaders)
  1053. viper.SetDefault("webdavd.cors.exposed_headers", globalConf.WebDAVD.Cors.ExposedHeaders)
  1054. viper.SetDefault("webdavd.cors.allow_credentials", globalConf.WebDAVD.Cors.AllowCredentials)
  1055. viper.SetDefault("webdavd.cors.max_age", globalConf.WebDAVD.Cors.MaxAge)
  1056. viper.SetDefault("webdavd.cache.users.expiration_time", globalConf.WebDAVD.Cache.Users.ExpirationTime)
  1057. viper.SetDefault("webdavd.cache.users.max_size", globalConf.WebDAVD.Cache.Users.MaxSize)
  1058. viper.SetDefault("webdavd.cache.mime_types.enabled", globalConf.WebDAVD.Cache.MimeTypes.Enabled)
  1059. viper.SetDefault("webdavd.cache.mime_types.max_size", globalConf.WebDAVD.Cache.MimeTypes.MaxSize)
  1060. viper.SetDefault("data_provider.driver", globalConf.ProviderConf.Driver)
  1061. viper.SetDefault("data_provider.name", globalConf.ProviderConf.Name)
  1062. viper.SetDefault("data_provider.host", globalConf.ProviderConf.Host)
  1063. viper.SetDefault("data_provider.port", globalConf.ProviderConf.Port)
  1064. viper.SetDefault("data_provider.username", globalConf.ProviderConf.Username)
  1065. viper.SetDefault("data_provider.password", globalConf.ProviderConf.Password)
  1066. viper.SetDefault("data_provider.sslmode", globalConf.ProviderConf.SSLMode)
  1067. viper.SetDefault("data_provider.connection_string", globalConf.ProviderConf.ConnectionString)
  1068. viper.SetDefault("data_provider.sql_tables_prefix", globalConf.ProviderConf.SQLTablesPrefix)
  1069. viper.SetDefault("data_provider.track_quota", globalConf.ProviderConf.TrackQuota)
  1070. viper.SetDefault("data_provider.pool_size", globalConf.ProviderConf.PoolSize)
  1071. viper.SetDefault("data_provider.users_base_dir", globalConf.ProviderConf.UsersBaseDir)
  1072. viper.SetDefault("data_provider.actions.execute_on", globalConf.ProviderConf.Actions.ExecuteOn)
  1073. viper.SetDefault("data_provider.actions.execute_for", globalConf.ProviderConf.Actions.ExecuteFor)
  1074. viper.SetDefault("data_provider.actions.hook", globalConf.ProviderConf.Actions.Hook)
  1075. viper.SetDefault("data_provider.external_auth_hook", globalConf.ProviderConf.ExternalAuthHook)
  1076. viper.SetDefault("data_provider.external_auth_scope", globalConf.ProviderConf.ExternalAuthScope)
  1077. viper.SetDefault("data_provider.credentials_path", globalConf.ProviderConf.CredentialsPath)
  1078. viper.SetDefault("data_provider.prefer_database_credentials", globalConf.ProviderConf.PreferDatabaseCredentials)
  1079. viper.SetDefault("data_provider.pre_login_hook", globalConf.ProviderConf.PreLoginHook)
  1080. viper.SetDefault("data_provider.post_login_hook", globalConf.ProviderConf.PostLoginHook)
  1081. viper.SetDefault("data_provider.post_login_scope", globalConf.ProviderConf.PostLoginScope)
  1082. viper.SetDefault("data_provider.check_password_hook", globalConf.ProviderConf.CheckPasswordHook)
  1083. viper.SetDefault("data_provider.check_password_scope", globalConf.ProviderConf.CheckPasswordScope)
  1084. viper.SetDefault("data_provider.password_hashing.bcrypt_options.cost", globalConf.ProviderConf.PasswordHashing.BcryptOptions.Cost)
  1085. viper.SetDefault("data_provider.password_hashing.argon2_options.memory", globalConf.ProviderConf.PasswordHashing.Argon2Options.Memory)
  1086. viper.SetDefault("data_provider.password_hashing.argon2_options.iterations", globalConf.ProviderConf.PasswordHashing.Argon2Options.Iterations)
  1087. viper.SetDefault("data_provider.password_hashing.argon2_options.parallelism", globalConf.ProviderConf.PasswordHashing.Argon2Options.Parallelism)
  1088. viper.SetDefault("data_provider.password_hashing.algo", globalConf.ProviderConf.PasswordHashing.Algo)
  1089. viper.SetDefault("data_provider.password_validation.admins.min_entropy", globalConf.ProviderConf.PasswordValidation.Admins.MinEntropy)
  1090. viper.SetDefault("data_provider.password_validation.users.min_entropy", globalConf.ProviderConf.PasswordValidation.Users.MinEntropy)
  1091. viper.SetDefault("data_provider.password_caching", globalConf.ProviderConf.PasswordCaching)
  1092. viper.SetDefault("data_provider.update_mode", globalConf.ProviderConf.UpdateMode)
  1093. viper.SetDefault("data_provider.skip_natural_keys_validation", globalConf.ProviderConf.SkipNaturalKeysValidation)
  1094. viper.SetDefault("data_provider.delayed_quota_update", globalConf.ProviderConf.DelayedQuotaUpdate)
  1095. viper.SetDefault("data_provider.create_default_admin", globalConf.ProviderConf.CreateDefaultAdmin)
  1096. viper.SetDefault("data_provider.is_shared", globalConf.ProviderConf.IsShared)
  1097. viper.SetDefault("httpd.templates_path", globalConf.HTTPDConfig.TemplatesPath)
  1098. viper.SetDefault("httpd.static_files_path", globalConf.HTTPDConfig.StaticFilesPath)
  1099. viper.SetDefault("httpd.backups_path", globalConf.HTTPDConfig.BackupsPath)
  1100. viper.SetDefault("httpd.openapi_path", globalConf.HTTPDConfig.OpenAPIPath)
  1101. viper.SetDefault("httpd.web_root", globalConf.HTTPDConfig.WebRoot)
  1102. viper.SetDefault("httpd.certificate_file", globalConf.HTTPDConfig.CertificateFile)
  1103. viper.SetDefault("httpd.certificate_key_file", globalConf.HTTPDConfig.CertificateKeyFile)
  1104. viper.SetDefault("httpd.ca_certificates", globalConf.HTTPDConfig.CACertificates)
  1105. viper.SetDefault("httpd.ca_revocation_lists", globalConf.HTTPDConfig.CARevocationLists)
  1106. viper.SetDefault("httpd.signing_passphrase", globalConf.HTTPDConfig.SigningPassphrase)
  1107. viper.SetDefault("httpd.max_upload_file_size", globalConf.HTTPDConfig.MaxUploadFileSize)
  1108. viper.SetDefault("httpd.cors.enabled", globalConf.HTTPDConfig.Cors.Enabled)
  1109. viper.SetDefault("httpd.cors.allowed_origins", globalConf.HTTPDConfig.Cors.AllowedOrigins)
  1110. viper.SetDefault("httpd.cors.allowed_methods", globalConf.HTTPDConfig.Cors.AllowedMethods)
  1111. viper.SetDefault("httpd.cors.allowed_headers", globalConf.HTTPDConfig.Cors.AllowedHeaders)
  1112. viper.SetDefault("httpd.cors.exposed_headers", globalConf.HTTPDConfig.Cors.ExposedHeaders)
  1113. viper.SetDefault("httpd.cors.allow_credentials", globalConf.HTTPDConfig.Cors.AllowCredentials)
  1114. viper.SetDefault("httpd.cors.max_age", globalConf.HTTPDConfig.Cors.MaxAge)
  1115. viper.SetDefault("http.timeout", globalConf.HTTPConfig.Timeout)
  1116. viper.SetDefault("http.retry_wait_min", globalConf.HTTPConfig.RetryWaitMin)
  1117. viper.SetDefault("http.retry_wait_max", globalConf.HTTPConfig.RetryWaitMax)
  1118. viper.SetDefault("http.retry_max", globalConf.HTTPConfig.RetryMax)
  1119. viper.SetDefault("http.ca_certificates", globalConf.HTTPConfig.CACertificates)
  1120. viper.SetDefault("http.skip_tls_verify", globalConf.HTTPConfig.SkipTLSVerify)
  1121. viper.SetDefault("kms.secrets.url", globalConf.KMSConfig.Secrets.URL)
  1122. viper.SetDefault("kms.secrets.master_key", globalConf.KMSConfig.Secrets.MasterKeyString)
  1123. viper.SetDefault("kms.secrets.master_key_path", globalConf.KMSConfig.Secrets.MasterKeyPath)
  1124. viper.SetDefault("telemetry.bind_port", globalConf.TelemetryConfig.BindPort)
  1125. viper.SetDefault("telemetry.bind_address", globalConf.TelemetryConfig.BindAddress)
  1126. viper.SetDefault("telemetry.enable_profiler", globalConf.TelemetryConfig.EnableProfiler)
  1127. viper.SetDefault("telemetry.auth_user_file", globalConf.TelemetryConfig.AuthUserFile)
  1128. viper.SetDefault("telemetry.certificate_file", globalConf.TelemetryConfig.CertificateFile)
  1129. viper.SetDefault("telemetry.certificate_key_file", globalConf.TelemetryConfig.CertificateKeyFile)
  1130. viper.SetDefault("telemetry.tls_cipher_suites", globalConf.TelemetryConfig.TLSCipherSuites)
  1131. viper.SetDefault("smtp.host", globalConf.SMTPConfig.Host)
  1132. viper.SetDefault("smtp.port", globalConf.SMTPConfig.Port)
  1133. viper.SetDefault("smtp.from", globalConf.SMTPConfig.From)
  1134. viper.SetDefault("smtp.user", globalConf.SMTPConfig.User)
  1135. viper.SetDefault("smtp.password", globalConf.SMTPConfig.Password)
  1136. viper.SetDefault("smtp.auth_type", globalConf.SMTPConfig.AuthType)
  1137. viper.SetDefault("smtp.encryption", globalConf.SMTPConfig.Encryption)
  1138. viper.SetDefault("smtp.domain", globalConf.SMTPConfig.Domain)
  1139. viper.SetDefault("smtp.templates_path", globalConf.SMTPConfig.TemplatesPath)
  1140. }
  1141. func lookupBoolFromEnv(envName string) (bool, bool) {
  1142. value, ok := os.LookupEnv(envName)
  1143. if ok {
  1144. converted, err := strconv.ParseBool(strings.TrimSpace(value))
  1145. if err == nil {
  1146. return converted, ok
  1147. }
  1148. }
  1149. return false, false
  1150. }
  1151. func lookupIntFromEnv(envName string) (int64, bool) {
  1152. value, ok := os.LookupEnv(envName)
  1153. if ok {
  1154. converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
  1155. if err == nil {
  1156. return converted, ok
  1157. }
  1158. }
  1159. return 0, false
  1160. }
  1161. func lookupStringListFromEnv(envName string) ([]string, bool) {
  1162. value, ok := os.LookupEnv(envName)
  1163. if ok {
  1164. var result []string
  1165. for _, v := range strings.Split(value, ",") {
  1166. val := strings.TrimSpace(v)
  1167. if val != "" {
  1168. result = append(result, val)
  1169. }
  1170. }
  1171. return result, true
  1172. }
  1173. return nil, false
  1174. }