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" "encoding/json"
"fmt" "fmt"
"github.com/go-playground/validator/v10"
"golang.org/x/exp/slog"
"nfsense.net/nfsense/internal/definitions/firewall" "nfsense.net/nfsense/internal/definitions/firewall"
"nfsense.net/nfsense/internal/definitions/network" "nfsense.net/nfsense/internal/definitions/network"
"nfsense.net/nfsense/internal/definitions/object" "nfsense.net/nfsense/internal/definitions/object"
@ -16,13 +14,13 @@ import (
) )
type Config struct { type Config struct {
ConfigVersion uint64 `json:"config_version" validate:"required,eq=1"` ConfigVersion uint64 `json:"config_version"`
Firewall firewall.Firewall `json:"firewall" validate:"required,dive"` Firewall firewall.Firewall `json:"firewall"`
Object object.Object `json:"object" validate:"required,dive"` Object object.Object `json:"object"`
Network network.Network `json:"network" validate:"required,dive"` Network network.Network `json:"network"`
Service service.Service `json:"service" validate:"required,dive"` Service service.Service `json:"service"`
VPN vpn.VPN `json:"vpn" validate:"required,dive"` VPN vpn.VPN `json:"vpn"`
System system.System `json:"system" validate:"required,dive"` System system.System `json:"system"`
} }
// Clone TODO find a better way to deep copy // Clone TODO find a better way to deep copy
@ -40,28 +38,5 @@ func (c *Config) Clone() *Config {
} }
func ValidateConfig(conf *Config) error { func ValidateConfig(conf *Config) error {
err := validation.ValidateConfig(*conf) return 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
} }

View file

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

View file

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

View file

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

View file

@ -6,10 +6,10 @@ import (
) )
type Interface struct { type Interface struct {
Alias string `json:"alias,omitempty" validate:"min=0,max=3"` Alias string `json:"alias,omitempty"`
Type InterfaceType `json:"type" validate:"min=0,max=3"` Type InterfaceType `json:"type"`
AddressingMode InterfaceAddressingMode `json:"addressing_mode" validate:"min=0,max=2"` AddressingMode InterfaceAddressingMode `json:"addressing_mode"`
Address *netip.Prefix `json:"address,omitempty" validate:"excluded_unless=AddressingMode 1"` Address *netip.Prefix `json:"address,omitempty"`
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,6 +1,6 @@
package network package network
type Network struct { type Network struct {
Interfaces map[string]Interface `json:"interfaces" validate:"required,dive"` Interfaces map[string]Interface `json:"interfaces"`
StaticRoutes []StaticRoute `json:"static_routes" validate:"required,dive"` StaticRoutes []StaticRoute `json:"static_routes"`
} }

View file

@ -8,11 +8,11 @@ import (
) )
type Address struct { type Address struct {
Type AddressType `json:"type" validate:"min=0,max=3"` Type AddressType `json:"type"`
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"`
Range *netipx.IPRange `json:"range,omitempty" validate:"excluded_unless=Type 1"` Range *netipx.IPRange `json:"range,omitempty"`
NetworkAddress *netip.Prefix `json:"network,omitempty" validate:"excluded_unless=Type 2"` NetworkAddress *netip.Prefix `json:"network,omitempty"`
Children *[]string `json:"children,omitempty"` Children *[]string `json:"children,omitempty"`
} }

View file

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

View file

@ -6,13 +6,13 @@ import (
) )
type Service struct { type Service struct {
Type ServiceType `json:"type" validate:"min=0,max=3"` Type ServiceType `json:"type"`
Comment string `json:"comment,omitempty"` 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"` 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"` 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"` Children *[]string `json:"children,omitempty"`
} }

View file

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

View file

@ -1,5 +1,5 @@
package system package system
type System struct { 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 package vpn
type VPN struct { type VPN struct {
Wireguard Wireguard `json:"wireguard" validate:"required,dive"` Wireguard Wireguard `json:"wireguard"`
} }

View file

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