serverconf.go 15 KB

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