serverconf.go 19 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614615616617618619620621622623624625626627628629630631632633634635636637638639640641642643644645646647648649650651652653654655656657658659660661662663664665666667668669670671672673674675676677678679680681682683684685686687688689690691692693694695696697698699700701702703704705706707708709710711712713714715716717718719720721722723724725726727728729730731732733734735736
  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. // GetOwnerEmail - gets the owner email (saas)
  223. func GetOwnerEmail() string {
  224. return os.Getenv("SAAS_OWNER_EMAIL")
  225. }
  226. // GetMessageQueueEndpoint - gets the message queue endpoint
  227. func GetMessageQueueEndpoint() (string, bool) {
  228. host, _ := GetPublicIP()
  229. if os.Getenv("SERVER_BROKER_ENDPOINT") != "" {
  230. host = os.Getenv("SERVER_BROKER_ENDPOINT")
  231. } else if config.Config.Server.ServerBrokerEndpoint != "" {
  232. host = config.Config.Server.ServerBrokerEndpoint
  233. } else if os.Getenv("BROKER_ENDPOINT") != "" {
  234. host = os.Getenv("BROKER_ENDPOINT")
  235. } else if config.Config.Server.Broker != "" {
  236. host = config.Config.Server.Broker
  237. } else {
  238. host += ":1883" // default
  239. }
  240. return host, strings.Contains(host, "wss") || strings.Contains(host, "ssl") || strings.Contains(host, "mqtts")
  241. }
  242. // GetBrokerType - returns the type of MQ broker
  243. func GetBrokerType() string {
  244. if os.Getenv("BROKER_TYPE") != "" {
  245. return os.Getenv("BROKER_TYPE")
  246. } else {
  247. return "mosquitto"
  248. }
  249. }
  250. // GetMasterKey - gets the configured master key of server
  251. func GetMasterKey() string {
  252. key := ""
  253. if os.Getenv("MASTER_KEY") != "" {
  254. key = os.Getenv("MASTER_KEY")
  255. } else if config.Config.Server.MasterKey != "" {
  256. key = config.Config.Server.MasterKey
  257. }
  258. return key
  259. }
  260. // GetAllowedOrigin - get the allowed origin
  261. func GetAllowedOrigin() string {
  262. allowedorigin := "*"
  263. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  264. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  265. } else if config.Config.Server.AllowedOrigin != "" {
  266. allowedorigin = config.Config.Server.AllowedOrigin
  267. }
  268. return allowedorigin
  269. }
  270. // IsRestBackend - checks if rest is on or off
  271. func IsRestBackend() bool {
  272. isrest := true
  273. if os.Getenv("REST_BACKEND") != "" {
  274. if os.Getenv("REST_BACKEND") == "off" {
  275. isrest = false
  276. }
  277. } else if config.Config.Server.RestBackend != "" {
  278. if config.Config.Server.RestBackend == "off" {
  279. isrest = false
  280. }
  281. }
  282. return isrest
  283. }
  284. // IsMetricsExporter - checks if metrics exporter is on or off
  285. func IsMetricsExporter() bool {
  286. export := false
  287. if os.Getenv("METRICS_EXPORTER") != "" {
  288. if os.Getenv("METRICS_EXPORTER") == "on" {
  289. export = true
  290. }
  291. } else if config.Config.Server.MetricsExporter != "" {
  292. if config.Config.Server.MetricsExporter == "on" {
  293. export = true
  294. }
  295. }
  296. return export
  297. }
  298. // IsMessageQueueBackend - checks if message queue is on or off
  299. func IsMessageQueueBackend() bool {
  300. ismessagequeue := true
  301. if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
  302. if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
  303. ismessagequeue = false
  304. }
  305. } else if config.Config.Server.MessageQueueBackend != "" {
  306. if config.Config.Server.MessageQueueBackend == "off" {
  307. ismessagequeue = false
  308. }
  309. }
  310. return ismessagequeue
  311. }
  312. // Telemetry - checks if telemetry data should be sent
  313. func Telemetry() string {
  314. telemetry := "on"
  315. if os.Getenv("TELEMETRY") == "off" {
  316. telemetry = "off"
  317. }
  318. if config.Config.Server.Telemetry == "off" {
  319. telemetry = "off"
  320. }
  321. return telemetry
  322. }
  323. // GetServer - gets the server name
  324. func GetServer() string {
  325. server := ""
  326. if os.Getenv("SERVER_NAME") != "" {
  327. server = os.Getenv("SERVER_NAME")
  328. } else if config.Config.Server.Server != "" {
  329. server = config.Config.Server.Server
  330. }
  331. return server
  332. }
  333. func GetVerbosity() int32 {
  334. var verbosity = 0
  335. var err error
  336. if os.Getenv("VERBOSITY") != "" {
  337. verbosity, err = strconv.Atoi(os.Getenv("VERBOSITY"))
  338. if err != nil {
  339. verbosity = 0
  340. }
  341. } else if config.Config.Server.Verbosity != 0 {
  342. verbosity = int(config.Config.Server.Verbosity)
  343. }
  344. if verbosity < 0 || verbosity > 4 {
  345. verbosity = 0
  346. }
  347. return int32(verbosity)
  348. }
  349. // AutoUpdateEnabled returns a boolean indicating whether netclient auto update is enabled or disabled
  350. // default is enabled
  351. func AutoUpdateEnabled() bool {
  352. if os.Getenv("NETCLIENT_AUTO_UPDATE") == "disabled" {
  353. return false
  354. } else if config.Config.Server.NetclientAutoUpdate == "disabled" {
  355. return false
  356. }
  357. return true
  358. }
  359. // IsDNSMode - should it run with DNS
  360. func IsDNSMode() bool {
  361. isdns := true
  362. if os.Getenv("DNS_MODE") != "" {
  363. if os.Getenv("DNS_MODE") == "off" {
  364. isdns = false
  365. }
  366. } else if config.Config.Server.DNSMode != "" {
  367. if config.Config.Server.DNSMode == "off" {
  368. isdns = false
  369. }
  370. }
  371. return isdns
  372. }
  373. // IsDisplayKeys - should server be able to display keys?
  374. func IsDisplayKeys() bool {
  375. isdisplay := true
  376. if os.Getenv("DISPLAY_KEYS") != "" {
  377. if os.Getenv("DISPLAY_KEYS") == "off" {
  378. isdisplay = false
  379. }
  380. } else if config.Config.Server.DisplayKeys != "" {
  381. if config.Config.Server.DisplayKeys == "off" {
  382. isdisplay = false
  383. }
  384. }
  385. return isdisplay
  386. }
  387. // DisableRemoteIPCheck - disable the remote ip check
  388. func DisableRemoteIPCheck() bool {
  389. disabled := false
  390. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  391. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  392. disabled = true
  393. }
  394. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  395. if config.Config.Server.DisableRemoteIPCheck == "on" {
  396. disabled = true
  397. }
  398. }
  399. return disabled
  400. }
  401. // GetPublicIP - gets public ip
  402. func GetPublicIP() (string, error) {
  403. endpoint := ""
  404. var err error
  405. iplist := []string{"https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  406. publicIpService := os.Getenv("PUBLIC_IP_SERVICE")
  407. if publicIpService != "" {
  408. // prepend the user-specified service so it's checked first
  409. iplist = append([]string{publicIpService}, iplist...)
  410. } else if config.Config.Server.PublicIPService != "" {
  411. publicIpService = config.Config.Server.PublicIPService
  412. // prepend the user-specified service so it's checked first
  413. iplist = append([]string{publicIpService}, iplist...)
  414. }
  415. for _, ipserver := range iplist {
  416. client := &http.Client{
  417. Timeout: time.Second * 10,
  418. }
  419. resp, err := client.Get(ipserver)
  420. if err != nil {
  421. continue
  422. }
  423. defer resp.Body.Close()
  424. if resp.StatusCode == http.StatusOK {
  425. bodyBytes, err := io.ReadAll(resp.Body)
  426. if err != nil {
  427. continue
  428. }
  429. endpoint = string(bodyBytes)
  430. break
  431. }
  432. }
  433. if err == nil && endpoint == "" {
  434. err = errors.New("public address not found")
  435. }
  436. return endpoint, err
  437. }
  438. // GetPlatform - get the system type of server
  439. func GetPlatform() string {
  440. platform := "linux"
  441. if os.Getenv("PLATFORM") != "" {
  442. platform = os.Getenv("PLATFORM")
  443. } else if config.Config.Server.Platform != "" {
  444. platform = config.Config.Server.Platform
  445. }
  446. return platform
  447. }
  448. // GetSQLConn - get the sql connection string
  449. func GetSQLConn() string {
  450. sqlconn := "http://"
  451. if os.Getenv("SQL_CONN") != "" {
  452. sqlconn = os.Getenv("SQL_CONN")
  453. } else if config.Config.Server.SQLConn != "" {
  454. sqlconn = config.Config.Server.SQLConn
  455. }
  456. return sqlconn
  457. }
  458. // GetNodeID - gets the node id
  459. func GetNodeID() string {
  460. var id string
  461. var err error
  462. // id = getMacAddr()
  463. if os.Getenv("NODE_ID") != "" {
  464. id = os.Getenv("NODE_ID")
  465. } else if config.Config.Server.NodeID != "" {
  466. id = config.Config.Server.NodeID
  467. } else {
  468. id, err = os.Hostname()
  469. if err != nil {
  470. return ""
  471. }
  472. }
  473. return id
  474. }
  475. func SetNodeID(id string) {
  476. config.Config.Server.NodeID = id
  477. }
  478. // GetAuthProviderInfo = gets the oauth provider info
  479. func GetAuthProviderInfo() (pi []string) {
  480. var authProvider = ""
  481. defer func() {
  482. if authProvider == "oidc" {
  483. if os.Getenv("OIDC_ISSUER") != "" {
  484. pi = append(pi, os.Getenv("OIDC_ISSUER"))
  485. } else if config.Config.Server.OIDCIssuer != "" {
  486. pi = append(pi, config.Config.Server.OIDCIssuer)
  487. } else {
  488. pi = []string{"", "", ""}
  489. }
  490. }
  491. }()
  492. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  493. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  494. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  495. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  496. } else {
  497. authProvider = ""
  498. }
  499. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  500. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  501. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" || authProvider == "oidc" {
  502. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  503. }
  504. }
  505. return []string{"", "", ""}
  506. }
  507. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  508. func GetAzureTenant() string {
  509. var azureTenant = ""
  510. if os.Getenv("AZURE_TENANT") != "" {
  511. azureTenant = os.Getenv("AZURE_TENANT")
  512. } else if config.Config.Server.AzureTenant != "" {
  513. azureTenant = config.Config.Server.AzureTenant
  514. }
  515. return azureTenant
  516. }
  517. // GetMqPassword - fetches the MQ password
  518. func GetMqPassword() string {
  519. password := ""
  520. if os.Getenv("MQ_PASSWORD") != "" {
  521. password = os.Getenv("MQ_PASSWORD")
  522. } else if config.Config.Server.MQPassword != "" {
  523. password = config.Config.Server.MQPassword
  524. }
  525. return password
  526. }
  527. // GetMqUserName - fetches the MQ username
  528. func GetMqUserName() string {
  529. password := ""
  530. if os.Getenv("MQ_USERNAME") != "" {
  531. password = os.Getenv("MQ_USERNAME")
  532. } else if config.Config.Server.MQUserName != "" {
  533. password = config.Config.Server.MQUserName
  534. }
  535. return password
  536. }
  537. // GetMetricInterval - get the publish metric interval
  538. func GetMetricInterval() string {
  539. //default 15 minutes
  540. mi := "15"
  541. if os.Getenv("PUBLISH_METRIC_INTERVAL") != "" {
  542. mi = os.Getenv("PUBLISH_METRIC_INTERVAL")
  543. }
  544. return mi
  545. }
  546. // GetEmqxRestEndpoint - returns the REST API Endpoint of EMQX
  547. func GetEmqxRestEndpoint() string {
  548. return os.Getenv("EMQX_REST_ENDPOINT")
  549. }
  550. // IsBasicAuthEnabled - checks if basic auth has been configured to be turned off
  551. func IsBasicAuthEnabled() bool {
  552. var enabled = true //default
  553. if os.Getenv("BASIC_AUTH") != "" {
  554. enabled = os.Getenv("BASIC_AUTH") == "yes"
  555. } else if config.Config.Server.BasicAuth != "" {
  556. enabled = config.Config.Server.BasicAuth == "yes"
  557. }
  558. return enabled
  559. }
  560. // GetLicenseKey - retrieves pro license value from env or conf files
  561. func GetLicenseKey() string {
  562. licenseKeyValue := os.Getenv("LICENSE_KEY")
  563. if licenseKeyValue == "" {
  564. licenseKeyValue = config.Config.Server.LicenseValue
  565. }
  566. return licenseKeyValue
  567. }
  568. // GetNetmakerTenantID - get's the associated, Netmaker, tenant ID to verify ownership
  569. func GetNetmakerTenantID() string {
  570. netmakerTenantID := os.Getenv("NETMAKER_TENANT_ID")
  571. if netmakerTenantID == "" {
  572. netmakerTenantID = config.Config.Server.NetmakerTenantID
  573. }
  574. return netmakerTenantID
  575. }
  576. // GetUserLimit - fetches free tier limits on users
  577. func GetUserLimit() int {
  578. var userslimit int
  579. if os.Getenv("USERS_LIMIT") != "" {
  580. userslimit, _ = strconv.Atoi(os.Getenv("USERS_LIMIT"))
  581. } else {
  582. userslimit = config.Config.Server.UsersLimit
  583. }
  584. return userslimit
  585. }
  586. // GetNetworkLimit - fetches free tier limits on networks
  587. func GetNetworkLimit() int {
  588. var networkslimit int
  589. if os.Getenv("NETWORKS_LIMIT") != "" {
  590. networkslimit, _ = strconv.Atoi(os.Getenv("NETWORKS_LIMIT"))
  591. } else {
  592. networkslimit = config.Config.Server.NetworksLimit
  593. }
  594. return networkslimit
  595. }
  596. // GetMachinesLimit - fetches free tier limits on machines (clients + hosts)
  597. func GetMachinesLimit() int {
  598. if l, err := strconv.Atoi(os.Getenv("MACHINES_LIMIT")); err == nil {
  599. return l
  600. }
  601. return config.Config.Server.MachinesLimit
  602. }
  603. // GetIngressLimit - fetches free tier limits on ingresses
  604. func GetIngressLimit() int {
  605. if l, err := strconv.Atoi(os.Getenv("INGRESSES_LIMIT")); err == nil {
  606. return l
  607. }
  608. return config.Config.Server.IngressesLimit
  609. }
  610. // GetEgressLimit - fetches free tier limits on egresses
  611. func GetEgressLimit() int {
  612. if l, err := strconv.Atoi(os.Getenv("EGRESSES_LIMIT")); err == nil {
  613. return l
  614. }
  615. return config.Config.Server.EgressesLimit
  616. }
  617. // DeployedByOperator - returns true if the instance is deployed by netmaker operator
  618. func DeployedByOperator() bool {
  619. if os.Getenv("DEPLOYED_BY_OPERATOR") != "" {
  620. return os.Getenv("DEPLOYED_BY_OPERATOR") == "true"
  621. }
  622. return config.Config.Server.DeployedByOperator
  623. }
  624. // IsEndpointDetectionEnabled - returns true if endpoint detection enabled
  625. func IsEndpointDetectionEnabled() bool {
  626. var enabled = true //default
  627. if os.Getenv("ENDPOINT_DETECTION") != "" {
  628. enabled = os.Getenv("ENDPOINT_DETECTION") == "true"
  629. }
  630. return enabled
  631. }
  632. // GetEnvironment returns the environment the server is running in (e.g. dev, staging, prod...)
  633. func GetEnvironment() string {
  634. if env := os.Getenv("ENVIRONMENT"); env != "" {
  635. return env
  636. }
  637. if env := config.Config.Server.Environment; env != "" {
  638. return env
  639. }
  640. return ""
  641. }
  642. // GetEmqxDeployType - fetches emqx deploy type this server uses
  643. func GetEmqxDeployType() (deployType Emqxdeploy) {
  644. deployType = EmqxOnPremDeploy
  645. if os.Getenv("EMQX_DEPLOY_TYPE") == string(EmqxCloudDeploy) {
  646. deployType = EmqxCloudDeploy
  647. }
  648. return
  649. }
  650. // GetEmqxAppID - gets the emqx cloud app id
  651. func GetEmqxAppID() string {
  652. return os.Getenv("EMQX_APP_ID")
  653. }
  654. // GetEmqxAppSecret - gets the emqx cloud app secret
  655. func GetEmqxAppSecret() string {
  656. return os.Getenv("EMQX_APP_SECRET")
  657. }
  658. // GetAllowedEmailDomains - gets the allowed email domains for oauth signup
  659. func GetAllowedEmailDomains() string {
  660. allowedDomains := "*"
  661. if os.Getenv("ALLOWED_EMAIL_DOMAINS") != "" {
  662. allowedDomains = os.Getenv("ALLOWED_EMAIL_DOMAINS")
  663. } else if config.Config.Server.AllowedEmailDomains != "" {
  664. allowedDomains = config.Config.Server.AllowedEmailDomains
  665. }
  666. return allowedDomains
  667. }