2023-03-15 15:30:29 +01:00
|
|
|
package state
|
|
|
|
|
|
|
|
import (
|
2023-03-22 14:01:08 +01:00
|
|
|
"fmt"
|
2023-03-15 15:30:29 +01:00
|
|
|
"io/ioutil"
|
2023-03-21 13:35:16 +01:00
|
|
|
"net/netip"
|
2023-03-22 14:01:08 +01:00
|
|
|
"os"
|
2023-03-15 15:30:29 +01:00
|
|
|
|
2023-04-12 11:13:53 +02:00
|
|
|
"github.com/google/uuid"
|
2023-03-15 15:30:29 +01:00
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
// Config contains all MTD services and cloud provider configs
|
2023-03-15 15:30:29 +01:00
|
|
|
type Config struct {
|
2023-03-21 13:35:16 +01:00
|
|
|
MTD mtdconf `yaml:"mtd"`
|
|
|
|
AWS aws `yaml:"aws"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type mtdconf struct {
|
2023-03-22 14:01:08 +01:00
|
|
|
Services []Service `yaml:"services"`
|
2023-03-21 13:35:16 +01:00
|
|
|
}
|
2023-03-22 10:39:22 +01:00
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
// Service contains all necessary information about a service to identify it in the cloud as well as configuring a proxy for it
|
|
|
|
type Service struct {
|
2023-04-12 11:13:53 +02:00
|
|
|
ID CustomUUID `yaml:"id"`
|
2023-03-22 14:01:08 +01:00
|
|
|
CloudID string `yaml:"cloud_id"`
|
2023-03-21 13:35:16 +01:00
|
|
|
EntryIP netip.Addr `yaml:"entry_ip"`
|
|
|
|
EntryPort uint16 `yaml:"entry_port"`
|
|
|
|
ServiceIP netip.Addr `yaml:"service_ip"`
|
|
|
|
ServicePort uint16 `yaml:"service_port"`
|
|
|
|
}
|
2023-03-22 10:39:22 +01:00
|
|
|
|
2023-04-12 11:13:53 +02:00
|
|
|
// CustomUUID is an alias for uuid.UUID to enable custom unmarshal function
|
|
|
|
type CustomUUID uuid.UUID
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
type aws struct {
|
|
|
|
Regions []string `yaml:"regions"`
|
|
|
|
CredentialsPath string `yaml:"credentials_path"`
|
|
|
|
}
|
|
|
|
|
2023-04-12 11:13:53 +02:00
|
|
|
// UnmarshalYAML parses uuid in yaml to CustomUUID type
|
|
|
|
func (u *CustomUUID) UnmarshalYAML(value *yaml.Node) error {
|
|
|
|
id, err := uuid.Parse(value.Value)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
*u = CustomUUID(id)
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// MarshalYAML parses CustomUUID type to uuid string for yaml
|
|
|
|
func (u CustomUUID) MarshalYAML() (interface{}, error) {
|
|
|
|
return uuid.UUID(u).String(), nil
|
|
|
|
}
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
// LoadConf loads config from a yaml file
|
2023-03-22 14:01:08 +01:00
|
|
|
func LoadConf(filename string) (Config) {
|
2023-03-15 15:30:29 +01:00
|
|
|
var config Config
|
|
|
|
|
|
|
|
data, err := ioutil.ReadFile(filename)
|
|
|
|
if err != nil {
|
2023-03-22 14:01:08 +01:00
|
|
|
fmt.Println("Error reading file:", err)
|
|
|
|
|
|
|
|
fmt.Println("Attempting to load default config")
|
|
|
|
data, err = ioutil.ReadFile("config.default.yaml")
|
|
|
|
if err != nil {
|
|
|
|
fmt.Println("Error reading file:", err)
|
|
|
|
os.Exit(1)
|
|
|
|
}
|
2023-03-15 15:30:29 +01:00
|
|
|
}
|
|
|
|
|
2023-03-21 13:35:16 +01:00
|
|
|
err = yaml.Unmarshal([]byte(data), &config)
|
2023-03-15 15:30:29 +01:00
|
|
|
if err != nil {
|
2023-03-22 14:01:08 +01:00
|
|
|
fmt.Println("Error importing config:", err)
|
|
|
|
os.Exit(1)
|
2023-03-15 15:30:29 +01:00
|
|
|
}
|
2023-03-22 14:01:08 +01:00
|
|
|
fmt.Println("Imported config succesfully!")
|
|
|
|
return config
|
|
|
|
}
|
2023-03-15 15:30:29 +01:00
|
|
|
|
2023-03-22 14:01:08 +01:00
|
|
|
// SaveConf saves config to yaml file
|
|
|
|
func SaveConf(filename string, config Config) (error) {
|
|
|
|
yamlBytes, err := yaml.Marshal(&config)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = ioutil.WriteFile(filename, yamlBytes, 0644)
|
|
|
|
if err != nil {
|
|
|
|
return err
|
|
|
|
}
|
|
|
|
return nil
|
2023-03-22 14:24:56 +01:00
|
|
|
}
|