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",
where $CIDR is the first invalid cidr from the WEAVE_CIDR list.
  • Loading branch information
brb committed Jun 13, 2016
1 parent bf8c883 commit 9f043a4
Showing 1 changed file with 26 additions and 0 deletions.
26 changes: 26 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 Down Expand Up @@ -533,6 +539,26 @@ func callWeaveAttach(container *docker.Container, args []string) error {
return nil
}

func validateCIDRs(cidrs []string) error {
check := func(cidr string, prefixes []string) bool {
for _, prefix := range prefixes {
if strings.HasPrefix(cidr, prefix) {
if _, err := address.ParseCIDR(strings.TrimPrefix(cidr, prefix)); err == nil {
return true
}
}
}
return false
}

for _, cidr := range cidrs {
if cidr != "net:default" && !check(cidr, []string{"ip:", "net:", ""}) {
return fmt.Errorf("invalid CIDR: %s", cidr)
}
}
return nil
}

func (proxy *Proxy) weaveCIDRs(networkMode string, env []string) ([]string, error) {
if networkMode == "host" || strings.HasPrefix(networkMode, "container:") ||
// Anything else, other than blank/none/default/bridge, is some sort of network plugin
Expand Down

0 comments on commit 9f043a4

Please sign in to comment.