serverconf.go 22 KB

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