serverconf.go 15 KB

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