Seperate Definitions by Sub System

This commit is contained in:
Samuel Lorch 2023-04-13 17:03:11 +02:00
parent 14c8da64cc
commit 72bf96295d
31 changed files with 120 additions and 108 deletions

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/firewall"
) )
type GetDestinationNATRulesResult struct { type GetDestinationNATRulesResult struct {
DestinationNATRules []definitions.DestinationNATRule `json:"destination_nat_rules"` DestinationNATRules []firewall.DestinationNATRule `json:"destination_nat_rules"`
} }
func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{}) (GetDestinationNATRulesResult, error) { func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{}) (GetDestinationNATRulesResult, error) {
@ -18,7 +18,7 @@ func (f *Firewall) GetDestinationNATRules(ctx context.Context, params struct{})
} }
type CreateDestinationNATRuleParameters struct { type CreateDestinationNATRuleParameters struct {
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"` DestinationNATRule firewall.DestinationNATRule `json:"destination_nat_rule"`
} }
func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDestinationNATRuleParameters) (struct{}, error) { func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDestinationNATRuleParameters) (struct{}, error) {
@ -30,8 +30,8 @@ func (f *Firewall) CreateDestinationNATRule(ctx context.Context, params CreateDe
} }
type UpdateDestinationNATRuleParameters struct { type UpdateDestinationNATRuleParameters struct {
Index uint64 `json:"index"` Index uint64 `json:"index"`
DestinationNATRule definitions.DestinationNATRule `json:"destination_nat_rule"` DestinationNATRule firewall.DestinationNATRule `json:"destination_nat_rule"`
} }
func (f *Firewall) UpdateDestinationNATRule(ctx context.Context, params UpdateDestinationNATRuleParameters) (struct{}, error) { func (f *Firewall) UpdateDestinationNATRule(ctx context.Context, params UpdateDestinationNATRuleParameters) (struct{}, error) {
@ -61,7 +61,7 @@ func (f *Firewall) MoveDestinationNATRule(ctx context.Context, params MoveDestin
rule := conf.Firewall.DestinationNATRules[params.Index] rule := conf.Firewall.DestinationNATRules[params.Index]
sliceWithoutRule := append(conf.Firewall.DestinationNATRules[:params.Index], conf.Firewall.DestinationNATRules[params.Index+1:]...) sliceWithoutRule := append(conf.Firewall.DestinationNATRules[:params.Index], conf.Firewall.DestinationNATRules[params.Index+1:]...)
newSlice := make([]definitions.DestinationNATRule, params.ToIndex+1) newSlice := make([]firewall.DestinationNATRule, params.ToIndex+1)
copy(newSlice, sliceWithoutRule[:params.ToIndex]) copy(newSlice, sliceWithoutRule[:params.ToIndex])
newSlice[params.ToIndex] = rule newSlice[params.ToIndex] = rule
conf.Firewall.DestinationNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...) conf.Firewall.DestinationNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/firewall"
) )
type GetForwardRulesResult struct { type GetForwardRulesResult struct {
ForwardRules []definitions.ForwardRule `json:"forward_rules"` ForwardRules []firewall.ForwardRule `json:"forward_rules"`
} }
func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetForwardRulesResult, error) { func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetForwardRulesResult, error) {
@ -18,7 +18,7 @@ func (f *Firewall) GetForwardRules(ctx context.Context, params struct{}) (GetFor
} }
type CreateForwardRuleParameters struct { type CreateForwardRuleParameters struct {
ForwardRule definitions.ForwardRule `json:"forward_rule"` ForwardRule firewall.ForwardRule `json:"forward_rule"`
} }
func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRuleParameters) (struct{}, error) { func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRuleParameters) (struct{}, error) {
@ -30,8 +30,8 @@ func (f *Firewall) CreateForwardRule(ctx context.Context, params CreateForwardRu
} }
type UpdateForwardRuleParameters struct { type UpdateForwardRuleParameters struct {
Index uint64 `json:"index"` Index uint64 `json:"index"`
ForwardRule definitions.ForwardRule `json:"forward_rule"` ForwardRule firewall.ForwardRule `json:"forward_rule"`
} }
func (f *Firewall) UpdateForwardRule(ctx context.Context, params UpdateForwardRuleParameters) (struct{}, error) { func (f *Firewall) UpdateForwardRule(ctx context.Context, params UpdateForwardRuleParameters) (struct{}, error) {
@ -61,7 +61,7 @@ func (f *Firewall) MoveForwardRule(ctx context.Context, params MoveForwardRulePa
rule := conf.Firewall.ForwardRules[params.Index] rule := conf.Firewall.ForwardRules[params.Index]
sliceWithoutRule := append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...) sliceWithoutRule := append(conf.Firewall.ForwardRules[:params.Index], conf.Firewall.ForwardRules[params.Index+1:]...)
newSlice := make([]definitions.ForwardRule, params.ToIndex+1) newSlice := make([]firewall.ForwardRule, params.ToIndex+1)
copy(newSlice, sliceWithoutRule[:params.ToIndex]) copy(newSlice, sliceWithoutRule[:params.ToIndex])
newSlice[params.ToIndex] = rule newSlice[params.ToIndex] = rule
conf.Firewall.ForwardRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...) conf.Firewall.ForwardRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/firewall"
) )
type GetSourceNATRulesResult struct { type GetSourceNATRulesResult struct {
SourceNATRules []definitions.SourceNATRule `json:"source_nat_rules"` SourceNATRules []firewall.SourceNATRule `json:"source_nat_rules"`
} }
func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetSourceNATRulesResult, error) { func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetSourceNATRulesResult, error) {
@ -18,7 +18,7 @@ func (f *Firewall) GetSourceNATRules(ctx context.Context, params struct{}) (GetS
} }
type CreateSourceNATRuleParameters struct { type CreateSourceNATRuleParameters struct {
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"` SourceNATRule firewall.SourceNATRule `json:"source_nat_rule"`
} }
func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceNATRuleParameters) (struct{}, error) { func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceNATRuleParameters) (struct{}, error) {
@ -30,8 +30,8 @@ func (f *Firewall) CreateSourceNATRule(ctx context.Context, params CreateSourceN
} }
type UpdateSourceNATRuleParameters struct { type UpdateSourceNATRuleParameters struct {
Index uint64 `json:"index"` Index uint64 `json:"index"`
SourceNATRule definitions.SourceNATRule `json:"source_nat_rule"` SourceNATRule firewall.SourceNATRule `json:"source_nat_rule"`
} }
func (f *Firewall) UpdateSourceNATRule(ctx context.Context, params UpdateSourceNATRuleParameters) (struct{}, error) { func (f *Firewall) UpdateSourceNATRule(ctx context.Context, params UpdateSourceNATRuleParameters) (struct{}, error) {
@ -61,7 +61,7 @@ func (f *Firewall) MoveSourceNATRule(ctx context.Context, params MoveSourceNATRu
rule := conf.Firewall.SourceNATRules[params.Index] rule := conf.Firewall.SourceNATRules[params.Index]
sliceWithoutRule := append(conf.Firewall.SourceNATRules[:params.Index], conf.Firewall.SourceNATRules[params.Index+1:]...) sliceWithoutRule := append(conf.Firewall.SourceNATRules[:params.Index], conf.Firewall.SourceNATRules[params.Index+1:]...)
newSlice := make([]definitions.SourceNATRule, params.ToIndex+1) newSlice := make([]firewall.SourceNATRule, params.ToIndex+1)
copy(newSlice, sliceWithoutRule[:params.ToIndex]) copy(newSlice, sliceWithoutRule[:params.ToIndex])
newSlice[params.ToIndex] = rule newSlice[params.ToIndex] = rule
conf.Firewall.SourceNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...) conf.Firewall.SourceNATRules = append(newSlice, sliceWithoutRule[params.ToIndex:]...)

View file

@ -4,7 +4,7 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/network"
"nfsense.net/nfsense/internal/networkd/dbus" "nfsense.net/nfsense/internal/networkd/dbus"
) )
@ -28,7 +28,7 @@ type GetInterfaceParameters struct {
type GetInterfaceResult struct { type GetInterfaceResult struct {
Name string `json:"name"` Name string `json:"name"`
definitions.Interface network.Interface
} }
func (f *Network) GetInterface(ctx context.Context, params GetInterfaceParameters) (GetInterfaceResult, error) { func (f *Network) GetInterface(ctx context.Context, params GetInterfaceParameters) (GetInterfaceResult, error) {
@ -44,7 +44,7 @@ func (f *Network) GetInterface(ctx context.Context, params GetInterfaceParameter
} }
type GetInterfacesResult struct { type GetInterfacesResult struct {
Interfaces map[string]definitions.Interface Interfaces map[string]network.Interface
} }
func (f *Network) GetInterfaces(ctx context.Context, params struct{}) (GetInterfacesResult, error) { func (f *Network) GetInterfaces(ctx context.Context, params struct{}) (GetInterfacesResult, error) {
@ -55,7 +55,7 @@ func (f *Network) GetInterfaces(ctx context.Context, params struct{}) (GetInterf
type CreateInterfaceParameters struct { type CreateInterfaceParameters struct {
Name string `json:"name"` Name string `json:"name"`
definitions.Interface network.Interface
} }
func (f *Network) CreateInterface(ctx context.Context, params CreateInterfaceParameters) (struct{}, error) { func (f *Network) CreateInterface(ctx context.Context, params CreateInterfaceParameters) (struct{}, error) {
@ -73,7 +73,7 @@ func (f *Network) CreateInterface(ctx context.Context, params CreateInterfacePar
type UpdateInterfaceParameters struct { type UpdateInterfaceParameters struct {
Name string Name string
definitions.Interface network.Interface
} }
func (f *Network) UpdateInterface(ctx context.Context, params UpdateInterfaceParameters) (struct{}, error) { func (f *Network) UpdateInterface(ctx context.Context, params UpdateInterfaceParameters) (struct{}, error) {

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/network"
) )
type GetStaticRoutesResult struct { type GetStaticRoutesResult struct {
StaticRoutes []definitions.StaticRoute StaticRoutes []network.StaticRoute
} }
func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStaticRoutesResult, error) { func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStaticRoutesResult, error) {
@ -17,7 +17,7 @@ func (f *Network) GetStaticRoutes(ctx context.Context, params struct{}) (GetStat
}, nil }, nil
} }
func (f *Network) CreateStaticRoute(ctx context.Context, params definitions.StaticRoute) (struct{}, error) { func (f *Network) CreateStaticRoute(ctx context.Context, params network.StaticRoute) (struct{}, error) {
t, conf := f.ConfigManager.StartTransaction() t, conf := f.ConfigManager.StartTransaction()
defer t.Discard() defer t.Discard()
@ -27,7 +27,7 @@ func (f *Network) CreateStaticRoute(ctx context.Context, params definitions.Stat
type UpdateStaticRouteParameters struct { type UpdateStaticRouteParameters struct {
Index uint Index uint
definitions.StaticRoute network.StaticRoute
} }
func (f *Network) UpdateStaticRoute(ctx context.Context, params UpdateStaticRouteParameters) (struct{}, error) { func (f *Network) UpdateStaticRoute(ctx context.Context, params UpdateStaticRouteParameters) (struct{}, error) {

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/object"
) )
type GetAddressesResult struct { type GetAddressesResult struct {
Addresses map[string]definitions.Address Addresses map[string]object.Address
} }
func (f *Object) GetAddresses(ctx context.Context, params struct{}) (GetAddressesResult, error) { func (f *Object) GetAddresses(ctx context.Context, params struct{}) (GetAddressesResult, error) {
@ -19,7 +19,7 @@ func (f *Object) GetAddresses(ctx context.Context, params struct{}) (GetAddresse
type CreateAddressParameters struct { type CreateAddressParameters struct {
Name string Name string
Address definitions.Address Address object.Address
} }
func (f *Object) CreateAddress(ctx context.Context, params CreateAddressParameters) (struct{}, error) { func (f *Object) CreateAddress(ctx context.Context, params CreateAddressParameters) (struct{}, error) {
@ -37,7 +37,7 @@ func (f *Object) CreateAddress(ctx context.Context, params CreateAddressParamete
type UpdateAddressParameters struct { type UpdateAddressParameters struct {
Name string Name string
Address definitions.Address Address object.Address
} }
func (f *Object) UpdateAddress(ctx context.Context, params UpdateAddressParameters) (struct{}, error) { func (f *Object) UpdateAddress(ctx context.Context, params UpdateAddressParameters) (struct{}, error) {

View file

@ -4,11 +4,11 @@ import (
"context" "context"
"fmt" "fmt"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/object"
) )
type GetServicesResult struct { type GetServicesResult struct {
Services map[string]definitions.Service Services map[string]object.Service
} }
func (f *Object) GetServices(ctx context.Context, params struct{}) (GetServicesResult, error) { func (f *Object) GetServices(ctx context.Context, params struct{}) (GetServicesResult, error) {
@ -19,7 +19,7 @@ func (f *Object) GetServices(ctx context.Context, params struct{}) (GetServicesR
type CreateServiceParameters struct { type CreateServiceParameters struct {
Name string Name string
Service definitions.Service Service object.Service
} }
func (f *Object) CreateService(ctx context.Context, params CreateServiceParameters) (struct{}, error) { func (f *Object) CreateService(ctx context.Context, params CreateServiceParameters) (struct{}, error) {
@ -37,7 +37,7 @@ func (f *Object) CreateService(ctx context.Context, params CreateServiceParamete
type UpdateServiceParameters struct { type UpdateServiceParameters struct {
Name string Name string
Service definitions.Service Service object.Service
} }
func (f *Object) UpdateService(ctx context.Context, params UpdateServiceParameters) (struct{}, error) { func (f *Object) UpdateService(ctx context.Context, params UpdateServiceParameters) (struct{}, error) {

View file

@ -5,7 +5,7 @@ import (
"os" "os"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
// ApplyPendingChanges Takes all pending Changes and Tries to Apply them using the Registered Apply Functions. // ApplyPendingChanges Takes all pending Changes and Tries to Apply them using the Registered Apply Functions.
@ -47,6 +47,6 @@ func revertToCurrent(m *ConfigManager) error {
return nil return nil
} }
func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig definitions.Config, pendingConfig definitions.Config) error) { func (m *ConfigManager) RegisterApplyFunction(fn func(currentConfig config.Config, pendingConfig config.Config) error) {
m.applyFunctions = append(m.applyFunctions, fn) m.applyFunctions = append(m.applyFunctions, fn)
} }

View file

@ -1,11 +1,13 @@
package config package config
import "nfsense.net/nfsense/internal/definitions" import (
"nfsense.net/nfsense/internal/definitions/config"
)
func (m *ConfigManager) GetCurrentConfig() definitions.Config { func (m *ConfigManager) GetCurrentConfig() config.Config {
return *m.currentConfig.Clone() return *m.currentConfig.Clone()
} }
func (m *ConfigManager) GetPendingConfig() definitions.Config { func (m *ConfigManager) GetPendingConfig() config.Config {
return *m.pendingConfig.Clone() return *m.pendingConfig.Clone()
} }

View file

@ -5,11 +5,11 @@ import (
"fmt" "fmt"
"os" "os"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
func (m *ConfigManager) LoadCurrentConfigFromDisk() error { func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
var config definitions.Config var conf config.Config
configFile, err := os.Open(m.currentConfigFilePath) configFile, err := os.Open(m.currentConfigFilePath)
if err != nil { if err != nil {
return fmt.Errorf("opening Config File %w", err) return fmt.Errorf("opening Config File %w", err)
@ -18,22 +18,22 @@ func (m *ConfigManager) LoadCurrentConfigFromDisk() error {
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
jsonParser.DisallowUnknownFields() jsonParser.DisallowUnknownFields()
err = jsonParser.Decode(&config) err = jsonParser.Decode(&conf)
if err != nil { if err != nil {
return fmt.Errorf("decoding Config File %w", err) return fmt.Errorf("decoding Config File %w", err)
} }
err = definitions.ValidateConfig(&config) err = config.ValidateConfig(&conf)
if err != nil { if err != nil {
return fmt.Errorf("validating Config: %w", err) return fmt.Errorf("validating Config: %w", err)
} }
m.currentConfig = &config m.currentConfig = &conf
return nil return nil
} }
func (m *ConfigManager) LoadPendingConfigFromDisk() error { func (m *ConfigManager) LoadPendingConfigFromDisk() error {
var config definitions.Config var conf config.Config
configFile, err := os.Open(m.pendingConfigFilePath) configFile, err := os.Open(m.pendingConfigFilePath)
if err != nil { if err != nil {
return fmt.Errorf("opening Config File %w", err) return fmt.Errorf("opening Config File %w", err)
@ -42,16 +42,16 @@ func (m *ConfigManager) LoadPendingConfigFromDisk() error {
jsonParser := json.NewDecoder(configFile) jsonParser := json.NewDecoder(configFile)
jsonParser.DisallowUnknownFields() jsonParser.DisallowUnknownFields()
err = jsonParser.Decode(&config) err = jsonParser.Decode(&conf)
if err != nil { if err != nil {
return fmt.Errorf("decoding Config File %w", err) return fmt.Errorf("decoding Config File %w", err)
} }
err = definitions.ValidateConfig(&config) err = config.ValidateConfig(&conf)
if err != nil { if err != nil {
return fmt.Errorf("validating Config: %w", err) return fmt.Errorf("validating Config: %w", err)
} }
m.pendingConfig = &config m.pendingConfig = &conf
return nil return nil
} }

View file

@ -3,27 +3,27 @@ package config
import ( import (
"sync" "sync"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
type ConfigManager struct { type ConfigManager struct {
currentConfigFilePath string currentConfigFilePath string
pendingConfigFilePath string pendingConfigFilePath string
currentConfig *definitions.Config currentConfig *config.Config
pendingConfig *definitions.Config pendingConfig *config.Config
transactionMutex sync.Mutex transactionMutex sync.Mutex
applyFunctions []func(currentConfig definitions.Config, pendingConfig definitions.Config) error applyFunctions []func(currentConfig config.Config, pendingConfig config.Config) error
} }
func CreateConfigManager() *ConfigManager { func CreateConfigManager() *ConfigManager {
manager := ConfigManager{ manager := ConfigManager{
currentConfigFilePath: "config.json", currentConfigFilePath: "config.json",
pendingConfigFilePath: "pending.json", pendingConfigFilePath: "pending.json",
currentConfig: &definitions.Config{}, currentConfig: &config.Config{},
pendingConfig: &definitions.Config{}, pendingConfig: &config.Config{},
} }
return &manager return &manager
} }

View file

@ -5,10 +5,10 @@ import (
"fmt" "fmt"
"os" "os"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
func (m *ConfigManager) saveConfig(path string, conf *definitions.Config) error { func (m *ConfigManager) saveConfig(path string, conf *config.Config) error {
data, err := json.MarshalIndent(conf, "", " ") data, err := json.MarshalIndent(conf, "", " ")
if err != nil { if err != nil {
return fmt.Errorf("Marshal Config: %w", err) return fmt.Errorf("Marshal Config: %w", err)

View file

@ -4,17 +4,17 @@ import (
"fmt" "fmt"
"sync" "sync"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
type ConfigTransaction struct { type ConfigTransaction struct {
finished bool finished bool
mutex sync.Mutex mutex sync.Mutex
configManager *ConfigManager configManager *ConfigManager
changes *definitions.Config changes *config.Config
} }
func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *definitions.Config) { func (m *ConfigManager) StartTransaction() (*ConfigTransaction, *config.Config) {
m.transactionMutex.Lock() m.transactionMutex.Lock()
confCopy := m.pendingConfig.Clone() confCopy := m.pendingConfig.Clone()
return &ConfigTransaction{ return &ConfigTransaction{
@ -34,7 +34,7 @@ func (t *ConfigTransaction) Commit() error {
t.finished = true t.finished = true
defer t.configManager.transactionMutex.Unlock() defer t.configManager.transactionMutex.Unlock()
err := definitions.ValidateConfig(t.changes) err := config.ValidateConfig(t.changes)
if err != nil { if err != nil {
return fmt.Errorf("validating Config before Apply: %w", err) return fmt.Errorf("validating Config before Apply: %w", err)
} }

View file

@ -1,4 +1,4 @@
package definitions package common
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package definitions package common
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package definitions package common
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,4 +1,4 @@
package definitions package config
import ( import (
"encoding/json" "encoding/json"
@ -6,13 +6,16 @@ import (
"github.com/go-playground/validator/v10" "github.com/go-playground/validator/v10"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions/firewall"
"nfsense.net/nfsense/internal/definitions/network"
"nfsense.net/nfsense/internal/definitions/object"
) )
type Config struct { type Config struct {
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"` ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"`
Firewall Firewall `json:"firewall" validate:"required,dive"` Firewall firewall.Firewall `json:"firewall" validate:"required,dive"`
Object Object `json:"object" validate:"required,dive"` Object object.Object `json:"object" validate:"required,dive"`
Network Network `json:"network" validate:"required,dive"` Network network.Network `json:"network" validate:"required,dive"`
} }
// Clone TODO find a better way to deep copy // Clone TODO find a better way to deep copy

View file

@ -1,4 +1,4 @@
package definitions package firewall
type DestinationNATRule struct { type DestinationNATRule struct {
Rule Rule

View file

@ -1,4 +1,4 @@
package definitions package firewall
type Firewall struct { type Firewall struct {
ForwardRules []ForwardRule `json:"forward_rules" validate:"required,dive"` ForwardRules []ForwardRule `json:"forward_rules" validate:"required,dive"`

View file

@ -1,4 +1,4 @@
package definitions package firewall
type Match struct { type Match struct {
TCPDestinationPort uint64 `json:"tcp_destination_port,omitempty"` TCPDestinationPort uint64 `json:"tcp_destination_port,omitempty"`

View file

@ -1,4 +1,4 @@
package definitions package firewall
import "encoding/json" import "encoding/json"

View file

@ -1,4 +1,4 @@
package definitions package firewall
import "encoding/json" import "encoding/json"

View file

@ -1,14 +1,16 @@
package definitions package network
import ( import (
"encoding/json" "encoding/json"
"nfsense.net/nfsense/internal/definitions/common"
) )
type Interface struct { type Interface struct {
Alias string `json:"alias,omitempty" validate:"min=0,max=3"` Alias string `json:"alias,omitempty" validate:"min=0,max=3"`
Type InterfaceType `json:"type" validate:"min=0,max=3"` Type InterfaceType `json:"type" validate:"min=0,max=3"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"` AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
Address *IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"` Address *common.IPCIDR `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
HardwareDevice *string `json:"hardware_device,omitempty"` HardwareDevice *string `json:"hardware_device,omitempty"`
// TODO fix Validator for int pointers with min=0,max=4094 // TODO fix Validator for int pointers with min=0,max=4094
VlanID *uint `json:"vlan_id,omitempty"` VlanID *uint `json:"vlan_id,omitempty"`

View file

@ -1,4 +1,4 @@
package definitions package network
type Network struct { type Network struct {
Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"` Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"`

View file

@ -0,0 +1,15 @@
package network
import (
"net/netip"
"nfsense.net/nfsense/internal/definitions/common"
)
type StaticRoute struct {
Name string `json:"name,omitempty"`
Interface string `json:"interface,omitempty"`
Gateway netip.Addr `json:"gateway,omitempty"`
Destination common.IPNet `json:"destination,omitempty"`
Metric uint `json:"metric,omitempty"`
}

View file

@ -1,10 +1,11 @@
package definitions package object
import ( import (
"encoding/json" "encoding/json"
"net/netip" "net/netip"
"go4.org/netipx" "go4.org/netipx"
"nfsense.net/nfsense/internal/definitions/common"
) )
type Address struct { type Address struct {
@ -12,7 +13,7 @@ type Address struct {
Comment string `json:"comment,omitempty"` Comment string `json:"comment,omitempty"`
Host *netip.Addr `json:"host,omitempty" validate:"excluded_unless=Type 0"` Host *netip.Addr `json:"host,omitempty" validate:"excluded_unless=Type 0"`
Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"` Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"`
NetworkAddress *IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"` NetworkAddress *common.IPNet `json:"network,omitempty" validate:"excluded_unless=Type 2"`
Children *[]string `json:"children,omitempty"` Children *[]string `json:"children,omitempty"`
} }

View file

@ -1,4 +1,4 @@
package definitions package object
type Object struct { type Object struct {
Addresses map[string]Address `json:"addresses" validate:"required,dive"` Addresses map[string]Address `json:"addresses" validate:"required,dive"`

View file

@ -1,4 +1,4 @@
package definitions package object
import ( import (
"encoding/json" "encoding/json"

View file

@ -1,13 +0,0 @@
package definitions
import (
"net/netip"
)
type StaticRoute struct {
Name string `json:"name,omitempty"`
Interface string `json:"interface,omitempty"`
Gateway netip.Addr `json:"gateway,omitempty"`
Destination IPNet `json:"destination,omitempty"`
Metric uint `json:"metric,omitempty"`
}

View file

@ -8,12 +8,12 @@ import (
"path/filepath" "path/filepath"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
) )
const basepath = "/etc/systemd/network" const basepath = "/etc/systemd/network"
func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig definitions.Config) error { func ApplyNetworkdConfiguration(currentConfig config.Config, pendingConfig config.Config) error {
files, err := GenerateNetworkdConfiguration(pendingConfig) files, err := GenerateNetworkdConfiguration(pendingConfig)
if err != nil { if err != nil {
return fmt.Errorf("Generating Networkd Configuration: %w", err) return fmt.Errorf("Generating Networkd Configuration: %w", err)
@ -41,6 +41,7 @@ func ApplyNetworkdConfiguration(currentConfig definitions.Config, pendingConfig
} }
} }
// TODO Use dbus instead
cmd := exec.Command("systemctl", "restart", "systemd-networkd") cmd := exec.Command("systemctl", "restart", "systemd-networkd")
var out bytes.Buffer var out bytes.Buffer

View file

@ -5,7 +5,8 @@ import (
"fmt" "fmt"
"golang.org/x/exp/slog" "golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions" "nfsense.net/nfsense/internal/definitions/config"
"nfsense.net/nfsense/internal/definitions/network"
) )
type NetworkdConfigFile struct { type NetworkdConfigFile struct {
@ -15,9 +16,9 @@ type NetworkdConfigFile struct {
type InterfaceWithName struct { type InterfaceWithName struct {
Name string Name string
definitions.Interface network.Interface
Vlans []string Vlans []string
StaticRoutes []definitions.StaticRoute StaticRoutes []network.StaticRoute
} }
type BondMembership struct { type BondMembership struct {
@ -30,12 +31,12 @@ type BridgeMembership struct {
BridgeName string BridgeName string
} }
func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFile, error) { func GenerateNetworkdConfiguration(conf config.Config) ([]NetworkdConfigFile, error) {
files := []NetworkdConfigFile{} files := []NetworkdConfigFile{}
// Step 1 Generate vlan netdev files // Step 1 Generate vlan netdev files
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
if inter.Type == definitions.Vlan { if inter.Type == network.Vlan {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "create-vlan.netdev.tmpl", InterfaceWithName{ err := templates.ExecuteTemplate(buf, "create-vlan.netdev.tmpl", InterfaceWithName{
Name: name, Name: name,
@ -53,7 +54,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
// Step 2 Generate bond netdev files // Step 2 Generate bond netdev files
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
if inter.Type == definitions.Bond { if inter.Type == network.Bond {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "create-bond.netdev.tmpl", InterfaceWithName{ err := templates.ExecuteTemplate(buf, "create-bond.netdev.tmpl", InterfaceWithName{
Name: name, Name: name,
@ -71,7 +72,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
// Step 3 Generate bridge netdev files // Step 3 Generate bridge netdev files
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
if inter.Type == definitions.Bridge { if inter.Type == network.Bridge {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "create-bridge.netdev.tmpl", InterfaceWithName{ err := templates.ExecuteTemplate(buf, "create-bridge.netdev.tmpl", InterfaceWithName{
Name: name, Name: name,
@ -89,7 +90,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
// Step 4 Generate Bond Members // Step 4 Generate Bond Members
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
if inter.Type == definitions.Bond && inter.BondMembers != nil { if inter.Type == network.Bond && inter.BondMembers != nil {
for _, member := range *inter.BondMembers { for _, member := range *inter.BondMembers {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "bond-membership.network.tmpl", BondMembership{ err := templates.ExecuteTemplate(buf, "bond-membership.network.tmpl", BondMembership{
@ -109,7 +110,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
// Step 5 Generate Bridge Members // Step 5 Generate Bridge Members
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
if inter.Type == definitions.Bridge && inter.BridgeMembers != nil { if inter.Type == network.Bridge && inter.BridgeMembers != nil {
for _, member := range *inter.BridgeMembers { for _, member := range *inter.BridgeMembers {
buf := new(bytes.Buffer) buf := new(bytes.Buffer)
err := templates.ExecuteTemplate(buf, "bridge-membership.network.tmpl", BridgeMembership{ err := templates.ExecuteTemplate(buf, "bridge-membership.network.tmpl", BridgeMembership{
@ -131,10 +132,10 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
for name, inter := range conf.Network.Interfaces { for name, inter := range conf.Network.Interfaces {
// Vlans // Vlans
vlans := []string{} vlans := []string{}
if inter.Type != definitions.Vlan { if inter.Type != network.Vlan {
vlans := []string{} vlans := []string{}
for vlanName, vlanInter := range conf.Network.Interfaces { for vlanName, vlanInter := range conf.Network.Interfaces {
if vlanInter.Type == definitions.Vlan { if vlanInter.Type == network.Vlan {
if *vlanInter.VlanParent == name { if *vlanInter.VlanParent == name {
vlans = append(vlans, vlanName) vlans = append(vlans, vlanName)
} }
@ -144,7 +145,7 @@ func GenerateNetworkdConfiguration(conf definitions.Config) ([]NetworkdConfigFil
} }
// Static Routes // Static Routes
staticRoutes := []definitions.StaticRoute{} staticRoutes := []network.StaticRoute{}
for _, route := range conf.Network.StaticRoutes { for _, route := range conf.Network.StaticRoutes {
if route.Interface == name { if route.Interface == name {
staticRoutes = append(staticRoutes, route) staticRoutes = append(staticRoutes, route)