refactor node update validation

This commit is contained in:
Matthew R Kasun
2021-05-06 16:14:31 -04:00
parent 646f613b93
commit 99474f0d66
3 changed files with 17 additions and 74 deletions

View File

@@ -4,7 +4,6 @@ import (
"context" "context"
"fmt" "fmt"
"log" "log"
"net"
"time" "time"
"github.com/go-playground/validator/v10" "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 { func ValidateNodeUpdate(networkName string, node models.NodeUpdate) error {
v := validator.New() 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 { _ = v.RegisterValidation("network_exists", func(fl validator.FieldLevel) bool {
_, err := node.GetNetwork() _, err := node.GetNetwork()
return err == nil 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) err := v.Struct(node)
if err != nil { if err != nil {
for _, e := range err.(validator.ValidationErrors) { for _, e := range err.(validator.ValidationErrors) {
fmt.Println(e) fmt.Println(e)

View File

@@ -196,28 +196,28 @@ func TestValidateNodeUpdate(t *testing.T) {
node: models.NodeUpdate{ node: models.NodeUpdate{
Address: "256.0.0.1", 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{ NodeValidationUpdateTC{
testname: "BadAddress6", testname: "BadAddress6",
node: models.NodeUpdate{ node: models.NodeUpdate{
Address6: "2607::abcd:efgh::1", 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{ NodeValidationUpdateTC{
testname: "BadLocalAddress", testname: "BadLocalAddress",
node: models.NodeUpdate{ node: models.NodeUpdate{
LocalAddress: "10.0.200.300", 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{ NodeValidationUpdateTC{
testname: "InvalidName", testname: "InvalidName",
node: models.NodeUpdate{ node: models.NodeUpdate{
Name: "mynode*", Name: "mynode*",
}, },
errorMessage: "Field validation for 'Name' failed on the 'name_valid' tag", errorMessage: "Field validation for 'Name' failed on the 'alphanum' tag",
}, },
NodeValidationUpdateTC{ NodeValidationUpdateTC{
testname: "NameTooLong", testname: "NameTooLong",
@@ -243,16 +243,16 @@ func TestValidateNodeUpdate(t *testing.T) {
NodeValidationUpdateTC{ NodeValidationUpdateTC{
testname: "PublicKeyInvalid", testname: "PublicKeyInvalid",
node: models.NodeUpdate{ 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{ NodeValidationUpdateTC{
testname: "EndpointInvalid", testname: "EndpointInvalid",
node: models.NodeUpdate{ node: models.NodeUpdate{
Endpoint: "10.2.0.300", 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{ NodeValidationUpdateTC{
testname: "PersistentKeepaliveMax", testname: "PersistentKeepaliveMax",
@@ -266,7 +266,7 @@ func TestValidateNodeUpdate(t *testing.T) {
node: models.NodeUpdate{ node: models.NodeUpdate{
MacAddress: "01:02:03:04:05", 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{ NodeValidationUpdateTC{
testname: "MacAddressMissing", testname: "MacAddressMissing",
@@ -275,19 +275,12 @@ func TestValidateNodeUpdate(t *testing.T) {
}, },
errorMessage: "Field validation for 'MacAddress' failed on the 'required' tag", 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{ NodeValidationUpdateTC{
testname: "ShortPassword", testname: "ShortPassword",
node: models.NodeUpdate{ node: models.NodeUpdate{
Password: "1234", Password: "1234",
}, },
errorMessage: "Field validation for 'Password' failed on the 'password_check' tag", errorMessage: "Field validation for 'Password' failed on the 'min' tag",
}, },
NodeValidationUpdateTC{ NodeValidationUpdateTC{
testname: "NoNetwork", testname: "NoNetwork",

View File

@@ -51,13 +51,13 @@ type Node struct {
//node update struct --- only validations are different //node update struct --- only validations are different
type NodeUpdate struct { type NodeUpdate struct {
ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"` ID primitive.ObjectID `json:"_id,omitempty" bson:"_id,omitempty"`
Address string `json:"address" bson:"address" validate:"address_check"` Address string `json:"address" bson:"address" validate:"omitempty,ip"`
Address6 string `json:"address6" bson:"address6" validate:"address6_check"` Address6 string `json:"address6" bson:"address6" validate:"omitempty,ipv6"`
LocalAddress string `json:"localaddress" bson:"localaddress" validate:"localaddress_check"` LocalAddress string `json:"localaddress" bson:"localaddress" validate:"omitempty,ip"`
Name string `json:"name" bson:"name" validate:"omitempty,name_valid,max=12"` Name string `json:"name" bson:"name" validate:"omitempty,alphanum,max=12"`
ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"` ListenPort int32 `json:"listenport" bson:"listenport" validate:"omitempty,numeric,min=1024,max=65535"`
PublicKey string `json:"publickey" bson:"publickey" validate:"pubkey_check"` PublicKey string `json:"publickey" bson:"publickey" validate:"omitempty,base64"`
Endpoint string `json:"endpoint" bson:"endpoint" validate:"endpoint_check"` Endpoint string `json:"endpoint" bson:"endpoint" validate:"omitempty,ip"`
PostUp string `json:"postup" bson:"postup"` PostUp string `json:"postup" bson:"postup"`
PostDown string `json:"postdown" bson:"postdown"` PostDown string `json:"postdown" bson:"postdown"`
AllowedIPs string `json:"allowedips" bson:"allowedips"` AllowedIPs string `json:"allowedips" bson:"allowedips"`
@@ -70,9 +70,9 @@ type NodeUpdate struct {
ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"` ExpirationDateTime int64 `json:"expdatetime" bson:"expdatetime"`
LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"` LastPeerUpdate int64 `json:"lastpeerupdate" bson:"lastpeerupdate"`
LastCheckIn int64 `json:"lastcheckin" bson:"lastcheckin"` 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"` 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"` Network string `json:"network" bson:"network" validate:"network_exists"`
IsPending bool `json:"ispending" bson:"ispending"` IsPending bool `json:"ispending" bson:"ispending"`
IsGateway bool `json:"isgateway" bson:"isgateway"` IsGateway bool `json:"isgateway" bson:"isgateway"`