config.go 37 KB

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