server: Validate server arguments for duplicates. (#2554)

- Validates invalid format inputs.
- Validates duplicate entries.
- Validates sufficient amount of disks.

Partially fixes #2502
This commit is contained in:
Harshavardhana
2016-08-26 00:11:53 -07:00
parent 339425fd52
commit 780ccc26f7
10 changed files with 216 additions and 71 deletions

View File

@@ -20,6 +20,7 @@ import (
"encoding/base64"
"encoding/xml"
"errors"
"fmt"
"io"
"net"
"net/http"
@@ -46,6 +47,33 @@ func cloneHeader(h http.Header) http.Header {
return h2
}
// checkDuplicates - function to validate if there are duplicates in a slice of strings.
func checkDuplicates(list []string) error {
// Empty lists are not allowed.
if len(list) == 0 {
return errInvalidArgument
}
// Empty keys are not allowed.
for _, key := range list {
if key == "" {
return errInvalidArgument
}
}
listMaps := make(map[string]int)
// Navigate through each configs and count the entries.
for _, key := range list {
listMaps[key]++
}
// Validate if there are any duplicate counts.
for key, count := range listMaps {
if count != 1 {
return fmt.Errorf("Duplicate key: \"%s\" found of count: \"%d\"", key, count)
}
}
// No duplicates.
return nil
}
// splits network path into its components Address and Path.
func splitNetPath(networkPath string) (netAddr, netPath string, err error) {
if runtime.GOOS == "windows" {
@@ -54,16 +82,15 @@ func splitNetPath(networkPath string) (netAddr, netPath string, err error) {
}
}
networkParts := strings.SplitN(networkPath, ":", 2)
switch len(networkParts) {
case 1:
if len(networkParts) == 1 {
return "", networkPath, nil
case 2:
if networkParts[1] == "" {
return "", "", &net.AddrError{Err: "missing path in network path", Addr: networkPath}
} else if networkParts[0] == "" {
return "", "", &net.AddrError{Err: "missing address in network path", Addr: networkPath}
}
return networkParts[0], networkParts[1], nil
}
if networkParts[1] == "" {
return "", "", &net.AddrError{Err: "Missing path in network path", Addr: networkPath}
} else if networkParts[0] == "" {
return "", "", &net.AddrError{Err: "Missing address in network path", Addr: networkPath}
} else if !filepath.IsAbs(networkParts[1]) {
return "", "", &net.AddrError{Err: "Network path should be absolute", Addr: networkPath}
}
return networkParts[0], networkParts[1], nil
}