serverconf.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531532533534535536537538539540
  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. // 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. return cfg
  84. }
  85. // GetFrontendURL - gets the frontend url
  86. func GetFrontendURL() string {
  87. var frontend = ""
  88. if os.Getenv("FRONTEND_URL") != "" {
  89. frontend = os.Getenv("FRONTEND_URL")
  90. } else if config.Config.Server.FrontendURL != "" {
  91. frontend = config.Config.Server.FrontendURL
  92. }
  93. return frontend
  94. }
  95. // GetAPIConnString - gets the api connections string
  96. func GetAPIConnString() string {
  97. conn := ""
  98. if os.Getenv("SERVER_API_CONN_STRING") != "" {
  99. conn = os.Getenv("SERVER_API_CONN_STRING")
  100. } else if config.Config.Server.APIConnString != "" {
  101. conn = config.Config.Server.APIConnString
  102. }
  103. return conn
  104. }
  105. // GetVersion - version of netmaker
  106. func GetVersion() string {
  107. version := "0.9.4"
  108. if config.Config.Server.Version != "" {
  109. version = config.Config.Server.Version
  110. }
  111. return version
  112. }
  113. // GetDB - gets the database type
  114. func GetDB() string {
  115. database := "sqlite"
  116. if os.Getenv("DATABASE") != "" {
  117. database = os.Getenv("DATABASE")
  118. } else if config.Config.Server.Database != "" {
  119. database = config.Config.Server.Database
  120. }
  121. return database
  122. }
  123. // GetAPIHost - gets the api host
  124. func GetAPIHost() string {
  125. serverhost := "127.0.0.1"
  126. remoteip, _ := GetPublicIP()
  127. if os.Getenv("SERVER_HTTP_HOST") != "" {
  128. serverhost = os.Getenv("SERVER_HTTP_HOST")
  129. } else if config.Config.Server.APIHost != "" {
  130. serverhost = config.Config.Server.APIHost
  131. } else if os.Getenv("SERVER_HOST") != "" {
  132. serverhost = os.Getenv("SERVER_HOST")
  133. } else {
  134. if remoteip != "" {
  135. serverhost = remoteip
  136. }
  137. }
  138. return serverhost
  139. }
  140. // GetPodIP - get the pod's ip
  141. func GetPodIP() string {
  142. podip := "127.0.0.1"
  143. if os.Getenv("POD_IP") != "" {
  144. podip = os.Getenv("POD_IP")
  145. }
  146. return podip
  147. }
  148. // GetAPIPort - gets the api port
  149. func GetAPIPort() string {
  150. apiport := "8081"
  151. if os.Getenv("API_PORT") != "" {
  152. apiport = os.Getenv("API_PORT")
  153. } else if config.Config.Server.APIPort != "" {
  154. apiport = config.Config.Server.APIPort
  155. }
  156. return apiport
  157. }
  158. // GetCheckinInterval - get check in interval for nodes
  159. func GetCheckinInterval() string {
  160. seconds := "15"
  161. if os.Getenv("CHECKIN_INTERVAL") != "" {
  162. seconds = os.Getenv("CHECKIN_INTERVAL")
  163. } else if config.Config.Server.CheckinInterval != "" {
  164. seconds = config.Config.Server.CheckinInterval
  165. }
  166. return seconds
  167. }
  168. // GetDefaultNodeLimit - get node limit if one is set
  169. func GetDefaultNodeLimit() int32 {
  170. var limit int32
  171. limit = 999999999
  172. envlimit, err := strconv.Atoi(os.Getenv("DEFAULT_NODE_LIMIT"))
  173. if err == nil && envlimit != 0 {
  174. limit = int32(envlimit)
  175. } else if config.Config.Server.DefaultNodeLimit != 0 {
  176. limit = config.Config.Server.DefaultNodeLimit
  177. }
  178. return limit
  179. }
  180. // GetGRPCConnString - get grpc conn string
  181. func GetGRPCConnString() string {
  182. conn := ""
  183. if os.Getenv("SERVER_GRPC_CONN_STRING") != "" {
  184. conn = os.Getenv("SERVER_GRPC_CONN_STRING")
  185. } else if config.Config.Server.GRPCConnString != "" {
  186. conn = config.Config.Server.GRPCConnString
  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. // GetMasterKey - gets the configured master key of server
  228. func GetMasterKey() string {
  229. key := "secretkey"
  230. if os.Getenv("MASTER_KEY") != "" {
  231. key = os.Getenv("MASTER_KEY")
  232. } else if config.Config.Server.MasterKey != "" {
  233. key = config.Config.Server.MasterKey
  234. }
  235. return key
  236. }
  237. // GetDNSKey - gets the configured dns key of server
  238. func GetDNSKey() string {
  239. key := "secretkey"
  240. if os.Getenv("DNS_KEY") != "" {
  241. key = os.Getenv("DNS_KEY")
  242. } else if config.Config.Server.DNSKey != "" {
  243. key = config.Config.Server.DNSKey
  244. }
  245. return key
  246. }
  247. // GetAllowedOrigin - get the allowed origin
  248. func GetAllowedOrigin() string {
  249. allowedorigin := "*"
  250. if os.Getenv("CORS_ALLOWED_ORIGIN") != "" {
  251. allowedorigin = os.Getenv("CORS_ALLOWED_ORIGIN")
  252. } else if config.Config.Server.AllowedOrigin != "" {
  253. allowedorigin = config.Config.Server.AllowedOrigin
  254. }
  255. return allowedorigin
  256. }
  257. // IsRestBackend - checks if rest is on or off
  258. func IsRestBackend() bool {
  259. isrest := true
  260. if os.Getenv("REST_BACKEND") != "" {
  261. if os.Getenv("REST_BACKEND") == "off" {
  262. isrest = false
  263. }
  264. } else if config.Config.Server.RestBackend != "" {
  265. if config.Config.Server.RestBackend == "off" {
  266. isrest = false
  267. }
  268. }
  269. return isrest
  270. }
  271. // IsAgentBackend - checks if agent backed is on or off
  272. func IsAgentBackend() bool {
  273. isagent := true
  274. if os.Getenv("AGENT_BACKEND") != "" {
  275. if os.Getenv("AGENT_BACKEND") == "off" {
  276. isagent = false
  277. }
  278. } else if config.Config.Server.AgentBackend != "" {
  279. if config.Config.Server.AgentBackend == "off" {
  280. isagent = false
  281. }
  282. }
  283. return isagent
  284. }
  285. // IsClientMode - checks if it should run in client mode
  286. func IsClientMode() string {
  287. isclient := "on"
  288. if os.Getenv("CLIENT_MODE") != "" {
  289. if os.Getenv("CLIENT_MODE") == "off" {
  290. isclient = "off"
  291. }
  292. if os.Getenv("CLIENT_MODE") == "contained" {
  293. isclient = "contained"
  294. }
  295. } else if config.Config.Server.ClientMode != "" {
  296. if config.Config.Server.ClientMode == "off" {
  297. isclient = "off"
  298. }
  299. if config.Config.Server.ClientMode == "contained" {
  300. isclient = "contained"
  301. }
  302. }
  303. return isclient
  304. }
  305. // IsDNSMode - should it run with DNS
  306. func IsDNSMode() bool {
  307. isdns := true
  308. if os.Getenv("DNS_MODE") != "" {
  309. if os.Getenv("DNS_MODE") == "off" {
  310. isdns = false
  311. }
  312. } else if config.Config.Server.DNSMode != "" {
  313. if config.Config.Server.DNSMode == "off" {
  314. isdns = false
  315. }
  316. }
  317. return isdns
  318. }
  319. // IsDisplayKeys - should server be able to display keys?
  320. func IsDisplayKeys() bool {
  321. isdisplay := true
  322. if os.Getenv("DISPLAY_KEYS") != "" {
  323. if os.Getenv("DISPLAY_KEYS") == "off" {
  324. isdisplay = false
  325. }
  326. } else if config.Config.Server.DisplayKeys != "" {
  327. if config.Config.Server.DisplayKeys == "off" {
  328. isdisplay = false
  329. }
  330. }
  331. return isdisplay
  332. }
  333. // IsGRPCSSL - ssl grpc on or off
  334. func IsGRPCSSL() bool {
  335. isssl := false
  336. if os.Getenv("GRPC_SSL") != "" {
  337. if os.Getenv("GRPC_SSL") == "on" {
  338. isssl = true
  339. }
  340. } else if config.Config.Server.DNSMode != "" {
  341. if config.Config.Server.DNSMode == "on" {
  342. isssl = true
  343. }
  344. }
  345. return isssl
  346. }
  347. // DisableRemoteIPCheck - disable the remote ip check
  348. func DisableRemoteIPCheck() bool {
  349. disabled := false
  350. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  351. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  352. disabled = true
  353. }
  354. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  355. if config.Config.Server.DisableRemoteIPCheck == "on" {
  356. disabled = true
  357. }
  358. }
  359. return disabled
  360. }
  361. // DisableDefaultNet - disable default net
  362. func DisableDefaultNet() bool {
  363. disabled := false
  364. if os.Getenv("DISABLE_DEFAULT_NET") != "" {
  365. if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
  366. disabled = true
  367. }
  368. } else if config.Config.Server.DisableDefaultNet != "" {
  369. if config.Config.Server.DisableDefaultNet == "on" {
  370. disabled = true
  371. }
  372. }
  373. return disabled
  374. }
  375. // GetPublicIP - gets public ip
  376. func GetPublicIP() (string, error) {
  377. endpoint := ""
  378. var err error
  379. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  380. for _, ipserver := range iplist {
  381. resp, err := http.Get(ipserver)
  382. if err != nil {
  383. continue
  384. }
  385. defer resp.Body.Close()
  386. if resp.StatusCode == http.StatusOK {
  387. bodyBytes, err := io.ReadAll(resp.Body)
  388. if err != nil {
  389. continue
  390. }
  391. endpoint = string(bodyBytes)
  392. break
  393. }
  394. }
  395. if err == nil && endpoint == "" {
  396. err = errors.New("public address not found")
  397. }
  398. return endpoint, err
  399. }
  400. // GetPlatform - get the system type of server
  401. func GetPlatform() string {
  402. platform := "linux"
  403. if os.Getenv("PLATFORM") != "" {
  404. platform = os.Getenv("PLATFORM")
  405. } else if config.Config.Server.Platform != "" {
  406. platform = config.Config.Server.SQLConn
  407. }
  408. return platform
  409. }
  410. // GetSQLConn - get the sql connection string
  411. func GetSQLConn() string {
  412. sqlconn := "http://"
  413. if os.Getenv("SQL_CONN") != "" {
  414. sqlconn = os.Getenv("SQL_CONN")
  415. } else if config.Config.Server.SQLConn != "" {
  416. sqlconn = config.Config.Server.SQLConn
  417. }
  418. return sqlconn
  419. }
  420. // IsSplitDNS - checks if split dns is on
  421. func IsSplitDNS() bool {
  422. issplit := false
  423. if os.Getenv("IS_SPLIT_DNS") == "yes" {
  424. issplit = true
  425. } else if config.Config.Server.SplitDNS == "yes" {
  426. issplit = true
  427. }
  428. return issplit
  429. }
  430. // GetNodeID - gets the node id
  431. func GetNodeID() string {
  432. var id string
  433. id = getMacAddr()
  434. if os.Getenv("NODE_ID") != "" {
  435. id = os.Getenv("NODE_ID")
  436. } else if config.Config.Server.NodeID != "" {
  437. id = config.Config.Server.NodeID
  438. }
  439. return id
  440. }
  441. // GetServerCheckinInterval - gets the server check-in time
  442. func GetServerCheckinInterval() int64 {
  443. var t = int64(5)
  444. var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
  445. if envt > 0 {
  446. t = int64(envt)
  447. } else if config.Config.Server.ServerCheckinInterval > 0 {
  448. t = config.Config.Server.ServerCheckinInterval
  449. }
  450. return t
  451. }
  452. // GetAuthProviderInfo = gets the oauth provider info
  453. func GetAuthProviderInfo() []string {
  454. var authProvider = ""
  455. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  456. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  457. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  458. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  459. } else {
  460. authProvider = ""
  461. }
  462. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  463. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  464. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  465. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  466. }
  467. }
  468. return []string{"", "", ""}
  469. }
  470. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  471. func GetAzureTenant() string {
  472. var azureTenant = ""
  473. if os.Getenv("AZURE_TENANT") != "" {
  474. azureTenant = os.Getenv("AZURE_TENANT")
  475. } else if config.Config.Server.AzureTenant != "" {
  476. azureTenant = config.Config.Server.AzureTenant
  477. }
  478. return azureTenant
  479. }
  480. // GetMacAddr - get's mac address
  481. func getMacAddr() string {
  482. ifas, err := net.Interfaces()
  483. if err != nil {
  484. return ""
  485. }
  486. var as []string
  487. for _, ifa := range ifas {
  488. a := ifa.HardwareAddr.String()
  489. if a != "" {
  490. as = append(as, a)
  491. }
  492. }
  493. return as[0]
  494. }
  495. // GetRce - sees if Rce is enabled, off by default
  496. func GetRce() bool {
  497. return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
  498. }