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

Improve insecure registry validation #10493

Merged
merged 1 commit into from
Feb 20, 2021
Merged
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
34 changes: 22 additions & 12 deletions cmd/minikube/cmd/start.go
Original file line number Diff line number Diff line change
Expand Up @@ -27,6 +27,7 @@ import (
"os/user"
"regexp"
"runtime"
"strconv"
"strings"

"github.com/blang/semver"
Expand Down Expand Up @@ -72,7 +73,7 @@ var (
insecureRegistry []string
apiServerNames []string
apiServerIPs []net.IP
hostRe = regexp.MustCompile(`[\w\.-]+`)
hostRe = regexp.MustCompile(`^[^-][\w\.-]+$`)
)

func init() {
Expand Down Expand Up @@ -1066,28 +1067,37 @@ func validateRegistryMirror() {
}

// This function validates that the --insecure-registry follows one of the following formats:
// "<ip>:<port>" "<hostname>:<port>" "<network>/<netmask>"
// "<ip>[:<port>]" "<hostname>[:<port>]" "<network>/<netmask>"
func validateInsecureRegistry() {
if len(insecureRegistry) > 0 {
for _, addr := range insecureRegistry {
// Remove http or https from registryMirror
if strings.HasPrefix(strings.ToLower(addr), "http://") || strings.HasPrefix(strings.ToLower(addr), "https://") {
i := strings.Index(addr, "//")
addr = addr[i+2:]
} else if strings.Contains(addr, "://") || strings.HasSuffix(addr, ":") {
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>[:<port>], <hostname>[:<port>] or <network>/<netmask>", out.V{"addr": addr})
}
hostnameOrIP, port, err := net.SplitHostPort(addr)
if err != nil {
_, _, err := net.ParseCIDR(addr)
if err == nil {
continue
}
hostnameOrIP = addr
}
if port == "" {
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>:<port>, <hostname>:<port> or <network>/<netmask>", out.V{"addr": addr})
}
// checks both IPv4 and IPv6
ipAddr := net.ParseIP(hostnameOrIP)
if ipAddr != nil {
continue
if !hostRe.MatchString(hostnameOrIP) && net.ParseIP(hostnameOrIP) == nil {
// fmt.Printf("This is not hostname or ip %s", hostnameOrIP)
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>[:<port>], <hostname>[:<port>] or <network>/<netmask>", out.V{"addr": addr})
}
isValidHost := hostRe.MatchString(hostnameOrIP)
if err != nil || !isValidHost {
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>:<port>, <hostname>:<port> or <network>/<netmask>", out.V{"addr": addr})
if port != "" {
v, err := strconv.Atoi(port)
if err != nil {
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>[:<port>], <hostname>[:<port>] or <network>/<netmask>", out.V{"addr": addr})
}
if v < 0 || v > 65535 {
exit.Message(reason.Usage, "Sorry, the address provided with the --insecure-registry flag is invalid: {{.addr}}. Expected formtas are: <ip>[:<port>], <hostname>[:<port>] or <network>/<netmask>", out.V{"addr": addr})
}
}
}
}
Expand Down