2
0

serverconf.go 15 KB

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