Remove old, broken Validation

This commit is contained in:
Samuel Lorch 2023-10-09 23:37:40 +02:00
parent e12a1fe16f
commit f67ec52fae
13 changed files with 39 additions and 64 deletions

View file

@ -4,8 +4,6 @@ import (
"encoding/json"
"fmt"
"github.com/go-playground/validator/v10"
"golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions/firewall"
"nfsense.net/nfsense/internal/definitions/network"
"nfsense.net/nfsense/internal/definitions/object"
@ -16,13 +14,13 @@ import (
)
type Config struct {
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"`
Firewall firewall.Firewall `json:"firewall" validate:"required,dive"`
Object object.Object `json:"object" validate:"required,dive"`
Network network.Network `json:"network" validate:"required,dive"`
Service service.Service `json:"service" validate:"required,dive"`
VPN vpn.VPN `json:"vpn" validate:"required,dive"`
System system.System `json:"system" validate:"required,dive"`
ConfigVersion uint64 `json:"config_version"`
Firewall firewall.Firewall `json:"firewall"`
Object object.Object `json:"object"`
Network network.Network `json:"network"`
Service service.Service `json:"service"`
VPN vpn.VPN `json:"vpn"`
System system.System `json:"system"`
}
// Clone TODO find a better way to deep copy
@ -40,28 +38,5 @@ func (c *Config) Clone() *Config {
}
func ValidateConfig(conf *Config) error {
err := validation.ValidateConfig(*conf)
if err != nil {
return err
}
val := validator.New()
val.RegisterValidation("test", nilIfOtherNil)
return val.Struct(conf)
}
func nilIfOtherNil(fl validator.FieldLevel) bool {
slog.Info("Start", "field", fl.FieldName(), "param", fl.Param())
if !fl.Field().IsNil() {
slog.Info("Field is not nil", "field", fl.FieldName())
f := fl.Parent().FieldByName(fl.Param())
if f.IsZero() {
panic(fmt.Errorf("Param %v is not a Valid Field", fl.Param()))
}
if !f.IsNil() {
slog.Info("Fail", "field", fl.FieldName(), "param", fl.Param())
return false
}
}
slog.Info("Success", "field", fl.FieldName(), "param", fl.Param())
return true
return validation.ValidateConfig(*conf)
}

View file

@ -1,7 +1,7 @@
package firewall
type Firewall struct {
ForwardRules []ForwardRule `json:"forward_rules" validate:"required,dive"`
DestinationNATRules []DestinationNATRule `json:"destination_nat_rules" validate:"required,dive"`
SourceNATRules []SourceNATRule `json:"source_nat_rules" validate:"required,dive"`
ForwardRules []ForwardRule `json:"forward_rules"`
DestinationNATRules []DestinationNATRule `json:"destination_nat_rules"`
SourceNATRules []SourceNATRule `json:"source_nat_rules"`
}

View file

@ -3,15 +3,15 @@ package firewall
import "encoding/json"
type Rule struct {
Name string `json:"name" validate:"required"`
Match Match `json:"match" validate:"required,dive"`
Name string `json:"name"`
Match Match `json:"match"`
Comment string `json:"comment,omitempty"`
Counter bool `json:"counter,omitempty"`
}
type ForwardRule struct {
Rule
Verdict Verdict `json:"verdict" validate:"min=0,max=2"`
Verdict Verdict `json:"verdict"`
}
type Verdict int

View file

@ -4,7 +4,7 @@ import "encoding/json"
type SourceNATRule struct {
Rule
Type SnatType `json:"type" validate:"min=0,max=1"`
Type SnatType `json:"type"`
Address *string `json:"address,omitempty"`
Service *string `json:"service,omitempty"`
}

View file

@ -6,10 +6,10 @@ import (
)
type Interface struct {
Alias string `json:"alias,omitempty" validate:"min=0,max=3"`
Type InterfaceType `json:"type" validate:"min=0,max=3"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"`
Address *netip.Prefix `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"`
Alias string `json:"alias,omitempty"`
Type InterfaceType `json:"type"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode"`
Address *netip.Prefix `json:"address,omitempty"`
HardwareDevice *string `json:"hardware_device,omitempty"`
// TODO fix Validator for int pointers with min=0,max=4094
VlanID *uint `json:"vlan_id,omitempty"`

View file

@ -1,6 +1,6 @@
package network
type Network struct {
Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"`
StaticRoutes []StaticRoute `json:"static_routes" validate:"required,dive"`
Interfaces map[string]Interface `json:"interfaces"`
StaticRoutes []StaticRoute `json:"static_routes"`
}

View file

@ -8,11 +8,11 @@ import (
)
type Address struct {
Type AddressType `json:"type" validate:"min=0,max=3"`
Type AddressType `json:"type"`
Comment string `json:"comment,omitempty"`
Host *netip.Addr `json:"host,omitempty" validate:"excluded_unless=Type 0"`
Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"`
NetworkAddress *netip.Prefix `json:"network,omitempty" validate:"excluded_unless=Type 2"`
Host *netip.Addr `json:"host,omitempty"`
Range *netipx.IPRange `json:"range,omitempty"`
NetworkAddress *netip.Prefix `json:"network,omitempty"`
Children *[]string `json:"children,omitempty"`
}

View file

@ -1,6 +1,6 @@
package object
type Object struct {
Addresses map[string]Address `json:"addresses" validate:"required,dive"`
Services map[string]Service `json:"services" validate:"required,dive"`
Addresses map[string]Address `json:"addresses"`
Services map[string]Service `json:"services"`
}

View file

@ -6,13 +6,13 @@ import (
)
type Service struct {
Type ServiceType `json:"type" validate:"min=0,max=3"`
Type ServiceType `json:"type"`
Comment string `json:"comment,omitempty"`
SPortStart *uint32 `json:"sport_start,omitempty" validate:"excluded_unless=Type 0|excluded_unless=Type 1"`
SPortStart *uint32 `json:"sport_start,omitempty"`
SPortEnd *uint32 `json:"sport_end,omitempty"`
DPortStart *uint32 `json:"dport_start,omitempty" validate:"excluded_unless=Type 0|excluded_unless=Type 1"`
DPortStart *uint32 `json:"dport_start,omitempty"`
DPortEnd *uint32 `json:"dport_end,omitempty"`
ICMPCode *uint32 `json:"icmp_code,omitempty" validate:"excluded_unless=Type 2"`
ICMPCode *uint32 `json:"icmp_code,omitempty"`
Children *[]string `json:"children,omitempty"`
}

View file

@ -1,8 +1,8 @@
package service
type Service struct {
DHCPv4Servers []DHCPv4Server `json:"dhcp_v4_servers" validate:"required,dive"`
DHCPv6Servers []DHCPv6Server `json:"dhcp_v6_servers" validate:"required,dive"`
DNSServers []DNSServer `json:"dns_servers" validate:"required,dive"`
NTPServers []NTPServer `json:"ntp_servers" validate:"required,dive"`
DHCPv4Servers []DHCPv4Server `json:"dhcp_v4_servers"`
DHCPv6Servers []DHCPv6Server `json:"dhcp_v6_servers"`
DNSServers []DNSServer `json:"dns_servers"`
NTPServers []NTPServer `json:"ntp_servers"`
}

View file

@ -1,5 +1,5 @@
package system
type System struct {
Users map[string]User `json:"users" validate:"required,dive"`
Users map[string]User `json:"users"`
}

View file

@ -1,5 +1,5 @@
package vpn
type VPN struct {
Wireguard Wireguard `json:"wireguard" validate:"required,dive"`
Wireguard Wireguard `json:"wireguard"`
}

View file

@ -1,6 +1,6 @@
package vpn
type Wireguard struct {
Interfaces map[string]WireguardInterface `json:"interfaces" validate:"required,dive"`
Peers map[string]WireguardPeer `json:"peers" validate:"required,dive"`
Interfaces map[string]WireguardInterface `json:"interfaces"`
Peers map[string]WireguardPeer `json:"peers"`
}