serverconf.go 18 KB

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