netmaker/netclient/config/config.go

306 lines
8.5 KiB
Go
Raw Normal View History

2021-03-26 00:17:52 +08:00
package config
import (
2021-05-26 00:48:04 +08:00
//"github.com/davecgh/go-spew/spew"
2021-07-26 02:22:20 +08:00
"encoding/base64"
"encoding/json"
2021-05-26 00:48:04 +08:00
"errors"
"fmt"
"log"
2021-07-26 02:22:20 +08:00
"os"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/logger"
2021-05-26 04:09:49 +08:00
"github.com/gravitl/netmaker/models"
2021-09-20 02:03:47 +08:00
"github.com/gravitl/netmaker/netclient/ncutils"
2021-07-26 02:22:20 +08:00
"github.com/urfave/cli/v2"
"gopkg.in/yaml.v3"
2021-03-26 00:17:52 +08:00
)
2021-07-26 02:22:20 +08:00
2021-10-09 03:07:12 +08:00
// ClientConfig - struct for dealing with client configuration
2021-03-26 00:17:52 +08:00
type ClientConfig struct {
2021-11-16 00:42:52 +08:00
Server ServerConfig `yaml:"server"`
Node models.Node `yaml:"node"`
NetworkSettings models.Network `yaml:"networksettings"`
Network string `yaml:"network"`
Daemon string `yaml:"daemon"`
OperatingSystem string `yaml:"operatingsystem"`
DebugOn bool `yaml:"debugon"`
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// ServerConfig - struct for dealing with the server information for a netclient
2021-03-26 00:17:52 +08:00
type ServerConfig struct {
2022-02-19 05:27:54 +08:00
CoreDNSAddr string `yaml:"corednsaddr"`
GRPCAddress string `yaml:"grpcaddress"`
AccessKey string `yaml:"accesskey"`
GRPCSSL string `yaml:"grpcssl"`
CommsNetwork string `yaml:"commsnetwork"`
2021-03-26 00:17:52 +08:00
}
2021-10-09 03:07:12 +08:00
// Write - writes the config of a client to disk
2021-07-26 02:22:20 +08:00
func Write(config *ClientConfig, network string) error {
if network == "" {
2021-08-31 03:58:23 +08:00
err := errors.New("no network provided - exiting")
return err
}
2021-10-09 03:07:12 +08:00
_, err := os.Stat(ncutils.GetNetclientPath() + "/config")
2021-03-26 10:29:36 +08:00
if os.IsNotExist(err) {
os.MkdirAll(ncutils.GetNetclientPath()+"/config", 0700)
2021-03-26 10:29:36 +08:00
} else if err != nil {
2021-07-26 02:22:20 +08:00
return err
}
2021-09-20 02:03:47 +08:00
home := ncutils.GetNetclientPathSpecific()
2021-03-26 10:29:36 +08:00
2021-08-31 03:58:23 +08:00
file := fmt.Sprintf(home + "netconfig-" + network)
2021-07-26 02:22:20 +08:00
f, err := os.OpenFile(file, os.O_CREATE|os.O_WRONLY|os.O_TRUNC, os.ModePerm)
2021-08-31 03:58:23 +08:00
if err != nil {
return err
}
2021-07-26 02:22:20 +08:00
defer f.Close()
2021-03-26 00:17:52 +08:00
2021-05-26 00:48:04 +08:00
err = yaml.NewEncoder(f).Encode(config)
if err != nil {
return err
2021-03-26 00:17:52 +08:00
}
2022-02-06 03:26:19 +08:00
return f.Sync()
2021-03-26 00:17:52 +08:00
}
2021-07-26 02:22:20 +08:00
2022-02-21 07:29:29 +08:00
// ConfigFileExists - return true if config file exists
2022-02-21 06:47:03 +08:00
func (config *ClientConfig) ConfigFileExists() bool {
home := ncutils.GetNetclientPathSpecific()
file := fmt.Sprintf(home + "netconfig-" + config.Network)
info, err := os.Stat(file)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
}
2021-10-09 03:07:12 +08:00
// ClientConfig.ReadConfig - used to read config from client disk into memory
2021-07-26 02:22:20 +08:00
func (config *ClientConfig) ReadConfig() {
2021-03-26 00:17:52 +08:00
nofile := false
2021-03-26 10:29:36 +08:00
//home, err := homedir.Dir()
2021-09-20 02:03:47 +08:00
home := ncutils.GetNetclientPathSpecific()
2022-02-02 04:27:00 +08:00
2021-08-31 03:58:23 +08:00
file := fmt.Sprintf(home + "netconfig-" + config.Network)
2021-03-26 00:17:52 +08:00
//f, err := os.Open(file)
f, err := os.OpenFile(file, os.O_RDONLY, 0600)
2021-03-26 00:17:52 +08:00
if err != nil {
logger.Log(1, "trouble opening file: ", err.Error())
2021-03-26 00:17:52 +08:00
nofile = true
2021-03-26 10:29:36 +08:00
//fmt.Println("Could not access " + home + "/.netconfig, proceeding...")
2021-03-26 00:17:52 +08:00
}
defer f.Close()
//var cfg ClientConfig
if !nofile {
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&config)
if err != nil {
fmt.Println("no config or invalid")
fmt.Println(err)
log.Fatal(err)
}
}
}
2021-05-26 04:09:49 +08:00
2021-10-09 03:07:12 +08:00
// ModConfig - overwrites the node inside client config on disk
2021-08-03 06:06:26 +08:00
func ModConfig(node *models.Node) error {
network := node.Network
2021-07-26 02:22:20 +08:00
if network == "" {
2021-08-03 06:06:26 +08:00
return errors.New("no network provided")
2021-07-26 02:22:20 +08:00
}
2021-05-26 00:48:04 +08:00
var modconfig ClientConfig
2021-09-20 02:03:47 +08:00
if FileExists(ncutils.GetNetclientPathSpecific() + "netconfig-" + network) {
2021-05-26 00:48:04 +08:00
useconfig, err := ReadConfig(network)
if err != nil {
return err
}
modconfig = *useconfig
}
2021-08-31 03:58:23 +08:00
2021-08-06 02:40:48 +08:00
modconfig.Node = (*node)
2021-12-11 10:09:42 +08:00
modconfig.NetworkSettings = node.NetworkSettings
2022-02-06 03:26:19 +08:00
return Write(&modconfig, network)
2021-05-26 00:48:04 +08:00
}
2021-11-04 03:43:10 +08:00
// ModConfig - overwrites the node inside client config on disk
func SaveBackup(network string) error {
var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
if FileExists(configPath) {
2022-01-07 04:05:38 +08:00
input, err := os.ReadFile(configPath)
2021-11-04 03:43:10 +08:00
if err != nil {
logger.Log(0, "failed to read ", configPath, " to make a backup")
2021-11-04 03:43:10 +08:00
return err
}
if err = os.WriteFile(backupPath, input, 0600); err != nil {
logger.Log(0, "failed to copy backup to ", backupPath)
2021-11-04 03:43:10 +08:00
return err
}
}
return nil
}
// ReplaceWithBackup - replaces netconfig file with backup
func ReplaceWithBackup(network string) error {
var backupPath = ncutils.GetNetclientPathSpecific() + "backup.netconfig-" + network
var configPath = ncutils.GetNetclientPathSpecific() + "netconfig-" + network
if FileExists(backupPath) {
2022-01-07 04:05:38 +08:00
input, err := os.ReadFile(backupPath)
2021-11-04 03:43:10 +08:00
if err != nil {
logger.Log(0, "failed to read file ", backupPath, " to backup network: ", network)
2021-11-04 03:43:10 +08:00
return err
}
if err = os.WriteFile(configPath, input, 0600); err != nil {
logger.Log(0, "failed backup ", backupPath, " to ", configPath)
2021-11-04 03:43:10 +08:00
return err
}
}
logger.Log(0, "used backup file for network: ", network)
2021-11-04 03:43:10 +08:00
return nil
}
2021-10-09 03:07:12 +08:00
// GetCLIConfig - gets the cli flags as a config
2021-08-03 06:06:26 +08:00
func GetCLIConfig(c *cli.Context) (ClientConfig, string, error) {
2021-05-26 00:48:04 +08:00
var cfg ClientConfig
if c.String("token") != "" {
2021-07-26 02:22:20 +08:00
tokenbytes, err := base64.StdEncoding.DecodeString(c.String("token"))
if err != nil {
2021-05-26 00:48:04 +08:00
log.Println("error decoding token")
2021-08-03 06:06:26 +08:00
return cfg, "", err
2021-07-26 02:22:20 +08:00
}
var accesstoken models.AccessToken
if err := json.Unmarshal(tokenbytes, &accesstoken); err != nil {
2021-07-26 02:22:20 +08:00
log.Println("error converting token json to object", tokenbytes)
2021-08-03 06:06:26 +08:00
return cfg, "", err
}
2021-07-26 02:22:20 +08:00
if accesstoken.ServerConfig.GRPCConnString != "" {
cfg.Server.GRPCAddress = accesstoken.ServerConfig.GRPCConnString
}
2021-07-26 02:22:20 +08:00
cfg.Network = accesstoken.ClientConfig.Network
cfg.Node.Network = accesstoken.ClientConfig.Network
cfg.Server.AccessKey = accesstoken.ClientConfig.Key
cfg.Node.LocalRange = accesstoken.ClientConfig.LocalRange
cfg.Server.GRPCSSL = accesstoken.ServerConfig.GRPCSSL
2022-02-19 05:27:54 +08:00
cfg.Server.CommsNetwork = accesstoken.ServerConfig.CommsNetwork
2021-05-26 04:09:49 +08:00
if c.String("grpcserver") != "" {
cfg.Server.GRPCAddress = c.String("grpcserver")
2021-05-26 00:48:04 +08:00
}
if c.String("key") != "" {
cfg.Server.AccessKey = c.String("key")
}
if c.String("network") != "all" {
cfg.Network = c.String("network")
cfg.Node.Network = c.String("network")
}
if c.String("localrange") != "" {
cfg.Node.LocalRange = c.String("localrange")
}
2021-07-26 02:22:20 +08:00
if c.String("grpcssl") != "" {
cfg.Server.GRPCSSL = c.String("grpcssl")
}
if c.String("corednsaddr") != "" {
cfg.Server.CoreDNSAddr = c.String("corednsaddr")
}
2021-05-26 00:48:04 +08:00
} else {
2021-05-26 04:09:49 +08:00
cfg.Server.GRPCAddress = c.String("grpcserver")
2021-05-26 00:48:04 +08:00
cfg.Server.AccessKey = c.String("key")
2021-07-26 02:22:20 +08:00
cfg.Network = c.String("network")
cfg.Node.Network = c.String("network")
cfg.Node.LocalRange = c.String("localrange")
cfg.Server.GRPCSSL = c.String("grpcssl")
cfg.Server.CoreDNSAddr = c.String("corednsaddr")
2021-05-26 00:48:04 +08:00
}
cfg.Node.Name = c.String("name")
cfg.Node.Interface = c.String("interface")
cfg.Node.Password = c.String("password")
cfg.Node.MacAddress = c.String("macaddress")
cfg.Node.LocalAddress = c.String("localaddress")
2021-08-03 06:06:26 +08:00
cfg.Node.Address = c.String("address")
cfg.Node.Address6 = c.String("addressIPV6")
2022-02-16 04:40:38 +08:00
//cfg.Node.Roaming = c.String("roaming")
2021-09-29 22:02:34 +08:00
cfg.Node.DNSOn = c.String("dnson")
2021-06-02 23:00:10 +08:00
cfg.Node.IsLocal = c.String("islocal")
cfg.Node.IsStatic = c.String("isstatic")
2021-06-02 23:00:10 +08:00
cfg.Node.IsDualStack = c.String("isdualstack")
cfg.Node.PostUp = c.String("postup")
cfg.Node.PostDown = c.String("postdown")
2021-08-03 06:06:26 +08:00
cfg.Node.ListenPort = int32(c.Int("port"))
cfg.Node.PersistentKeepalive = int32(c.Int("keepalive"))
2021-06-02 23:00:10 +08:00
cfg.Node.PublicKey = c.String("publickey")
2021-08-03 06:06:26 +08:00
privateKey := c.String("privatekey")
2021-06-02 23:00:10 +08:00
cfg.Node.Endpoint = c.String("endpoint")
cfg.Node.IPForwarding = c.String("ipforwarding")
cfg.OperatingSystem = c.String("operatingsystem")
cfg.Daemon = c.String("daemon")
2021-07-26 02:22:20 +08:00
cfg.Node.UDPHolePunch = c.String("udpholepunch")
2021-08-31 03:58:23 +08:00
cfg.Node.MTU = int32(c.Int("mtu"))
2021-05-26 00:48:04 +08:00
2021-08-03 06:06:26 +08:00
return cfg, privateKey, nil
2021-07-26 02:22:20 +08:00
}
2021-05-26 00:48:04 +08:00
2021-10-09 03:07:12 +08:00
// ReadConfig - reads a config of a client from disk for specified network
func ReadConfig(network string) (*ClientConfig, error) {
2021-07-26 02:22:20 +08:00
if network == "" {
2021-08-31 03:58:23 +08:00
err := errors.New("no network provided - exiting")
2021-07-26 02:22:20 +08:00
return nil, err
}
2021-03-26 00:17:52 +08:00
nofile := false
2021-09-20 02:03:47 +08:00
home := ncutils.GetNetclientPathSpecific()
2021-08-31 03:58:23 +08:00
file := fmt.Sprintf(home + "netconfig-" + network)
2021-03-26 00:17:52 +08:00
f, err := os.Open(file)
2021-05-26 00:48:04 +08:00
2021-03-26 00:17:52 +08:00
if err != nil {
2021-11-04 03:43:10 +08:00
if err = ReplaceWithBackup(network); err != nil {
nofile = true
}
f, err = os.Open(file)
if err != nil {
nofile = true
}
2021-03-26 00:17:52 +08:00
}
defer f.Close()
var cfg ClientConfig
if !nofile {
decoder := yaml.NewDecoder(f)
err = decoder.Decode(&cfg)
if err != nil {
fmt.Println("trouble decoding file")
return nil, err
2021-03-26 00:17:52 +08:00
}
}
return &cfg, err
2021-03-26 00:17:52 +08:00
}
2021-05-26 00:48:04 +08:00
2021-10-09 03:07:12 +08:00
// FileExists - checks if a file exists on disk
2021-05-26 00:48:04 +08:00
func FileExists(f string) bool {
2021-07-26 02:22:20 +08:00
info, err := os.Stat(f)
if os.IsNotExist(err) {
return false
}
return !info.IsDir()
2021-05-26 00:48:04 +08:00
}
2021-08-03 06:06:26 +08:00
2021-10-09 03:07:12 +08:00
// GetNode - parses a network specified client config for node data
2021-08-03 06:06:26 +08:00
func GetNode(network string) models.Node {
modcfg, err := ReadConfig(network)
if err != nil {
log.Fatalf("Error: %v", err)
}
var node models.Node
node.Fill(&modcfg.Node)
return node
}