Skip to content

Commit

Permalink
Fixup server based consideration for multiple hosts validation #59
Browse files Browse the repository at this point in the history
  • Loading branch information
Marcel Ludwig committed Nov 9, 2020
1 parent bb42be3 commit dd80fd2
Show file tree
Hide file tree
Showing 2 changed files with 54 additions and 7 deletions.
43 changes: 39 additions & 4 deletions config/runtime/server.go
Original file line number Diff line number Diff line change
Expand Up @@ -296,8 +296,24 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {
// validate the format, validating for a valid host or port is out of scope.
validFormat := regexp.MustCompile(`^([a-z0-9.-]+|\*)(:\*|:\d{1,5})?$`)

portMap := map[int]string{}
portMap := map[int][]string{}
isHostsMandatory := len(conf.Server) > 1

contains := func(s []string, needle string) bool {
for _, n := range s {
if n == needle {
return true
}
}
return false
}

for _, srv := range conf.Server {
if isHostsMandatory && len(srv.Hosts) == 0 {
return fmt.Errorf("hosts attribute is mandatory for multiple servers: %q", srv.Name)
}

srvPortMap := map[int][]string{}
for _, host := range srv.Hosts {
if !validFormat.MatchString(host) {
return fmt.Errorf("host format is invalid: %q", host)
Expand All @@ -308,11 +324,30 @@ func validatePortHosts(conf *config.Gateway, configuredPort int) error {
return err
}

if h, ok := portMap[po]; ok && h == ho {
return fmt.Errorf("conflict: host %q already defined for port: %d", ho, po)
if _, ok := srvPortMap[po]; !ok {
srvPortMap[po] = []string{ho}
}

portMap[po] = ho
if !contains(srvPortMap[po], ho) {
srvPortMap[po] = append(srvPortMap[po], ho)
}
}

// srvPortMap contains all unique host port combinations and should not exist twice per port.
for po, ho := range srvPortMap {
hosts, ok := portMap[po]
if !ok {
portMap[po] = ho
continue
}

for _, h := range hosts {
if contains(srvPortMap[po], h) {
return fmt.Errorf("conflict: host %q already defined for port: %d", ho, po)
} else {
srvPortMap[po] = append(srvPortMap[po], h)
}
}
}
}

Expand Down
18 changes: 15 additions & 3 deletions config/runtime/server_internal_test.go
Original file line number Diff line number Diff line change
Expand Up @@ -55,7 +55,7 @@ func TestServer_validatePortHosts(t *testing.T) {
},
}, 8080,
},
true,
false,
},
{
"Same host/port in two servers with *",
Expand Down Expand Up @@ -86,8 +86,8 @@ func TestServer_validatePortHosts(t *testing.T) {
args{
&config.Gateway{
Server: []*config.Server{
{Hosts: []string{"example.com"}},
{Hosts: []string{"example.com"}},
{Hosts: []string{"example.com", "couper.io"}},
{Hosts: []string{"example.com", "couper.io"}},
},
}, 8080,
},
Expand All @@ -105,6 +105,18 @@ func TestServer_validatePortHosts(t *testing.T) {
},
true,
},
{
"Same port /w different host in two servers",
args{
&config.Gateway{
Server: []*config.Server{
{Hosts: []string{"*", "example.com:9090"}},
{Hosts: []string{"couper.io:9090"}},
},
}, 8080,
},
false,
},
{
"Host is mandatory for multiple servers",
args{
Expand Down

0 comments on commit dd80fd2

Please sign in to comment.