serverconf.go 22 KB

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