From afbfed5cb6d0d3868029ad2f80b3ec0cb22900b8 Mon Sep 17 00:00:00 2001 From: Samuel Lorch Date: Mon, 24 Apr 2023 19:50:17 +0200 Subject: [PATCH] Add NTP Server Configuration --- internal/chrony/apply.go | 37 ++++++++++++++++++++++++++++ internal/chrony/config.go | 17 +++++++++++++ internal/chrony/template.go | 26 +++++++++++++++++++ internal/chrony/template/config.tmpl | 13 ++++++++++ main.go | 2 ++ 5 files changed, 95 insertions(+) create mode 100644 internal/chrony/apply.go create mode 100644 internal/chrony/config.go create mode 100644 internal/chrony/template.go create mode 100644 internal/chrony/template/config.tmpl diff --git a/internal/chrony/apply.go b/internal/chrony/apply.go new file mode 100644 index 0000000..1690a64 --- /dev/null +++ b/internal/chrony/apply.go @@ -0,0 +1,37 @@ +package chrony + +import ( + "context" + "fmt" + + systemctl "github.com/coreos/go-systemd/v22/dbus" + "nfsense.net/nfsense/internal/definitions/config" + "nfsense.net/nfsense/internal/util" +) + +const chronyConfigFile = "/etc/chrony.conf" + +func ApplyNTPConfiguration(currentConfig config.Config, pendingConfig config.Config) error { + + conf, err := GenerateChronyConfiguration(pendingConfig) + if err != nil { + return fmt.Errorf("Generating Chrony Configuration: %w", err) + } + + err = util.OverwriteFile(chronyConfigFile, conf) + if err != nil { + return fmt.Errorf("Writing Chrony Configuration: %w", err) + } + + conn, err := systemctl.NewSystemConnectionContext(context.Background()) + if err != nil { + return fmt.Errorf("Opening Dbus Connection: %w", err) + } + + _, err = conn.ReloadOrRestartUnitContext(context.Background(), "chronyd.service", "replace", nil) + if err != nil { + return fmt.Errorf("restarting chronyd.service: %w", err) + } + + return nil +} diff --git a/internal/chrony/config.go b/internal/chrony/config.go new file mode 100644 index 0000000..cbde8e9 --- /dev/null +++ b/internal/chrony/config.go @@ -0,0 +1,17 @@ +package chrony + +import ( + "bytes" + "fmt" + + "nfsense.net/nfsense/internal/definitions/config" +) + +func GenerateChronyConfiguration(conf config.Config) (string, error) { + buf := new(bytes.Buffer) + err := templates.ExecuteTemplate(buf, "config.tmpl", conf) + if err != nil { + return "", fmt.Errorf("executing server.tmpl template: %w", err) + } + return buf.String(), nil +} diff --git a/internal/chrony/template.go b/internal/chrony/template.go new file mode 100644 index 0000000..9341652 --- /dev/null +++ b/internal/chrony/template.go @@ -0,0 +1,26 @@ +package chrony + +import ( + "embed" + "text/template" + + "nfsense.net/nfsense/internal/definitions/config" +) + +//go:embed template +var templateFS embed.FS +var templates *template.Template + +func init() { + var err error + templates, err = template.New("").Funcs(template.FuncMap{ + "getInterfaceNetworkAddressCIDR": getInterfaceNetworkAddressCIDR, + }).ParseFS(templateFS, "template/*.tmpl") + if err != nil { + panic(err) + } +} + +func getInterfaceNetworkAddressCIDR(conf config.Config, name string) string { + return conf.Network.Interfaces[name].Address.Masked().String() +} diff --git a/internal/chrony/template/config.tmpl b/internal/chrony/template/config.tmpl new file mode 100644 index 0000000..78fce09 --- /dev/null +++ b/internal/chrony/template/config.tmpl @@ -0,0 +1,13 @@ +pool pool.ntp.org iburst +driftfile /var/lib/chrony/drift +makestep 1.0 3 +rtcsync +keyfile /etc/chrony.keys +ntsdumpdir /var/lib/chrony +leapsectz right/UTC +logdir /var/log/chrony + +# Allowed Networks +{{- range $i, $server := .Service.NTPServers }} +allow {{ getInterfaceNetworkAddressCIDR $ $server.Interface }} +{{- end }} \ No newline at end of file diff --git a/main.go b/main.go index f4c190a..f591495 100644 --- a/main.go +++ b/main.go @@ -16,6 +16,7 @@ import ( "nfsense.net/nfsense/internal/api/network" "nfsense.net/nfsense/internal/api/object" "nfsense.net/nfsense/internal/api/service" + "nfsense.net/nfsense/internal/chrony" "nfsense.net/nfsense/internal/config" dhcp "nfsense.net/nfsense/internal/dhcp_server" "nfsense.net/nfsense/internal/jsonrpc" @@ -104,4 +105,5 @@ func RegisterAPIMethods(apiHandler *jsonrpc.Handler, configManager *config.Confi func RegisterApplyFunctions(configManager *config.ConfigManager) { configManager.RegisterApplyFunction(networkd.ApplyNetworkdConfiguration) configManager.RegisterApplyFunction(dhcp.ApplyDHCPServerConfiguration) + configManager.RegisterApplyFunction(chrony.ApplyNTPConfiguration) }