Skip to content
This repository has been archived by the owner on Jun 20, 2024. It is now read-only.

Commit

Permalink
Check CIDRs before attaching a container in proxy
Browse files Browse the repository at this point in the history
If any cidr is invalid, a user will get the following error:
"docker: Error response from daemon: invalid CIDR: $CIDR"
  • Loading branch information
brb committed Jun 10, 2016
1 parent bf8c883 commit 5a25074
Showing 1 changed file with 18 additions and 0 deletions.
18 changes: 18 additions & 0 deletions proxy/proxy.go
Original file line number Diff line number Diff line change
Expand Up @@ -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 (
Expand Down Expand Up @@ -506,6 +507,11 @@ func (proxy *Proxy) attach(containerID string) error {
}
Log.Infof("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " "))
args := []string{"attach"}

if err := validateCIDRs(cidrs); err != nil {
return err
}

args = append(args, cidrs...)
if !proxy.NoRewriteHosts {
args = append(args, "--rewrite-hosts")
Expand All @@ -523,6 +529,18 @@ func (proxy *Proxy) attach(containerID string) error {
return callWeaveAttach(container, args)
}

func validateCIDRs(cidrs []string) error {
for _, cidr := range cidrs {
if cidr != "net:default" {
trim := strings.TrimPrefix(strings.TrimPrefix(cidr, "ip:"), "net:")
if _, err := address.ParseCIDR(trim); err != nil {
return fmt.Errorf("invalid CIDR: %s", cidr, err)
}
}
}
return nil
}

func callWeaveAttach(container *docker.Container, args []string) error {
if _, stderr, err := callWeave(args...); err != nil {
Log.Warningf("Attaching container %s to weave network failed: %s", container.ID, string(stderr))
Expand Down

0 comments on commit 5a25074

Please sign in to comment.