config.go 67 KB

12345678910111213141516171819202122232425262728293031323334353637383940414243444546474849505152535455565758596061626364656667686970717273747576777879808182838485868788899091929394959697989910010110210310410510610710810911011111211311411511611711811912012112212312412512612712812913013113213313413513613713813914014114214314414514614714814915015115215315415515615715815916016116216316416516616716816917017117217317417517617717817918018118218318418518618718818919019119219319419519619719819920020120220320420520620720820921021121221321421521621721821922022122222322422522622722822923023123223323423523623723823924024124224324424524624724824925025125225325425525625725825926026126226326426526626726826927027127227327427527627727827928028128228328428528628728828929029129229329429529629729829930030130230330430530630730830931031131231331431531631731831932032132232332432532632732832933033133233333433533633733833934034134234334434534634734834935035135235335435535635735835936036136236336436536636736836937037137237337437537637737837938038138238338438538638738838939039139239339439539639739839940040140240340440540640740840941041141241341441541641741841942042142242342442542642742842943043143243343443543643743843944044144244344444544644744844945045145245345445545645745845946046146246346446546646746846947047147247347447547647747847948048148248348448548648748848949049149249349449549649749849950050150250350450550650750850951051151251351451551651751851952052152252352452552652752852953053153253353453553653753853954054154254354454554654754854955055155255355455555655755855956056156256356456556656756856957057157257357457557657757857958058158258358458558658758858959059159259359459559659759859960060160260360460560660760860961061161261361461561661761861962062162262362462562662762862963063163263363463563663763863964064164264364464564664764864965065165265365465565665765865966066166266366466566666766866967067167267367467567667767867968068168268368468568668768868969069169269369469569669769869970070170270370470570670770870971071171271371471571671771871972072172272372472572672772872973073173273373473573673773873974074174274374474574674774874975075175275375475575675775875976076176276376476576676776876977077177277377477577677777877978078178278378478578678778878979079179279379479579679779879980080180280380480580680780880981081181281381481581681781881982082182282382482582682782882983083183283383483583683783883984084184284384484584684784884985085185285385485585685785885986086186286386486586686786886987087187287387487587687787887988088188288388488588688788888989089189289389489589689789889990090190290390490590690790890991091191291391491591691791891992092192292392492592692792892993093193293393493593693793893994094194294394494594694794894995095195295395495595695795895996096196296396496596696796896997097197297397497597697797897998098198298398498598698798898999099199299399499599699799899910001001100210031004100510061007100810091010101110121013101410151016101710181019102010211022102310241025102610271028102910301031103210331034103510361037103810391040104110421043104410451046104710481049105010511052105310541055105610571058105910601061106210631064106510661067106810691070107110721073107410751076107710781079108010811082108310841085108610871088108910901091109210931094109510961097109810991100110111021103110411051106110711081109111011111112111311141115111611171118111911201121112211231124112511261127112811291130113111321133113411351136113711381139114011411142114311441145114611471148114911501151115211531154115511561157115811591160116111621163116411651166116711681169117011711172117311741175117611771178117911801181118211831184118511861187118811891190119111921193119411951196119711981199120012011202120312041205120612071208120912101211121212131214121512161217121812191220122112221223122412251226122712281229123012311232123312341235123612371238123912401241124212431244124512461247124812491250125112521253125412551256125712581259126012611262126312641265126612671268126912701271127212731274127512761277127812791280128112821283128412851286128712881289129012911292129312941295129612971298129913001301130213031304130513061307130813091310131113121313131413151316131713181319132013211322132313241325132613271328132913301331133213331334133513361337133813391340134113421343134413451346134713481349135013511352135313541355135613571358135913601361136213631364136513661367136813691370137113721373137413751376137713781379138013811382138313841385138613871388138913901391139213931394139513961397139813991400140114021403140414051406140714081409141014111412141314141415141614171418141914201421142214231424142514261427142814291430143114321433143414351436143714381439144014411442144314441445144614471448144914501451145214531454145514561457145814591460146114621463146414651466146714681469147014711472147314741475147614771478147914801481148214831484148514861487148814891490149114921493149414951496149714981499150015011502150315041505150615071508150915101511151215131514151515161517151815191520152115221523152415251526152715281529153015311532153315341535153615371538153915401541154215431544154515461547154815491550155115521553155415551556155715581559156015611562156315641565156615671568156915701571157215731574157515761577157815791580158115821583158415851586158715881589159015911592159315941595159615971598159916001601160216031604160516061607160816091610161116121613161416151616161716181619162016211622162316241625162616271628162916301631163216331634163516361637163816391640164116421643164416451646164716481649165016511652165316541655165616571658165916601661166216631664166516661667166816691670167116721673167416751676167716781679168016811682168316841685168616871688168916901691169216931694169516961697169816991700170117021703170417051706170717081709171017111712171317141715171617171718171917201721172217231724172517261727172817291730173117321733173417351736173717381739174017411742174317441745174617471748174917501751175217531754175517561757175817591760176117621763176417651766176717681769177017711772177317741775177617771778177917801781178217831784178517861787178817891790179117921793179417951796179717981799180018011802180318041805180618071808180918101811181218131814181518161817181818191820182118221823182418251826182718281829183018311832183318341835183618371838183918401841184218431844184518461847184818491850185118521853185418551856185718581859186018611862186318641865186618671868186918701871187218731874187518761877187818791880188118821883188418851886188718881889189018911892189318941895189618971898189919001901190219031904190519061907190819091910191119121913191419151916191719181919192019211922192319241925192619271928192919301931193219331934193519361937193819391940194119421943194419451946194719481949195019511952195319541955195619571958195919601961196219631964196519661967196819691970197119721973197419751976197719781979198019811982198319841985198619871988198919901991199219931994199519961997199819992000200120022003200420052006200720082009201020112012201320142015201620172018201920202021202220232024202520262027
  1. // Copyright (C) 2019-2022 Nicola Murino
  2. //
  3. // This program is free software: you can redistribute it and/or modify
  4. // it under the terms of the GNU Affero General Public License as published
  5. // by the Free Software Foundation, version 3.
  6. //
  7. // This program is distributed in the hope that it will be useful,
  8. // but WITHOUT ANY WARRANTY; without even the implied warranty of
  9. // MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE. See the
  10. // GNU Affero General Public License for more details.
  11. //
  12. // You should have received a copy of the GNU Affero General Public License
  13. // along with this program. If not, see <https://www.gnu.org/licenses/>.
  14. // Package config manages the configuration
  15. package config
  16. import (
  17. "errors"
  18. "fmt"
  19. "os"
  20. "path/filepath"
  21. "strconv"
  22. "strings"
  23. "github.com/spf13/viper"
  24. "github.com/drakkan/sftpgo/v2/acme"
  25. "github.com/drakkan/sftpgo/v2/command"
  26. "github.com/drakkan/sftpgo/v2/common"
  27. "github.com/drakkan/sftpgo/v2/dataprovider"
  28. "github.com/drakkan/sftpgo/v2/ftpd"
  29. "github.com/drakkan/sftpgo/v2/httpclient"
  30. "github.com/drakkan/sftpgo/v2/httpd"
  31. "github.com/drakkan/sftpgo/v2/kms"
  32. "github.com/drakkan/sftpgo/v2/logger"
  33. "github.com/drakkan/sftpgo/v2/mfa"
  34. "github.com/drakkan/sftpgo/v2/plugin"
  35. "github.com/drakkan/sftpgo/v2/sftpd"
  36. "github.com/drakkan/sftpgo/v2/smtp"
  37. "github.com/drakkan/sftpgo/v2/telemetry"
  38. "github.com/drakkan/sftpgo/v2/util"
  39. "github.com/drakkan/sftpgo/v2/version"
  40. "github.com/drakkan/sftpgo/v2/webdavd"
  41. )
  42. const (
  43. logSender = "config"
  44. // configName defines the name for config file.
  45. // This name does not include the extension, viper will search for files
  46. // with supported extensions such as "sftpgo.json", "sftpgo.yaml" and so on
  47. configName = "sftpgo"
  48. // ConfigEnvPrefix defines a prefix that environment variables will use
  49. configEnvPrefix = "sftpgo"
  50. )
  51. var (
  52. globalConf globalConfig
  53. defaultSFTPDBanner = fmt.Sprintf("SFTPGo_%v", version.Get().Version)
  54. defaultFTPDBanner = fmt.Sprintf("SFTPGo %v ready", version.Get().Version)
  55. defaultInstallCodeHint = "Installation code"
  56. defaultSFTPDBinding = sftpd.Binding{
  57. Address: "",
  58. Port: 2022,
  59. ApplyProxyConfig: true,
  60. }
  61. defaultFTPDBinding = ftpd.Binding{
  62. Address: "",
  63. Port: 0,
  64. ApplyProxyConfig: true,
  65. TLSMode: 0,
  66. CertificateFile: "",
  67. CertificateKeyFile: "",
  68. MinTLSVersion: 12,
  69. ForcePassiveIP: "",
  70. PassiveIPOverrides: nil,
  71. ClientAuthType: 0,
  72. TLSCipherSuites: nil,
  73. PassiveConnectionsSecurity: 0,
  74. ActiveConnectionsSecurity: 0,
  75. Debug: false,
  76. }
  77. defaultWebDAVDBinding = webdavd.Binding{
  78. Address: "",
  79. Port: 0,
  80. EnableHTTPS: false,
  81. CertificateFile: "",
  82. CertificateKeyFile: "",
  83. MinTLSVersion: 12,
  84. ClientAuthType: 0,
  85. TLSCipherSuites: nil,
  86. Prefix: "",
  87. ProxyAllowed: nil,
  88. ClientIPProxyHeader: "",
  89. ClientIPHeaderDepth: 0,
  90. }
  91. defaultHTTPDBinding = httpd.Binding{
  92. Address: "",
  93. Port: 8080,
  94. EnableWebAdmin: true,
  95. EnableWebClient: true,
  96. EnabledLoginMethods: 0,
  97. EnableHTTPS: false,
  98. CertificateFile: "",
  99. CertificateKeyFile: "",
  100. MinTLSVersion: 12,
  101. ClientAuthType: 0,
  102. TLSCipherSuites: nil,
  103. ProxyAllowed: nil,
  104. ClientIPProxyHeader: "",
  105. ClientIPHeaderDepth: 0,
  106. HideLoginURL: 0,
  107. RenderOpenAPI: true,
  108. WebClientIntegrations: nil,
  109. OIDC: httpd.OIDC{
  110. ClientID: "",
  111. ClientSecret: "",
  112. ConfigURL: "",
  113. RedirectBaseURL: "",
  114. UsernameField: "",
  115. RoleField: "",
  116. ImplicitRoles: false,
  117. Scopes: []string{"openid", "profile", "email"},
  118. CustomFields: []string{},
  119. },
  120. Security: httpd.SecurityConf{
  121. Enabled: false,
  122. AllowedHosts: nil,
  123. AllowedHostsAreRegex: false,
  124. HostsProxyHeaders: nil,
  125. HTTPSRedirect: false,
  126. HTTPSHost: "",
  127. HTTPSProxyHeaders: nil,
  128. STSSeconds: 0,
  129. STSIncludeSubdomains: false,
  130. STSPreload: false,
  131. ContentTypeNosniff: false,
  132. ContentSecurityPolicy: "",
  133. PermissionsPolicy: "",
  134. CrossOriginOpenerPolicy: "",
  135. ExpectCTHeader: "",
  136. },
  137. Branding: httpd.Branding{},
  138. }
  139. defaultRateLimiter = common.RateLimiterConfig{
  140. Average: 0,
  141. Period: 1000,
  142. Burst: 1,
  143. Type: 2,
  144. Protocols: []string{common.ProtocolSSH, common.ProtocolFTP, common.ProtocolWebDAV, common.ProtocolHTTP},
  145. AllowList: []string{},
  146. GenerateDefenderEvents: false,
  147. EntriesSoftLimit: 100,
  148. EntriesHardLimit: 150,
  149. }
  150. defaultTOTP = mfa.TOTPConfig{
  151. Name: "Default",
  152. Issuer: "SFTPGo",
  153. Algo: mfa.TOTPAlgoSHA1,
  154. }
  155. )
  156. type globalConfig struct {
  157. Common common.Configuration `json:"common" mapstructure:"common"`
  158. ACME acme.Configuration `json:"acme" mapstructure:"acme"`
  159. SFTPD sftpd.Configuration `json:"sftpd" mapstructure:"sftpd"`
  160. FTPD ftpd.Configuration `json:"ftpd" mapstructure:"ftpd"`
  161. WebDAVD webdavd.Configuration `json:"webdavd" mapstructure:"webdavd"`
  162. ProviderConf dataprovider.Config `json:"data_provider" mapstructure:"data_provider"`
  163. HTTPDConfig httpd.Conf `json:"httpd" mapstructure:"httpd"`
  164. HTTPConfig httpclient.Config `json:"http" mapstructure:"http"`
  165. CommandConfig command.Config `json:"command" mapstructure:"command"`
  166. KMSConfig kms.Configuration `json:"kms" mapstructure:"kms"`
  167. MFAConfig mfa.Config `json:"mfa" mapstructure:"mfa"`
  168. TelemetryConfig telemetry.Conf `json:"telemetry" mapstructure:"telemetry"`
  169. PluginsConfig []plugin.Config `json:"plugins" mapstructure:"plugins"`
  170. SMTPConfig smtp.Config `json:"smtp" mapstructure:"smtp"`
  171. }
  172. func init() {
  173. Init()
  174. }
  175. // Init initializes the global configuration.
  176. // It is not supposed to be called outside of this package.
  177. // It is exported to minimize refactoring efforts. Will eventually disappear.
  178. func Init() {
  179. // create a default configuration to use if no config file is provided
  180. globalConf = globalConfig{
  181. Common: common.Configuration{
  182. IdleTimeout: 15,
  183. UploadMode: 0,
  184. Actions: common.ProtocolActions{
  185. ExecuteOn: []string{},
  186. ExecuteSync: []string{},
  187. Hook: "",
  188. },
  189. SetstatMode: 0,
  190. TempPath: "",
  191. ProxyProtocol: 0,
  192. ProxyAllowed: []string{},
  193. PostConnectHook: "",
  194. PostDisconnectHook: "",
  195. DataRetentionHook: "",
  196. MaxTotalConnections: 0,
  197. MaxPerHostConnections: 20,
  198. WhiteListFile: "",
  199. DefenderConfig: common.DefenderConfig{
  200. Enabled: false,
  201. Driver: common.DefenderDriverMemory,
  202. BanTime: 30,
  203. BanTimeIncrement: 50,
  204. Threshold: 15,
  205. ScoreInvalid: 2,
  206. ScoreValid: 1,
  207. ScoreLimitExceeded: 3,
  208. ObservationTime: 30,
  209. EntriesSoftLimit: 100,
  210. EntriesHardLimit: 150,
  211. SafeListFile: "",
  212. BlockListFile: "",
  213. SafeList: []string{},
  214. BlockList: []string{},
  215. },
  216. RateLimitersConfig: []common.RateLimiterConfig{defaultRateLimiter},
  217. },
  218. ACME: acme.Configuration{
  219. Email: "",
  220. KeyType: "4096",
  221. CertsPath: "certs",
  222. CAEndpoint: "https://acme-v02.api.letsencrypt.org/directory",
  223. Domains: []string{},
  224. RenewDays: 30,
  225. HTTP01Challenge: acme.HTTP01Challenge{
  226. Port: 80,
  227. WebRoot: "",
  228. ProxyHeader: "",
  229. },
  230. TLSALPN01Challenge: acme.TLSALPN01Challenge{
  231. Port: 0,
  232. },
  233. },
  234. SFTPD: sftpd.Configuration{
  235. Bindings: []sftpd.Binding{defaultSFTPDBinding},
  236. MaxAuthTries: 0,
  237. Banner: defaultSFTPDBanner,
  238. HostKeys: []string{},
  239. HostCertificates: []string{},
  240. HostKeyAlgorithms: []string{},
  241. KexAlgorithms: []string{},
  242. Ciphers: []string{},
  243. MACs: []string{},
  244. TrustedUserCAKeys: []string{},
  245. RevokedUserCertsFile: "",
  246. LoginBannerFile: "",
  247. EnabledSSHCommands: []string{},
  248. KeyboardInteractiveAuthentication: false,
  249. KeyboardInteractiveHook: "",
  250. PasswordAuthentication: true,
  251. FolderPrefix: "",
  252. },
  253. FTPD: ftpd.Configuration{
  254. Bindings: []ftpd.Binding{defaultFTPDBinding},
  255. Banner: defaultFTPDBanner,
  256. BannerFile: "",
  257. ActiveTransfersPortNon20: true,
  258. PassivePortRange: ftpd.PortRange{
  259. Start: 50000,
  260. End: 50100,
  261. },
  262. DisableActiveMode: false,
  263. EnableSite: false,
  264. HASHSupport: 0,
  265. CombineSupport: 0,
  266. CertificateFile: "",
  267. CertificateKeyFile: "",
  268. CACertificates: []string{},
  269. CARevocationLists: []string{},
  270. },
  271. WebDAVD: webdavd.Configuration{
  272. Bindings: []webdavd.Binding{defaultWebDAVDBinding},
  273. CertificateFile: "",
  274. CertificateKeyFile: "",
  275. CACertificates: []string{},
  276. CARevocationLists: []string{},
  277. Cors: webdavd.CorsConfig{
  278. Enabled: false,
  279. AllowedOrigins: []string{},
  280. AllowedMethods: []string{},
  281. AllowedHeaders: []string{},
  282. ExposedHeaders: []string{},
  283. AllowCredentials: false,
  284. MaxAge: 0,
  285. },
  286. Cache: webdavd.Cache{
  287. Users: webdavd.UsersCacheConfig{
  288. ExpirationTime: 0,
  289. MaxSize: 50,
  290. },
  291. MimeTypes: webdavd.MimeCacheConfig{
  292. Enabled: true,
  293. MaxSize: 1000,
  294. },
  295. },
  296. },
  297. ProviderConf: dataprovider.Config{
  298. Driver: "sqlite",
  299. Name: "sftpgo.db",
  300. Host: "",
  301. Port: 0,
  302. Username: "",
  303. Password: "",
  304. ConnectionString: "",
  305. SQLTablesPrefix: "",
  306. SSLMode: 0,
  307. RootCert: "",
  308. ClientCert: "",
  309. ClientKey: "",
  310. TrackQuota: 2,
  311. PoolSize: 0,
  312. UsersBaseDir: "",
  313. Actions: dataprovider.ObjectsActions{
  314. ExecuteOn: []string{},
  315. ExecuteFor: []string{},
  316. Hook: "",
  317. },
  318. ExternalAuthHook: "",
  319. ExternalAuthScope: 0,
  320. PreLoginHook: "",
  321. PostLoginHook: "",
  322. PostLoginScope: 0,
  323. CheckPasswordHook: "",
  324. CheckPasswordScope: 0,
  325. PasswordHashing: dataprovider.PasswordHashing{
  326. Argon2Options: dataprovider.Argon2Options{
  327. Memory: 65536,
  328. Iterations: 1,
  329. Parallelism: 2,
  330. },
  331. BcryptOptions: dataprovider.BcryptOptions{
  332. Cost: 10,
  333. },
  334. Algo: dataprovider.HashingAlgoBcrypt,
  335. },
  336. PasswordValidation: dataprovider.PasswordValidation{
  337. Admins: dataprovider.PasswordValidationRules{
  338. MinEntropy: 0,
  339. },
  340. Users: dataprovider.PasswordValidationRules{
  341. MinEntropy: 0,
  342. },
  343. },
  344. PasswordCaching: true,
  345. UpdateMode: 0,
  346. DelayedQuotaUpdate: 0,
  347. CreateDefaultAdmin: false,
  348. NamingRules: 1,
  349. IsShared: 0,
  350. BackupsPath: "backups",
  351. },
  352. HTTPDConfig: httpd.Conf{
  353. Bindings: []httpd.Binding{defaultHTTPDBinding},
  354. TemplatesPath: "templates",
  355. StaticFilesPath: "static",
  356. OpenAPIPath: "openapi",
  357. WebRoot: "",
  358. CertificateFile: "",
  359. CertificateKeyFile: "",
  360. CACertificates: nil,
  361. CARevocationLists: nil,
  362. SigningPassphrase: "",
  363. TokenValidation: 0,
  364. MaxUploadFileSize: 1048576000,
  365. Cors: httpd.CorsConfig{
  366. Enabled: false,
  367. AllowedOrigins: []string{},
  368. AllowedMethods: []string{},
  369. AllowedHeaders: []string{},
  370. ExposedHeaders: []string{},
  371. AllowCredentials: false,
  372. MaxAge: 0,
  373. },
  374. Setup: httpd.SetupConfig{
  375. InstallationCode: "",
  376. InstallationCodeHint: defaultInstallCodeHint,
  377. },
  378. HideSupportLink: false,
  379. },
  380. HTTPConfig: httpclient.Config{
  381. Timeout: 20,
  382. RetryWaitMin: 2,
  383. RetryWaitMax: 30,
  384. RetryMax: 3,
  385. CACertificates: nil,
  386. Certificates: nil,
  387. SkipTLSVerify: false,
  388. Headers: nil,
  389. },
  390. CommandConfig: command.Config{
  391. Timeout: 30,
  392. Env: nil,
  393. Commands: nil,
  394. },
  395. KMSConfig: kms.Configuration{
  396. Secrets: kms.Secrets{
  397. URL: "",
  398. MasterKeyString: "",
  399. MasterKeyPath: "",
  400. },
  401. },
  402. MFAConfig: mfa.Config{
  403. TOTP: nil,
  404. },
  405. TelemetryConfig: telemetry.Conf{
  406. BindPort: 0,
  407. BindAddress: "127.0.0.1",
  408. EnableProfiler: false,
  409. AuthUserFile: "",
  410. CertificateFile: "",
  411. CertificateKeyFile: "",
  412. MinTLSVersion: 12,
  413. TLSCipherSuites: nil,
  414. },
  415. SMTPConfig: smtp.Config{
  416. Host: "",
  417. Port: 25,
  418. From: "",
  419. User: "",
  420. Password: "",
  421. AuthType: 0,
  422. Encryption: 0,
  423. Domain: "",
  424. TemplatesPath: "templates",
  425. },
  426. PluginsConfig: nil,
  427. }
  428. viper.SetEnvPrefix(configEnvPrefix)
  429. replacer := strings.NewReplacer(".", "__")
  430. viper.SetEnvKeyReplacer(replacer)
  431. viper.SetConfigName(configName)
  432. setViperDefaults()
  433. viper.AutomaticEnv()
  434. viper.AllowEmptyEnv(true)
  435. }
  436. // GetCommonConfig returns the common protocols configuration
  437. func GetCommonConfig() common.Configuration {
  438. return globalConf.Common
  439. }
  440. // SetCommonConfig sets the common protocols configuration
  441. func SetCommonConfig(config common.Configuration) {
  442. globalConf.Common = config
  443. }
  444. // GetSFTPDConfig returns the configuration for the SFTP server
  445. func GetSFTPDConfig() sftpd.Configuration {
  446. return globalConf.SFTPD
  447. }
  448. // SetSFTPDConfig sets the configuration for the SFTP server
  449. func SetSFTPDConfig(config sftpd.Configuration) {
  450. globalConf.SFTPD = config
  451. }
  452. // GetFTPDConfig returns the configuration for the FTP server
  453. func GetFTPDConfig() ftpd.Configuration {
  454. return globalConf.FTPD
  455. }
  456. // SetFTPDConfig sets the configuration for the FTP server
  457. func SetFTPDConfig(config ftpd.Configuration) {
  458. globalConf.FTPD = config
  459. }
  460. // GetWebDAVDConfig returns the configuration for the WebDAV server
  461. func GetWebDAVDConfig() webdavd.Configuration {
  462. return globalConf.WebDAVD
  463. }
  464. // SetWebDAVDConfig sets the configuration for the WebDAV server
  465. func SetWebDAVDConfig(config webdavd.Configuration) {
  466. globalConf.WebDAVD = config
  467. }
  468. // GetHTTPDConfig returns the configuration for the HTTP server
  469. func GetHTTPDConfig() httpd.Conf {
  470. return globalConf.HTTPDConfig
  471. }
  472. // SetHTTPDConfig sets the configuration for the HTTP server
  473. func SetHTTPDConfig(config httpd.Conf) {
  474. globalConf.HTTPDConfig = config
  475. }
  476. // GetProviderConf returns the configuration for the data provider
  477. func GetProviderConf() dataprovider.Config {
  478. return globalConf.ProviderConf
  479. }
  480. // SetProviderConf sets the configuration for the data provider
  481. func SetProviderConf(config dataprovider.Config) {
  482. globalConf.ProviderConf = config
  483. }
  484. // GetHTTPConfig returns the configuration for HTTP clients
  485. func GetHTTPConfig() httpclient.Config {
  486. return globalConf.HTTPConfig
  487. }
  488. // GetCommandConfig returns the configuration for external commands
  489. func GetCommandConfig() command.Config {
  490. return globalConf.CommandConfig
  491. }
  492. // GetKMSConfig returns the KMS configuration
  493. func GetKMSConfig() kms.Configuration {
  494. return globalConf.KMSConfig
  495. }
  496. // SetKMSConfig sets the kms configuration
  497. func SetKMSConfig(config kms.Configuration) {
  498. globalConf.KMSConfig = config
  499. }
  500. // GetTelemetryConfig returns the telemetry configuration
  501. func GetTelemetryConfig() telemetry.Conf {
  502. return globalConf.TelemetryConfig
  503. }
  504. // SetTelemetryConfig sets the telemetry configuration
  505. func SetTelemetryConfig(config telemetry.Conf) {
  506. globalConf.TelemetryConfig = config
  507. }
  508. // GetPluginsConfig returns the plugins configuration
  509. func GetPluginsConfig() []plugin.Config {
  510. return globalConf.PluginsConfig
  511. }
  512. // SetPluginsConfig sets the plugin configuration
  513. func SetPluginsConfig(config []plugin.Config) {
  514. globalConf.PluginsConfig = config
  515. }
  516. // GetMFAConfig returns multi-factor authentication config
  517. func GetMFAConfig() mfa.Config {
  518. return globalConf.MFAConfig
  519. }
  520. // GetSMTPConfig returns the SMTP configuration
  521. func GetSMTPConfig() smtp.Config {
  522. return globalConf.SMTPConfig
  523. }
  524. // GetACMEConfig returns the ACME configuration
  525. func GetACMEConfig() acme.Configuration {
  526. return globalConf.ACME
  527. }
  528. // HasServicesToStart returns true if the config defines at least a service to start.
  529. // Supported services are SFTP, FTP and WebDAV
  530. func HasServicesToStart() bool {
  531. if globalConf.SFTPD.ShouldBind() {
  532. return true
  533. }
  534. if globalConf.FTPD.ShouldBind() {
  535. return true
  536. }
  537. if globalConf.WebDAVD.ShouldBind() {
  538. return true
  539. }
  540. return false
  541. }
  542. func getRedactedPassword(value string) string {
  543. if value == "" {
  544. return value
  545. }
  546. return "[redacted]"
  547. }
  548. func getRedactedGlobalConf() globalConfig {
  549. conf := globalConf
  550. conf.Common.Actions.Hook = util.GetRedactedURL(conf.Common.Actions.Hook)
  551. conf.Common.StartupHook = util.GetRedactedURL(conf.Common.StartupHook)
  552. conf.Common.PostConnectHook = util.GetRedactedURL(conf.Common.PostConnectHook)
  553. conf.Common.PostDisconnectHook = util.GetRedactedURL(conf.Common.PostDisconnectHook)
  554. conf.Common.DataRetentionHook = util.GetRedactedURL(conf.Common.DataRetentionHook)
  555. conf.SFTPD.KeyboardInteractiveHook = util.GetRedactedURL(conf.SFTPD.KeyboardInteractiveHook)
  556. conf.HTTPDConfig.SigningPassphrase = getRedactedPassword(conf.HTTPDConfig.SigningPassphrase)
  557. conf.HTTPDConfig.Setup.InstallationCode = getRedactedPassword(conf.HTTPDConfig.Setup.InstallationCode)
  558. conf.ProviderConf.Password = getRedactedPassword(conf.ProviderConf.Password)
  559. conf.ProviderConf.Actions.Hook = util.GetRedactedURL(conf.ProviderConf.Actions.Hook)
  560. conf.ProviderConf.ExternalAuthHook = util.GetRedactedURL(conf.ProviderConf.ExternalAuthHook)
  561. conf.ProviderConf.PreLoginHook = util.GetRedactedURL(conf.ProviderConf.PreLoginHook)
  562. conf.ProviderConf.PostLoginHook = util.GetRedactedURL(conf.ProviderConf.PostLoginHook)
  563. conf.ProviderConf.CheckPasswordHook = util.GetRedactedURL(conf.ProviderConf.CheckPasswordHook)
  564. conf.SMTPConfig.Password = getRedactedPassword(conf.SMTPConfig.Password)
  565. conf.HTTPDConfig.Bindings = nil
  566. for _, binding := range globalConf.HTTPDConfig.Bindings {
  567. binding.OIDC.ClientID = getRedactedPassword(binding.OIDC.ClientID)
  568. binding.OIDC.ClientSecret = getRedactedPassword(binding.OIDC.ClientSecret)
  569. conf.HTTPDConfig.Bindings = append(conf.HTTPDConfig.Bindings, binding)
  570. }
  571. return conf
  572. }
  573. func setConfigFile(configDir, configFile string) {
  574. if configFile == "" {
  575. return
  576. }
  577. if !filepath.IsAbs(configFile) && util.IsFileInputValid(configFile) {
  578. configFile = filepath.Join(configDir, configFile)
  579. }
  580. viper.SetConfigFile(configFile)
  581. }
  582. // LoadConfig loads the configuration
  583. // configDir will be added to the configuration search paths.
  584. // The search path contains by default the current directory and on linux it contains
  585. // $HOME/.config/sftpgo and /etc/sftpgo too.
  586. // configFile is an absolute or relative path (to the config dir) to the configuration file.
  587. func LoadConfig(configDir, configFile string) error {
  588. var err error
  589. viper.AddConfigPath(configDir)
  590. setViperAdditionalConfigPaths()
  591. viper.AddConfigPath(".")
  592. setConfigFile(configDir, configFile)
  593. if err = viper.ReadInConfig(); err != nil {
  594. // if the user specify a configuration file we get os.ErrNotExist.
  595. // viper.ConfigFileNotFoundError is returned if viper is unable
  596. // to find sftpgo.{json,yaml, etc..} in any of the search paths
  597. if errors.As(err, &viper.ConfigFileNotFoundError{}) {
  598. logger.Debug(logSender, "", "no configuration file found")
  599. } else {
  600. // should we return the error and not start here?
  601. logger.Warn(logSender, "", "error loading configuration file: %v", err)
  602. logger.WarnToConsole("error loading configuration file: %v", err)
  603. }
  604. globalConf.MFAConfig.TOTP = []mfa.TOTPConfig{defaultTOTP}
  605. }
  606. err = viper.Unmarshal(&globalConf)
  607. if err != nil {
  608. logger.Warn(logSender, "", "error parsing configuration file: %v", err)
  609. logger.WarnToConsole("error parsing configuration file: %v", err)
  610. return err
  611. }
  612. // viper only supports slice of strings from env vars, so we use our custom method
  613. loadBindingsFromEnv()
  614. resetInvalidConfigs()
  615. logger.Debug(logSender, "", "config file used: '%#v', config loaded: %+v", viper.ConfigFileUsed(), getRedactedGlobalConf())
  616. return nil
  617. }
  618. func isUploadModeValid() bool {
  619. return globalConf.Common.UploadMode >= 0 && globalConf.Common.UploadMode <= 2
  620. }
  621. func isProxyProtocolValid() bool {
  622. return globalConf.Common.ProxyProtocol >= 0 && globalConf.Common.ProxyProtocol <= 2
  623. }
  624. func isExternalAuthScopeValid() bool {
  625. return globalConf.ProviderConf.ExternalAuthScope >= 0 && globalConf.ProviderConf.ExternalAuthScope <= 15
  626. }
  627. func resetInvalidConfigs() {
  628. if strings.TrimSpace(globalConf.SFTPD.Banner) == "" {
  629. globalConf.SFTPD.Banner = defaultSFTPDBanner
  630. }
  631. if strings.TrimSpace(globalConf.FTPD.Banner) == "" {
  632. globalConf.FTPD.Banner = defaultFTPDBanner
  633. }
  634. if strings.TrimSpace(globalConf.HTTPDConfig.Setup.InstallationCodeHint) == "" {
  635. globalConf.HTTPDConfig.Setup.InstallationCodeHint = defaultInstallCodeHint
  636. }
  637. if globalConf.ProviderConf.UsersBaseDir != "" && !util.IsFileInputValid(globalConf.ProviderConf.UsersBaseDir) {
  638. warn := fmt.Sprintf("invalid users base dir %#v will be ignored", globalConf.ProviderConf.UsersBaseDir)
  639. globalConf.ProviderConf.UsersBaseDir = ""
  640. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  641. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  642. }
  643. if !isUploadModeValid() {
  644. warn := fmt.Sprintf("invalid upload_mode 0, 1 and 2 are supported, configured: %v reset upload_mode to 0",
  645. globalConf.Common.UploadMode)
  646. globalConf.Common.UploadMode = 0
  647. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  648. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  649. }
  650. if !isProxyProtocolValid() {
  651. warn := fmt.Sprintf("invalid proxy_protocol 0, 1 and 2 are supported, configured: %v reset proxy_protocol to 0",
  652. globalConf.Common.ProxyProtocol)
  653. globalConf.Common.ProxyProtocol = 0
  654. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  655. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  656. }
  657. if !isExternalAuthScopeValid() {
  658. warn := fmt.Sprintf("invalid external_auth_scope: %v reset to 0", globalConf.ProviderConf.ExternalAuthScope)
  659. globalConf.ProviderConf.ExternalAuthScope = 0
  660. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  661. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  662. }
  663. if globalConf.Common.DefenderConfig.Enabled && globalConf.Common.DefenderConfig.Driver == common.DefenderDriverProvider {
  664. if !globalConf.ProviderConf.IsDefenderSupported() {
  665. warn := fmt.Sprintf("provider based defender is not supported with data provider %#v, "+
  666. "the memory defender implementation will be used. If you want to use the provider defender "+
  667. "implementation please switch to a shared/distributed data provider",
  668. globalConf.ProviderConf.Driver)
  669. globalConf.Common.DefenderConfig.Driver = common.DefenderDriverMemory
  670. logger.Warn(logSender, "", "Non-fatal configuration error: %v", warn)
  671. logger.WarnToConsole("Non-fatal configuration error: %v", warn)
  672. }
  673. }
  674. }
  675. func loadBindingsFromEnv() {
  676. for idx := 0; idx < 10; idx++ {
  677. getTOTPFromEnv(idx)
  678. getRateLimitersFromEnv(idx)
  679. getPluginsFromEnv(idx)
  680. getSFTPDBindindFromEnv(idx)
  681. getFTPDBindingFromEnv(idx)
  682. getWebDAVDBindingFromEnv(idx)
  683. getHTTPDBindingFromEnv(idx)
  684. getHTTPClientCertificatesFromEnv(idx)
  685. getHTTPClientHeadersFromEnv(idx)
  686. getCommandConfigsFromEnv(idx)
  687. }
  688. }
  689. func getTOTPFromEnv(idx int) {
  690. totpConfig := defaultTOTP
  691. if len(globalConf.MFAConfig.TOTP) > idx {
  692. totpConfig = globalConf.MFAConfig.TOTP[idx]
  693. }
  694. isSet := false
  695. name, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__NAME", idx))
  696. if ok {
  697. totpConfig.Name = name
  698. isSet = true
  699. }
  700. issuer, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ISSUER", idx))
  701. if ok {
  702. totpConfig.Issuer = issuer
  703. isSet = true
  704. }
  705. algo, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_MFA__TOTP__%v__ALGO", idx))
  706. if ok {
  707. totpConfig.Algo = algo
  708. isSet = true
  709. }
  710. if isSet {
  711. if len(globalConf.MFAConfig.TOTP) > idx {
  712. globalConf.MFAConfig.TOTP[idx] = totpConfig
  713. } else {
  714. globalConf.MFAConfig.TOTP = append(globalConf.MFAConfig.TOTP, totpConfig)
  715. }
  716. }
  717. }
  718. func getRateLimitersFromEnv(idx int) {
  719. rtlConfig := defaultRateLimiter
  720. if len(globalConf.Common.RateLimitersConfig) > idx {
  721. rtlConfig = globalConf.Common.RateLimitersConfig[idx]
  722. }
  723. isSet := false
  724. average, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__AVERAGE", idx))
  725. if ok {
  726. rtlConfig.Average = average
  727. isSet = true
  728. }
  729. period, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PERIOD", idx))
  730. if ok {
  731. rtlConfig.Period = period
  732. isSet = true
  733. }
  734. burst, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__BURST", idx))
  735. if ok {
  736. rtlConfig.Burst = int(burst)
  737. isSet = true
  738. }
  739. rtlType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__TYPE", idx))
  740. if ok {
  741. rtlConfig.Type = int(rtlType)
  742. isSet = true
  743. }
  744. protocols, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__PROTOCOLS", idx))
  745. if ok {
  746. rtlConfig.Protocols = protocols
  747. isSet = true
  748. }
  749. allowList, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ALLOW_LIST", idx))
  750. if ok {
  751. rtlConfig.AllowList = allowList
  752. isSet = true
  753. }
  754. generateEvents, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__GENERATE_DEFENDER_EVENTS", idx))
  755. if ok {
  756. rtlConfig.GenerateDefenderEvents = generateEvents
  757. isSet = true
  758. }
  759. softLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_SOFT_LIMIT", idx))
  760. if ok {
  761. rtlConfig.EntriesSoftLimit = int(softLimit)
  762. isSet = true
  763. }
  764. hardLimit, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMON__RATE_LIMITERS__%v__ENTRIES_HARD_LIMIT", idx))
  765. if ok {
  766. rtlConfig.EntriesHardLimit = int(hardLimit)
  767. isSet = true
  768. }
  769. if isSet {
  770. if len(globalConf.Common.RateLimitersConfig) > idx {
  771. globalConf.Common.RateLimitersConfig[idx] = rtlConfig
  772. } else {
  773. globalConf.Common.RateLimitersConfig = append(globalConf.Common.RateLimitersConfig, rtlConfig)
  774. }
  775. }
  776. }
  777. func getKMSPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  778. isSet := false
  779. kmsScheme, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__SCHEME", idx))
  780. if ok {
  781. pluginConfig.KMSOptions.Scheme = kmsScheme
  782. isSet = true
  783. }
  784. kmsEncStatus, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__KMS_OPTIONS__ENCRYPTED_STATUS", idx))
  785. if ok {
  786. pluginConfig.KMSOptions.EncryptedStatus = kmsEncStatus
  787. isSet = true
  788. }
  789. return isSet
  790. }
  791. func getAuthPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  792. isSet := false
  793. authScope, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTH_OPTIONS__SCOPE", idx))
  794. if ok {
  795. pluginConfig.AuthOptions.Scope = int(authScope)
  796. isSet = true
  797. }
  798. return isSet
  799. }
  800. func getNotifierPluginFromEnv(idx int, pluginConfig *plugin.Config) bool {
  801. isSet := false
  802. notifierFsEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__FS_EVENTS", idx))
  803. if ok {
  804. pluginConfig.NotifierOptions.FsEvents = notifierFsEvents
  805. isSet = true
  806. }
  807. notifierProviderEvents, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_EVENTS", idx))
  808. if ok {
  809. pluginConfig.NotifierOptions.ProviderEvents = notifierProviderEvents
  810. isSet = true
  811. }
  812. notifierProviderObjects, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__PROVIDER_OBJECTS", idx))
  813. if ok {
  814. pluginConfig.NotifierOptions.ProviderObjects = notifierProviderObjects
  815. isSet = true
  816. }
  817. notifierRetryMaxTime, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_MAX_TIME", idx))
  818. if ok {
  819. pluginConfig.NotifierOptions.RetryMaxTime = int(notifierRetryMaxTime)
  820. isSet = true
  821. }
  822. notifierRetryQueueMaxSize, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__NOTIFIER_OPTIONS__RETRY_QUEUE_MAX_SIZE", idx))
  823. if ok {
  824. pluginConfig.NotifierOptions.RetryQueueMaxSize = int(notifierRetryQueueMaxSize)
  825. isSet = true
  826. }
  827. return isSet
  828. }
  829. func getPluginsFromEnv(idx int) {
  830. pluginConfig := plugin.Config{}
  831. if len(globalConf.PluginsConfig) > idx {
  832. pluginConfig = globalConf.PluginsConfig[idx]
  833. }
  834. isSet := false
  835. pluginType, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__TYPE", idx))
  836. if ok {
  837. pluginConfig.Type = pluginType
  838. isSet = true
  839. }
  840. if getNotifierPluginFromEnv(idx, &pluginConfig) {
  841. isSet = true
  842. }
  843. if getKMSPluginFromEnv(idx, &pluginConfig) {
  844. isSet = true
  845. }
  846. if getAuthPluginFromEnv(idx, &pluginConfig) {
  847. isSet = true
  848. }
  849. cmd, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__CMD", idx))
  850. if ok {
  851. pluginConfig.Cmd = cmd
  852. isSet = true
  853. }
  854. cmdArgs, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__ARGS", idx))
  855. if ok {
  856. pluginConfig.Args = cmdArgs
  857. isSet = true
  858. }
  859. pluginHash, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__SHA256SUM", idx))
  860. if ok {
  861. pluginConfig.SHA256Sum = pluginHash
  862. isSet = true
  863. }
  864. autoMTLS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_PLUGINS__%v__AUTO_MTLS", idx))
  865. if ok {
  866. pluginConfig.AutoMTLS = autoMTLS
  867. isSet = true
  868. }
  869. if isSet {
  870. if len(globalConf.PluginsConfig) > idx {
  871. globalConf.PluginsConfig[idx] = pluginConfig
  872. } else {
  873. globalConf.PluginsConfig = append(globalConf.PluginsConfig, pluginConfig)
  874. }
  875. }
  876. }
  877. func getSFTPDBindindFromEnv(idx int) {
  878. binding := defaultSFTPDBinding
  879. if len(globalConf.SFTPD.Bindings) > idx {
  880. binding = globalConf.SFTPD.Bindings[idx]
  881. }
  882. isSet := false
  883. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__PORT", idx))
  884. if ok {
  885. binding.Port = int(port)
  886. isSet = true
  887. }
  888. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__ADDRESS", idx))
  889. if ok {
  890. binding.Address = address
  891. isSet = true
  892. }
  893. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_SFTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  894. if ok {
  895. binding.ApplyProxyConfig = applyProxyConfig
  896. isSet = true
  897. }
  898. if isSet {
  899. if len(globalConf.SFTPD.Bindings) > idx {
  900. globalConf.SFTPD.Bindings[idx] = binding
  901. } else {
  902. globalConf.SFTPD.Bindings = append(globalConf.SFTPD.Bindings, binding)
  903. }
  904. }
  905. }
  906. func getFTPDPassiveIPOverridesFromEnv(idx int) []ftpd.PassiveIPOverride {
  907. var overrides []ftpd.PassiveIPOverride
  908. if len(globalConf.FTPD.Bindings) > idx {
  909. overrides = globalConf.FTPD.Bindings[idx].PassiveIPOverrides
  910. }
  911. for subIdx := 0; subIdx < 10; subIdx++ {
  912. var override ftpd.PassiveIPOverride
  913. var replace bool
  914. if len(globalConf.FTPD.Bindings) > idx && len(globalConf.FTPD.Bindings[idx].PassiveIPOverrides) > subIdx {
  915. override = globalConf.FTPD.Bindings[idx].PassiveIPOverrides[subIdx]
  916. replace = true
  917. }
  918. ip, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__IP", idx, subIdx))
  919. if ok {
  920. override.IP = ip
  921. }
  922. networks, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_IP_OVERRIDES__%v__NETWORKS",
  923. idx, subIdx))
  924. if ok {
  925. override.Networks = networks
  926. }
  927. if len(override.Networks) > 0 {
  928. if replace {
  929. overrides[subIdx] = override
  930. } else {
  931. overrides = append(overrides, override)
  932. }
  933. }
  934. }
  935. return overrides
  936. }
  937. func getDefaultFTPDBinding(idx int) ftpd.Binding {
  938. binding := defaultFTPDBinding
  939. if len(globalConf.FTPD.Bindings) > idx {
  940. binding = globalConf.FTPD.Bindings[idx]
  941. }
  942. return binding
  943. }
  944. func getFTPDBindingFromEnv(idx int) {
  945. binding := getDefaultFTPDBinding(idx)
  946. isSet := false
  947. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PORT", idx))
  948. if ok {
  949. binding.Port = int(port)
  950. isSet = true
  951. }
  952. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ADDRESS", idx))
  953. if ok {
  954. binding.Address = address
  955. isSet = true
  956. }
  957. applyProxyConfig, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__APPLY_PROXY_CONFIG", idx))
  958. if ok {
  959. binding.ApplyProxyConfig = applyProxyConfig
  960. isSet = true
  961. }
  962. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  963. if ok {
  964. binding.CertificateFile = certificateFile
  965. isSet = true
  966. }
  967. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  968. if ok {
  969. binding.CertificateKeyFile = certificateKeyFile
  970. isSet = true
  971. }
  972. tlsMode, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_MODE", idx))
  973. if ok {
  974. binding.TLSMode = int(tlsMode)
  975. isSet = true
  976. }
  977. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  978. if ok {
  979. binding.MinTLSVersion = int(tlsVer)
  980. isSet = true
  981. }
  982. passiveIP, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__FORCE_PASSIVE_IP", idx))
  983. if ok {
  984. binding.ForcePassiveIP = passiveIP
  985. isSet = true
  986. }
  987. passiveIPOverrides := getFTPDPassiveIPOverridesFromEnv(idx)
  988. if len(passiveIPOverrides) > 0 {
  989. binding.PassiveIPOverrides = passiveIPOverrides
  990. isSet = true
  991. }
  992. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  993. if ok {
  994. binding.ClientAuthType = int(clientAuthType)
  995. isSet = true
  996. }
  997. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  998. if ok {
  999. binding.TLSCipherSuites = tlsCiphers
  1000. isSet = true
  1001. }
  1002. pasvSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__PASSIVE_CONNECTIONS_SECURITY", idx))
  1003. if ok {
  1004. binding.PassiveConnectionsSecurity = int(pasvSecurity)
  1005. isSet = true
  1006. }
  1007. activeSecurity, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__ACTIVE_CONNECTIONS_SECURITY", idx))
  1008. if ok {
  1009. binding.ActiveConnectionsSecurity = int(activeSecurity)
  1010. isSet = true
  1011. }
  1012. debug, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_FTPD__BINDINGS__%v__DEBUG", idx))
  1013. if ok {
  1014. binding.Debug = debug
  1015. isSet = true
  1016. }
  1017. applyFTPDBindingFromEnv(idx, isSet, binding)
  1018. }
  1019. func applyFTPDBindingFromEnv(idx int, isSet bool, binding ftpd.Binding) {
  1020. if isSet {
  1021. if len(globalConf.FTPD.Bindings) > idx {
  1022. globalConf.FTPD.Bindings[idx] = binding
  1023. } else {
  1024. globalConf.FTPD.Bindings = append(globalConf.FTPD.Bindings, binding)
  1025. }
  1026. }
  1027. }
  1028. func getWebDAVDBindingProxyConfigsFromEnv(idx int, binding *webdavd.Binding) bool {
  1029. isSet := false
  1030. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PROXY_ALLOWED", idx))
  1031. if ok {
  1032. binding.ProxyAllowed = proxyAllowed
  1033. isSet = true
  1034. }
  1035. clientIPProxyHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_PROXY_HEADER", idx))
  1036. if ok {
  1037. binding.ClientIPProxyHeader = clientIPProxyHeader
  1038. isSet = true
  1039. }
  1040. clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx))
  1041. if ok {
  1042. binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
  1043. isSet = true
  1044. }
  1045. return isSet
  1046. }
  1047. func getWebDAVDBindingFromEnv(idx int) {
  1048. binding := defaultWebDAVDBinding
  1049. if len(globalConf.WebDAVD.Bindings) > idx {
  1050. binding = globalConf.WebDAVD.Bindings[idx]
  1051. }
  1052. isSet := false
  1053. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PORT", idx))
  1054. if ok {
  1055. binding.Port = int(port)
  1056. isSet = true
  1057. }
  1058. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ADDRESS", idx))
  1059. if ok {
  1060. binding.Address = address
  1061. isSet = true
  1062. }
  1063. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  1064. if ok {
  1065. binding.CertificateFile = certificateFile
  1066. isSet = true
  1067. }
  1068. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  1069. if ok {
  1070. binding.CertificateKeyFile = certificateKeyFile
  1071. isSet = true
  1072. }
  1073. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__ENABLE_HTTPS", idx))
  1074. if ok {
  1075. binding.EnableHTTPS = enableHTTPS
  1076. isSet = true
  1077. }
  1078. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  1079. if ok {
  1080. binding.MinTLSVersion = int(tlsVer)
  1081. isSet = true
  1082. }
  1083. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  1084. if ok {
  1085. binding.ClientAuthType = int(clientAuthType)
  1086. isSet = true
  1087. }
  1088. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  1089. if ok {
  1090. binding.TLSCipherSuites = tlsCiphers
  1091. isSet = true
  1092. }
  1093. if getWebDAVDBindingProxyConfigsFromEnv(idx, &binding) {
  1094. isSet = true
  1095. }
  1096. prefix, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_WEBDAVD__BINDINGS__%v__PREFIX", idx))
  1097. if ok {
  1098. binding.Prefix = prefix
  1099. isSet = true
  1100. }
  1101. if isSet {
  1102. if len(globalConf.WebDAVD.Bindings) > idx {
  1103. globalConf.WebDAVD.Bindings[idx] = binding
  1104. } else {
  1105. globalConf.WebDAVD.Bindings = append(globalConf.WebDAVD.Bindings, binding)
  1106. }
  1107. }
  1108. }
  1109. func getHTTPDSecurityProxyHeadersFromEnv(idx int) []httpd.HTTPSProxyHeader {
  1110. var httpsProxyHeaders []httpd.HTTPSProxyHeader
  1111. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1112. httpsProxyHeaders = globalConf.HTTPDConfig.Bindings[idx].Security.HTTPSProxyHeaders
  1113. }
  1114. for subIdx := 0; subIdx < 10; subIdx++ {
  1115. var httpsProxyHeader httpd.HTTPSProxyHeader
  1116. var replace bool
  1117. if len(globalConf.HTTPDConfig.Bindings) > idx &&
  1118. len(globalConf.HTTPDConfig.Bindings[idx].Security.HTTPSProxyHeaders) > subIdx {
  1119. httpsProxyHeader = httpsProxyHeaders[subIdx]
  1120. replace = true
  1121. }
  1122. proxyKey, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_PROXY_HEADERS__%v__KEY",
  1123. idx, subIdx))
  1124. if ok {
  1125. httpsProxyHeader.Key = proxyKey
  1126. }
  1127. proxyVal, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_PROXY_HEADERS__%v__VALUE",
  1128. idx, subIdx))
  1129. if ok {
  1130. httpsProxyHeader.Value = proxyVal
  1131. }
  1132. if httpsProxyHeader.Key != "" && httpsProxyHeader.Value != "" {
  1133. if replace {
  1134. httpsProxyHeaders[subIdx] = httpsProxyHeader
  1135. } else {
  1136. httpsProxyHeaders = append(httpsProxyHeaders, httpsProxyHeader)
  1137. }
  1138. }
  1139. }
  1140. return httpsProxyHeaders
  1141. }
  1142. func getHTTPDSecurityConfFromEnv(idx int) (httpd.SecurityConf, bool) { //nolint:gocyclo
  1143. result := defaultHTTPDBinding.Security
  1144. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1145. result = globalConf.HTTPDConfig.Bindings[idx].Security
  1146. }
  1147. isSet := false
  1148. enabled, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ENABLED", idx))
  1149. if ok {
  1150. result.Enabled = enabled
  1151. isSet = true
  1152. }
  1153. allowedHosts, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ALLOWED_HOSTS", idx))
  1154. if ok {
  1155. result.AllowedHosts = allowedHosts
  1156. isSet = true
  1157. }
  1158. allowedHostsAreRegex, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__ALLOWED_HOSTS_ARE_REGEX", idx))
  1159. if ok {
  1160. result.AllowedHostsAreRegex = allowedHostsAreRegex
  1161. isSet = true
  1162. }
  1163. hostsProxyHeaders, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HOSTS_PROXY_HEADERS", idx))
  1164. if ok {
  1165. result.HostsProxyHeaders = hostsProxyHeaders
  1166. isSet = true
  1167. }
  1168. httpsRedirect, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_REDIRECT", idx))
  1169. if ok {
  1170. result.HTTPSRedirect = httpsRedirect
  1171. isSet = true
  1172. }
  1173. httpsHost, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__HTTPS_HOST", idx))
  1174. if ok {
  1175. result.HTTPSHost = httpsHost
  1176. isSet = true
  1177. }
  1178. httpsProxyHeaders := getHTTPDSecurityProxyHeadersFromEnv(idx)
  1179. if len(httpsProxyHeaders) > 0 {
  1180. result.HTTPSProxyHeaders = httpsProxyHeaders
  1181. isSet = true
  1182. }
  1183. stsSeconds, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_SECONDS", idx))
  1184. if ok {
  1185. result.STSSeconds = stsSeconds
  1186. isSet = true
  1187. }
  1188. stsIncludeSubDomains, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_INCLUDE_SUBDOMAINS", idx))
  1189. if ok {
  1190. result.STSIncludeSubdomains = stsIncludeSubDomains
  1191. isSet = true
  1192. }
  1193. stsPreload, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__STS_PRELOAD", idx))
  1194. if ok {
  1195. result.STSPreload = stsPreload
  1196. isSet = true
  1197. }
  1198. contentTypeNosniff, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CONTENT_TYPE_NOSNIFF", idx))
  1199. if ok {
  1200. result.ContentTypeNosniff = contentTypeNosniff
  1201. isSet = true
  1202. }
  1203. contentSecurityPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CONTENT_SECURITY_POLICY", idx))
  1204. if ok {
  1205. result.ContentSecurityPolicy = contentSecurityPolicy
  1206. isSet = true
  1207. }
  1208. permissionsPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__PERMISSIONS_POLICY", idx))
  1209. if ok {
  1210. result.PermissionsPolicy = permissionsPolicy
  1211. isSet = true
  1212. }
  1213. crossOriginOpenedPolicy, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__CROSS_ORIGIN_OPENER_POLICY", idx))
  1214. if ok {
  1215. result.CrossOriginOpenerPolicy = crossOriginOpenedPolicy
  1216. isSet = true
  1217. }
  1218. expectCTHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__SECURITY__EXPECT_CT_HEADER", idx))
  1219. if ok {
  1220. result.ExpectCTHeader = expectCTHeader
  1221. isSet = true
  1222. }
  1223. return result, isSet
  1224. }
  1225. func getHTTPDOIDCFromEnv(idx int) (httpd.OIDC, bool) {
  1226. result := defaultHTTPDBinding.OIDC
  1227. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1228. result = globalConf.HTTPDConfig.Bindings[idx].OIDC
  1229. }
  1230. isSet := false
  1231. clientID, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CLIENT_ID", idx))
  1232. if ok {
  1233. result.ClientID = clientID
  1234. isSet = true
  1235. }
  1236. clientSecret, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CLIENT_SECRET", idx))
  1237. if ok {
  1238. result.ClientSecret = clientSecret
  1239. isSet = true
  1240. }
  1241. configURL, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CONFIG_URL", idx))
  1242. if ok {
  1243. result.ConfigURL = configURL
  1244. isSet = true
  1245. }
  1246. redirectBaseURL, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__REDIRECT_BASE_URL", idx))
  1247. if ok {
  1248. result.RedirectBaseURL = redirectBaseURL
  1249. isSet = true
  1250. }
  1251. usernameField, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__USERNAME_FIELD", idx))
  1252. if ok {
  1253. result.UsernameField = usernameField
  1254. isSet = true
  1255. }
  1256. scopes, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__SCOPES", idx))
  1257. if ok {
  1258. result.Scopes = scopes
  1259. isSet = true
  1260. }
  1261. roleField, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__ROLE_FIELD", idx))
  1262. if ok {
  1263. result.RoleField = roleField
  1264. isSet = true
  1265. }
  1266. implicitRoles, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__IMPLICIT_ROLES", idx))
  1267. if ok {
  1268. result.ImplicitRoles = implicitRoles
  1269. isSet = true
  1270. }
  1271. customFields, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__OIDC__CUSTOM_FIELDS", idx))
  1272. if ok {
  1273. result.CustomFields = customFields
  1274. isSet = true
  1275. }
  1276. return result, isSet
  1277. }
  1278. func getHTTPDUIBrandingFromEnv(prefix string, branding httpd.UIBranding) (httpd.UIBranding, bool) {
  1279. isSet := false
  1280. name, ok := os.LookupEnv(fmt.Sprintf("%s__NAME", prefix))
  1281. if ok {
  1282. branding.Name = name
  1283. isSet = true
  1284. }
  1285. shortName, ok := os.LookupEnv(fmt.Sprintf("%s__SHORT_NAME", prefix))
  1286. if ok {
  1287. branding.ShortName = shortName
  1288. isSet = true
  1289. }
  1290. faviconPath, ok := os.LookupEnv(fmt.Sprintf("%s__FAVICON_PATH", prefix))
  1291. if ok {
  1292. branding.FaviconPath = faviconPath
  1293. isSet = true
  1294. }
  1295. logoPath, ok := os.LookupEnv(fmt.Sprintf("%s__LOGO_PATH", prefix))
  1296. if ok {
  1297. branding.LogoPath = logoPath
  1298. isSet = true
  1299. }
  1300. loginImagePath, ok := os.LookupEnv(fmt.Sprintf("%s__LOGIN_IMAGE_PATH", prefix))
  1301. if ok {
  1302. branding.LoginImagePath = loginImagePath
  1303. isSet = true
  1304. }
  1305. disclaimerName, ok := os.LookupEnv(fmt.Sprintf("%s__DISCLAIMER_NAME", prefix))
  1306. if ok {
  1307. branding.DisclaimerName = disclaimerName
  1308. isSet = true
  1309. }
  1310. disclaimerPath, ok := os.LookupEnv(fmt.Sprintf("%s__DISCLAIMER_PATH", prefix))
  1311. if ok {
  1312. branding.DisclaimerPath = disclaimerPath
  1313. isSet = true
  1314. }
  1315. defaultCSSPath, ok := os.LookupEnv(fmt.Sprintf("%s__DEFAULT_CSS", prefix))
  1316. if ok {
  1317. branding.DefaultCSS = defaultCSSPath
  1318. isSet = true
  1319. }
  1320. extraCSS, ok := lookupStringListFromEnv(fmt.Sprintf("%s__EXTRA_CSS", prefix))
  1321. if ok {
  1322. branding.ExtraCSS = extraCSS
  1323. isSet = true
  1324. }
  1325. return branding, isSet
  1326. }
  1327. func getHTTPDBrandingFromEnv(idx int) (httpd.Branding, bool) {
  1328. result := defaultHTTPDBinding.Branding
  1329. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1330. result = globalConf.HTTPDConfig.Bindings[idx].Branding
  1331. }
  1332. isSet := false
  1333. webAdmin, ok := getHTTPDUIBrandingFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__BRANDING__WEB_ADMIN", idx),
  1334. result.WebAdmin)
  1335. if ok {
  1336. result.WebAdmin = webAdmin
  1337. isSet = true
  1338. }
  1339. webClient, ok := getHTTPDUIBrandingFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__BRANDING__WEB_CLIENT", idx),
  1340. result.WebClient)
  1341. if ok {
  1342. result.WebClient = webClient
  1343. isSet = true
  1344. }
  1345. return result, isSet
  1346. }
  1347. func getHTTPDWebClientIntegrationsFromEnv(idx int) []httpd.WebClientIntegration {
  1348. var integrations []httpd.WebClientIntegration
  1349. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1350. integrations = globalConf.HTTPDConfig.Bindings[idx].WebClientIntegrations
  1351. }
  1352. for subIdx := 0; subIdx < 10; subIdx++ {
  1353. var integration httpd.WebClientIntegration
  1354. var replace bool
  1355. if len(globalConf.HTTPDConfig.Bindings) > idx &&
  1356. len(globalConf.HTTPDConfig.Bindings[idx].WebClientIntegrations) > subIdx {
  1357. integration = integrations[subIdx]
  1358. replace = true
  1359. }
  1360. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__URL", idx, subIdx))
  1361. if ok {
  1362. integration.URL = url
  1363. }
  1364. extensions, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__WEB_CLIENT_INTEGRATIONS__%v__FILE_EXTENSIONS",
  1365. idx, subIdx))
  1366. if ok {
  1367. integration.FileExtensions = extensions
  1368. }
  1369. if integration.URL != "" && len(integration.FileExtensions) > 0 {
  1370. if replace {
  1371. integrations[subIdx] = integration
  1372. } else {
  1373. integrations = append(integrations, integration)
  1374. }
  1375. }
  1376. }
  1377. return integrations
  1378. }
  1379. func getDefaultHTTPBinding(idx int) httpd.Binding {
  1380. binding := defaultHTTPDBinding
  1381. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1382. binding = globalConf.HTTPDConfig.Bindings[idx]
  1383. }
  1384. return binding
  1385. }
  1386. func getHTTPDNestedObjectsFromEnv(idx int, binding *httpd.Binding) bool {
  1387. isSet := false
  1388. webClientIntegrations := getHTTPDWebClientIntegrationsFromEnv(idx)
  1389. if len(webClientIntegrations) > 0 {
  1390. binding.WebClientIntegrations = webClientIntegrations
  1391. isSet = true
  1392. }
  1393. oidc, ok := getHTTPDOIDCFromEnv(idx)
  1394. if ok {
  1395. binding.OIDC = oidc
  1396. isSet = true
  1397. }
  1398. securityConf, ok := getHTTPDSecurityConfFromEnv(idx)
  1399. if ok {
  1400. binding.Security = securityConf
  1401. isSet = true
  1402. }
  1403. brandingConf, ok := getHTTPDBrandingFromEnv(idx)
  1404. if ok {
  1405. binding.Branding = brandingConf
  1406. isSet = true
  1407. }
  1408. return isSet
  1409. }
  1410. func getHTTPDBindingProxyConfigsFromEnv(idx int, binding *httpd.Binding) bool {
  1411. isSet := false
  1412. proxyAllowed, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PROXY_ALLOWED", idx))
  1413. if ok {
  1414. binding.ProxyAllowed = proxyAllowed
  1415. isSet = true
  1416. }
  1417. clientIPProxyHeader, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_PROXY_HEADER", idx))
  1418. if ok {
  1419. binding.ClientIPProxyHeader = clientIPProxyHeader
  1420. isSet = true
  1421. }
  1422. clientIPHeaderDepth, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_IP_HEADER_DEPTH", idx))
  1423. if ok {
  1424. binding.ClientIPHeaderDepth = int(clientIPHeaderDepth)
  1425. isSet = true
  1426. }
  1427. return isSet
  1428. }
  1429. func getHTTPDBindingFromEnv(idx int) { //nolint:gocyclo
  1430. binding := getDefaultHTTPBinding(idx)
  1431. isSet := false
  1432. port, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__PORT", idx))
  1433. if ok {
  1434. binding.Port = int(port)
  1435. isSet = true
  1436. }
  1437. address, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ADDRESS", idx))
  1438. if ok {
  1439. binding.Address = address
  1440. isSet = true
  1441. }
  1442. certificateFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CERTIFICATE_FILE", idx))
  1443. if ok {
  1444. binding.CertificateFile = certificateFile
  1445. isSet = true
  1446. }
  1447. certificateKeyFile, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CERTIFICATE_KEY_FILE", idx))
  1448. if ok {
  1449. binding.CertificateKeyFile = certificateKeyFile
  1450. isSet = true
  1451. }
  1452. enableWebAdmin, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_ADMIN", idx))
  1453. if ok {
  1454. binding.EnableWebAdmin = enableWebAdmin
  1455. isSet = true
  1456. }
  1457. enableWebClient, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_WEB_CLIENT", idx))
  1458. if ok {
  1459. binding.EnableWebClient = enableWebClient
  1460. isSet = true
  1461. }
  1462. enabledLoginMethods, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLED_LOGIN_METHODS", idx))
  1463. if ok {
  1464. binding.EnabledLoginMethods = int(enabledLoginMethods)
  1465. isSet = true
  1466. }
  1467. renderOpenAPI, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__RENDER_OPENAPI", idx))
  1468. if ok {
  1469. binding.RenderOpenAPI = renderOpenAPI
  1470. isSet = true
  1471. }
  1472. enableHTTPS, ok := lookupBoolFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__ENABLE_HTTPS", idx))
  1473. if ok {
  1474. binding.EnableHTTPS = enableHTTPS
  1475. isSet = true
  1476. }
  1477. tlsVer, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__MIN_TLS_VERSION", idx))
  1478. if ok {
  1479. binding.MinTLSVersion = int(tlsVer)
  1480. isSet = true
  1481. }
  1482. clientAuthType, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__CLIENT_AUTH_TYPE", idx))
  1483. if ok {
  1484. binding.ClientAuthType = int(clientAuthType)
  1485. isSet = true
  1486. }
  1487. tlsCiphers, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__TLS_CIPHER_SUITES", idx))
  1488. if ok {
  1489. binding.TLSCipherSuites = tlsCiphers
  1490. isSet = true
  1491. }
  1492. if getHTTPDBindingProxyConfigsFromEnv(idx, &binding) {
  1493. isSet = true
  1494. }
  1495. hideLoginURL, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_HTTPD__BINDINGS__%v__HIDE_LOGIN_URL", idx))
  1496. if ok {
  1497. binding.HideLoginURL = int(hideLoginURL)
  1498. isSet = true
  1499. }
  1500. if getHTTPDNestedObjectsFromEnv(idx, &binding) {
  1501. isSet = true
  1502. }
  1503. setHTTPDBinding(isSet, binding, idx)
  1504. }
  1505. func setHTTPDBinding(isSet bool, binding httpd.Binding, idx int) {
  1506. if isSet {
  1507. if len(globalConf.HTTPDConfig.Bindings) > idx {
  1508. globalConf.HTTPDConfig.Bindings[idx] = binding
  1509. } else {
  1510. globalConf.HTTPDConfig.Bindings = append(globalConf.HTTPDConfig.Bindings, binding)
  1511. }
  1512. }
  1513. }
  1514. func getHTTPClientCertificatesFromEnv(idx int) {
  1515. tlsCert := httpclient.TLSKeyPair{}
  1516. if len(globalConf.HTTPConfig.Certificates) > idx {
  1517. tlsCert = globalConf.HTTPConfig.Certificates[idx]
  1518. }
  1519. cert, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__CERT", idx))
  1520. if ok {
  1521. tlsCert.Cert = cert
  1522. }
  1523. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__CERTIFICATES__%v__KEY", idx))
  1524. if ok {
  1525. tlsCert.Key = key
  1526. }
  1527. if tlsCert.Cert != "" && tlsCert.Key != "" {
  1528. if len(globalConf.HTTPConfig.Certificates) > idx {
  1529. globalConf.HTTPConfig.Certificates[idx] = tlsCert
  1530. } else {
  1531. globalConf.HTTPConfig.Certificates = append(globalConf.HTTPConfig.Certificates, tlsCert)
  1532. }
  1533. }
  1534. }
  1535. func getHTTPClientHeadersFromEnv(idx int) {
  1536. header := httpclient.Header{}
  1537. if len(globalConf.HTTPConfig.Headers) > idx {
  1538. header = globalConf.HTTPConfig.Headers[idx]
  1539. }
  1540. key, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__KEY", idx))
  1541. if ok {
  1542. header.Key = key
  1543. }
  1544. value, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__VALUE", idx))
  1545. if ok {
  1546. header.Value = value
  1547. }
  1548. url, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_HTTP__HEADERS__%v__URL", idx))
  1549. if ok {
  1550. header.URL = url
  1551. }
  1552. if header.Key != "" && header.Value != "" {
  1553. if len(globalConf.HTTPConfig.Headers) > idx {
  1554. globalConf.HTTPConfig.Headers[idx] = header
  1555. } else {
  1556. globalConf.HTTPConfig.Headers = append(globalConf.HTTPConfig.Headers, header)
  1557. }
  1558. }
  1559. }
  1560. func getCommandConfigsFromEnv(idx int) {
  1561. cfg := command.Command{}
  1562. if len(globalConf.CommandConfig.Commands) > idx {
  1563. cfg = globalConf.CommandConfig.Commands[idx]
  1564. }
  1565. path, ok := os.LookupEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__PATH", idx))
  1566. if ok {
  1567. cfg.Path = path
  1568. }
  1569. timeout, ok := lookupIntFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__TIMEOUT", idx))
  1570. if ok {
  1571. cfg.Timeout = int(timeout)
  1572. }
  1573. env, ok := lookupStringListFromEnv(fmt.Sprintf("SFTPGO_COMMAND__COMMANDS__%v__ENV", idx))
  1574. if ok {
  1575. cfg.Env = env
  1576. }
  1577. if cfg.Path != "" {
  1578. if len(globalConf.CommandConfig.Commands) > idx {
  1579. globalConf.CommandConfig.Commands[idx] = cfg
  1580. } else {
  1581. globalConf.CommandConfig.Commands = append(globalConf.CommandConfig.Commands, cfg)
  1582. }
  1583. }
  1584. }
  1585. func setViperDefaults() {
  1586. viper.SetDefault("common.idle_timeout", globalConf.Common.IdleTimeout)
  1587. viper.SetDefault("common.upload_mode", globalConf.Common.UploadMode)
  1588. viper.SetDefault("common.actions.execute_on", globalConf.Common.Actions.ExecuteOn)
  1589. viper.SetDefault("common.actions.execute_sync", globalConf.Common.Actions.ExecuteSync)
  1590. viper.SetDefault("common.actions.hook", globalConf.Common.Actions.Hook)
  1591. viper.SetDefault("common.setstat_mode", globalConf.Common.SetstatMode)
  1592. viper.SetDefault("common.temp_path", globalConf.Common.TempPath)
  1593. viper.SetDefault("common.proxy_protocol", globalConf.Common.ProxyProtocol)
  1594. viper.SetDefault("common.proxy_allowed", globalConf.Common.ProxyAllowed)
  1595. viper.SetDefault("common.post_connect_hook", globalConf.Common.PostConnectHook)
  1596. viper.SetDefault("common.post_disconnect_hook", globalConf.Common.PostDisconnectHook)
  1597. viper.SetDefault("common.data_retention_hook", globalConf.Common.DataRetentionHook)
  1598. viper.SetDefault("common.max_total_connections", globalConf.Common.MaxTotalConnections)
  1599. viper.SetDefault("common.max_per_host_connections", globalConf.Common.MaxPerHostConnections)
  1600. viper.SetDefault("common.whitelist_file", globalConf.Common.WhiteListFile)
  1601. viper.SetDefault("common.defender.enabled", globalConf.Common.DefenderConfig.Enabled)
  1602. viper.SetDefault("common.defender.driver", globalConf.Common.DefenderConfig.Driver)
  1603. viper.SetDefault("common.defender.ban_time", globalConf.Common.DefenderConfig.BanTime)
  1604. viper.SetDefault("common.defender.ban_time_increment", globalConf.Common.DefenderConfig.BanTimeIncrement)
  1605. viper.SetDefault("common.defender.threshold", globalConf.Common.DefenderConfig.Threshold)
  1606. viper.SetDefault("common.defender.score_invalid", globalConf.Common.DefenderConfig.ScoreInvalid)
  1607. viper.SetDefault("common.defender.score_valid", globalConf.Common.DefenderConfig.ScoreValid)
  1608. viper.SetDefault("common.defender.score_limit_exceeded", globalConf.Common.DefenderConfig.ScoreLimitExceeded)
  1609. viper.SetDefault("common.defender.observation_time", globalConf.Common.DefenderConfig.ObservationTime)
  1610. viper.SetDefault("common.defender.entries_soft_limit", globalConf.Common.DefenderConfig.EntriesSoftLimit)
  1611. viper.SetDefault("common.defender.entries_hard_limit", globalConf.Common.DefenderConfig.EntriesHardLimit)
  1612. viper.SetDefault("common.defender.safelist_file", globalConf.Common.DefenderConfig.SafeListFile)
  1613. viper.SetDefault("common.defender.blocklist_file", globalConf.Common.DefenderConfig.BlockListFile)
  1614. viper.SetDefault("common.defender.safelist", globalConf.Common.DefenderConfig.SafeList)
  1615. viper.SetDefault("common.defender.blocklist", globalConf.Common.DefenderConfig.BlockList)
  1616. viper.SetDefault("acme.email", globalConf.ACME.Email)
  1617. viper.SetDefault("acme.key_type", globalConf.ACME.KeyType)
  1618. viper.SetDefault("acme.certs_path", globalConf.ACME.CertsPath)
  1619. viper.SetDefault("acme.ca_endpoint", globalConf.ACME.CAEndpoint)
  1620. viper.SetDefault("acme.domains", globalConf.ACME.Domains)
  1621. viper.SetDefault("acme.renew_days", globalConf.ACME.RenewDays)
  1622. viper.SetDefault("acme.http01_challenge.port", globalConf.ACME.HTTP01Challenge.Port)
  1623. viper.SetDefault("acme.http01_challenge.webroot", globalConf.ACME.HTTP01Challenge.WebRoot)
  1624. viper.SetDefault("acme.http01_challenge.proxy_header", globalConf.ACME.HTTP01Challenge.ProxyHeader)
  1625. viper.SetDefault("acme.tls_alpn01_challenge.port", globalConf.ACME.TLSALPN01Challenge.Port)
  1626. viper.SetDefault("sftpd.max_auth_tries", globalConf.SFTPD.MaxAuthTries)
  1627. viper.SetDefault("sftpd.banner", globalConf.SFTPD.Banner)
  1628. viper.SetDefault("sftpd.host_keys", globalConf.SFTPD.HostKeys)
  1629. viper.SetDefault("sftpd.host_certificates", globalConf.SFTPD.HostCertificates)
  1630. viper.SetDefault("sftpd.host_key_algorithms", globalConf.SFTPD.HostKeyAlgorithms)
  1631. viper.SetDefault("sftpd.kex_algorithms", globalConf.SFTPD.KexAlgorithms)
  1632. viper.SetDefault("sftpd.ciphers", globalConf.SFTPD.Ciphers)
  1633. viper.SetDefault("sftpd.macs", globalConf.SFTPD.MACs)
  1634. viper.SetDefault("sftpd.trusted_user_ca_keys", globalConf.SFTPD.TrustedUserCAKeys)
  1635. viper.SetDefault("sftpd.revoked_user_certs_file", globalConf.SFTPD.RevokedUserCertsFile)
  1636. viper.SetDefault("sftpd.login_banner_file", globalConf.SFTPD.LoginBannerFile)
  1637. viper.SetDefault("sftpd.enabled_ssh_commands", sftpd.GetDefaultSSHCommands())
  1638. viper.SetDefault("sftpd.keyboard_interactive_authentication", globalConf.SFTPD.KeyboardInteractiveAuthentication)
  1639. viper.SetDefault("sftpd.keyboard_interactive_auth_hook", globalConf.SFTPD.KeyboardInteractiveHook)
  1640. viper.SetDefault("sftpd.password_authentication", globalConf.SFTPD.PasswordAuthentication)
  1641. viper.SetDefault("sftpd.folder_prefix", globalConf.SFTPD.FolderPrefix)
  1642. viper.SetDefault("ftpd.banner", globalConf.FTPD.Banner)
  1643. viper.SetDefault("ftpd.banner_file", globalConf.FTPD.BannerFile)
  1644. viper.SetDefault("ftpd.active_transfers_port_non_20", globalConf.FTPD.ActiveTransfersPortNon20)
  1645. viper.SetDefault("ftpd.passive_port_range.start", globalConf.FTPD.PassivePortRange.Start)
  1646. viper.SetDefault("ftpd.passive_port_range.end", globalConf.FTPD.PassivePortRange.End)
  1647. viper.SetDefault("ftpd.disable_active_mode", globalConf.FTPD.DisableActiveMode)
  1648. viper.SetDefault("ftpd.enable_site", globalConf.FTPD.EnableSite)
  1649. viper.SetDefault("ftpd.hash_support", globalConf.FTPD.HASHSupport)
  1650. viper.SetDefault("ftpd.combine_support", globalConf.FTPD.CombineSupport)
  1651. viper.SetDefault("ftpd.certificate_file", globalConf.FTPD.CertificateFile)
  1652. viper.SetDefault("ftpd.certificate_key_file", globalConf.FTPD.CertificateKeyFile)
  1653. viper.SetDefault("ftpd.ca_certificates", globalConf.FTPD.CACertificates)
  1654. viper.SetDefault("ftpd.ca_revocation_lists", globalConf.FTPD.CARevocationLists)
  1655. viper.SetDefault("webdavd.certificate_file", globalConf.WebDAVD.CertificateFile)
  1656. viper.SetDefault("webdavd.certificate_key_file", globalConf.WebDAVD.CertificateKeyFile)
  1657. viper.SetDefault("webdavd.ca_certificates", globalConf.WebDAVD.CACertificates)
  1658. viper.SetDefault("webdavd.ca_revocation_lists", globalConf.WebDAVD.CARevocationLists)
  1659. viper.SetDefault("webdavd.cors.enabled", globalConf.WebDAVD.Cors.Enabled)
  1660. viper.SetDefault("webdavd.cors.allowed_origins", globalConf.WebDAVD.Cors.AllowedOrigins)
  1661. viper.SetDefault("webdavd.cors.allowed_methods", globalConf.WebDAVD.Cors.AllowedMethods)
  1662. viper.SetDefault("webdavd.cors.allowed_headers", globalConf.WebDAVD.Cors.AllowedHeaders)
  1663. viper.SetDefault("webdavd.cors.exposed_headers", globalConf.WebDAVD.Cors.ExposedHeaders)
  1664. viper.SetDefault("webdavd.cors.allow_credentials", globalConf.WebDAVD.Cors.AllowCredentials)
  1665. viper.SetDefault("webdavd.cors.max_age", globalConf.WebDAVD.Cors.MaxAge)
  1666. viper.SetDefault("webdavd.cache.users.expiration_time", globalConf.WebDAVD.Cache.Users.ExpirationTime)
  1667. viper.SetDefault("webdavd.cache.users.max_size", globalConf.WebDAVD.Cache.Users.MaxSize)
  1668. viper.SetDefault("webdavd.cache.mime_types.enabled", globalConf.WebDAVD.Cache.MimeTypes.Enabled)
  1669. viper.SetDefault("webdavd.cache.mime_types.max_size", globalConf.WebDAVD.Cache.MimeTypes.MaxSize)
  1670. viper.SetDefault("data_provider.driver", globalConf.ProviderConf.Driver)
  1671. viper.SetDefault("data_provider.name", globalConf.ProviderConf.Name)
  1672. viper.SetDefault("data_provider.host", globalConf.ProviderConf.Host)
  1673. viper.SetDefault("data_provider.port", globalConf.ProviderConf.Port)
  1674. viper.SetDefault("data_provider.username", globalConf.ProviderConf.Username)
  1675. viper.SetDefault("data_provider.password", globalConf.ProviderConf.Password)
  1676. viper.SetDefault("data_provider.sslmode", globalConf.ProviderConf.SSLMode)
  1677. viper.SetDefault("data_provider.root_cert", globalConf.ProviderConf.RootCert)
  1678. viper.SetDefault("data_provider.client_cert", globalConf.ProviderConf.ClientCert)
  1679. viper.SetDefault("data_provider.client_key", globalConf.ProviderConf.ClientKey)
  1680. viper.SetDefault("data_provider.connection_string", globalConf.ProviderConf.ConnectionString)
  1681. viper.SetDefault("data_provider.sql_tables_prefix", globalConf.ProviderConf.SQLTablesPrefix)
  1682. viper.SetDefault("data_provider.track_quota", globalConf.ProviderConf.TrackQuota)
  1683. viper.SetDefault("data_provider.pool_size", globalConf.ProviderConf.PoolSize)
  1684. viper.SetDefault("data_provider.users_base_dir", globalConf.ProviderConf.UsersBaseDir)
  1685. viper.SetDefault("data_provider.actions.execute_on", globalConf.ProviderConf.Actions.ExecuteOn)
  1686. viper.SetDefault("data_provider.actions.execute_for", globalConf.ProviderConf.Actions.ExecuteFor)
  1687. viper.SetDefault("data_provider.actions.hook", globalConf.ProviderConf.Actions.Hook)
  1688. viper.SetDefault("data_provider.external_auth_hook", globalConf.ProviderConf.ExternalAuthHook)
  1689. viper.SetDefault("data_provider.external_auth_scope", globalConf.ProviderConf.ExternalAuthScope)
  1690. viper.SetDefault("data_provider.pre_login_hook", globalConf.ProviderConf.PreLoginHook)
  1691. viper.SetDefault("data_provider.post_login_hook", globalConf.ProviderConf.PostLoginHook)
  1692. viper.SetDefault("data_provider.post_login_scope", globalConf.ProviderConf.PostLoginScope)
  1693. viper.SetDefault("data_provider.check_password_hook", globalConf.ProviderConf.CheckPasswordHook)
  1694. viper.SetDefault("data_provider.check_password_scope", globalConf.ProviderConf.CheckPasswordScope)
  1695. viper.SetDefault("data_provider.password_hashing.bcrypt_options.cost", globalConf.ProviderConf.PasswordHashing.BcryptOptions.Cost)
  1696. viper.SetDefault("data_provider.password_hashing.argon2_options.memory", globalConf.ProviderConf.PasswordHashing.Argon2Options.Memory)
  1697. viper.SetDefault("data_provider.password_hashing.argon2_options.iterations", globalConf.ProviderConf.PasswordHashing.Argon2Options.Iterations)
  1698. viper.SetDefault("data_provider.password_hashing.argon2_options.parallelism", globalConf.ProviderConf.PasswordHashing.Argon2Options.Parallelism)
  1699. viper.SetDefault("data_provider.password_hashing.algo", globalConf.ProviderConf.PasswordHashing.Algo)
  1700. viper.SetDefault("data_provider.password_validation.admins.min_entropy", globalConf.ProviderConf.PasswordValidation.Admins.MinEntropy)
  1701. viper.SetDefault("data_provider.password_validation.users.min_entropy", globalConf.ProviderConf.PasswordValidation.Users.MinEntropy)
  1702. viper.SetDefault("data_provider.password_caching", globalConf.ProviderConf.PasswordCaching)
  1703. viper.SetDefault("data_provider.update_mode", globalConf.ProviderConf.UpdateMode)
  1704. viper.SetDefault("data_provider.delayed_quota_update", globalConf.ProviderConf.DelayedQuotaUpdate)
  1705. viper.SetDefault("data_provider.create_default_admin", globalConf.ProviderConf.CreateDefaultAdmin)
  1706. viper.SetDefault("data_provider.naming_rules", globalConf.ProviderConf.NamingRules)
  1707. viper.SetDefault("data_provider.is_shared", globalConf.ProviderConf.IsShared)
  1708. viper.SetDefault("data_provider.backups_path", globalConf.ProviderConf.BackupsPath)
  1709. viper.SetDefault("httpd.templates_path", globalConf.HTTPDConfig.TemplatesPath)
  1710. viper.SetDefault("httpd.static_files_path", globalConf.HTTPDConfig.StaticFilesPath)
  1711. viper.SetDefault("httpd.openapi_path", globalConf.HTTPDConfig.OpenAPIPath)
  1712. viper.SetDefault("httpd.web_root", globalConf.HTTPDConfig.WebRoot)
  1713. viper.SetDefault("httpd.certificate_file", globalConf.HTTPDConfig.CertificateFile)
  1714. viper.SetDefault("httpd.certificate_key_file", globalConf.HTTPDConfig.CertificateKeyFile)
  1715. viper.SetDefault("httpd.ca_certificates", globalConf.HTTPDConfig.CACertificates)
  1716. viper.SetDefault("httpd.ca_revocation_lists", globalConf.HTTPDConfig.CARevocationLists)
  1717. viper.SetDefault("httpd.signing_passphrase", globalConf.HTTPDConfig.SigningPassphrase)
  1718. viper.SetDefault("httpd.token_validation", globalConf.HTTPDConfig.TokenValidation)
  1719. viper.SetDefault("httpd.max_upload_file_size", globalConf.HTTPDConfig.MaxUploadFileSize)
  1720. viper.SetDefault("httpd.cors.enabled", globalConf.HTTPDConfig.Cors.Enabled)
  1721. viper.SetDefault("httpd.cors.allowed_origins", globalConf.HTTPDConfig.Cors.AllowedOrigins)
  1722. viper.SetDefault("httpd.cors.allowed_methods", globalConf.HTTPDConfig.Cors.AllowedMethods)
  1723. viper.SetDefault("httpd.cors.allowed_headers", globalConf.HTTPDConfig.Cors.AllowedHeaders)
  1724. viper.SetDefault("httpd.cors.exposed_headers", globalConf.HTTPDConfig.Cors.ExposedHeaders)
  1725. viper.SetDefault("httpd.cors.allow_credentials", globalConf.HTTPDConfig.Cors.AllowCredentials)
  1726. viper.SetDefault("httpd.cors.max_age", globalConf.HTTPDConfig.Cors.MaxAge)
  1727. viper.SetDefault("httpd.setup.installation_code", globalConf.HTTPDConfig.Setup.InstallationCode)
  1728. viper.SetDefault("httpd.setup.installation_code_hint", globalConf.HTTPDConfig.Setup.InstallationCodeHint)
  1729. viper.SetDefault("httpd.hide_support_link", globalConf.HTTPDConfig.HideSupportLink)
  1730. viper.SetDefault("http.timeout", globalConf.HTTPConfig.Timeout)
  1731. viper.SetDefault("http.retry_wait_min", globalConf.HTTPConfig.RetryWaitMin)
  1732. viper.SetDefault("http.retry_wait_max", globalConf.HTTPConfig.RetryWaitMax)
  1733. viper.SetDefault("http.retry_max", globalConf.HTTPConfig.RetryMax)
  1734. viper.SetDefault("http.ca_certificates", globalConf.HTTPConfig.CACertificates)
  1735. viper.SetDefault("http.skip_tls_verify", globalConf.HTTPConfig.SkipTLSVerify)
  1736. viper.SetDefault("command.timeout", globalConf.CommandConfig.Timeout)
  1737. viper.SetDefault("command.env", globalConf.CommandConfig.Env)
  1738. viper.SetDefault("kms.secrets.url", globalConf.KMSConfig.Secrets.URL)
  1739. viper.SetDefault("kms.secrets.master_key", globalConf.KMSConfig.Secrets.MasterKeyString)
  1740. viper.SetDefault("kms.secrets.master_key_path", globalConf.KMSConfig.Secrets.MasterKeyPath)
  1741. viper.SetDefault("telemetry.bind_port", globalConf.TelemetryConfig.BindPort)
  1742. viper.SetDefault("telemetry.bind_address", globalConf.TelemetryConfig.BindAddress)
  1743. viper.SetDefault("telemetry.enable_profiler", globalConf.TelemetryConfig.EnableProfiler)
  1744. viper.SetDefault("telemetry.auth_user_file", globalConf.TelemetryConfig.AuthUserFile)
  1745. viper.SetDefault("telemetry.certificate_file", globalConf.TelemetryConfig.CertificateFile)
  1746. viper.SetDefault("telemetry.certificate_key_file", globalConf.TelemetryConfig.CertificateKeyFile)
  1747. viper.SetDefault("telemetry.min_tls_version", globalConf.TelemetryConfig.MinTLSVersion)
  1748. viper.SetDefault("telemetry.tls_cipher_suites", globalConf.TelemetryConfig.TLSCipherSuites)
  1749. viper.SetDefault("smtp.host", globalConf.SMTPConfig.Host)
  1750. viper.SetDefault("smtp.port", globalConf.SMTPConfig.Port)
  1751. viper.SetDefault("smtp.from", globalConf.SMTPConfig.From)
  1752. viper.SetDefault("smtp.user", globalConf.SMTPConfig.User)
  1753. viper.SetDefault("smtp.password", globalConf.SMTPConfig.Password)
  1754. viper.SetDefault("smtp.auth_type", globalConf.SMTPConfig.AuthType)
  1755. viper.SetDefault("smtp.encryption", globalConf.SMTPConfig.Encryption)
  1756. viper.SetDefault("smtp.domain", globalConf.SMTPConfig.Domain)
  1757. viper.SetDefault("smtp.templates_path", globalConf.SMTPConfig.TemplatesPath)
  1758. }
  1759. func lookupBoolFromEnv(envName string) (bool, bool) {
  1760. value, ok := os.LookupEnv(envName)
  1761. if ok {
  1762. converted, err := strconv.ParseBool(strings.TrimSpace(value))
  1763. if err == nil {
  1764. return converted, ok
  1765. }
  1766. }
  1767. return false, false
  1768. }
  1769. func lookupIntFromEnv(envName string) (int64, bool) {
  1770. value, ok := os.LookupEnv(envName)
  1771. if ok {
  1772. converted, err := strconv.ParseInt(strings.TrimSpace(value), 10, 64)
  1773. if err == nil {
  1774. return converted, ok
  1775. }
  1776. }
  1777. return 0, false
  1778. }
  1779. func lookupStringListFromEnv(envName string) ([]string, bool) {
  1780. value, ok := os.LookupEnv(envName)
  1781. if ok {
  1782. var result []string
  1783. for _, v := range strings.Split(value, ",") {
  1784. val := strings.TrimSpace(v)
  1785. if val != "" {
  1786. result = append(result, val)
  1787. }
  1788. }
  1789. return result, true
  1790. }
  1791. return nil, false
  1792. }