config.go 8.3 KB

123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169170171172173174175176177178179180181182183184185186187188189190191192193194195196197198199200201202203204205206207208209210211212213214215216217218219220221222223224225226227228229230231232233234235236237238239240241242243244245246247248249250251252253254255256257258259260261262263264265266267268269270271272273274275276277278279280281282283284285286287288289290291292293294295296297298299300301302
  1. package config
  2. import (
  3. //"github.com/davecgh/go-spew/spew"
  4. "crypto/ed25519"
  5. "crypto/x509"
  6. "crypto/x509/pkix"
  7. "errors"
  8. "fmt"
  9. "log"
  10. "os"
  11. "github.com/gravitl/netmaker/logger"
  12. "github.com/gravitl/netmaker/models"
  13. "github.com/gravitl/netmaker/netclient/ncutils"
  14. "github.com/urfave/cli/v2"
  15. "gopkg.in/yaml.v3"
  16. )
  17. // ClientConfig - struct for dealing with client configuration
  18. type ClientConfig struct {
  19. Server ServerConfig `yaml:"server"`
  20. Node models.Node `yaml:"node"`
  21. NetworkSettings models.Network `yaml:"networksettings"`
  22. Network string `yaml:"network"`
  23. Daemon string `yaml:"daemon"`
  24. OperatingSystem string `yaml:"operatingsystem"`
  25. DebugOn bool `yaml:"debugon"`
  26. }
  27. // ServerConfig - struct for dealing with the server information for a netclient
  28. type ServerConfig struct {
  29. CoreDNSAddr string `yaml:"corednsaddr"`
  30. AccessKey string `yaml:"accesskey"`
  31. Server string `yaml:"server"`
  32. API string `yaml:"api"`
  33. }
  34. // RegisterRequest - struct for registation with netmaker server
  35. type RegisterRequest struct {
  36. Key ed25519.PrivateKey
  37. CommonName pkix.Name
  38. }
  39. // RegisterResponse - the response to register function
  40. type RegisterResponse struct {
  41. CA x509.Certificate
  42. CAPubKey ed25519.PublicKey
  43. Cert x509.Certificate
  44. CertPubKey ed25519.PublicKey
  45. }
  46. // Write - writes the config of a client to disk
  47. func Write(config *ClientConfig, network string) error {
  48. if network == "" {
  49. err := errors.New("no network provided - exiting")
  50. return err
  51. }
  52. _, err := os.Stat(ncutils.GetNetclientPath() + "/config")
  53. if os.IsNotExist(err) {
  54. os.MkdirAll(ncutils.GetNetclientPath()+"/config", 0700)
  55. } else if err != nil {
  56. return err
  57. }
  58. home := ncutils.GetNetclientPathSpecific()
  59. file := fmt.Sprintf(home + "netconfig-" + network)
  60. f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
  61. if err != nil {
  62. return err
  63. }
  64. defer f.Close()
  65. err = yaml.NewEncoder(f).Encode(config)
  66. if err != nil {
  67. return err
  68. }
  69. return f.Sync()
  70. }
  71. // ConfigFileExists - return true if config file exists
  72. func (config *ClientConfig) ConfigFileExists() bool {
  73. home := ncutils.GetNetclientPathSpecific()
  74. file := fmt.Sprintf(home + "netconfig-" + config.Network)
  75. info, err := os.Stat(file)
  76. if os.IsNotExist(err) {
  77. return false
  78. }
  79. return !info.IsDir()
  80. }
  81. // ClientConfig.ReadConfig - used to read config from client disk into memory
  82. func (config *ClientConfig) ReadConfig() {
  83. nofile := false
  84. //home, err := homedir.Dir()
  85. home := ncutils.GetNetclientPathSpecific()
  86. file := fmt.Sprintf(home + "netconfig-" + config.Network)
  87. //f, err := os.Open(file)
  88. f, err := os.OpenFile(file, os.O_RDONLY, 0600)
  89. if err != nil {
  90. logger.Log(1, "trouble opening file: ", err.Error())
  91. nofile = true
  92. //fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
  93. }
  94. defer f.Close()
  95. //var cfg ClientConfig
  96. if !nofile {
  97. decoder := yaml.NewDecoder(f)
  98. err = decoder.Decode(&config)
  99. if err != nil {
  100. fmt.Println("no config or invalid")
  101. fmt.Println(err)
  102. log.Fatal(err)
  103. }
  104. }
  105. }
  106. // ModConfig - overwrites the node inside client config on disk
  107. func ModConfig(node *models.Node) error {
  108. network := node.Network
  109. if network == "" {
  110. return errors.New("no network provided")
  111. }
  112. var modconfig ClientConfig
  113. if FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network) {
  114. useconfig, err := ReadConfig(network)
  115. if err != nil {
  116. return err
  117. }
  118. modconfig = *useconfig
  119. }
  120. modconfig.Node = (*node)
  121. modconfig.NetworkSettings = node.NetworkSettings
  122. return Write(&modconfig, network)
  123. }
  124. // ModConfig - overwrites the node inside client config on disk
  125. func SaveBackup(network string) error {
  126. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  127. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  128. if FileExists(configPath) {
  129. input, err := os.ReadFile(configPath)
  130. if err != nil {
  131. logger.Log(0, "failed to read ", configPath, " to make a backup")
  132. return err
  133. }
  134. if err = os.WriteFile(backupPath, input, 0600); err != nil {
  135. logger.Log(0, "failed to copy backup to ", backupPath)
  136. return err
  137. }
  138. }
  139. return nil
  140. }
  141. // ReplaceWithBackup - replaces netconfig file with backup
  142. func ReplaceWithBackup(network string) error {
  143. var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
  144. var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
  145. if FileExists(backupPath) {
  146. input, err := os.ReadFile(backupPath)
  147. if err != nil {
  148. logger.Log(0, "failed to read file ", backupPath, " to backup network: ", network)
  149. return err
  150. }
  151. if err = os.WriteFile(configPath, input, 0600); err != nil {
  152. logger.Log(0, "failed backup ", backupPath, " to ", configPath)
  153. return err
  154. }
  155. }
  156. logger.Log(0, "used backup file for network: ", network)
  157. return nil
  158. }
  159. // GetCLIConfig - gets the cli flags as a config
  160. func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
  161. var cfg ClientConfig
  162. if c.String("token") != "" {
  163. accesstoken, err := ParseAccessToken(c.String("token"))
  164. if err != nil {
  165. return cfg, "", err
  166. }
  167. cfg.Network = accesstoken.ClientConfig.Network
  168. cfg.Node.Network = accesstoken.ClientConfig.Network
  169. cfg.Server.AccessKey = accesstoken.ClientConfig.Key
  170. cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
  171. cfg.Server.Server = accesstoken.ServerConfig.Server
  172. cfg.Server.API = accesstoken.ServerConfig.APIConnString
  173. if c.String("key") != "" {
  174. cfg.Server.AccessKey = c.String("key")
  175. }
  176. if c.String("network") != "all" {
  177. cfg.Network = c.String("network")
  178. cfg.Node.Network = c.String("network")
  179. }
  180. if c.String("localrange") != "" {
  181. cfg.Node.LocalRange = c.String("localrange")
  182. }
  183. if c.String("corednsaddr") != "" {
  184. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  185. }
  186. if c.String("apiserver") != "" {
  187. cfg.Server.API = c.String("apiserver")
  188. }
  189. } else {
  190. cfg.Server.AccessKey = c.String("key")
  191. cfg.Network = c.String("network")
  192. cfg.Node.Network = c.String("network")
  193. cfg.Node.LocalRange = c.String("localrange")
  194. cfg.Server.CoreDNSAddr = c.String("corednsaddr")
  195. cfg.Server.API = c.String("apiserver")
  196. }
  197. cfg.Node.Name = c.String("name")
  198. cfg.Node.Interface = c.String("interface")
  199. cfg.Node.Password = c.String("password")
  200. cfg.Node.MacAddress = c.String("macaddress")
  201. cfg.Node.LocalAddress = c.String("localaddress")
  202. cfg.Node.Address = c.String("address")
  203. cfg.Node.Address6 = c.String("address6")
  204. //cfg.Node.Roaming = c.String("roaming")
  205. cfg.Node.DNSOn = c.String("dnson")
  206. cfg.Node.IsLocal = c.String("islocal")
  207. cfg.Node.IsStatic = c.String("isstatic")
  208. cfg.Node.PostUp = c.String("postup")
  209. cfg.Node.PostDown = c.String("postdown")
  210. cfg.Node.ListenPort = int32(c.Int("port"))
  211. cfg.Node.PersistentKeepalive = int32(c.Int("keepalive"))
  212. cfg.Node.PublicKey = c.String("publickey")
  213. privateKey := c.String("privatekey")
  214. cfg.Node.Endpoint = c.String("endpoint")
  215. cfg.Node.IPForwarding = c.String("ipforwarding")
  216. cfg.OperatingSystem = c.String("operatingsystem")
  217. cfg.Daemon = c.String("daemon")
  218. cfg.Node.UDPHolePunch = c.String("udpholepunch")
  219. cfg.Node.MTU = int32(c.Int("mtu"))
  220. return cfg, privateKey, nil
  221. }
  222. // ReadConfig - reads a config of a client from disk for specified network
  223. func ReadConfig(network string) (*ClientConfig, error) {
  224. if network == "" {
  225. err := errors.New("no network provided - exiting")
  226. return nil, err
  227. }
  228. nofile := false
  229. home := ncutils.GetNetclientPathSpecific()
  230. file := fmt.Sprintf(home + "netconfig-" + network)
  231. f, err := os.Open(file)
  232. if err != nil {
  233. if err = ReplaceWithBackup(network); err != nil {
  234. nofile = true
  235. }
  236. f, err = os.Open(file)
  237. if err != nil {
  238. nofile = true
  239. }
  240. }
  241. defer f.Close()
  242. var cfg ClientConfig
  243. if !nofile {
  244. decoder := yaml.NewDecoder(f)
  245. err = decoder.Decode(&cfg)
  246. if err != nil {
  247. fmt.Println("trouble decoding file")
  248. return nil, err
  249. }
  250. }
  251. return &cfg, err
  252. }
  253. // FileExists - checks if a file exists on disk
  254. func FileExists(f string) bool {
  255. info, err := os.Stat(f)
  256. if os.IsNotExist(err) {
  257. return false
  258. }
  259. return !info.IsDir()
  260. }
  261. // GetNode - parses a network specified client config for node data
  262. func GetNode(network string) models.Node {
  263. modcfg, err := ReadConfig(network)
  264. if err != nil {
  265. log.Fatalf("Error: %v", err)
  266. }
  267. var node models.Node
  268. node.Fill(&modcfg.Node)
  269. return node
  270. }