serverconf.go 14 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567
  1. package servercfg
  2. import (
  3. "errors"
  4. "io"
  5. "net"
  6. "net/http"
  7. "os"
  8. "strconv"
  9. "strings"
  10. "github.com/gravitl/netmaker/config"
  11. )
  12. // SetHost - sets the host ip
  13. func SetHost() error {
  14. remoteip, err := GetPublicIP()
  15. if err != nil {
  16. return err
  17. }
  18. os.Setenv("SERVER_HOST", remoteip)
  19. return nil
  20. }
  21. // GetServerConfig - gets the server config into memory from file or env
  22. func GetServerConfig() config.ServerConfig {
  23. var cfg config.ServerConfig
  24. cfg.APIConnString = GetAPIConnString()
  25. cfg.CoreDNSAddr = GetCoreDNSAddr()
  26. cfg.APIHost = GetAPIHost()
  27. cfg.APIPort = GetAPIPort()
  28. cfg.GRPCConnString = GetGRPCConnString()
  29. cfg.GRPCHost = GetGRPCHost()
  30. cfg.GRPCPort = GetGRPCPort()
  31. cfg.MasterKey = "(hidden)"
  32. cfg.DNSKey = "(hidden)"
  33. cfg.AllowedOrigin = GetAllowedOrigin()
  34. cfg.RestBackend = "off"
  35. cfg.NodeID = GetNodeID()
  36. cfg.CheckinInterval = GetCheckinInterval()
  37. cfg.ServerCheckinInterval = GetServerCheckinInterval()
  38. if IsRestBackend() {
  39. cfg.RestBackend = "on"
  40. }
  41. cfg.AgentBackend = "off"
  42. if IsAgentBackend() {
  43. cfg.AgentBackend = "on"
  44. }
  45. cfg.ClientMode = "off"
  46. if IsClientMode() != "off" {
  47. cfg.ClientMode = IsClientMode()
  48. }
  49. cfg.DNSMode = "off"
  50. if IsDNSMode() {
  51. cfg.DNSMode = "on"
  52. }
  53. cfg.DisplayKeys = "off"
  54. if IsDisplayKeys() {
  55. cfg.DisplayKeys = "on"
  56. }
  57. cfg.GRPCSSL = "off"
  58. if IsGRPCSSL() {
  59. cfg.GRPCSSL = "on"
  60. }
  61. cfg.DisableRemoteIPCheck = "off"
  62. if DisableRemoteIPCheck() {
  63. cfg.DisableRemoteIPCheck = "on"
  64. }
  65. cfg.DisableDefaultNet = "off"
  66. if DisableDefaultNet() {
  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. if GetRce() {
  79. cfg.RCE = "on"
  80. } else {
  81. cfg.RCE = "off"
  82. }
  83. return cfg
  84. }
  85. // GetFrontendURL - gets the frontend url
  86. func GetFrontendURL() string {
  87. var frontend = ""
  88. if os.Getenv("FRONTEND_URL") != "" {
  89. frontend = os.Getenv("FRONTEND_URL")
  90. } else if config.Config.Server.FrontendURL != "" {
  91. frontend = config.Config.Server.FrontendURL
  92. }
  93. return frontend
  94. }
  95. // GetAPIConnString - gets the api connections string
  96. func GetAPIConnString() string {
  97. conn := ""
  98. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  99. conn = os.Getenv("SERVER_API_CONN_STRING")
  100. } else if config.Config.Server.APIConnString != "" {
  101. conn = config.Config.Server.APIConnString
  102. }
  103. return conn
  104. }
  105. // GetVersion - version of netmaker
  106. func GetVersion() string {
  107. version := "0.10.0"
  108. if config.Config.Server.Version != "" {
  109. version = config.Config.Server.Version
  110. }
  111. return version
  112. }
  113. // GetDB - gets the database type
  114. func GetDB() string {
  115. database := "sqlite"
  116. if os.Getenv("DATABASE") != "" {
  117. database = os.Getenv("DATABASE")
  118. } else if config.Config.Server.Database != "" {
  119. database = config.Config.Server.Database
  120. }
  121. return database
  122. }
  123. // GetAPIHost - gets the api host
  124. func GetAPIHost() string {
  125. serverhost := "127.0.0.1"
  126. remoteip, _ := GetPublicIP()
  127. if os.Getenv("SERVER_HTTP_HOST") != "" {
  128. serverhost = os.Getenv("SERVER_HTTP_HOST")
  129. } else if config.Config.Server.APIHost != "" {
  130. serverhost = config.Config.Server.APIHost
  131. } else if os.Getenv("SERVER_HOST") != "" {
  132. serverhost = os.Getenv("SERVER_HOST")
  133. } else {
  134. if remoteip != "" {
  135. serverhost = remoteip
  136. }
  137. }
  138. return serverhost
  139. }
  140. // GetPodIP - get the pod's ip
  141. func GetPodIP() string {
  142. podip := "127.0.0.1"
  143. if os.Getenv("POD_IP") != "" {
  144. podip = os.Getenv("POD_IP")
  145. }
  146. return podip
  147. }
  148. // GetAPIPort - gets the api port
  149. func GetAPIPort() string {
  150. apiport := "8081"
  151. if os.Getenv("API_PORT") != "" {
  152. apiport = os.Getenv("API_PORT")
  153. } else if config.Config.Server.APIPort != "" {
  154. apiport = config.Config.Server.APIPort
  155. }
  156. return apiport
  157. }
  158. // GetCheckinInterval - get check in interval for nodes
  159. func GetCheckinInterval() string {
  160. seconds := "15"
  161. if os.Getenv("CHECKIN_INTERVAL") != "" {
  162. seconds = os.Getenv("CHECKIN_INTERVAL")
  163. } else if config.Config.Server.CheckinInterval != "" {
  164. seconds = config.Config.Server.CheckinInterval
  165. }
  166. return seconds
  167. }
  168. // GetDefaultNodeLimit - get node limit if one is set
  169. func GetDefaultNodeLimit() int32 {
  170. var limit int32
  171. limit = 999999999
  172. envlimit, err := strconv.Atoi(os.Getenv("DEFAULT_NODE_LIMIT"))
  173. if err == nil && envlimit != 0 {
  174. limit = int32(envlimit)
  175. } else if config.Config.Server.DefaultNodeLimit != 0 {
  176. limit = config.Config.Server.DefaultNodeLimit
  177. }
  178. return limit
  179. }
  180. // GetGRPCConnString - get grpc conn string
  181. func GetGRPCConnString() string {
  182. conn := ""
  183. if os.Getenv("SERVER_GRPC_CONN_STRING") != "" {
  184. conn = os.Getenv("SERVER_GRPC_CONN_STRING")
  185. } else if config.Config.Server.GRPCConnString != "" {
  186. conn = config.Config.Server.GRPCConnString
  187. }
  188. return conn
  189. }
  190. // GetCoreDNSAddr - gets the core dns address
  191. func GetCoreDNSAddr() string {
  192. addr, _ := GetPublicIP()
  193. if os.Getenv("COREDNS_ADDR") != "" {
  194. addr = os.Getenv("COREDNS_ADDR")
  195. } else if config.Config.Server.CoreDNSAddr != "" {
  196. addr = config.Config.Server.GRPCConnString
  197. }
  198. return addr
  199. }
  200. // GetGRPCHost - get the grpc host url
  201. func GetGRPCHost() string {
  202. serverhost := "127.0.0.1"
  203. remoteip, _ := GetPublicIP()
  204. if os.Getenv("SERVER_GRPC_HOST") != "" {
  205. serverhost = os.Getenv("SERVER_GRPC_HOST")
  206. } else if config.Config.Server.GRPCHost != "" {
  207. serverhost = config.Config.Server.GRPCHost
  208. } else if os.Getenv("SERVER_HOST") != "" {
  209. serverhost = os.Getenv("SERVER_HOST")
  210. } else {
  211. if remoteip != "" {
  212. serverhost = remoteip
  213. }
  214. }
  215. return serverhost
  216. }
  217. // GetGRPCPort - gets the grpc port
  218. func GetGRPCPort() string {
  219. grpcport := "50051"
  220. if os.Getenv("GRPC_PORT") != "" {
  221. grpcport = os.Getenv("GRPC_PORT")
  222. } else if config.Config.Server.GRPCPort != "" {
  223. grpcport = config.Config.Server.GRPCPort
  224. }
  225. return grpcport
  226. }
  227. // GetMessageQueueEndpoint - gets the message queue endpoint
  228. func GetMessageQueueEndpoint() string {
  229. host, _ := GetPublicIP()
  230. if os.Getenv("MQ_HOST") != "" {
  231. host = os.Getenv("MQ_HOST")
  232. } else if config.Config.Server.MQHOST != "" {
  233. host = config.Config.Server.MQHOST
  234. }
  235. //Do we want MQ port configurable???
  236. return host + ":1883"
  237. }
  238. // GetMasterKey - gets the configured master key of server
  239. func GetMasterKey() string {
  240. key := "secretkey"
  241. if os.Getenv("MASTER_KEY") != "" {
  242. key = os.Getenv("MASTER_KEY")
  243. } else if config.Config.Server.MasterKey != "" {
  244. key = config.Config.Server.MasterKey
  245. }
  246. return key
  247. }
  248. // GetDNSKey - gets the configured dns key of server
  249. func GetDNSKey() string {
  250. key := "secretkey"
  251. if os.Getenv("DNS_KEY") != "" {
  252. key = os.Getenv("DNS_KEY")
  253. } else if config.Config.Server.DNSKey != "" {
  254. key = config.Config.Server.DNSKey
  255. }
  256. return key
  257. }
  258. // GetAllowedOrigin - get the allowed origin
  259. func GetAllowedOrigin() string {
  260. allowedorigin := "*"
  261. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  262. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  263. } else if config.Config.Server.AllowedOrigin != "" {
  264. allowedorigin = config.Config.Server.AllowedOrigin
  265. }
  266. return allowedorigin
  267. }
  268. // IsRestBackend - checks if rest is on or off
  269. func IsRestBackend() bool {
  270. isrest := true
  271. if os.Getenv("REST_BACKEND") != "" {
  272. if os.Getenv("REST_BACKEND") == "off" {
  273. isrest = false
  274. }
  275. } else if config.Config.Server.RestBackend != "" {
  276. if config.Config.Server.RestBackend == "off" {
  277. isrest = false
  278. }
  279. }
  280. return isrest
  281. }
  282. // IsAgentBackend - checks if agent backed is on or off
  283. func IsAgentBackend() bool {
  284. isagent := true
  285. if os.Getenv("AGENT_BACKEND") != "" {
  286. if os.Getenv("AGENT_BACKEND") == "off" {
  287. isagent = false
  288. }
  289. } else if config.Config.Server.AgentBackend != "" {
  290. if config.Config.Server.AgentBackend == "off" {
  291. isagent = false
  292. }
  293. }
  294. return isagent
  295. }
  296. // IsMessageQueueBackend - checks if message queue is on or off
  297. func IsMessageQueueBackend() bool {
  298. ismessagequeue := true
  299. if os.Getenv("MESSAGEQUEUE_BACKEND") != "" {
  300. if os.Getenv("MESSAGEQUEUE_BACKEND") == "off" {
  301. ismessagequeue = false
  302. }
  303. } else if config.Config.Server.MessageQueueBackend != "" {
  304. if config.Config.Server.MessageQueueBackend == "off" {
  305. ismessagequeue = false
  306. }
  307. }
  308. return ismessagequeue
  309. }
  310. // IsClientMode - checks if it should run in client mode
  311. func IsClientMode() string {
  312. isclient := "on"
  313. if os.Getenv("CLIENT_MODE") != "" {
  314. if os.Getenv("CLIENT_MODE") == "off" {
  315. isclient = "off"
  316. }
  317. if os.Getenv("CLIENT_MODE") == "contained" {
  318. isclient = "contained"
  319. }
  320. } else if config.Config.Server.ClientMode != "" {
  321. if config.Config.Server.ClientMode == "off" {
  322. isclient = "off"
  323. }
  324. if config.Config.Server.ClientMode == "contained" {
  325. isclient = "contained"
  326. }
  327. }
  328. return isclient
  329. }
  330. // IsDNSMode - should it run with DNS
  331. func IsDNSMode() bool {
  332. isdns := true
  333. if os.Getenv("DNS_MODE") != "" {
  334. if os.Getenv("DNS_MODE") == "off" {
  335. isdns = false
  336. }
  337. } else if config.Config.Server.DNSMode != "" {
  338. if config.Config.Server.DNSMode == "off" {
  339. isdns = false
  340. }
  341. }
  342. return isdns
  343. }
  344. // IsDisplayKeys - should server be able to display keys?
  345. func IsDisplayKeys() bool {
  346. isdisplay := true
  347. if os.Getenv("DISPLAY_KEYS") != "" {
  348. if os.Getenv("DISPLAY_KEYS") == "off" {
  349. isdisplay = false
  350. }
  351. } else if config.Config.Server.DisplayKeys != "" {
  352. if config.Config.Server.DisplayKeys == "off" {
  353. isdisplay = false
  354. }
  355. }
  356. return isdisplay
  357. }
  358. // IsGRPCSSL - ssl grpc on or off
  359. func IsGRPCSSL() bool {
  360. isssl := false
  361. if os.Getenv("GRPC_SSL") != "" {
  362. if os.Getenv("GRPC_SSL") == "on" {
  363. isssl = true
  364. }
  365. } else if config.Config.Server.DNSMode != "" {
  366. if config.Config.Server.DNSMode == "on" {
  367. isssl = true
  368. }
  369. }
  370. return isssl
  371. }
  372. // DisableRemoteIPCheck - disable the remote ip check
  373. func DisableRemoteIPCheck() bool {
  374. disabled := false
  375. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  376. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  377. disabled = true
  378. }
  379. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  380. if config.Config.Server.DisableRemoteIPCheck == "on" {
  381. disabled = true
  382. }
  383. }
  384. return disabled
  385. }
  386. // DisableDefaultNet - disable default net
  387. func DisableDefaultNet() bool {
  388. disabled := false
  389. if os.Getenv("DISABLE_DEFAULT_NET") != "" {
  390. if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
  391. disabled = true
  392. }
  393. } else if config.Config.Server.DisableDefaultNet != "" {
  394. if config.Config.Server.DisableDefaultNet == "on" {
  395. disabled = true
  396. }
  397. }
  398. return disabled
  399. }
  400. // GetPublicIP - gets public ip
  401. func GetPublicIP() (string, error) {
  402. endpoint := ""
  403. var err error
  404. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  405. for _, ipserver := range iplist {
  406. resp, err := http.Get(ipserver)
  407. if err != nil {
  408. continue
  409. }
  410. defer resp.Body.Close()
  411. if resp.StatusCode == http.StatusOK {
  412. bodyBytes, err := io.ReadAll(resp.Body)
  413. if err != nil {
  414. continue
  415. }
  416. endpoint = string(bodyBytes)
  417. break
  418. }
  419. }
  420. if err == nil && endpoint == "" {
  421. err = errors.New("public address not found")
  422. }
  423. return endpoint, err
  424. }
  425. // GetPlatform - get the system type of server
  426. func GetPlatform() string {
  427. platform := "linux"
  428. if os.Getenv("PLATFORM") != "" {
  429. platform = os.Getenv("PLATFORM")
  430. } else if config.Config.Server.Platform != "" {
  431. platform = config.Config.Server.SQLConn
  432. }
  433. return platform
  434. }
  435. // GetSQLConn - get the sql connection string
  436. func GetSQLConn() string {
  437. sqlconn := "http://"
  438. if os.Getenv("SQL_CONN") != "" {
  439. sqlconn = os.Getenv("SQL_CONN")
  440. } else if config.Config.Server.SQLConn != "" {
  441. sqlconn = config.Config.Server.SQLConn
  442. }
  443. return sqlconn
  444. }
  445. // IsSplitDNS - checks if split dns is on
  446. func IsSplitDNS() bool {
  447. issplit := false
  448. if os.Getenv("IS_SPLIT_DNS") == "yes" {
  449. issplit = true
  450. } else if config.Config.Server.SplitDNS == "yes" {
  451. issplit = true
  452. }
  453. return issplit
  454. }
  455. // GetNodeID - gets the node id
  456. func GetNodeID() string {
  457. var id string
  458. id = getMacAddr()
  459. if os.Getenv("NODE_ID") != "" {
  460. id = os.Getenv("NODE_ID")
  461. } else if config.Config.Server.NodeID != "" {
  462. id = config.Config.Server.NodeID
  463. }
  464. return id
  465. }
  466. // GetServerCheckinInterval - gets the server check-in time
  467. func GetServerCheckinInterval() int64 {
  468. var t = int64(5)
  469. var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
  470. if envt > 0 {
  471. t = int64(envt)
  472. } else if config.Config.Server.ServerCheckinInterval > 0 {
  473. t = config.Config.Server.ServerCheckinInterval
  474. }
  475. return t
  476. }
  477. // GetAuthProviderInfo = gets the oauth provider info
  478. func GetAuthProviderInfo() []string {
  479. var authProvider = ""
  480. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  481. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  482. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  483. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  484. } else {
  485. authProvider = ""
  486. }
  487. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  488. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  489. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  490. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  491. }
  492. }
  493. return []string{"", "", ""}
  494. }
  495. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  496. func GetAzureTenant() string {
  497. var azureTenant = ""
  498. if os.Getenv("AZURE_TENANT") != "" {
  499. azureTenant = os.Getenv("AZURE_TENANT")
  500. } else if config.Config.Server.AzureTenant != "" {
  501. azureTenant = config.Config.Server.AzureTenant
  502. }
  503. return azureTenant
  504. }
  505. // GetMacAddr - get's mac address
  506. func getMacAddr() string {
  507. ifas, err := net.Interfaces()
  508. if err != nil {
  509. return ""
  510. }
  511. var as []string
  512. for _, ifa := range ifas {
  513. a := ifa.HardwareAddr.String()
  514. if a != "" {
  515. as = append(as, a)
  516. }
  517. }
  518. return as[0]
  519. }
  520. // GetRce - sees if Rce is enabled, off by default
  521. func GetRce() bool {
  522. return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
  523. }