Merge pull request #2365 from weaveworks/issues/2283-validate-cidrs
Validate CIDRs in proxy before attaching a container Fixes #2283
This commit is contained in:
commit
aca34191da
1 file changed
+22
@@ -20,6 +20,7 @@ import (
|
|||||||
"github.com/weaveworks/weave/common"
|
"github.com/weaveworks/weave/common"
|
||||||
weavedocker "github.com/weaveworks/weave/common/docker"
|
weavedocker "github.com/weaveworks/weave/common/docker"
|
||||||
weavenet "github.com/weaveworks/weave/net"
|
weavenet "github.com/weaveworks/weave/net"
|
||||||
|
"github.com/weaveworks/weave/net/address"
|
||||||
)
|
)
|
||||||
|
|
||||||
const (
|
const (
|
||||||
@@ -488,6 +489,10 @@ func (proxy *Proxy) attach(containerID string, orDie, killProcess bool) error {
|
|||||||
return nil
|
return nil
|
||||||
}
|
}
|
||||||
Log.Infof("Attaching container %s with WEAVE_CIDR \"%s\" to weave network", container.ID, strings.Join(cidrs, " "))
|
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 := []string{"attach"}
|
||||||
args = append(args, cidrs...)
|
args = append(args, cidrs...)
|
||||||
if !proxy.NoRewriteHosts {
|
if !proxy.NoRewriteHosts {
|
||||||
@@ -523,6 +528,23 @@ func callWeaveAttach(container *docker.Container, args []string) error {
|
|||||||
return nil
|
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) {
|
func (proxy *Proxy) weaveCIDRs(networkMode string, env []string) ([]string, error) {
|
||||||
if networkMode == "host" || strings.HasPrefix(networkMode, "container:") {
|
if networkMode == "host" || strings.HasPrefix(networkMode, "container:") {
|
||||||
return nil, fmt.Errorf("the container has '--net=%s'", networkMode)
|
return nil, fmt.Errorf("the container has '--net=%s'", networkMode)
|
||||||
|
|||||||
Reference in new issue
Block a user