serverconf.go 13 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302303304305306307308309310311312313314315316317318319320321322323324325326327328329330331332333334335336337338339340341342343344345346347348349350351352353354355356357358359360361362363364365366367368369370371372373374375376377378379380381382383384385386387388389390391392393394395396397398399400401402403404405406407408409410411412413414415416417418419420421422423424425426427428429430431432433434435436437438439440441442443444445446447448449450451452453454455456457458459460461462463464465466467468469470471472473474475476477478479480481482483484485486487488489490491492493494495496497498499500501502503504505506507508509510511512513514515516517518519520521522523524525526527528529530531
  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.10.0"
  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") == "off" {
  289. isclient = "off"
  290. }
  291. if config.Config.Server.ClientMode == "off" {
  292. isclient = "off"
  293. }
  294. return isclient
  295. }
  296. // IsDNSMode - should it run with DNS
  297. func IsDNSMode() bool {
  298. isdns := true
  299. if os.Getenv("DNS_MODE") != "" {
  300. if os.Getenv("DNS_MODE") == "off" {
  301. isdns = false
  302. }
  303. } else if config.Config.Server.DNSMode != "" {
  304. if config.Config.Server.DNSMode == "off" {
  305. isdns = false
  306. }
  307. }
  308. return isdns
  309. }
  310. // IsDisplayKeys - should server be able to display keys?
  311. func IsDisplayKeys() bool {
  312. isdisplay := true
  313. if os.Getenv("DISPLAY_KEYS") != "" {
  314. if os.Getenv("DISPLAY_KEYS") == "off" {
  315. isdisplay = false
  316. }
  317. } else if config.Config.Server.DisplayKeys != "" {
  318. if config.Config.Server.DisplayKeys == "off" {
  319. isdisplay = false
  320. }
  321. }
  322. return isdisplay
  323. }
  324. // IsGRPCSSL - ssl grpc on or off
  325. func IsGRPCSSL() bool {
  326. isssl := false
  327. if os.Getenv("GRPC_SSL") != "" {
  328. if os.Getenv("GRPC_SSL") == "on" {
  329. isssl = true
  330. }
  331. } else if config.Config.Server.DNSMode != "" {
  332. if config.Config.Server.DNSMode == "on" {
  333. isssl = true
  334. }
  335. }
  336. return isssl
  337. }
  338. // DisableRemoteIPCheck - disable the remote ip check
  339. func DisableRemoteIPCheck() bool {
  340. disabled := false
  341. if os.Getenv("DISABLE_REMOTE_IP_CHECK") != "" {
  342. if os.Getenv("DISABLE_REMOTE_IP_CHECK") == "on" {
  343. disabled = true
  344. }
  345. } else if config.Config.Server.DisableRemoteIPCheck != "" {
  346. if config.Config.Server.DisableRemoteIPCheck == "on" {
  347. disabled = true
  348. }
  349. }
  350. return disabled
  351. }
  352. // DisableDefaultNet - disable default net
  353. func DisableDefaultNet() bool {
  354. disabled := false
  355. if os.Getenv("DISABLE_DEFAULT_NET") != "" {
  356. if os.Getenv("DISABLE_DEFAULT_NET") == "on" {
  357. disabled = true
  358. }
  359. } else if config.Config.Server.DisableDefaultNet != "" {
  360. if config.Config.Server.DisableDefaultNet == "on" {
  361. disabled = true
  362. }
  363. }
  364. return disabled
  365. }
  366. // GetPublicIP - gets public ip
  367. func GetPublicIP() (string, error) {
  368. endpoint := ""
  369. var err error
  370. iplist := []string{"https://ip.server.gravitl.com", "https://ifconfig.me", "https://api.ipify.org", "https://ipinfo.io/ip"}
  371. for _, ipserver := range iplist {
  372. resp, err := http.Get(ipserver)
  373. if err != nil {
  374. continue
  375. }
  376. defer resp.Body.Close()
  377. if resp.StatusCode == http.StatusOK {
  378. bodyBytes, err := io.ReadAll(resp.Body)
  379. if err != nil {
  380. continue
  381. }
  382. endpoint = string(bodyBytes)
  383. break
  384. }
  385. }
  386. if err == nil && endpoint == "" {
  387. err = errors.New("public address not found")
  388. }
  389. return endpoint, err
  390. }
  391. // GetPlatform - get the system type of server
  392. func GetPlatform() string {
  393. platform := "linux"
  394. if os.Getenv("PLATFORM") != "" {
  395. platform = os.Getenv("PLATFORM")
  396. } else if config.Config.Server.Platform != "" {
  397. platform = config.Config.Server.SQLConn
  398. }
  399. return platform
  400. }
  401. // GetSQLConn - get the sql connection string
  402. func GetSQLConn() string {
  403. sqlconn := "http://"
  404. if os.Getenv("SQL_CONN") != "" {
  405. sqlconn = os.Getenv("SQL_CONN")
  406. } else if config.Config.Server.SQLConn != "" {
  407. sqlconn = config.Config.Server.SQLConn
  408. }
  409. return sqlconn
  410. }
  411. // IsSplitDNS - checks if split dns is on
  412. func IsSplitDNS() bool {
  413. issplit := false
  414. if os.Getenv("IS_SPLIT_DNS") == "yes" {
  415. issplit = true
  416. } else if config.Config.Server.SplitDNS == "yes" {
  417. issplit = true
  418. }
  419. return issplit
  420. }
  421. // GetNodeID - gets the node id
  422. func GetNodeID() string {
  423. var id string
  424. id = getMacAddr()
  425. if os.Getenv("NODE_ID") != "" {
  426. id = os.Getenv("NODE_ID")
  427. } else if config.Config.Server.NodeID != "" {
  428. id = config.Config.Server.NodeID
  429. }
  430. return id
  431. }
  432. // GetServerCheckinInterval - gets the server check-in time
  433. func GetServerCheckinInterval() int64 {
  434. var t = int64(5)
  435. var envt, _ = strconv.Atoi(os.Getenv("SERVER_CHECKIN_INTERVAL"))
  436. if envt > 0 {
  437. t = int64(envt)
  438. } else if config.Config.Server.ServerCheckinInterval > 0 {
  439. t = config.Config.Server.ServerCheckinInterval
  440. }
  441. return t
  442. }
  443. // GetAuthProviderInfo = gets the oauth provider info
  444. func GetAuthProviderInfo() []string {
  445. var authProvider = ""
  446. if os.Getenv("AUTH_PROVIDER") != "" && os.Getenv("CLIENT_ID") != "" && os.Getenv("CLIENT_SECRET") != "" {
  447. authProvider = strings.ToLower(os.Getenv("AUTH_PROVIDER"))
  448. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  449. return []string{authProvider, os.Getenv("CLIENT_ID"), os.Getenv("CLIENT_SECRET")}
  450. } else {
  451. authProvider = ""
  452. }
  453. } else if config.Config.Server.AuthProvider != "" && config.Config.Server.ClientID != "" && config.Config.Server.ClientSecret != "" {
  454. authProvider = strings.ToLower(config.Config.Server.AuthProvider)
  455. if authProvider == "google" || authProvider == "azure-ad" || authProvider == "github" {
  456. return []string{authProvider, config.Config.Server.ClientID, config.Config.Server.ClientSecret}
  457. }
  458. }
  459. return []string{"", "", ""}
  460. }
  461. // GetAzureTenant - retrieve the azure tenant ID from env variable or config file
  462. func GetAzureTenant() string {
  463. var azureTenant = ""
  464. if os.Getenv("AZURE_TENANT") != "" {
  465. azureTenant = os.Getenv("AZURE_TENANT")
  466. } else if config.Config.Server.AzureTenant != "" {
  467. azureTenant = config.Config.Server.AzureTenant
  468. }
  469. return azureTenant
  470. }
  471. // GetMacAddr - get's mac address
  472. func getMacAddr() string {
  473. ifas, err := net.Interfaces()
  474. if err != nil {
  475. return ""
  476. }
  477. var as []string
  478. for _, ifa := range ifas {
  479. a := ifa.HardwareAddr.String()
  480. if a != "" {
  481. as = append(as, a)
  482. }
  483. }
  484. return as[0]
  485. }
  486. // GetRce - sees if Rce is enabled, off by default
  487. func GetRce() bool {
  488. return os.Getenv("RCE") == "on" || config.Config.Server.RCE == "on"
  489. }