config.go 68 KB

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