serverconf.go 15 KB

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