2022-10-26 21:21:31 +00:00
|
|
|
package daemon
|
2021-04-20 21:31:37 +00:00
|
|
|
|
2024-06-17 18:51:02 +00:00
|
|
|
import (
|
|
|
|
"fmt"
|
|
|
|
"io"
|
|
|
|
"isle/yamlutil"
|
|
|
|
"os"
|
|
|
|
"path/filepath"
|
|
|
|
"strconv"
|
|
|
|
|
|
|
|
"github.com/imdario/mergo"
|
|
|
|
"gopkg.in/yaml.v3"
|
|
|
|
)
|
|
|
|
|
|
|
|
func defaultConfigPath(appDirPath string) string {
|
|
|
|
return filepath.Join(appDirPath, "etc", "daemon.yml")
|
|
|
|
}
|
2022-10-26 19:47:39 +00:00
|
|
|
|
2023-09-01 14:45:21 +00:00
|
|
|
type ConfigTun struct {
|
|
|
|
Device string `yaml:"device"`
|
|
|
|
}
|
|
|
|
|
2021-04-20 21:31:37 +00:00
|
|
|
type ConfigFirewall struct {
|
|
|
|
Conntrack ConfigConntrack `yaml:"conntrack"`
|
|
|
|
Outbound []ConfigFirewallRule `yaml:"outbound"`
|
|
|
|
Inbound []ConfigFirewallRule `yaml:"inbound"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigConntrack struct {
|
|
|
|
TCPTimeout string `yaml:"tcp_timeout"`
|
|
|
|
UDPTimeout string `yaml:"udp_timeout"`
|
|
|
|
DefaultTimeout string `yaml:"default_timeout"`
|
|
|
|
MaxConnections int `yaml:"max_connections"`
|
|
|
|
}
|
|
|
|
|
|
|
|
type ConfigFirewallRule struct {
|
|
|
|
Port string `yaml:"port,omitempty"`
|
|
|
|
Code string `yaml:"code,omitempty"`
|
|
|
|
Proto string `yaml:"proto,omitempty"`
|
|
|
|
Host string `yaml:"host,omitempty"`
|
|
|
|
Group string `yaml:"group,omitempty"`
|
|
|
|
Groups []string `yaml:"groups,omitempty"`
|
|
|
|
CIDR string `yaml:"cidr,omitempty"`
|
|
|
|
CASha string `yaml:"ca_sha,omitempty"`
|
|
|
|
CAName string `yaml:"ca_name,omitempty"`
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
// ConfigStorageAllocation describes the structure of each storage allocation
|
|
|
|
// within the daemon config file.
|
|
|
|
type ConfigStorageAllocation struct {
|
2022-10-15 14:28:03 +00:00
|
|
|
DataPath string `yaml:"data_path"`
|
|
|
|
MetaPath string `yaml:"meta_path"`
|
|
|
|
Capacity int `yaml:"capacity"`
|
2022-10-16 19:12:33 +00:00
|
|
|
S3APIPort int `yaml:"s3_api_port"`
|
2022-10-15 14:28:03 +00:00
|
|
|
RPCPort int `yaml:"rpc_port"`
|
2022-10-16 19:22:58 +00:00
|
|
|
AdminPort int `yaml:"admin_port"`
|
2022-11-13 19:09:03 +00:00
|
|
|
|
|
|
|
// Zone is a secret option which makes it easier to test garage bugs, but
|
|
|
|
// which we don't want users to otherwise know about.
|
|
|
|
Zone string `yaml:"zone"`
|
2021-04-20 21:31:37 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
// Config describes the structure of the daemon config file.
|
|
|
|
type Config struct {
|
2021-04-20 21:31:37 +00:00
|
|
|
DNS struct {
|
|
|
|
Resolvers []string `yaml:"resolvers"`
|
|
|
|
} `yaml:"dns"`
|
|
|
|
VPN struct {
|
|
|
|
PublicAddr string `yaml:"public_addr"`
|
|
|
|
Firewall ConfigFirewall `yaml:"firewall"`
|
2023-09-01 14:45:21 +00:00
|
|
|
Tun ConfigTun `yaml:"tun"`
|
2021-04-20 21:31:37 +00:00
|
|
|
} `yaml:"vpn"`
|
|
|
|
Storage struct {
|
2022-10-26 21:21:31 +00:00
|
|
|
Allocations []ConfigStorageAllocation
|
2021-04-20 21:31:37 +00:00
|
|
|
} `yaml:"storage"`
|
|
|
|
}
|
2022-10-26 19:47:39 +00:00
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
func (c *Config) fillDefaults() {
|
2022-10-26 19:47:39 +00:00
|
|
|
|
|
|
|
var firewallGarageInbound []ConfigFirewallRule
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
for i := range c.Storage.Allocations {
|
|
|
|
if c.Storage.Allocations[i].RPCPort == 0 {
|
|
|
|
c.Storage.Allocations[i].RPCPort = 3900 + (i * 10)
|
2022-10-26 19:47:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
if c.Storage.Allocations[i].S3APIPort == 0 {
|
|
|
|
c.Storage.Allocations[i].S3APIPort = 3901 + (i * 10)
|
2022-10-26 19:47:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
if c.Storage.Allocations[i].AdminPort == 0 {
|
|
|
|
c.Storage.Allocations[i].AdminPort = 3902 + (i * 10)
|
2022-10-26 19:47:39 +00:00
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
alloc := c.Storage.Allocations[i]
|
2022-10-26 19:47:39 +00:00
|
|
|
|
|
|
|
firewallGarageInbound = append(
|
|
|
|
firewallGarageInbound,
|
|
|
|
ConfigFirewallRule{
|
|
|
|
Port: strconv.Itoa(alloc.S3APIPort),
|
|
|
|
Proto: "tcp",
|
|
|
|
Host: "any",
|
|
|
|
},
|
|
|
|
ConfigFirewallRule{
|
|
|
|
Port: strconv.Itoa(alloc.RPCPort),
|
|
|
|
Proto: "tcp",
|
|
|
|
Host: "any",
|
|
|
|
},
|
|
|
|
)
|
|
|
|
}
|
|
|
|
|
2022-10-26 21:21:31 +00:00
|
|
|
c.VPN.Firewall.Inbound = append(
|
|
|
|
c.VPN.Firewall.Inbound,
|
2022-10-26 19:47:39 +00:00
|
|
|
firewallGarageInbound...,
|
|
|
|
)
|
|
|
|
}
|
2024-06-17 18:51:02 +00:00
|
|
|
|
|
|
|
// CopyDefaultConfig copies the daemon config file embedded in the AppDir into
|
|
|
|
// the given io.Writer.
|
|
|
|
func CopyDefaultConfig(into io.Writer, appDirPath string) error {
|
|
|
|
|
|
|
|
defaultConfigPath := defaultConfigPath(appDirPath)
|
|
|
|
|
|
|
|
f, err := os.Open(defaultConfigPath)
|
|
|
|
if err != nil {
|
|
|
|
return fmt.Errorf("opening daemon config at %q: %w", defaultConfigPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
defer f.Close()
|
|
|
|
|
|
|
|
if _, err := io.Copy(into, f); err != nil {
|
|
|
|
return fmt.Errorf("copying daemon config from %q: %w", defaultConfigPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
return nil
|
|
|
|
}
|
|
|
|
|
|
|
|
// LoadConfig loads the daemon config from userConfigPath, merges it with
|
|
|
|
// the default found in the appDirPath, and returns the result.
|
|
|
|
//
|
|
|
|
// If userConfigPath is not given then the default is loaded and returned.
|
|
|
|
func LoadConfig(
|
|
|
|
appDirPath, userConfigPath string,
|
|
|
|
) (
|
|
|
|
Config, error,
|
|
|
|
) {
|
|
|
|
|
|
|
|
defaultConfigPath := defaultConfigPath(appDirPath)
|
|
|
|
|
|
|
|
var fullDaemon map[string]interface{}
|
|
|
|
|
|
|
|
if err := yamlutil.LoadYamlFile(&fullDaemon, defaultConfigPath); err != nil {
|
|
|
|
return Config{}, fmt.Errorf("parsing default daemon config file: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
if userConfigPath != "" {
|
|
|
|
|
|
|
|
var daemonConfig map[string]interface{}
|
|
|
|
if err := yamlutil.LoadYamlFile(&daemonConfig, userConfigPath); err != nil {
|
|
|
|
return Config{}, fmt.Errorf("parsing %q: %w", userConfigPath, err)
|
|
|
|
}
|
|
|
|
|
|
|
|
err := mergo.Merge(&fullDaemon, daemonConfig, mergo.WithOverride)
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("merging contents of file %q: %w", userConfigPath, err)
|
|
|
|
}
|
|
|
|
}
|
|
|
|
|
|
|
|
fullDaemonB, err := yaml.Marshal(fullDaemon)
|
|
|
|
|
|
|
|
if err != nil {
|
|
|
|
return Config{}, fmt.Errorf("yaml marshaling: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
var config Config
|
|
|
|
if err := yaml.Unmarshal(fullDaemonB, &config); err != nil {
|
|
|
|
return Config{}, fmt.Errorf("yaml unmarshaling back into Config struct: %w", err)
|
|
|
|
}
|
|
|
|
|
|
|
|
config.fillDefaults()
|
|
|
|
|
|
|
|
return config, nil
|
|
|
|
}
|