diff --git a/controllers/common.go b/controllers/common.go index 0f3a6e08..5d09ccb2 100644 --- a/controllers/common.go +++ b/controllers/common.go @@ -4,7 +4,6 @@ import ( "context" "fmt" "log" - "net" "time" "github.com/go-playground/validator/v10" @@ -79,61 +78,12 @@ func ValidateNodeCreate(networkName string, node models.Node) error { } func ValidateNodeUpdate(networkName string, node models.NodeUpdate) error { - v := validator.New() - _ = v.RegisterValidation("address_check", func(fl validator.FieldLevel) bool { - isIpv4 := functions.IsIpNet(node.Address) - empty := node.Address == "" - return (empty || isIpv4) - }) - _ = v.RegisterValidation("address6_check", func(fl validator.FieldLevel) bool { - isIpv6 := functions.IsIpNet(node.Address6) - empty := node.Address6 == "" - return (empty || isIpv6) - }) - _ = v.RegisterValidation("endpoint_check", func(fl validator.FieldLevel) bool { - //var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint) - isIp := functions.IsIpNet(node.Address) - empty := node.Endpoint == "" - return (empty || isIp) - }) - _ = v.RegisterValidation("localaddress_check", func(fl validator.FieldLevel) bool { - //var isFieldUnique bool = functions.IsFieldUnique(networkName, "endpoint", node.Endpoint) - isIp := functions.IsIpNet(node.LocalAddress) - empty := node.LocalAddress == "" - return (empty || isIp) - }) - _ = v.RegisterValidation("macaddress_unique", func(fl validator.FieldLevel) bool { - return true - }) - - _ = v.RegisterValidation("macaddress_valid", func(fl validator.FieldLevel) bool { - _, err := net.ParseMAC(node.MacAddress) - return err == nil - }) - - _ = v.RegisterValidation("name_valid", func(fl validator.FieldLevel) bool { - isvalid := functions.NameInNodeCharSet(node.Name) - return isvalid - }) - _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool { _, err := node.GetNetwork() return err == nil }) - _ = v.RegisterValidation("pubkey_check", func(fl validator.FieldLevel) bool { - empty := node.PublicKey == "" - isBase64 := functions.IsBase64(node.PublicKey) - return (empty || isBase64) - }) - _ = v.RegisterValidation("password_check", func(fl validator.FieldLevel) bool { - empty := node.Password == "" - goodLength := len(node.Password) > 5 - return (empty || goodLength) - }) - err := v.Struct(node) - if err != nil { for _, e := range err.(validator.ValidationErrors) { fmt.Println(e) diff --git a/controllers/common_test.go b/controllers/common_test.go index 88ae9b14..a86a0a7a 100644 --- a/controllers/common_test.go +++ b/controllers/common_test.go @@ -196,28 +196,28 @@ func TestValidateNodeUpdate(t *testing.T) { node: models.NodeUpdate{ Address: "256.0.0.1", }, - errorMessage: "Field validation for 'Address' failed on the 'address_check' tag", + errorMessage: "Field validation for 'Address' failed on the 'ip' tag", }, NodeValidationUpdateTC{ testname: "BadAddress6", node: models.NodeUpdate{ Address6: "2607::abcd:efgh::1", }, - errorMessage: "Field validation for 'Address6' failed on the 'address6_check' tag", + errorMessage: "Field validation for 'Address6' failed on the 'ipv6' tag", }, NodeValidationUpdateTC{ testname: "BadLocalAddress", node: models.NodeUpdate{ LocalAddress: "10.0.200.300", }, - errorMessage: "Field validation for 'LocalAddress' failed on the 'localaddress_check' tag", + errorMessage: "Field validation for 'LocalAddress' failed on the 'ip' tag", }, NodeValidationUpdateTC{ testname: "InvalidName", node: models.NodeUpdate{ Name: "mynode*", }, - errorMessage: "Field validation for 'Name' failed on the 'name_valid' tag", + errorMessage: "Field validation for 'Name' failed on the 'alphanum' tag", }, NodeValidationUpdateTC{ testname: "NameTooLong", @@ -243,16 +243,16 @@ func TestValidateNodeUpdate(t *testing.T) { NodeValidationUpdateTC{ testname: "PublicKeyInvalid", node: models.NodeUpdate{ - PublicKey: "", + PublicKey: "bad&key", }, - errorMessage: "Field validation for 'PublicKey' failed on the 'pubkey_check' tag", + errorMessage: "Field validation for 'PublicKey' failed on the 'base64' tag", }, NodeValidationUpdateTC{ testname: "EndpointInvalid", node: models.NodeUpdate{ Endpoint: "10.2.0.300", }, - errorMessage: "Field validation for 'Endpoint' failed on the 'endpoint_check' tag", + errorMessage: "Field validation for 'Endpoint' failed on the 'ip' tag", }, NodeValidationUpdateTC{ testname: "PersistentKeepaliveMax", @@ -266,7 +266,7 @@ func TestValidateNodeUpdate(t *testing.T) { node: models.NodeUpdate{ MacAddress: "01:02:03:04:05", }, - errorMessage: "Field validation for 'MacAddress' failed on the 'macaddress_valid' tag", + errorMessage: "Field validation for 'MacAddress' failed on the 'mac' tag", }, NodeValidationUpdateTC{ testname: "MacAddressMissing", @@ -275,19 +275,12 @@ func TestValidateNodeUpdate(t *testing.T) { }, errorMessage: "Field validation for 'MacAddress' failed on the 'required' tag", }, - NodeValidationUpdateTC{ - testname: "EmptyPassword", - node: models.NodeUpdate{ - Password: "", - }, - errorMessage: "Field validation for 'Password' failed on the 'password_check' tag", - }, NodeValidationUpdateTC{ testname: "ShortPassword", node: models.NodeUpdate{ Password: "1234", }, - errorMessage: "Field validation for 'Password' failed on the 'password_check' tag", + errorMessage: "Field validation for 'Password' failed on the 'min' tag", }, NodeValidationUpdateTC{ testname: "NoNetwork", diff --git a/models/node.go b/models/node.go index 5fe8eb85..9db7f789 100644 --- a/models/node.go +++ b/models/node.go @@ -51,13 +51,13 @@ type Node struct { //node update struct --- only validations are different type NodeUpdate struct { ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"` - Address string `json:"address" bson:"address" validate:"address_check"` - Address6 string `json:"address6" bson:"address6" validate:"address6_check"` - LocalAddress string `json:"localaddress" bson:"localaddress" validate:"localaddress_check"` - Name string `json:"name" bson:"name" validate:"omitempty,name_valid,max=12"` + Address string `json:"address" bson:"address" validate:"omitempty,ip"` + Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"` + LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"` + Name string `json:"name" bson:"name" validate:"omitempty,alphanum,max=12"` ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"` - PublicKey string `json:"publickey" bson:"publickey" validate:"pubkey_check"` - Endpoint string `json:"endpoint" bson:"endpoint" validate:"endpoint_check"` + PublicKey string `json:"publickey" bson:"publickey" validate:"omitempty,base64"` + Endpoint string `json:"endpoint" bson:"endpoint" validate:"omitempty,ip"` PostUp string `json:"postup" bson:"postup"` PostDown string `json:"postdown" bson:"postdown"` AllowedIPs string `json:"allowedips" bson:"allowedips"` @@ -70,9 +70,9 @@ type NodeUpdate struct { ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"` LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"` LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"` - MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,macaddress_valid,macaddress_unique"` + MacAddress string `json:"macaddress" bson:"macaddress" validate:"required,mac"` CheckInInterval int32 `json:"checkininterval" bson:"checkininterval"` - Password string `json:"password" bson:"password" validate:"password_check"` + Password string `json:"password" bson:"password" validate:"omitempty,min=5"` Network string `json:"network" bson:"network" validate:"network_exists"` IsPending bool `json:"ispending" bson:"ispending"` IsGateway bool `json:"isgateway" bson:"isgateway"`