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