config.go 68 KB

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