serverconf.go 22 KB

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