Added validation of ServiceMeta in Catalog

Fixed Error Message when ServiceMeta is not valid

Added Unit test for adding a Service with badly formatted ServiceMeta
This commit is contained in:
Pierre Souchay 2018-03-27 22:22:42 +02:00
parent 6022c7a209
commit 980189a33f
3 changed files with 17 additions and 1 deletions

View File

@ -581,7 +581,7 @@ func (s *HTTPServer) AgentRegisterService(resp http.ResponseWriter, req *http.Re
ns := args.NodeService() ns := args.NodeService()
if err := structs.ValidateMetadata(ns.ServiceMeta, false); err != nil { if err := structs.ValidateMetadata(ns.ServiceMeta, false); err != nil {
resp.WriteHeader(http.StatusBadRequest) resp.WriteHeader(http.StatusBadRequest)
fmt.Fprint(resp, fmt.Errorf("Invalid Meta: %v", err)) fmt.Fprint(resp, fmt.Errorf("Invalid Service Meta: %v", err))
return nil, nil return nil, nil
} }

View File

@ -610,6 +610,9 @@ func (s *Store) ensureServiceTxn(tx *memdb.Txn, idx uint64, node string, svc *st
return fmt.Errorf("failed service lookup: %s", err) return fmt.Errorf("failed service lookup: %s", err)
} }
if err = structs.ValidateMetadata(svc.ServiceMeta, false); err != nil {
return fmt.Errorf("Invalid Service Meta for node %s and serviceID %s: %v", node, svc.ID, err)
}
// Create the service node entry and populate the indexes. Note that // Create the service node entry and populate the indexes. Note that
// conversion doesn't populate any of the node-specific information. // conversion doesn't populate any of the node-specific information.
// That's always populated when we read from the state store. // That's always populated when we read from the state store.

View File

@ -69,6 +69,19 @@ func TestStateStore_EnsureRegistration(t *testing.T) {
} }
verifyNode() verifyNode()
// Add in a invalid service definition with too long Key value for ServiceMeta
req.Service = &structs.NodeService{
ID: "redis1",
Service: "redis",
Address: "1.1.1.1",
Port: 8080,
ServiceMeta: map[string]string{strings.Repeat("a", 129): "somevalue"},
Tags: []string{"master"},
}
if err := s.EnsureRegistration(9, req); err == nil {
t.Fatalf("Service should not have been registered since ServiceMeta is invalid")
}
// Add in a service definition. // Add in a service definition.
req.Service = &structs.NodeService{ req.Service = &structs.NodeService{
ID: "redis1", ID: "redis1",