Validate unix sockets and ip addresses as needed, more test cases

This commit is contained in:
Preetha Appan 2017-07-28 17:18:10 -05:00
parent 4cec55e8db
commit 5aeab1463b
2 changed files with 23 additions and 12 deletions

View File

@ -2127,7 +2127,7 @@ func ReadConfigPaths(paths []string) (*Config, error) {
// and performs go-sockaddr/template Parse on each known address in case the // and performs go-sockaddr/template Parse on each known address in case the
// user specified a template config for any of their values. // user specified a template config for any of their values.
func (c *Config) ResolveTmplAddrs() (err error) { func (c *Config) ResolveTmplAddrs() (err error) {
parse := func(addr *string, validateIP bool, name string) { parse := func(addr *string, socketAllowed bool, name string) {
if *addr == "" || err != nil { if *addr == "" || err != nil {
return return
} }
@ -2137,26 +2137,31 @@ func (c *Config) ResolveTmplAddrs() (err error) {
err = fmt.Errorf("Resolution of %s failed: %v", name, err) err = fmt.Errorf("Resolution of %s failed: %v", name, err)
return return
} }
ipAddr := net.ParseIP(ip)
if validateIP && net.ParseIP(ip) == nil { if !socketAllowed && ipAddr == nil {
err = fmt.Errorf("Failed to parse %s: %v", name, ip) err = fmt.Errorf("Failed to parse %s: %v", name, ip)
return return
} }
if socketAllowed && socketPath(ip) == "" && ipAddr == nil {
err = fmt.Errorf("Failed to parse %s, is not a valid IP address or socket: %v", name, ip)
return
}
*addr = ip *addr = ip
} }
if c == nil { if c == nil {
return return
} }
parse(&c.Addresses.DNS, false, "DNS address") parse(&c.Addresses.DNS, true, "DNS address")
parse(&c.Addresses.HTTP, false, "HTTP address") parse(&c.Addresses.HTTP, true, "HTTP address")
parse(&c.Addresses.HTTPS, false, "HTTPS address") parse(&c.Addresses.HTTPS, true, "HTTPS address")
parse(&c.AdvertiseAddr, true, "Advertise address") parse(&c.AdvertiseAddr, false, "Advertise address")
parse(&c.AdvertiseAddrWan, true, "Advertise WAN address") parse(&c.AdvertiseAddrWan, false, "Advertise WAN address")
parse(&c.BindAddr, false, "Bind address") parse(&c.BindAddr, true, "Bind address")
parse(&c.ClientAddr, false, "Client address") parse(&c.ClientAddr, true, "Client address")
parse(&c.SerfLanBindAddr, true, "Serf LAN address") parse(&c.SerfLanBindAddr, false, "Serf LAN address")
parse(&c.SerfWanBindAddr, true, "Serf WAN address") parse(&c.SerfWanBindAddr, false, "Serf WAN address")
return return
} }

View File

@ -71,6 +71,12 @@ func TestDecodeConfig(t *testing.T) {
parseTemplateErr: errors.New("Failed to parse Advertise WAN address: unix:///path/to/file"), parseTemplateErr: errors.New("Failed to parse Advertise WAN address: unix:///path/to/file"),
c: &Config{AdvertiseAddrWan: "unix:///path/to/file"}, c: &Config{AdvertiseAddrWan: "unix:///path/to/file"},
}, },
{
in: `{"addresses":{"http":"notunix://blah"}}`,
parseTemplateErr: errors.New("Failed to parse HTTP address, is not a valid IP address or socket: notunix://blah"),
c: &Config{Addresses: AddressConfig{HTTP: "notunix://blah"}},
},
// happy flows in alphabetical order // happy flows in alphabetical order
{ {
in: `{"acl_agent_master_token":"a"}`, in: `{"acl_agent_master_token":"a"}`,