serverconf.go 15 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540541542543544545546547548549550551552553554555556557558559560561562563564565566567568569570571572573574575576577578579580581582583584585586587588589590591592593594595596597598599600601602603604605606607608609610611612613614
  1. package servercfg
  2. import (
  3. "errors"
  4. "io"
  5. "net/http"
  6. "os"
  7. "strconv"
  8. "strings"
  9. "github.com/gravitl/netmaker/config"
  10. )
  11. var Version = "dev"
  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. // SetVersion - set version of netmaker
  111. func SetVersion(v string) {
  112. Version = v
  113. }
  114. // GetVersion - version of netmaker
  115. func GetVersion() string {
  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. } else {
  193. conn = GetGRPCHost() + ":" + GetGRPCPort()
  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.GRPCSSL != "" {
  386. if config.Config.Server.GRPCSSL == "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. var err error
  501. // id = getMacAddr()
  502. if os.Getenv("NODE_ID") != "" {
  503. id = os.Getenv("NODE_ID")
  504. } else if config.Config.Server.NodeID != "" {
  505. id = config.Config.Server.NodeID
  506. } else {
  507. id, err = os.Hostname()
  508. if err != nil {
  509. return ""
  510. }
  511. }
  512. return id
  513. }
  514. func SetNodeID(id string) {
  515. config.Config.Server.NodeID = id
  516. }
  517. // GetServerCheckinInterval - gets the server check-in time
  518. func GetServerCheckinInterval() int64 {
  519. var t = int64(5)
  520. var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
  521. if envt > 0 {
  522. t = int64(envt)
  523. } else if config.Config.Server.ServerCheckinInterval > 0 {
  524. t = config.Config.Server.ServerCheckinInterval
  525. }
  526. return t
  527. }
  528. // GetAuthProviderInfo = gets the oauth provider info
  529. func GetAuthProviderInfo() []string {
  530. var authProvider = ""
  531. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  532. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  533. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  534. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  535. } else {
  536. authProvider = ""
  537. }
  538. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  539. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  540. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  541. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  542. }
  543. }
  544. return []string{"", "", ""}
  545. }
  546. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  547. func GetAzureTenant() string {
  548. var azureTenant = ""
  549. if os.Getenv("AZURE_TENANT") != "" {
  550. azureTenant = os.Getenv("AZURE_TENANT")
  551. } else if config.Config.Server.AzureTenant != "" {
  552. azureTenant = config.Config.Server.AzureTenant
  553. }
  554. return azureTenant
  555. }
  556. // GetRce - sees if Rce is enabled, off by default
  557. func GetRce() bool {
  558. return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
  559. }
  560. // GetDebug -- checks if debugging is enabled, off by default
  561. func GetDebug() bool {
  562. return os.Getenv("DEBUG") == "on" || config.Config.Server.Debug == true
  563. }