2023-10-01 20:01:35 +02:00
|
|
|
// conf defines configuration file parsing for golang
|
|
|
|
package conf
|
|
|
|
|
|
|
|
import (
|
|
|
|
"os"
|
|
|
|
|
|
|
|
logging "github.com/tim-beatham/wgmesh/pkg/log"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
type WgMeshConfigurationError struct {
|
|
|
|
msg string
|
|
|
|
}
|
|
|
|
|
|
|
|
func (m *WgMeshConfigurationError) Error() string {
|
|
|
|
return m.msg
|
|
|
|
}
|
|
|
|
|
2023-10-01 20:01:35 +02:00
|
|
|
type WgMeshConfiguration struct {
|
2023-10-28 17:38:25 +02:00
|
|
|
// CertificatePath is the path to the certificate to use in mTLS
|
|
|
|
CertificatePath string `yaml:"certificatePath"`
|
|
|
|
// PrivateKeypath is the path to the clients private key in mTLS
|
|
|
|
PrivateKeyPath string `yaml:"privateKeyPath"`
|
|
|
|
// CaCeritifcatePath path to the certificate of the trust certificate authority
|
|
|
|
CaCertificatePath string `yaml:"caCertificatePath"`
|
|
|
|
// SkipCertVerification specify to skip certificate verification. Should only be used
|
|
|
|
// in test environments
|
|
|
|
SkipCertVerification bool `yaml:"skipCertVerification"`
|
|
|
|
// Port to run the GrpcServer on
|
|
|
|
GrpcPort string `yaml:"gRPCPort"`
|
2023-10-26 17:53:12 +02:00
|
|
|
// AdvertiseRoutes advertises other meshes if the node is in multiple meshes
|
|
|
|
AdvertiseRoutes bool `yaml:"advertiseRoutes"`
|
2023-10-28 17:38:25 +02:00
|
|
|
// Endpoint is the IP in which this computer is publicly reachable.
|
|
|
|
// usecase is when the node has multiple IP addresses
|
2023-11-03 16:24:18 +01:00
|
|
|
Endpoint string `yaml:"publicEndpoint"`
|
|
|
|
ClusterSize int `yaml:"clusterSize"`
|
|
|
|
SyncRate float64 `yaml:"syncRate"`
|
|
|
|
InterClusterChance float64 `yaml:"interClusterChance"`
|
|
|
|
BranchRate int `yaml:"branchRate"`
|
|
|
|
InfectionCount int `yaml:"infectionCount"`
|
|
|
|
KeepAliveRate int `yaml:"keepAliveRate"`
|
2023-10-01 20:01:35 +02:00
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
func ValidateConfiguration(c *WgMeshConfiguration) error {
|
|
|
|
if len(c.CertificatePath) == 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "A public certificate must be specified for mTLS",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.PrivateKeyPath) == 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "A private key must be specified for mTLS",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.CaCertificatePath) == 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "A ca certificate must be specified for mTLS",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if len(c.GrpcPort) == 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "A grpc port must be specified",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.ClusterSize <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "A cluster size must not be 0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.SyncRate <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "SyncRate cannot be negative",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.BranchRate <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "Branch rate cannot be negative",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InfectionCount <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "Infection count cannot be less than 1",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.KeepAliveRate <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "KeepAliveRate cannot be less than negative",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
if c.InterClusterChance <= 0 {
|
|
|
|
return &WgMeshConfigurationError{
|
|
|
|
msg: "Intercluster chance cannot be less than 0",
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
2023-10-28 17:38:25 +02:00
|
|
|
// ParseConfiguration parses the mesh configuration
|
2023-10-01 20:01:35 +02:00
|
|
|
func ParseConfiguration(filePath string) (*WgMeshConfiguration, error) {
|
|
|
|
var conf WgMeshConfiguration
|
|
|
|
|
|
|
|
yamlBytes, err := os.ReadFile(filePath)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf("Read file error: %s\n", err.Error())
|
2023-10-01 20:01:35 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
|
|
|
err = yaml.Unmarshal(yamlBytes, &conf)
|
|
|
|
|
|
|
|
if err != nil {
|
2023-10-24 01:12:38 +02:00
|
|
|
logging.Log.WriteErrorf("Unmarshal error: %s\n", err.Error())
|
2023-10-01 20:01:35 +02:00
|
|
|
return nil, err
|
|
|
|
}
|
|
|
|
|
2023-11-05 19:03:58 +01:00
|
|
|
return &conf, ValidateConfiguration(&conf)
|
2023-10-01 20:01:35 +02:00
|
|
|
}
|