f-86: hostsvc: relais DNS et NTP par hôte, socket de contrôle #86
Signed-off-by: GnomeZworc <nicolas.boufidjeline@g3e.fr>
This commit is contained in:
parent
ae7213a9d1
commit
d4b83bdb17
20 changed files with 2235 additions and 0 deletions
79
internal/config/agent/hostsvc.go
Normal file
79
internal/config/agent/hostsvc.go
Normal file
|
|
@ -0,0 +1,79 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"fmt"
|
||||
"net"
|
||||
"strconv"
|
||||
)
|
||||
|
||||
const (
|
||||
RateLimitPerVPC = 100
|
||||
DNSPort = 53
|
||||
NTPPort = 123
|
||||
)
|
||||
|
||||
type HostSvc struct {
|
||||
DNSUpstream []string `mapstructure:"dns_upstream"`
|
||||
NTPUpstream []string `mapstructure:"ntp_upstream"`
|
||||
RateLimitPerVPC int `mapstructure:"rate_limit_per_vpc"`
|
||||
}
|
||||
|
||||
func (h HostSvc) DNS() []string { return withPort(h.DNSUpstream, DNSPort) }
|
||||
func (h HostSvc) NTP() []string { return withPort(h.NTPUpstream, NTPPort) }
|
||||
|
||||
func (h HostSvc) Rate() int {
|
||||
if h.RateLimitPerVPC == 0 {
|
||||
return RateLimitPerVPC
|
||||
}
|
||||
return h.RateLimitPerVPC
|
||||
}
|
||||
|
||||
func withPort(servers []string, port int) []string {
|
||||
out := make([]string, 0, len(servers))
|
||||
for _, s := range servers {
|
||||
if net.ParseIP(s) != nil {
|
||||
out = append(out, net.JoinHostPort(s, strconv.Itoa(port)))
|
||||
continue
|
||||
}
|
||||
out = append(out, s)
|
||||
}
|
||||
return out
|
||||
}
|
||||
|
||||
func (h HostSvc) Validate() error {
|
||||
if h.RateLimitPerVPC <= 0 {
|
||||
return fmt.Errorf("hostsvc.rate_limit_per_vpc %d must be a positive number of requests per second", h.RateLimitPerVPC)
|
||||
}
|
||||
for _, list := range []struct {
|
||||
key string
|
||||
servers []string
|
||||
}{
|
||||
{"dns_upstream", h.DNSUpstream},
|
||||
{"ntp_upstream", h.NTPUpstream},
|
||||
} {
|
||||
for _, s := range list.servers {
|
||||
if err := validServer(s); err != nil {
|
||||
return fmt.Errorf("hostsvc.%s: %w", list.key, err)
|
||||
}
|
||||
}
|
||||
}
|
||||
return nil
|
||||
}
|
||||
|
||||
func validServer(s string) error {
|
||||
if net.ParseIP(s) != nil {
|
||||
return nil
|
||||
}
|
||||
host, port, err := net.SplitHostPort(s)
|
||||
if err != nil {
|
||||
return fmt.Errorf("%q is neither an ip nor ip:port", s)
|
||||
}
|
||||
if net.ParseIP(host) == nil {
|
||||
return fmt.Errorf("%q: %q is not an ip address", s, host)
|
||||
}
|
||||
n, err := strconv.Atoi(port)
|
||||
if err != nil || n < 1 || n > 65535 {
|
||||
return fmt.Errorf("%q: invalid port %q", s, port)
|
||||
}
|
||||
return nil
|
||||
}
|
||||
51
internal/config/agent/hostsvc_test.go
Normal file
51
internal/config/agent/hostsvc_test.go
Normal file
|
|
@ -0,0 +1,51 @@
|
|||
package configuration
|
||||
|
||||
import (
|
||||
"reflect"
|
||||
"strings"
|
||||
"testing"
|
||||
)
|
||||
|
||||
func TestLoadConfig_HostSvcDefaults(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, ""))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if len(cfg.HostSvc.DNS()) != 0 || len(cfg.HostSvc.NTP()) != 0 {
|
||||
t.Errorf("without upstreams the lists must stay empty, the host serves: %+v", cfg.HostSvc)
|
||||
}
|
||||
if cfg.HostSvc.Rate() != 100 {
|
||||
t.Errorf("default rate per vpc = %d, want 100", cfg.HostSvc.Rate())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_HostSvcDefaultPorts(t *testing.T) {
|
||||
cfg, err := LoadConfig(writeYAML(t, "hostsvc:\n dns_upstream: [192.0.2.53, \"192.0.2.54:5353\"]\n ntp_upstream: [192.0.2.123]\n rate_limit_per_vpc: 20\n"))
|
||||
if err != nil {
|
||||
t.Fatalf("LoadConfig: %v", err)
|
||||
}
|
||||
if got, want := cfg.HostSvc.DNS(), []string{"192.0.2.53:53", "192.0.2.54:5353"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("dns = %v, want %v", got, want)
|
||||
}
|
||||
if got, want := cfg.HostSvc.NTP(), []string{"192.0.2.123:123"}; !reflect.DeepEqual(got, want) {
|
||||
t.Errorf("ntp = %v, want %v", got, want)
|
||||
}
|
||||
if cfg.HostSvc.Rate() != 20 {
|
||||
t.Errorf("rate = %d, want 20", cfg.HostSvc.Rate())
|
||||
}
|
||||
}
|
||||
|
||||
func TestLoadConfig_HostSvcInvalidIsRefused(t *testing.T) {
|
||||
for _, c := range []struct{ yaml, msg string }{
|
||||
{"hostsvc:\n rate_limit_per_vpc: 0\n", "hostsvc.rate_limit_per_vpc 0 must be a positive number"},
|
||||
{"hostsvc:\n rate_limit_per_vpc: -5\n", "hostsvc.rate_limit_per_vpc -5"},
|
||||
{"hostsvc:\n dns_upstream: [dns.example]\n", `hostsvc.dns_upstream: "dns.example" is neither an ip nor ip:port`},
|
||||
{"hostsvc:\n dns_upstream: [\"dns.example:53\"]\n", `"dns.example" is not an ip address`},
|
||||
{"hostsvc:\n ntp_upstream: [\"192.0.2.1:0\"]\n", `hostsvc.ntp_upstream: "192.0.2.1:0": invalid port "0"`},
|
||||
{"hostsvc:\n ntp_upstream: [\"192.0.2.1:70000\"]\n", `invalid port "70000"`},
|
||||
} {
|
||||
if _, err := LoadConfig(writeYAML(t, c.yaml)); err == nil || !strings.Contains(err.Error(), c.msg) {
|
||||
t.Errorf("%q: error %v, want %q", c.yaml, err, c.msg)
|
||||
}
|
||||
}
|
||||
}
|
||||
|
|
@ -54,6 +54,7 @@ type Config struct {
|
|||
} `mapstructure:"watchdog"`
|
||||
Network Network `mapstructure:"network"`
|
||||
BGP BGP `mapstructure:"bgp"`
|
||||
HostSvc HostSvc `mapstructure:"hostsvc"`
|
||||
DefaultInterface string `mapstructure:"default_interface"`
|
||||
Interfaces map[string]string `mapstructure:"interfaces"`
|
||||
}
|
||||
|
|
@ -79,6 +80,9 @@ func LoadConfig(path string) (*Config, error) {
|
|||
v.SetDefault("times.poll_interval", PollInterval)
|
||||
v.SetDefault("metadata.run_dir", "/run/two/metadata")
|
||||
v.SetDefault("dhcp.backend", BackendTwo)
|
||||
v.SetDefault("hostsvc.dns_upstream", []string{})
|
||||
v.SetDefault("hostsvc.ntp_upstream", []string{})
|
||||
v.SetDefault("hostsvc.rate_limit_per_vpc", RateLimitPerVPC)
|
||||
v.SetDefault("qemu.ovmf_code_path", "/usr/share/OVMF/OVMF_CODE.fd")
|
||||
v.SetDefault("qemu.ovmf_vars_template", "/usr/share/OVMF/OVMF_VARS.fd")
|
||||
v.SetDefault("qemu.uefi_vars_dir", "/run/two/vms/uefi")
|
||||
|
|
@ -111,6 +115,9 @@ func LoadConfig(path string) (*Config, error) {
|
|||
if err := cfg.Times.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
if err := cfg.HostSvc.Validate(); err != nil {
|
||||
return nil, fmt.Errorf("%s: %w", path, err)
|
||||
}
|
||||
|
||||
return &cfg, nil
|
||||
}
|
||||
|
|
|
|||
Loading…
Add table
Add a link
Reference in a new issue