From df5508c0f3b338989803ab3385acf5f54c6402cf Mon Sep 17 00:00:00 2001 From: Martynas Pumputis Date: Fri, 10 Jun 2016 16:44:54 +0100 Subject: [PATCH] Check CIDRs before attaching a container in proxy If any cidr is invalid, a user will get the following error: "docker: Error response from daemon: invalid WEAVE_CIDR: $CIDR". --- proxy/proxy.go | 22 ++++++++++++++++++++++ 1 file changed, 22 insertions(+) diff --git a/proxy/proxy.go b/proxy/proxy.go index 54bedc65..28013ca9 100644 --- a/proxy/proxy.go +++ b/proxy/proxy.go @@ -20,6 +20,7 @@ import ( "github.com/weaveworks/weave/common" weavedocker "github.com/weaveworks/weave/common/docker" weavenet "github.com/weaveworks/weave/net" + "github.com/weaveworks/weave/net/address" ) const ( @@ -488,6 +489,10 @@ func (proxy *Proxy) attach(containerID string, orDie, killProcess bool) error { return nil } Log.Infof("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " ")) + if err := validateCIDRs(cidrs); err != nil { + return err + } + args := []string{"attach"} args = append(args, cidrs...) if !proxy.NoRewriteHosts { @@ -523,6 +528,23 @@ func callWeaveAttach(container *docker.Container, args []string) error { return nil } +func validateCIDRs(cidrs []string) error { + for _, cidr := range cidrs { + if cidr == "net:default" { + continue + } + for _, prefix := range []string{"ip:", "net:", ""} { + if strings.HasPrefix(cidr, prefix) { + if _, err := address.ParseCIDR(strings.TrimPrefix(cidr, prefix)); err == nil { + break + } + return fmt.Errorf("invalid WEAVE_CIDR: %s", cidr) + } + } + } + return nil +} + func (proxy *Proxy) weaveCIDRs(networkMode string, env []string) ([]string, error) { if networkMode == "host" || strings.HasPrefix(networkMode, "container:") { return nil, fmt.Errorf("the container has '--net=%s'", networkMode)