2
0

serverconf.go 22 KB

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