mirror of https://github.com/hashicorp/consul
agent: Disallow :: and [::] as service address
parent
e365ef12cf
commit
8ad52ee9b5
|
@ -454,7 +454,7 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
|
|||
|
||||
// Check the service address here and in the catalog RPC endpoint
|
||||
// since service registration isn't sychronous.
|
||||
if args.Address == "0.0.0.0" {
|
||||
if args.Address == "0.0.0.0" || args.Address == "::" || args.Address == "[::]" {
|
||||
resp.WriteHeader(400)
|
||||
fmt.Fprintf(resp, "Invalid service address")
|
||||
return nil, nil
|
||||
|
|
|
@ -1503,13 +1503,15 @@ func TestAgent_RegisterService_InvalidAddress(t *testing.T) {
|
|||
defer srv.Shutdown()
|
||||
defer srv.agent.Shutdown()
|
||||
|
||||
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
||||
t.Run("addr "+addr, func(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "/v1/agent/service/register?token=abc123", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
args := &ServiceDefinition{
|
||||
Name: "test",
|
||||
Address: "0.0.0.0",
|
||||
Address: addr,
|
||||
Port: 8000,
|
||||
}
|
||||
req.Body = encodeReq(args)
|
||||
|
@ -1522,6 +1524,8 @@ func TestAgent_RegisterService_InvalidAddress(t *testing.T) {
|
|||
if got, want := resp.Body.String(), "Invalid service address"; got != want {
|
||||
t.Fatalf("got body %q want %q", got, want)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestAgent_DeregisterService(t *testing.T) {
|
||||
|
|
|
@ -63,7 +63,8 @@ func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
|
|||
|
||||
testrpc.WaitForLeader(t, srv.agent.RPC, "dc1")
|
||||
|
||||
// Register node
|
||||
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
||||
t.Run("addr "+addr, func(t *testing.T) {
|
||||
req, err := http.NewRequest("GET", "/v1/catalog/register", nil)
|
||||
if err != nil {
|
||||
t.Fatalf("err: %v", err)
|
||||
|
@ -73,7 +74,7 @@ func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
|
|||
Address: "127.0.0.1",
|
||||
Service: &structs.NodeService{
|
||||
Service: "test",
|
||||
Address: "0.0.0.0",
|
||||
Address: addr,
|
||||
Port: 8080,
|
||||
},
|
||||
}
|
||||
|
@ -83,6 +84,8 @@ func TestCatalogRegister_Service_InvalidAddress(t *testing.T) {
|
|||
if err == nil || err.Error() != "Invalid service address" {
|
||||
t.Fatalf("err: %v", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalogDeregister(t *testing.T) {
|
||||
|
|
|
@ -54,7 +54,7 @@ func (c *Catalog) Register(args *structs.RegisterRequest, reply *struct{}) error
|
|||
|
||||
// Check the service address here and in the agent endpoint
|
||||
// since service registration isn't sychronous.
|
||||
if args.Service.Address == "0.0.0.0" {
|
||||
if args.Service.Address == "0.0.0.0" || args.Service.Address == "::" || args.Service.Address == "[::]" {
|
||||
return fmt.Errorf("Invalid service address")
|
||||
}
|
||||
|
||||
|
|
|
@ -53,13 +53,15 @@ func TestCatalog_RegisterService_InvalidAddress(t *testing.T) {
|
|||
codec := rpcClient(t, s1)
|
||||
defer codec.Close()
|
||||
|
||||
for _, addr := range []string{"0.0.0.0", "::", "[::]"} {
|
||||
t.Run("addr "+addr, func(t *testing.T) {
|
||||
arg := structs.RegisterRequest{
|
||||
Datacenter: "dc1",
|
||||
Node: "foo",
|
||||
Address: "127.0.0.1",
|
||||
Service: &structs.NodeService{
|
||||
Service: "db",
|
||||
Address: "0.0.0.0",
|
||||
Address: addr,
|
||||
Port: 8000,
|
||||
},
|
||||
}
|
||||
|
@ -69,6 +71,8 @@ func TestCatalog_RegisterService_InvalidAddress(t *testing.T) {
|
|||
if err == nil || err.Error() != "Invalid service address" {
|
||||
t.Fatalf("got error %v want 'Invalid service address'", err)
|
||||
}
|
||||
})
|
||||
}
|
||||
}
|
||||
|
||||
func TestCatalog_Register_NodeID(t *testing.T) {
|
||||
|
|
Loading…
Reference in New Issue