serverconf.go 15 KB

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