Skip to content

Commit

Permalink
Issue #154: validation regexp + parsing the env var value correctly (…
Browse files Browse the repository at this point in the history
…prepared for ipv6)

Signed-off-by: Jirka Kremser <[email protected]>
  • Loading branch information
jkremser committed Sep 7, 2021
1 parent 8886cdd commit 9c77160
Show file tree
Hide file tree
Showing 2 changed files with 35 additions and 15 deletions.
45 changes: 31 additions & 14 deletions controllers/depresolver/depresolver_config.go
Original file line number Diff line number Diff line change
Expand Up @@ -19,6 +19,7 @@ package depresolver

import (
"fmt"
"net"
"strconv"
"strings"

Expand Down Expand Up @@ -127,15 +128,10 @@ func (dr *DependencyResolver) validateConfig(config *Config, recognizedDNSTypes
return err
}
}
// todo: this
// err = field(EdgeDNSServersKey, config.EdgeDNSServers).isNotEmpty().matchRegexps(hostNameRegex, ipAddressRegex).err
// if err != nil {
// return err
// }
// err = field(EdgeDNSServerPortKey, config.EdgeDNSServerPort).isHigherThanZero().err
// if err != nil {
// return err
// }
err = field(EdgeDNSServersKey, config.EdgeDNSServers).isNotEmpty().matchRegexps(hostNamesWithPortsRegex, ipAddressRegex).err
if err != nil {
return err
}
err = field(EdgeDNSZoneKey, config.EdgeDNSZone).isNotEmpty().matchRegexp(hostNameRegex).err
if err != nil {
return err
Expand Down Expand Up @@ -226,13 +222,34 @@ func parseMetricsAddr(metricsAddr string) (host string, port int, err error) {
func parseEdgeDNSServers(serverList string) []utils.DNSServer {
var r []utils.DNSServer
chunks := strings.Split(serverList, ",")
// foreach chunk
var host, portStr string
var err error
for _, chunk := range chunks {
// if it contains colon
// chunk
switch strings.Count(chunk, ":") {
case 0: //ipv4 or domain
host = chunk
portStr = "53"
case 1: //ipv4 or domain
host, portStr, err = net.SplitHostPort(host)
if err != nil {
portStr = "53"
}
default: //ipv6
if net.ParseIP(chunk).To16() != nil {
host = chunk
} else {
host = chunk[:strings.LastIndex(chunk, ":")]
portStr = chunk[strings.LastIndex(chunk, ":")+1:]
}
}
var port int
port, err = strconv.Atoi(portStr)
if err != nil || port < 1 {
port = 53
}
r = append(r, utils.DNSServer{
Host: chunk,
Port: 53,
Host: host,
Port: port,
})
}
return r
Expand Down
5 changes: 4 additions & 1 deletion controllers/depresolver/depresolver_validator.go
Original file line number Diff line number Diff line change
Expand Up @@ -26,8 +26,11 @@ import (
const (
// hostNameRegex allows cloud region formats; e.g. af-south-1
geoTagRegex = "^[a-zA-Z\\-\\d]*$"
// hostNameRegex is valid as per RFC 1123 that allows hostname segments could start with a digit

// hostNameRegex is valid as per RFC 1123 that allows hostname segments could start with a digit
hostNameRegex = "^(([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])$"
// hostnames are valid as per the previous regexp, it may also contain :123 port and multiple comma-separated entries are supported
hostNamesWithPortsRegex = "^((([a-zA-Z0-9]|[a-zA-Z0-9][a-zA-Z0-9\\-]*[a-zA-Z0-9])\\.)*([A-Za-z0-9]|[A-Za-z0-9][A-Za-z0-9\\-]*[A-Za-z0-9])(:\\d{1,5})?,?)+[^,]$"
// ipAddressRegex matches valid IPv4 addresses
ipAddressRegex = "^(([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])\\.){3}([0-9]|[1-9][0-9]|1[0-9]{2}|2[0-4][0-9]|25[0-5])$"
// versionNumberRegex matches version in formats 0.1.2, v0.1.2, v0.1.2-alpha
Expand Down

0 comments on commit 9c77160

Please sign in to comment.