serverconf.go 20 KB

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