Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

validate dns entry does not contain whitespace #2512

Merged
merged 3 commits into from
Aug 22, 2023
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
13 changes: 13 additions & 0 deletions controllers/dns_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -400,6 +400,19 @@ func TestValidateDNSCreate(t *testing.T) {
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'name_unique' tag")
})
t.Run("WhiteSpace", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: "white space", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})
t.Run("AllSpaces", func(t *testing.T) {
entry := models.DNSEntry{Address: "10.10.10.5", Name: " ", Network: "skynet"}
err := logic.ValidateDNSCreate(entry)
assert.NotNil(t, err)
assert.Contains(t, err.Error(), "Field validation for 'Name' failed on the 'whitespace' tag")
})

}

func createHost() {
Expand Down
11 changes: 11 additions & 0 deletions logic/dns.go
Original file line number Diff line number Diff line change
Expand Up @@ -3,6 +3,7 @@ package logic
import (
"encoding/json"
"os"
"regexp"
"sort"

validator "github.com/go-playground/validator/v10"
Expand Down Expand Up @@ -203,6 +204,11 @@ func ValidateDNSCreate(entry models.DNSEntry) error {

v := validator.New()

_ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
match, _ := regexp.MatchString(`\s`, entry.Name)
return !match
})

_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
num, err := GetDNSEntryNum(entry.Name, entry.Network)
return err == nil && num == 0
Expand All @@ -227,6 +233,11 @@ func ValidateDNSUpdate(change models.DNSEntry, entry models.DNSEntry) error {

v := validator.New()

_ = v.RegisterValidation("whitespace", func(f1 validator.FieldLevel) bool {
match, _ := regexp.MatchString(`\s`, entry.Name)
return !match
})

_ = v.RegisterValidation("name_unique", func(fl validator.FieldLevel) bool {
//if name & net not changing name we are good
if change.Name == entry.Name && change.Network == entry.Network {
Expand Down
8 changes: 4 additions & 4 deletions models/dnsEntry.go
Original file line number Diff line number Diff line change
Expand Up @@ -42,8 +42,8 @@ type DNSUpdate struct {

// DNSEntry - a DNS entry represented as struct
type DNSEntry struct {
Address string `json:"address" bson:"address" validate:"ip"`
Address6 string `json:"address6" bson:"address6"`
Name string `json:"name" bson:"name" validate:"required,name_unique,min=1,max=192"`
Network string `json:"network" bson:"network" validate:"network_exists"`
Address string `json:"address" validate:"ip"`
Address6 string `json:"address6"`
Name string `json:"name" validate:"required,name_unique,min=1,max=192,whitespace"`
Network string `json:"network" validate:"network_exists"`
}