config.go 8.2 KB

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