serverconf.go 21 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736737738739740741742743744745746747748749750751752753754755756757758759760761762763764765766767768769770771772773774775776777778779780781782783784785786787788789790791792793794795796797798799800801802803804805806807808809810811812813814815816
  1. package servercfg
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "time"
  10. "github.com/gravitl/netmaker/config"
  11. "github.com/gravitl/netmaker/models"
  12. )
  13. // EmqxBrokerType denotes the broker type for EMQX MQTT
  14. const EmqxBrokerType = "emqx"
  15. // Emqxdeploy - emqx deploy type
  16. type Emqxdeploy string
  17. var (
  18. Version = "dev"
  19. IsPro = false
  20. ErrLicenseValidation error
  21. EmqxCloudDeploy Emqxdeploy = "cloud"
  22. EmqxOnPremDeploy Emqxdeploy = "on-prem"
  23. )
  24. // SetHost - sets the host ip
  25. func SetHost() error {
  26. remoteip, err := GetPublicIP()
  27. if err != nil {
  28. return err
  29. }
  30. os.Setenv("SERVER_HOST", remoteip)
  31. return nil
  32. }
  33. // GetServerConfig - gets the server config into memory from file or env
  34. func GetServerConfig() config.ServerConfig {
  35. var cfg config.ServerConfig
  36. cfg.APIConnString = GetAPIConnString()
  37. cfg.CoreDNSAddr = GetCoreDNSAddr()
  38. cfg.APIHost = GetAPIHost()
  39. cfg.APIPort = GetAPIPort()
  40. cfg.MasterKey = "(hidden)"
  41. cfg.DNSKey = "(hidden)"
  42. cfg.AllowedOrigin = GetAllowedOrigin()
  43. cfg.RestBackend = "off"
  44. cfg.NodeID = GetNodeID()
  45. cfg.BrokerType = GetBrokerType()
  46. cfg.EmqxRestEndpoint = GetEmqxRestEndpoint()
  47. if AutoUpdateEnabled() {
  48. cfg.NetclientAutoUpdate = "enabled"
  49. } else {
  50. cfg.NetclientAutoUpdate = "disabled"
  51. }
  52. if IsRestBackend() {
  53. cfg.RestBackend = "on"
  54. }
  55. cfg.DNSMode = "off"
  56. if IsDNSMode() {
  57. cfg.DNSMode = "on"
  58. }
  59. cfg.DisplayKeys = "off"
  60. if IsDisplayKeys() {
  61. cfg.DisplayKeys = "on"
  62. }
  63. cfg.DisableRemoteIPCheck = "off"
  64. if DisableRemoteIPCheck() {
  65. cfg.DisableRemoteIPCheck = "on"
  66. }
  67. cfg.Database = GetDB()
  68. cfg.Platform = GetPlatform()
  69. cfg.Version = GetVersion()
  70. // == auth config ==
  71. var authInfo = GetAuthProviderInfo()
  72. cfg.AuthProvider = authInfo[0]
  73. cfg.ClientID = authInfo[1]
  74. cfg.ClientSecret = authInfo[2]
  75. cfg.FrontendURL = GetFrontendURL()
  76. cfg.Telemetry = Telemetry()
  77. cfg.Server = GetServer()
  78. cfg.Verbosity = GetVerbosity()
  79. cfg.IsPro = "no"
  80. if IsPro {
  81. cfg.IsPro = "yes"
  82. }
  83. cfg.JwtValidityDuration = GetJwtValidityDuration()
  84. cfg.RacAutoDisable = GetRacAutoDisable()
  85. cfg.MetricInterval = GetMetricInterval()
  86. return cfg
  87. }
  88. // GetJwtValidityDuration - returns the JWT validity duration in seconds
  89. func GetJwtValidityDuration() time.Duration {
  90. var defaultDuration = time.Duration(24) * time.Hour
  91. if os.Getenv("JWT_VALIDITY_DURATION") != "" {
  92. t, err := strconv.Atoi(os.Getenv("JWT_VALIDITY_DURATION"))
  93. if err != nil {
  94. return defaultDuration
  95. }
  96. return time.Duration(t) * time.Second
  97. }
  98. return defaultDuration
  99. }
  100. // GetRacAutoDisable - returns whether the feature to autodisable RAC is enabled
  101. func GetRacAutoDisable() bool {
  102. return os.Getenv("RAC_AUTO_DISABLE") == "true"
  103. }
  104. // GetServerInfo - gets the server config into memory from file or env
  105. func GetServerInfo() models.ServerConfig {
  106. var cfg models.ServerConfig
  107. cfg.Server = GetServer()
  108. if GetBrokerType() == EmqxBrokerType {
  109. cfg.MQUserName = "HOST_ID"
  110. cfg.MQPassword = "HOST_PASS"
  111. } else {
  112. cfg.MQUserName = GetMqUserName()
  113. cfg.MQPassword = GetMqPassword()
  114. }
  115. cfg.API = GetAPIConnString()
  116. cfg.CoreDNSAddr = GetCoreDNSAddr()
  117. cfg.APIPort = GetAPIPort()
  118. cfg.DNSMode = "off"
  119. cfg.Broker = GetPublicBrokerEndpoint()
  120. cfg.BrokerType = GetBrokerType()
  121. if IsDNSMode() {
  122. cfg.DNSMode = "on"
  123. }
  124. cfg.Version = GetVersion()
  125. cfg.IsPro = IsPro
  126. cfg.MetricInterval = GetMetricInterval()
  127. return cfg
  128. }
  129. // GetFrontendURL - gets the frontend url
  130. func GetFrontendURL() string {
  131. var frontend = ""
  132. if os.Getenv("FRONTEND_URL") != "" {
  133. frontend = os.Getenv("FRONTEND_URL")
  134. } else if config.Config.Server.FrontendURL != "" {
  135. frontend = config.Config.Server.FrontendURL
  136. }
  137. return frontend
  138. }
  139. // GetAPIConnString - gets the api connections string
  140. func GetAPIConnString() string {
  141. conn := ""
  142. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  143. conn = os.Getenv("SERVER_API_CONN_STRING")
  144. } else if config.Config.Server.APIConnString != "" {
  145. conn = config.Config.Server.APIConnString
  146. }
  147. return conn
  148. }
  149. // SetVersion - set version of netmaker
  150. func SetVersion(v string) {
  151. Version = v
  152. }
  153. // GetVersion - version of netmaker
  154. func GetVersion() string {
  155. return Version
  156. }
  157. // GetDB - gets the database type
  158. func GetDB() string {
  159. database := "sqlite"
  160. if os.Getenv("DATABASE") != "" {
  161. database = os.Getenv("DATABASE")
  162. } else if config.Config.Server.Database != "" {
  163. database = config.Config.Server.Database
  164. }
  165. return database
  166. }
  167. // CacheEnabled - checks if cache is enabled
  168. func CacheEnabled() bool {
  169. caching := true
  170. if os.Getenv("CACHING_ENABLED") == "false" {
  171. caching = false
  172. } else if config.Config.Server.CacheEnabled == "false" {
  173. caching = false
  174. }
  175. return caching
  176. }
  177. // GetAPIHost - gets the api host
  178. func GetAPIHost() string {
  179. serverhost := "127.0.0.1"
  180. remoteip, _ := GetPublicIP()
  181. if os.Getenv("SERVER_HTTP_HOST") != "" {
  182. serverhost = os.Getenv("SERVER_HTTP_HOST")
  183. } else if config.Config.Server.APIHost != "" {
  184. serverhost = config.Config.Server.APIHost
  185. } else if os.Getenv("SERVER_HOST") != "" {
  186. serverhost = os.Getenv("SERVER_HOST")
  187. } else {
  188. if remoteip != "" {
  189. serverhost = remoteip
  190. }
  191. }
  192. return serverhost
  193. }
  194. // GetAPIPort - gets the api port
  195. func GetAPIPort() string {
  196. apiport := "8081"
  197. if os.Getenv("API_PORT") != "" {
  198. apiport = os.Getenv("API_PORT")
  199. } else if config.Config.Server.APIPort != "" {
  200. apiport = config.Config.Server.APIPort
  201. }
  202. return apiport
  203. }
  204. // GetCoreDNSAddr - gets the core dns address
  205. func GetCoreDNSAddr() string {
  206. addr, _ := GetPublicIP()
  207. if os.Getenv("COREDNS_ADDR") != "" {
  208. addr = os.Getenv("COREDNS_ADDR")
  209. } else if config.Config.Server.CoreDNSAddr != "" {
  210. addr = config.Config.Server.CoreDNSAddr
  211. }
  212. return addr
  213. }
  214. // GetPublicBrokerEndpoint - returns the public broker endpoint which shall be used by netclient
  215. func GetPublicBrokerEndpoint() string {
  216. if os.Getenv("BROKER_ENDPOINT") != "" {
  217. return os.Getenv("BROKER_ENDPOINT")
  218. } else {
  219. return config.Config.Server.Broker
  220. }
  221. }
  222. func GetSmtpHost() string {
  223. v := ""
  224. if fromEnv := os.Getenv("SMTP_HOST"); fromEnv != "" {
  225. v = fromEnv
  226. } else if fromCfg := config.Config.Server.SmtpHost; fromCfg != "" {
  227. v = fromCfg
  228. }
  229. return v
  230. }
  231. func GetSmtpPort() int {
  232. v := 587
  233. if fromEnv := os.Getenv("SMTP_PORT"); fromEnv != "" {
  234. port, err := strconv.Atoi(fromEnv)
  235. if err == nil {
  236. v = port
  237. }
  238. } else if fromCfg := config.Config.Server.SmtpPort; fromCfg != 0 {
  239. v = fromCfg
  240. }
  241. return v
  242. }
  243. func GetSenderEmail() string {
  244. v := ""
  245. if fromEnv := os.Getenv("EMAIL_SENDER_ADDR"); fromEnv != "" {
  246. v = fromEnv
  247. } else if fromCfg := config.Config.Server.EmailSenderAddr; fromCfg != "" {
  248. v = fromCfg
  249. }
  250. return v
  251. }
  252. func GetSenderUser() string {
  253. v := ""
  254. if fromEnv := os.Getenv("EMAIL_SENDER_USER"); fromEnv != "" {
  255. v = fromEnv
  256. } else if fromCfg := config.Config.Server.EmailSenderUser; fromCfg != "" {
  257. v = fromCfg
  258. }
  259. return v
  260. }
  261. func GetEmaiSenderPassword() string {
  262. v := ""
  263. if fromEnv := os.Getenv("EMAIL_SENDER_PASSWORD"); fromEnv != "" {
  264. v = fromEnv
  265. } else if fromCfg := config.Config.Server.EmailSenderPassword; fromCfg != "" {
  266. v = fromCfg
  267. }
  268. return v
  269. }
  270. // GetOwnerEmail - gets the owner email (saas)
  271. func GetOwnerEmail() string {
  272. return os.Getenv("SAAS_OWNER_EMAIL")
  273. }
  274. // GetMessageQueueEndpoint - gets the message queue endpoint
  275. func GetMessageQueueEndpoint() (string, bool) {
  276. host, _ := GetPublicIP()
  277. if os.Getenv("SERVER_BROKER_ENDPOINT") != "" {
  278. host = os.Getenv("SERVER_BROKER_ENDPOINT")
  279. } else if config.Config.Server.ServerBrokerEndpoint != "" {
  280. host = config.Config.Server.ServerBrokerEndpoint
  281. } else if os.Getenv("BROKER_ENDPOINT") != "" {
  282. host = os.Getenv("BROKER_ENDPOINT")
  283. } else if config.Config.Server.Broker != "" {
  284. host = config.Config.Server.Broker
  285. } else {
  286. host += ":1883" // default
  287. }
  288. return host, strings.Contains(host, "wss") || strings.Contains(host, "ssl") || strings.Contains(host, "mqtts")
  289. }
  290. // GetBrokerType - returns the type of MQ broker
  291. func GetBrokerType() string {
  292. if os.Getenv("BROKER_TYPE") != "" {
  293. return os.Getenv("BROKER_TYPE")
  294. } else {
  295. return "mosquitto"
  296. }
  297. }
  298. // GetMasterKey - gets the configured master key of server
  299. func GetMasterKey() string {
  300. key := ""
  301. if os.Getenv("MASTER_KEY") != "" {
  302. key = os.Getenv("MASTER_KEY")
  303. } else if config.Config.Server.MasterKey != "" {
  304. key = config.Config.Server.MasterKey
  305. }
  306. return key
  307. }
  308. // GetAllowedOrigin - get the allowed origin
  309. func GetAllowedOrigin() string {
  310. allowedorigin := "*"
  311. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  312. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  313. } else if config.Config.Server.AllowedOrigin != "" {
  314. allowedorigin = config.Config.Server.AllowedOrigin
  315. }
  316. return allowedorigin
  317. }
  318. // IsRestBackend - checks if rest is on or off
  319. func IsRestBackend() bool {
  320. isrest := true
  321. if os.Getenv("REST_BACKEND") != "" {
  322. if os.Getenv("REST_BACKEND") == "off" {
  323. isrest = false
  324. }
  325. } else if config.Config.Server.RestBackend != "" {
  326. if config.Config.Server.RestBackend == "off" {
  327. isrest = false
  328. }
  329. }
  330. return isrest
  331. }
  332. // IsMetricsExporter - checks if metrics exporter is on or off
  333. func IsMetricsExporter() bool {
  334. export := false
  335. if os.Getenv("METRICS_EXPORTER") != "" {
  336. if os.Getenv("METRICS_EXPORTER") == "on" {
  337. export = true
  338. }
  339. } else if config.Config.Server.MetricsExporter != "" {
  340. if config.Config.Server.MetricsExporter == "on" {
  341. export = true
  342. }
  343. }
  344. return export
  345. }
  346. // IsMessageQueueBackend - checks if message queue is on or off
  347. func IsMessageQueueBackend() bool {
  348. ismessagequeue := true
  349. if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
  350. if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
  351. ismessagequeue = false
  352. }
  353. } else if config.Config.Server.MessageQueueBackend != "" {
  354. if config.Config.Server.MessageQueueBackend == "off" {
  355. ismessagequeue = false
  356. }
  357. }
  358. return ismessagequeue
  359. }
  360. // Telemetry - checks if telemetry data should be sent
  361. func Telemetry() string {
  362. telemetry := "on"
  363. if os.Getenv("TELEMETRY") == "off" {
  364. telemetry = "off"
  365. }
  366. if config.Config.Server.Telemetry == "off" {
  367. telemetry = "off"
  368. }
  369. return telemetry
  370. }
  371. // GetServer - gets the server name
  372. func GetServer() string {
  373. server := ""
  374. if os.Getenv("SERVER_NAME") != "" {
  375. server = os.Getenv("SERVER_NAME")
  376. } else if config.Config.Server.Server != "" {
  377. server = config.Config.Server.Server
  378. }
  379. return server
  380. }
  381. func GetVerbosity() int32 {
  382. var verbosity = 0
  383. var err error
  384. if os.Getenv("VERBOSITY") != "" {
  385. verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
  386. if err != nil {
  387. verbosity = 0
  388. }
  389. } else if config.Config.Server.Verbosity != 0 {
  390. verbosity = int(config.Config.Server.Verbosity)
  391. }
  392. if verbosity < 0 || verbosity > 4 {
  393. verbosity = 0
  394. }
  395. return int32(verbosity)
  396. }
  397. // AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled
  398. // default is enabled
  399. func AutoUpdateEnabled() bool {
  400. if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {
  401. return false
  402. } else if config.Config.Server.NetclientAutoUpdate == "disabled" {
  403. return false
  404. }
  405. return true
  406. }
  407. // IsDNSMode - should it run with DNS
  408. func IsDNSMode() bool {
  409. isdns := true
  410. if os.Getenv("DNS_MODE") != "" {
  411. if os.Getenv("DNS_MODE") == "off" {
  412. isdns = false
  413. }
  414. } else if config.Config.Server.DNSMode != "" {
  415. if config.Config.Server.DNSMode == "off" {
  416. isdns = false
  417. }
  418. }
  419. return isdns
  420. }
  421. // IsDisplayKeys - should server be able to display keys?
  422. func IsDisplayKeys() bool {
  423. isdisplay := true
  424. if os.Getenv("DISPLAY_KEYS") != "" {
  425. if os.Getenv("DISPLAY_KEYS") == "off" {
  426. isdisplay = false
  427. }
  428. } else if config.Config.Server.DisplayKeys != "" {
  429. if config.Config.Server.DisplayKeys == "off" {
  430. isdisplay = false
  431. }
  432. }
  433. return isdisplay
  434. }
  435. // DisableRemoteIPCheck - disable the remote ip check
  436. func DisableRemoteIPCheck() bool {
  437. disabled := false
  438. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  439. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  440. disabled = true
  441. }
  442. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  443. if config.Config.Server.DisableRemoteIPCheck == "on" {
  444. disabled = true
  445. }
  446. }
  447. return disabled
  448. }
  449. // GetPublicIP - gets public ip
  450. func GetPublicIP() (string, error) {
  451. endpoint := ""
  452. var err error
  453. iplist := []string{"https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  454. publicIpService := os.Getenv("PUBLIC_IP_SERVICE")
  455. if publicIpService != "" {
  456. // prepend the user-specified service so it's checked first
  457. iplist = append([]string{publicIpService}, iplist...)
  458. } else if config.Config.Server.PublicIPService != "" {
  459. publicIpService = config.Config.Server.PublicIPService
  460. // prepend the user-specified service so it's checked first
  461. iplist = append([]string{publicIpService}, iplist...)
  462. }
  463. for _, ipserver := range iplist {
  464. client := &http.Client{
  465. Timeout: time.Second * 10,
  466. }
  467. resp, err := client.Get(ipserver)
  468. if err != nil {
  469. continue
  470. }
  471. defer resp.Body.Close()
  472. if resp.StatusCode == http.StatusOK {
  473. bodyBytes, err := io.ReadAll(resp.Body)
  474. if err != nil {
  475. continue
  476. }
  477. endpoint = string(bodyBytes)
  478. break
  479. }
  480. }
  481. if endpoint == "" {
  482. err = errors.New("public address not found")
  483. }
  484. return endpoint, err
  485. }
  486. // GetPlatform - get the system type of server
  487. func GetPlatform() string {
  488. platform := "linux"
  489. if os.Getenv("PLATFORM") != "" {
  490. platform = os.Getenv("PLATFORM")
  491. } else if config.Config.Server.Platform != "" {
  492. platform = config.Config.Server.Platform
  493. }
  494. return platform
  495. }
  496. // GetSQLConn - get the sql connection string
  497. func GetSQLConn() string {
  498. sqlconn := "http://"
  499. if os.Getenv("SQL_CONN") != "" {
  500. sqlconn = os.Getenv("SQL_CONN")
  501. } else if config.Config.Server.SQLConn != "" {
  502. sqlconn = config.Config.Server.SQLConn
  503. }
  504. return sqlconn
  505. }
  506. // GetNodeID - gets the node id
  507. func GetNodeID() string {
  508. var id string
  509. var err error
  510. // id = getMacAddr()
  511. if os.Getenv("NODE_ID") != "" {
  512. id = os.Getenv("NODE_ID")
  513. } else if config.Config.Server.NodeID != "" {
  514. id = config.Config.Server.NodeID
  515. } else {
  516. id, err = os.Hostname()
  517. if err != nil {
  518. return ""
  519. }
  520. }
  521. return id
  522. }
  523. func SetNodeID(id string) {
  524. config.Config.Server.NodeID = id
  525. }
  526. // GetAuthProviderInfo = gets the oauth provider info
  527. func GetAuthProviderInfo() (pi []string) {
  528. var authProvider = ""
  529. defer func() {
  530. if authProvider == "oidc" {
  531. if os.Getenv("OIDC_ISSUER") != "" {
  532. pi = append(pi, os.Getenv("OIDC_ISSUER"))
  533. } else if config.Config.Server.OIDCIssuer != "" {
  534. pi = append(pi, config.Config.Server.OIDCIssuer)
  535. } else {
  536. pi = []string{"", "", ""}
  537. }
  538. }
  539. }()
  540. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  541. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  542. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  543. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  544. } else {
  545. authProvider = ""
  546. }
  547. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  548. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  549. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  550. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  551. }
  552. }
  553. return []string{"", "", ""}
  554. }
  555. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  556. func GetAzureTenant() string {
  557. var azureTenant = ""
  558. if os.Getenv("AZURE_TENANT") != "" {
  559. azureTenant = os.Getenv("AZURE_TENANT")
  560. } else if config.Config.Server.AzureTenant != "" {
  561. azureTenant = config.Config.Server.AzureTenant
  562. }
  563. return azureTenant
  564. }
  565. // GetMqPassword - fetches the MQ password
  566. func GetMqPassword() string {
  567. password := ""
  568. if os.Getenv("MQ_PASSWORD") != "" {
  569. password = os.Getenv("MQ_PASSWORD")
  570. } else if config.Config.Server.MQPassword != "" {
  571. password = config.Config.Server.MQPassword
  572. }
  573. return password
  574. }
  575. // GetMqUserName - fetches the MQ username
  576. func GetMqUserName() string {
  577. password := ""
  578. if os.Getenv("MQ_USERNAME") != "" {
  579. password = os.Getenv("MQ_USERNAME")
  580. } else if config.Config.Server.MQUserName != "" {
  581. password = config.Config.Server.MQUserName
  582. }
  583. return password
  584. }
  585. // GetMetricInterval - get the publish metric interval
  586. func GetMetricInterval() string {
  587. //default 15 minutes
  588. mi := "15"
  589. if os.Getenv("PUBLISH_METRIC_INTERVAL") != "" {
  590. mi = os.Getenv("PUBLISH_METRIC_INTERVAL")
  591. }
  592. return mi
  593. }
  594. // GetBatchPeerUpdate - if batch peer update
  595. func GetBatchPeerUpdate() bool {
  596. enabled := true
  597. if os.Getenv("PEER_UPDATE_BATCH") != "" {
  598. enabled = os.Getenv("PEER_UPDATE_BATCH") == "true"
  599. }
  600. return enabled
  601. }
  602. // GetPeerUpdateBatchSize - get the batch size for peer update
  603. func GetPeerUpdateBatchSize() int {
  604. //default 50
  605. batchSize := 50
  606. if os.Getenv("PEER_UPDATE_BATCH_SIZE") != "" {
  607. b, e := strconv.Atoi(os.Getenv("PEER_UPDATE_BATCH_SIZE"))
  608. if e == nil && b > 0 && b < 1000 {
  609. batchSize = b
  610. }
  611. }
  612. return batchSize
  613. }
  614. // GetEmqxRestEndpoint - returns the REST API Endpoint of EMQX
  615. func GetEmqxRestEndpoint() string {
  616. return os.Getenv("EMQX_REST_ENDPOINT")
  617. }
  618. // IsBasicAuthEnabled - checks if basic auth has been configured to be turned off
  619. func IsBasicAuthEnabled() bool {
  620. var enabled = true //default
  621. if os.Getenv("BASIC_AUTH") != "" {
  622. enabled = os.Getenv("BASIC_AUTH") == "yes"
  623. } else if config.Config.Server.BasicAuth != "" {
  624. enabled = config.Config.Server.BasicAuth == "yes"
  625. }
  626. return enabled
  627. }
  628. // GetLicenseKey - retrieves pro license value from env or conf files
  629. func GetLicenseKey() string {
  630. licenseKeyValue := os.Getenv("LICENSE_KEY")
  631. if licenseKeyValue == "" {
  632. licenseKeyValue = config.Config.Server.LicenseValue
  633. }
  634. return licenseKeyValue
  635. }
  636. // GetNetmakerTenantID - get's the associated, Netmaker, tenant ID to verify ownership
  637. func GetNetmakerTenantID() string {
  638. netmakerTenantID := os.Getenv("NETMAKER_TENANT_ID")
  639. if netmakerTenantID == "" {
  640. netmakerTenantID = config.Config.Server.NetmakerTenantID
  641. }
  642. return netmakerTenantID
  643. }
  644. // GetUserLimit - fetches free tier limits on users
  645. func GetUserLimit() int {
  646. var userslimit int
  647. if os.Getenv("USERS_LIMIT") != "" {
  648. userslimit, _ = strconv.Atoi(os.Getenv("USERS_LIMIT"))
  649. } else {
  650. userslimit = config.Config.Server.UsersLimit
  651. }
  652. return userslimit
  653. }
  654. // GetNetworkLimit - fetches free tier limits on networks
  655. func GetNetworkLimit() int {
  656. var networkslimit int
  657. if os.Getenv("NETWORKS_LIMIT") != "" {
  658. networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))
  659. } else {
  660. networkslimit = config.Config.Server.NetworksLimit
  661. }
  662. return networkslimit
  663. }
  664. // GetMachinesLimit - fetches free tier limits on machines (clients + hosts)
  665. func GetMachinesLimit() int {
  666. if l, err := strconv.Atoi(os.Getenv("MACHINES_LIMIT")); err == nil {
  667. return l
  668. }
  669. return config.Config.Server.MachinesLimit
  670. }
  671. // GetIngressLimit - fetches free tier limits on ingresses
  672. func GetIngressLimit() int {
  673. if l, err := strconv.Atoi(os.Getenv("INGRESSES_LIMIT")); err == nil {
  674. return l
  675. }
  676. return config.Config.Server.IngressesLimit
  677. }
  678. // GetEgressLimit - fetches free tier limits on egresses
  679. func GetEgressLimit() int {
  680. if l, err := strconv.Atoi(os.Getenv("EGRESSES_LIMIT")); err == nil {
  681. return l
  682. }
  683. return config.Config.Server.EgressesLimit
  684. }
  685. // DeployedByOperator - returns true if the instance is deployed by netmaker operator
  686. func DeployedByOperator() bool {
  687. if os.Getenv("DEPLOYED_BY_OPERATOR") != "" {
  688. return os.Getenv("DEPLOYED_BY_OPERATOR") == "true"
  689. }
  690. return config.Config.Server.DeployedByOperator
  691. }
  692. // IsEndpointDetectionEnabled - returns true if endpoint detection enabled
  693. func IsEndpointDetectionEnabled() bool {
  694. var enabled = true //default
  695. if os.Getenv("ENDPOINT_DETECTION") != "" {
  696. enabled = os.Getenv("ENDPOINT_DETECTION") == "true"
  697. }
  698. return enabled
  699. }
  700. // GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
  701. func GetEnvironment() string {
  702. if env := os.Getenv("ENVIRONMENT"); env != "" {
  703. return env
  704. }
  705. if env := config.Config.Server.Environment; env != "" {
  706. return env
  707. }
  708. return ""
  709. }
  710. // GetEmqxDeployType - fetches emqx deploy type this server uses
  711. func GetEmqxDeployType() (deployType Emqxdeploy) {
  712. deployType = EmqxOnPremDeploy
  713. if os.Getenv("EMQX_DEPLOY_TYPE") == string(EmqxCloudDeploy) {
  714. deployType = EmqxCloudDeploy
  715. }
  716. return
  717. }
  718. // GetEmqxAppID - gets the emqx cloud app id
  719. func GetEmqxAppID() string {
  720. return os.Getenv("EMQX_APP_ID")
  721. }
  722. // GetEmqxAppSecret - gets the emqx cloud app secret
  723. func GetEmqxAppSecret() string {
  724. return os.Getenv("EMQX_APP_SECRET")
  725. }
  726. // GetAllowedEmailDomains - gets the allowed email domains for oauth signup
  727. func GetAllowedEmailDomains() string {
  728. allowedDomains := "*"
  729. if os.Getenv("ALLOWED_EMAIL_DOMAINS") != "" {
  730. allowedDomains = os.Getenv("ALLOWED_EMAIL_DOMAINS")
  731. } else if config.Config.Server.AllowedEmailDomains != "" {
  732. allowedDomains = config.Config.Server.AllowedEmailDomains
  733. }
  734. return allowedDomains
  735. }
  736. // GetNmBaseDomain - fetches nm base domain
  737. func GetNmBaseDomain() string {
  738. return os.Getenv("NM_DOMAIN")
  739. }