serverconf.go 15 KB

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